]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #12919 from annando/html-media-update
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 985976633d1aefcbf1ce841c0dbee64a9ebb6140..7da110f6716dfd5fb6a2e284e727e19d85e551b8 100644 (file)
@@ -41,7 +41,6 @@ use Friendica\Model\User;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
-use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\HTTPSignature;
@@ -115,8 +114,8 @@ class Transmitter
                        return false;
                }
 
-               $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
-               $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
+               $activity_id = self::activityIDFromContact($contact['id']);
+               $success = self::sendActivity('Follow', $url, 0, $activity_id);
                if ($success) {
                        Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
                }
@@ -239,133 +238,6 @@ class Transmitter
                return $data;
        }
 
-       /**
-        * Public posts for the given owner
-        *
-        * @param array   $owner     Owner array
-        * @param integer $page      Page number
-        * @param integer $max_id    Maximum ID
-        * @param string  $requester URL of requesting account
-        * @param boolean $nocache   Wether to bypass caching
-        * @return array of posts
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       public static function getOutbox(array $owner, int $page = null, int $max_id = null, string $requester = ''): array
-       {
-               $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]];
-
-               if (!empty($requester)) {
-                       $requester_id = Contact::getIdForURL($requester, $owner['uid']);
-                       if (!empty($requester_id)) {
-                               $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']);
-                               if (!empty($permissionSets)) {
-                                       $condition = ['psid' => array_merge($permissionSets->column('id'),
-                                                       [DI::permissionSet()->selectPublicForUser($owner['uid'])])];
-                               }
-                       }
-               }
-
-               $condition = array_merge($condition, [
-                       'uid'           => $owner['uid'],
-                       'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
-                       'gravity'        => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
-                       'network'        => Protocol::FEDERATED,
-                       'parent-network' => Protocol::FEDERATED,
-                       'origin'         => true,
-                       'deleted'        => false,
-                       'visible'        => true
-               ]);
-
-               $apcontact = APContact::getByURL($owner['url']);
-
-               return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, null, $apcontact['statuses_count']);
-       }
-
-       public static function getInbox(int $uid, int $page = null, int $max_id = null)
-       {
-               $owner = User::getOwnerDataById($uid);
-
-               $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid];
-
-               return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null);
-       }
-
-       public static function getPublicInbox(int $uid, int $page = null, int $max_id = null)
-       {
-               $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => Item::PUBLIC,
-                       'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'author-blocked' => false, 'author-hidden' => false];
-
-               return self::getCollection($condition, DI::baseUrl() . '/inbox', $page, $max_id, $uid, null);
-       }
-
-       private static function getCollection(array $condition, string $path, int $page = null, int $max_id = null, int $uid = null, int $total_items = null)
-       {
-               $data = ['@context' => ActivityPub::CONTEXT];
-               $data['id'] = $path;
-               $data['type'] = 'OrderedCollection';
-
-               if (!is_null($total_items)) {
-                       $data['totalItems'] = $total_items;
-               }
-
-               if (!empty($page)) {
-                       $data['id'] .= '?' . http_build_query(['page' => $page]);
-               }
-
-               if (empty($page) && empty($max_id)) {
-                       $data['first'] = $path . '?page=1';
-               } else {
-                       $data['type'] = 'OrderedCollectionPage';
-                       $list = [];
-
-                       if (!empty($max_id)) {
-                               $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]);
-                       }
-       
-                       if (!empty($page)) {
-                               $params = ['limit' => [($page - 1) * 20, 20], 'order' => ['uri-id' => true]];
-                       } else {
-                               $params = ['limit' => 20, 'order' => ['uri-id' => true]];
-                       }
-
-                       if (!is_null($uid)) {
-                               $items = Post::selectForUser($uid, ['id', 'uri-id'], $condition, $params);
-                       } else {
-                               $items = Post::select(['id', 'uri-id'], $condition, $params);
-                       }
-
-                       $last_id = 0;
-                       while ($item = Post::fetch($items)) {
-                               $activity = self::createActivityFromItem($item['id'], false, !is_null($uid));
-                               if (!empty($activity)) {
-                                       $list[]  = $activity;
-                                       $last_id = $item['uri-id'];
-                                       continue;
-                               }
-                       }
-                       DBA::close($items);
-
-                       if (count($list) == 20) {
-                               $data['next'] = $path . '?max_id=' . $last_id;
-                       }
-
-                       // Fix the cached total item count when it is lower than the real count
-                       if (!is_null($total_items)) {
-                               $total = (($page - 1) * 20) + $data['totalItems'];
-                               if ($total > $data['totalItems']) {
-                                       $data['totalItems'] = $total;
-                               }
-                       }
-
-                       $data['partOf'] = $path;
-
-                       $data['orderedItems'] = $list;
-               }
-
-               return $data;
-       }
-
        /**
         * Public posts for the given owner
         *
@@ -458,7 +330,7 @@ class Transmitter
                return [
                        'type' => 'Service',
                        'name' =>  App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
-                       'url' => DI::baseUrl()->get()
+                       'url' => DI::baseUrl()
                ];
        }
 
@@ -527,40 +399,42 @@ class Transmitter
                        'owner' => $owner['url'],
                        'publicKeyPem' => $owner['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
-
-               $resourceid = Photo::ridFromURI($owner['photo']);
-               if (!empty($resourceid)) {
-                       $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
-                       if (!empty($photo['type'])) {
-                               $data['icon']['mediaType'] = $photo['type'];
-                       }
-               }
-
-               if (!empty($owner['header'])) {
-                       $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
+               if ($uid != 0) {
+                       $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
 
-                       $resourceid = Photo::ridFromURI($owner['header']);
+                       $resourceid = Photo::ridFromURI($owner['photo']);
                        if (!empty($resourceid)) {
                                $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
                                if (!empty($photo['type'])) {
-                                       $data['image']['mediaType'] = $photo['type'];
+                                       $data['icon']['mediaType'] = $photo['type'];
                                }
                        }
-               }
 
-               $custom_fields = [];
+                       if (!empty($owner['header'])) {
+                               $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
 
-               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)
-                       ];
-               };
+                               $resourceid = Photo::ridFromURI($owner['header']);
+                               if (!empty($resourceid)) {
+                                       $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
+                                       if (!empty($photo['type'])) {
+                                               $data['image']['mediaType'] = $photo['type'];
+                                       }
+                               }
+                       }
 
-               if (!empty($custom_fields)) {
-                       $data['attachment'] = $custom_fields;
+                       $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();
@@ -569,6 +443,34 @@ class Transmitter
                return $data;
        }
 
+       /**
+        * Get a minimal actror array for the C2S API
+        *
+        * @param integer $cid
+        * @return array
+        */
+       private static function getActorArrayByCid(int $cid): array
+       {
+               $contact = Contact::getById($cid);
+               $data = [
+                       'id'                        => $contact['url'],
+                       'type'                      => $data['type'] = ActivityPub::ACCOUNT_TYPES[$contact['contact-type']],
+                       'url'                       => $contact['alias'],
+                       'preferredUsername'         => $contact['nick'],
+                       'name'                      => $contact['name'],
+                       'icon'                      => ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($cid, '', $contact['updated'])],
+                       'image'                     => ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($cid, '', $contact['updated'])],
+                       'manuallyApprovesFollowers' => (bool)$contact['manually-approve'],
+                       'discoverable'              => !$contact['unsearchable'],
+               ];
+
+               if (empty($data['url'])) {
+                       $data['url'] = $data['id'];
+               }
+
+               return $data;
+       }
+
        /**
         * @param string $username
         * @return array
@@ -1325,7 +1227,7 @@ class Transmitter
         */
        private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false)
        {
-               if (!$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
+               if (!$api_mode && !$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
                        $data = Post\Activity::getByURIId($item['uri-id']);
                        if (!$item['origin'] && !empty($data)) {
                                if (!$object_mode) {
@@ -1368,9 +1270,17 @@ class Transmitter
                $data['type'] = $type;
 
                if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
-                       $data['actor'] = $item['author-link'];
+                       $link = $item['author-link'];
+                       $id   = $item['author-id'];
                } else {
-                       $data['actor'] = $item['owner-link'];
+                       $link = $item['owner-link'];
+                       $id   = $item['owner-id'];
+               }
+
+               if ($api_mode) {
+                       $data['actor'] = self::getActorArrayByCid($id);
+               } else {
+                       $data['actor'] = $link;
                }
 
                $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
@@ -1380,15 +1290,14 @@ class Transmitter
                $data = array_merge($data, self::createPermissionBlockForItem($item, false));
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
-                       $data['object'] = self::createNote($item);
-                       $data['published'] = DateTimeFormat::utcNow(DateTimeFormat::ATOM);
+                       $data['object'] = self::createNote($item, $api_mode);
                } elseif ($data['type'] == 'Add') {
                        $data = self::createAddTag($item, $data);
                } elseif ($data['type'] == 'Announce') {
                        if ($item['verb'] == ACTIVITY::ANNOUNCE) {
                                $data['object'] = $item['thr-parent'];
                        } else {
-                               $data = self::createAnnounce($item, $data);
+                               $data = self::createAnnounce($item, $data, $api_mode);
                        }
                } elseif ($data['type'] == 'Follow') {
                        $data['object'] = $item['parent-uri'];
@@ -1646,11 +1555,12 @@ class Transmitter
         * Creates a note/article object array
         *
         * @param array $item
+        * @param bool  $api_mode
         * @return array with the object data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function createNote(array $item): array
+       public static function createNote(array $item, bool $api_mode = false): array
        {
                if (empty($item)) {
                        return [];
@@ -1710,7 +1620,11 @@ class Transmitter
                }
 
                $data['url'] = $link ?? $item['plink'];
-               $data['attributedTo'] = $item['author-link'];
+               if ($api_mode) {
+                       $data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
+               } else {
+                       $data['attributedTo'] = $item['author-link'];
+               }
                $data['sensitive'] = self::isSensitive($item['uri-id']);
 
                if (!empty($item['conversation']) && ($item['conversation'] != './')) {
@@ -1725,6 +1639,8 @@ class Transmitter
 
                $real_quote = false;
 
+               $item = Post\Media::addHTMLAttachmentToItem($item);
+
                $body = $item['body'];
 
                if ($type == 'Note') {
@@ -1761,7 +1677,7 @@ class Transmitter
                        if ($type == 'Page') {
                                // When we transmit "Page" posts we have to remove the attachment.
                                // The attachment contains the link that we already transmit in the "url" field.
-                               $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
+                               $body = BBCode::removeAttachment($body);
                        }
 
                        $body = BBCode::setMentionsToNicknames($body);
@@ -1793,7 +1709,7 @@ class Transmitter
                                        $richbody = DI::contentItem()->addSharedPost($item, $richbody);
                                }
                        }
-                       $richbody = BBCode::removeAttachment($richbody);
+                       $richbody = BBCode::replaceAttachment($richbody);
 
                        $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
                }
@@ -1882,17 +1798,18 @@ class Transmitter
         *
         * @param array $item Item array
         * @param array $activity activity data
+        * @param bool  $api_mode
         * @return array with activity data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function createAnnounce(array $item, array $activity): array
+       private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
        {
                $orig_body = $item['body'];
                $announce = self::getAnnounceArray($item);
                if (empty($announce)) {
                        $activity['type'] = 'Create';
-                       $activity['object'] = self::createNote($item);
+                       $activity['object'] = self::createNote($item, $api_mode);
                        return $activity;
                }
 
@@ -1906,7 +1823,7 @@ class Transmitter
                // Quote
                $activity['type'] = 'Create';
                $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
-               $activity['object'] = self::createNote($item);
+               $activity['object'] = self::createNote($item, $api_mode);
 
                /// @todo Finally descide how to implement this in AP. This is a possible way:
                $activity['object']['attachment'][] = self::createNote($announce['object']);