]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #10463 from annando/proxy-security
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 68bac70000afed6a02656156c617c7a6f52ff896..9ff3957c1e8eaedc93ca3c5f812d4b205ec4915b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -33,6 +33,7 @@ use Friendica\DI;
 use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
+use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\Profile;
@@ -67,13 +68,9 @@ class Transmitter
         */
        public static function addRelayServerInboxes(array $inboxes = [])
        {
-               $contacts = DBA::select('apcontact', ['inbox'],
-                       ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)",
-                               'Application', 0, Contact::FRIEND]);
-               while ($contact = DBA::fetch($contacts)) {
+               foreach (Relay::getList(['inbox']) as $contact) {
                        $inboxes[$contact['inbox']] = $contact['inbox'];
                }
-               DBA::close($contacts);
 
                return $inboxes;
        }
@@ -86,12 +83,12 @@ class Transmitter
         */
        public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = [])
        {
-               $item = Item::selectFirst(['uid'], ['id' => $item_id]);
+               $item = Post::selectFirst(['uid'], ['id' => $item_id]);
                if (empty($item)) {
                        return $inboxes;
                }
 
-               $relays = Relay::getList($item_id, [], [Protocol::ACTIVITYPUB]);
+               $relays = Relay::getDirectRelayList($item_id);
                if (empty($relays)) {
                        return $inboxes;
                }
@@ -146,7 +143,7 @@ class Transmitter
 
                return $success;
        }
