]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #10798 from annando/q-calls
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 20b8f2a26221f5bc766c5a4c06a8689e5c2bece5..d84baa55588e55bdaff1a26a3803eefca112c179 100644 (file)
@@ -23,7 +23,6 @@ namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
-use Friendica\Content\Text\Plaintext;
 use Friendica\Core\Cache\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -35,12 +34,12 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GServer;
 use Friendica\Model\Item;
-use Friendica\Model\ItemURI;
-use Friendica\Model\Profile;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
+use Friendica\Model\Profile;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
+use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Relay;
@@ -116,7 +115,7 @@ class Transmitter
                $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
                $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
                if ($success) {
-                       DBA::update('contact', ['rel' => Contact::FRIEND], ['id' => $contact['id']]);
+                       Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
                }
 
                return $success;
@@ -138,7 +137,7 @@ class Transmitter
 
                $success = self::sendContactUndo($url, $contact['id'], 0);
                if ($success || $force) {
-                       DBA::update('contact', ['rel' => Contact::NOTHING], ['id' => $contact['id']]);
+                       Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
                }
 
                return $success;
@@ -310,13 +309,18 @@ class Transmitter
        /**
         * Return the ActivityPub profile of the given user
         *
-        * @param integer $uid User ID
+        * @param int $uid User ID
         * @return array with profile data
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws HTTPException\NotFoundException
+        * @throws HTTPException\InternalServerErrorException
         */
-       public static function getProfile($uid)
+       public static function getProfile(int $uid): array
        {
                $owner = User::getOwnerDataById($uid);
+               if (!isset($owner['id'])) {
+                       DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid, 'callstack' => System::callstack(20)]);
+                       throw new HTTPException\NotFoundException('User not found.');
+               }
 
                $data = ['@context' => ActivityPub::CONTEXT];
                $data['id'] = $owner['url'];
@@ -348,14 +352,25 @@ class Transmitter
                        $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
                }
 
+               if (!empty($owner['xmpp']) || !empty($owner['matrix'])) {
+                       $data['vcard:hasInstantMessage'] = [];
+
+                       if (!empty($owner['xmpp'])) {
+                               $data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
+                       }
+                       if (!empty($owner['matrix'])) {
+                               $data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
+                       }
+               }
+
                $data['url'] = $owner['url'];
                $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
-               $data['discoverable'] = $owner['net-publish'];
+               $data['discoverable'] = (bool)$owner['net-publish'];
                $data['publicKey'] = ['id' => $owner['url'] . '#main-key',
                        'owner' => $owner['url'],
                        'publicKeyPem' => $owner['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               $data['icon'] = ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($owner['id'], '', $owner['updated'])];
+               $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrlForId($uid)];
 
                $resourceid = Photo::ridFromURI($owner['photo']);
                if (!empty($resourceid)) {
@@ -377,6 +392,20 @@ class Transmitter
                        }
                }
 
+               $custom_fields = [];
+
+               foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
+                       $custom_fields[] = [
+                               'type' => 'PropertyValue',
+                               'name' => $profile_field->label,
+                               'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
+                       ];
+               };
+
+               if (!empty($custom_fields)) {
+                       $data['attachment'] = $custom_fields;
+               }
+
                $data['generator'] = self::getService();
 
                // tags: https://kitty.town/@inmysocks/100656097926961126.json
@@ -862,6 +891,9 @@ class Transmitter
                }
 
                $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               if (!DBA::isResult($reply)) {
+                       $reply = $mail;
+               }
 
                // Making the post more compatible for Mastodon by:
                // - Making it a note and not an article (no title)
@@ -1855,10 +1887,11 @@ class Transmitter
         * @param string  $inbox Target inbox
         *
         * @return boolean was the transmission successful?
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws HTTPException\InternalServerErrorException
+        * @throws HTTPException\NotFoundException
         * @throws \ImagickException
         */
-       public static function sendProfileUpdate($uid, $inbox)
+       public static function sendProfileUpdate(int $uid, string $inbox): bool
        {
                $owner = User::getOwnerDataById($uid);
                $profile = APContact::getByURL($owner['url']);
@@ -2014,15 +2047,16 @@ class Transmitter
         * @param string  $target Target profile
         * @param         $id
         * @param integer $uid    User ID
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @return bool Operation success
+        * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function sendContactReject($target, $id, $uid)
+       public static function sendContactReject($target, $id, $uid): bool
        {
                $profile = APContact::getByURL($target);
                if (empty($profile['inbox'])) {
                        Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
-                       return;
+                       return false;
                }
 
                $owner = User::getOwnerDataById($uid);
@@ -2042,7 +2076,7 @@ class Transmitter
                Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
 
                $signed = LDSignature::sign($data, $owner);
-               HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+               return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
        }
 
        /**