-       
+
        /**
         * Collects a list of contacts of the given owner
         *
@@ -257,9 +254,9 @@ class Transmitter
                $condition = array_merge($condition,
                        ['author-id' => $public_contact,
                        'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
-                       'deleted' => false, 'visible' => true, 'moderated' => false]);
+                       'deleted' => false, 'visible' => true]);
 
-               $count = DBA::count('item', $condition);
+               $count = Post::count($condition);
 
                $data = ['@context' => ActivityPub::CONTEXT];
                $data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
@@ -274,8 +271,8 @@ class Transmitter
 
                        $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
 
-                       $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
-                       while ($item = Item::fetch($items)) {
+                       $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
+                       while ($item = Post::fetch($items)) {
                                $activity = self::createActivityFromItem($item['id'], true);
                                $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
 
@@ -284,6 +281,7 @@ class Transmitter
                                        $list[] = $activity['object'];
                                }
                        }
+                       DBA::close($items);
 
                        if (!empty($list)) {
                                $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
@@ -327,13 +325,13 @@ class Transmitter
                                return [];
                        }
 
-                       $fields = ['locality', 'region', 'country-name'];
+                       $fields = ['locality', 'region', 'country-name', 'net-publish'];
                        $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
                        if (!DBA::isResult($profile)) {
                                return [];
                        }
 
-                       $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
+                       $fields = ['id', 'name', 'url', 'location', 'about', 'avatar', 'photo', 'updated'];
                        $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
                        if (!DBA::isResult($contact)) {
                                return [];
@@ -342,7 +340,7 @@ class Transmitter
                        $contact = User::getSystemAccount();
                        $user = ['guid' => '', 'nickname' => $contact['nick'], 'pubkey' => $contact['pubkey'],
                                'account-type' => $contact['contact-type'], 'page-flags' => User::PAGE_FLAGS_NORMAL];
-                       $profile = ['locality' => '', 'region' => '', 'country-name' => ''];
+                       $profile = ['locality' => '', 'region' => '', 'country-name' => '', 'net-publish' => false];
                }
 
                $data = ['@context' => ActivityPub::CONTEXT];
@@ -353,7 +351,7 @@ class Transmitter
                }
 
                $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
-               
+
                if ($uid != 0) {
                        $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
                        $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
@@ -377,12 +375,32 @@ class Transmitter
 
                $data['url'] = $contact['url'];
                $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
+               $data['discoverable'] = $profile['net-publish'];
                $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
                        'owner' => $contact['url'],
                        'publicKeyPem' => $user['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               $data['icon'] = ['type' => 'Image',
-                       'url' => $contact['photo']];
+               $data['icon'] = ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($contact['id'], '', $contact['updated'])];
+
+               $resourceid = Photo::ridFromURI($contact['photo']);
+               if (!empty($resourceid)) {
+                       $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
+                       if (!empty($photo['type'])) {
+                               $data['icon']['mediaType'] = $photo['type'];
+                       }
+               }
+
+               if (!empty($contact['header'])) {
+                       $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($contact['id'], '', $contact['updated'])];
+
+                       $resourceid = Photo::ridFromURI($contact['header']);
+                       if (!empty($resourceid)) {
+                               $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
+                               if (!empty($photo['type'])) {
+                                       $data['image']['mediaType'] = $photo['type'];
+                               }
+                       }
+               }
 
                $data['generator'] = self::getService();
 
@@ -480,7 +498,7 @@ class Transmitter
                        return false;
                }
 
-               return Item::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
+               return Post::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
        }
 
        /**
@@ -531,6 +549,8 @@ class Transmitter
                        $actor_profile = APContact::getByURL($item['author-link']);
                }
 
+               $exclusive = false;
+
                $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
 
                if ($item['private'] != Item::PRIVATE) {
@@ -555,6 +575,12 @@ class Transmitter
                        foreach ($terms as $term) {
                                $profile = APContact::getByURL($term['url'], false);
                                if (!empty($profile)) {
+                                       if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
+                                               $exclusive = true;
+                                               if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
+                                                       $data['cc'][] = $profile['followers'];
+                                               }
+                                       }
                                        $data['to'][] = $profile['url'];
                                }
                        }
@@ -564,8 +590,8 @@ class Transmitter
                        foreach ($terms as $term) {
                                $cid = Contact::getIdForURL($term['url'], $item['uid']);
                                if (!empty($cid) && in_array($cid, $receiver_list)) {
-                                       $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
-                                       if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
+                                       $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol', 'gsid'], ['id' => $cid, 'network' => Protocol::FEDERATED]);
+                                       if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
                                                continue;
                                        }
 
@@ -576,8 +602,8 @@ class Transmitter
                        }
 
                        foreach ($receiver_list as $receiver) {
-                               $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
-                               if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
+                               $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
+                               if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
                                        continue;
                                }
 
@@ -592,8 +618,8 @@ class Transmitter
                }
 
                if (!empty($item['parent'])) {
-                       $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
-                       while ($parent = Item::fetch($parents)) {
+                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
+                       while ($parent = Post::fetch($parents)) {
                                if ($parent['gravity'] == GRAVITY_PARENT) {
                                        $profile = APContact::getByURL($parent['owner-link'], false);
                                        if (!empty($profile)) {
@@ -602,7 +628,7 @@ class Transmitter
                                                        // But comments to forums aren't directed to the followers collection
                                                        // This rule is only valid when the actor isn't the forum.
                                                        // The forum needs to transmit their content to their followers.
-                                                       if (($profile['type'] == 'Group') && ($profile['url'] != $actor_profile['url'])) {
+                                                       if (($profile['type'] == 'Group') && ($profile['url'] != ($actor_profile['url'] ?? ''))) {
                                                                $data['to'][] = $profile['url'];
                                                        } else {
                                                                $data['cc'][] = $profile['url'];
@@ -610,8 +636,10 @@ class Transmitter
                                                                        $data['cc'][] = $actor_profile['followers'];
                                                                }
                                                        }
-                                               } else {
-                                                       // Public thread parent post always are directed to the followers
+                                               } elseif (!$exclusive) {
+                                                       // Public thread parent post always are directed to the followers.
+                                                       // This mustn't be done by posts that are directed to forum servers via the exclusive mention.
+                                                       // But possibly in that case we could add the "followers" collection of the forum to the message.
                                                        if (($item['private'] != Item::PRIVATE) && !$forum_mode) {
                                                                $data['cc'][] = $actor_profile['followers'];
                                                        }
@@ -689,6 +717,23 @@ class Transmitter
                return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
        }
 
+       /**
+        * Check if a given contact should be delivered via AP
+        *
+        * @param array $contact
+        * @param array $networks
+        * @return bool
+        * @throws Exception
+        */
+       private static function isAPContact(array $contact, array $networks)
+       {
+               if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
+                       return true;
+               }
+
+               return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
+       }
+
        /**
         * Fetches a list of inboxes of followers of a given user
         *
@@ -721,19 +766,15 @@ class Transmitter
                        $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
                }
 
-               $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false];
+               $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false, 'network' => Protocol::FEDERATED];
 
                if (!empty($uid)) {
                        $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
                }
 
-               $contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol'], $condition);
+               $contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
                while ($contact = DBA::fetch($contacts)) {
-                       if (Contact::isLocal($contact['url'])) {
-                               continue;
-                       }
-
-                       if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
+                       if (!self::isAPContact($contact, $networks)) {
                                continue;
                        }
 
@@ -747,7 +788,7 @@ class Transmitter
 
                        $profile = APContact::getByURL($contact['url'], false);
                        if (!empty($profile)) {
-                               if (empty($profile['sharedinbox']) || $personal) {
+                               if (empty($profile['sharedinbox']) || $personal || Contact::isLocal($contact['url'])) {
                                        $target = $profile['inbox'];
                                } else {
                                        $target = $profile['sharedinbox'];
@@ -810,15 +851,11 @@ class Transmitter
                                if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
                                        $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal, self::isAPPost($last_id)));
                                } else {
-                                       if (Contact::isLocal($receiver)) {
-                                               continue;
-                                       }
-
                                        $profile = APContact::getByURL($receiver, false);
                                        if (!empty($profile)) {
                                                $contact = Contact::getByURLForUser($receiver, $uid, false, ['id']);
 
-                                               if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
+                                               if (empty($profile['sharedinbox']) || $personal || $blindcopy || Contact::isLocal($receiver)) {
                                                        $target = $profile['inbox'];
                                                } else {
                                                        $target = $profile['sharedinbox'];
@@ -842,38 +879,41 @@ class Transmitter
         * @return array
         * @throws \Exception
         */
-       public static function ItemArrayFromMail($mail_id)
+       public static function ItemArrayFromMail($mail_id, $use_title = false)
        {
                $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
                if (!DBA::isResult($mail)) {
                        return [];
                }
 
-               $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
-
-               $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
 
                // Making the post more compatible for Mastodon by:
                // - Making it a note and not an article (no title)
                // - Moving the title into the "summary" field that is used as a "content warning"
-               $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
-               $mail['title'] = '';
-
-               $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
-               $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
-               $mail['allow_gid'] = '';
-               $mail['deny_cid'] = '';
-               $mail['deny_gid'] = '';
-               $mail['private'] = true;
-               $mail['deleted'] = false;
-               $mail['edited'] = $mail['created'];
-               $mail['plink'] = $mail['uri'];
-               $mail['thr-parent'] = $reply['uri'];
-               $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
 
-               $mail['event-type'] = '';
-
-               $mail['parent'] = 0;
+               if (!$use_title) {
+                       $mail['body']         = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
+                       $mail['title']        = '';
+               }
+
+               $mail['author-link']      = $mail['owner-link'] = $mail['from-url'];
+               $mail['owner-id']         = $mail['author-id'];
+               $mail['allow_cid']        = '<'.$mail['contact-id'].'>';
+               $mail['allow_gid']        = '';
+               $mail['deny_cid']         = '';
+               $mail['deny_gid']         = '';
+               $mail['private']          = Item::PRIVATE;
+               $mail['deleted']          = false;
+               $mail['edited']           = $mail['created'];
+               $mail['plink']            = DI::baseUrl() . '/message/' . $mail['id'];
+               $mail['parent-uri']       = $reply['uri'];
+               $mail['parent-uri-id']    = $reply['uri-id'];
+               $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
+               $mail['gravity']          = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
+               $mail['event-type']       = '';
+               $mail['language']         = '';
+               $mail['parent']           = 0;
 
                return $mail;
        }
@@ -1026,20 +1066,20 @@ class Transmitter
        public static function createActivityFromItem(int $item_id, bool $object_mode = false)
        {
                Logger::info('Fetching activity', ['item' => $item_id]);
-               $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
+               $item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
                if (!DBA::isResult($item)) {
                        return false;
                }
 
                // In case of a forum post ensure to return the original post if author and forum are on the same machine
-               if (!empty($item['forum_mode'])) {
+               if (($item['gravity'] == GRAVITY_PARENT) && !empty($item['forum_mode'])) {
                        $author = Contact::getById($item['author-id'], ['nurl']);
                        if (!empty($author['nurl'])) {
                                $self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]);
                                if (!empty($self['uid'])) {
-                                       $forum_item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
-                                       if (DBA::isResult($item)) {
-                                               $item = $forum_item; 
+                                       $forum_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
+                                       if (DBA::isResult($forum_item)) {
+                                               $item = $forum_item;
                                        }
                                }
                        }
@@ -1050,25 +1090,27 @@ class Transmitter
                        return false;
                }
 
-               $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
-               $conversation = DBA::selectFirst('conversation', ['source'], $condition);
-               if (!$item['origin'] && DBA::isResult($conversation)) {
-                       $data = json_decode($conversation['source'], true);
-                       if (!empty($data['type'])) {
-                               if (in_array($data['type'], ['Create', 'Update'])) {
-                                       if ($object_mode) {
-                                               unset($data['@context']);
-                                               unset($data['signature']);
-                                       }
-                                       Logger::info('Return stored conversation', ['item' => $item_id]);
-                                       return $data;
-                               } elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
-                                       if (!empty($data['@context'])) {
-                                               $context = $data['@context'];
-                                               unset($data['@context']);
+               if (!$item['deleted']) {
+                       $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
+                       $conversation = DBA::selectFirst('conversation', ['source'], $condition);
+                       if (!$item['origin'] && DBA::isResult($conversation)) {
+                               $data = json_decode($conversation['source'], true);
+                               if (!empty($data['type'])) {
+                                       if (in_array($data['type'], ['Create', 'Update'])) {
+                                               if ($object_mode) {
+                                                       unset($data['@context']);
+                                                       unset($data['signature']);
+                                               }
+                                               Logger::info('Return stored conversation', ['item' => $item_id]);
+                                               return $data;
+                                       } elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
+                                               if (!empty($data['@context'])) {
+                                                       $context = $data['@context'];
+                                                       unset($data['@context']);
+                                               }
+                                               unset($data['actor']);
+                                               $object = $data;
                                        }
-                                       unset($data['actor']);
-                                       $object = $data;
                                }
                        }
                }
@@ -1087,7 +1129,9 @@ class Transmitter
                        $data = [];
                }
 
-               if (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) {
+               if ($type == 'Delete') {
+                       $data['id'] = Item::newURI($item['uid'], $item['guid']) . '/' . $type;;
+               } elseif (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) {
                        $data['id'] = $item['uri'];
                } else {
                        $data['id'] = $item['uri'] . '/' . $type;
@@ -1237,53 +1281,88 @@ class Transmitter
        {
                $attachments = [];
 
-               // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
-               // It will be reactivated, once this cleared.
-               /*
-               $attach_data = BBCode::getAttachmentData($item['body']);
-               if (!empty($attach_data['url'])) {
-                       $attachment = ['type' => 'Page',
-                               'mediaType' => 'text/html',
-                               'url' => $attach_data['url']];
-
-                       if (!empty($attach_data['title'])) {
-                               $attachment['name'] = $attach_data['title'];
+               $uriids = [$item['uri-id']];
+               $shared = BBCode::fetchShareAttributes($item['body']);
+               if (!empty($shared['guid'])) {
+                       $shared_item = Post::selectFirst(['uri-id'], ['guid' => $shared['guid']]);
+                       if (!empty($shared_item['uri-id'])) {
+                               $uriids[] = $shared_item['uri-id'];
                        }
+               }
 
-                       if (!empty($attach_data['description'])) {
-                               $attachment['summary'] = $attach_data['description'];
-                       }
+               $urls = [];
+               foreach ($uriids as $uriid) {
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
+                               }
+                               $urls[] = $attachment['url'];
+
+                               $attach = ['type' => 'Document',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                               
+                               if (!empty($attachment['height'])) {
+                                       $attach['height'] = $attachment['height'];
+                               }
 
-                       if (!empty($attach_data['image'])) {
-                               $imgdata = Images::getInfoFromURLCached($attach_data['image']);
-                               if ($imgdata) {
-                                       $attachment['icon'] = ['type' => 'Image',
-                                               'mediaType' => $imgdata['mime'],
-                                               'width' => $imgdata[0],
-                                               'height' => $imgdata[1],
-                                               'url' => $attach_data['image']];
+                               if (!empty($attachment['width'])) {
+                                       $attach['width'] = $attachment['width'];
+                               }
+       
+                               if (!empty($attachment['preview'])) {
+                                       $attach['image'] = $attachment['preview'];
                                }
-                       }
 
-                       $attachments[] = $attachment;
-               }
-               */
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
-                       $attachments[] = ['type' => 'Document',
-                               'mediaType' => $attachment['mimetype'],
-                               'url' => $attachment['url'],
-                               'name' => $attachment['description']];
+                               $attachments[] = $attach;
+                       }
                }
 
                if ($type != 'Note') {
                        return $attachments;
                }
 
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
-                       $attachments[] = ['type' => 'Document',
-                               'mediaType' => $attachment['mimetype'],
-                               'url' => $attachment['url'],
-                               'name' => $attachment['description']];
+               foreach ($uriids as $uriid) {
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
+                               }
+                               $urls[] = $attachment['url'];
+
+                               $attach = ['type' => 'Document',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                               
+                               if (!empty($attachment['height'])) {
+                                       $attach['height'] = $attachment['height'];
+                               }
+
+                               if (!empty($attachment['width'])) {
+                                       $attach['width'] = $attachment['width'];
+                               }
+
+                               if (!empty($attachment['preview'])) {
+                                       $attach['image'] = $attachment['preview'];
+                               }
+
+                               $attachments[] = $attach;       
+                       }
+                       // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
+                       // It will be activated, once this cleared.
+                       /*
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::HTML]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
+                               }
+                               $urls[] = $attachment['url'];
+
+                               $attachments[] = ['type' => 'Page',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                       }*/
                }
 
                return $attachments;
@@ -1307,7 +1386,28 @@ class Transmitter
                        return $match[0];
                }
 
-               return '[url=' . ($data['alias'] ?: $data['url']) . ']@' . $data['nick'] . '[/url]';
+               return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]';
+       }
+
+       /**
+        * Callback function to replace a Friendica style mention in a mention for a summary
+        *
+        * @param array $match Matching values for the callback
+        * @return string Replaced mention
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function mentionAddrCallback($match)
+       {
+               if (empty($match[1])) {
+                       return '';
+               }
+
+               $data = Contact::getByURL($match[1], false, ['addr']);
+               if (empty($data['addr'])) {
+                       return $match[0];
+               }
+
+               return '@' . $data['addr'];
        }
 
        /**
@@ -1385,7 +1485,7 @@ class Transmitter
         * @return array with the event data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function createEvent($item)
+       private static function createEvent($item)
        {
                $event = [];
                $event['name'] = $item['event-summary'];
@@ -1401,6 +1501,8 @@ class Transmitter
                        $event['location'] = self::createLocation($item);
                }
 
+               $event['dfrn:adjust'] = (bool)$item['event-adjust'];
+
                return $event;
        }
 
@@ -1469,10 +1571,21 @@ class Transmitter
 
                if ($type == 'Note') {
                        $body = $item['raw-body'] ?? self::removePictures($body);
-               } elseif (($type == 'Article') && empty($data['summary'])) {
-                       $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
                }
 
+               /**
+                * @todo Improve the automated summary
+                * This part is currently deactivated. The automated summary seems to be more
+                * confusing than helping. But possibly we will find a better way.
+                * So the code is left here for now as a reminder
+                *
+                * } elseif (($type == 'Article') && empty($data['summary'])) {
+                *              $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
+                *              $summary = preg_replace_callback($regexp, ['self', 'mentionAddrCallback'], $body);
+                *              $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000));
+                * }
+                */
+
                if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
                        $body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
                }
@@ -1495,7 +1608,7 @@ class Transmitter
                        $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
                        $richbody = BBCode::removeAttachment($richbody);
 
-                       $data['contentMap'][$language] = BBCode::convert($richbody, false);
+                       $data['contentMap'][$language] = BBCode::convert($richbody, false, BBCode::EXTERNAL);
                }
 
                $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
@@ -1626,7 +1739,7 @@ class Transmitter
                        return [];
                }
 
-               $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
+               $reshared_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['guid' => $reshared['guid']]);
                if (!DBA::isResult($reshared_item)) {
                        return [];
                }
@@ -1885,7 +1998,7 @@ class Transmitter
 
                $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
                        'author-id' => Contact::getPublicIdByUserId($uid)];
-               if (Item::exists($condition)) {
+               if (Post::exists($condition)) {
                        Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
                        return false;
                }