]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge branch 'develop' of github.com:annando/friendica into develop
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index d17e269edf9d516bf30c0db82546c1d8221d071f..553993edca300717b225bcf6a232e2c9ba7f5063 100644 (file)
@@ -427,12 +427,13 @@ class Transmitter
         * Returns an array with permissions of the thread parent of the given item array
         *
         * @param array $item
+        * @param bool  $is_forum_thread
         *
         * @return array with permissions
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function fetchPermissionBlockFromThreadParent($item)
+       private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_forum_thread)
        {
                if (empty($item['thr-parent-id'])) {
                        return [];
@@ -462,7 +463,9 @@ class Transmitter
                $type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc'];
                foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC]) as $receiver) {
                        if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
-                               $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
+                               if (!$is_forum_thread) {
+                                       $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
+                               }
                        } elseif (!in_array($receiver['url'], $exclude)) {
                                $permissions[$type[$receiver['type']]][] = $receiver['url'];
                        }
@@ -523,6 +526,14 @@ class Transmitter
                        $always_bcc = true;
                }
 
+               $parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
+               if (($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
+                       $profile = APContact::getByURL($parent['causer-link'], false);
+                       $is_forum_thread = isset($profile['type']) && $profile['type'] == 'Group';
+               } else {
+                       $is_forum_thread = false;
+               }
+
                if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery') || self::isAPPost($last_id)) {
                        // Will be activated in a later step
                        $networks = Protocol::FEDERATED;
@@ -553,7 +564,7 @@ class Transmitter
                                $data['cc'][] = $announce['actor']['url'];
                        }
 
-                       $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item));
+                       $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_forum_thread));
 
                        // Check if the item is completely public or unlisted
                        if ($item['private'] == Item::PUBLIC) {
@@ -619,7 +630,7 @@ class Transmitter
                }
 
                if (!empty($item['parent'])) {
-                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
+                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']], ['order' => ['id']]);
                        while ($parent = Post::fetch($parents)) {
                                if ($parent['gravity'] == GRAVITY_PARENT) {
                                        $profile = APContact::getByURL($parent['owner-link'], false);
@@ -633,11 +644,11 @@ class Transmitter
                                                                $data['to'][] = $profile['url'];
                                                        } else {
                                                                $data['cc'][] = $profile['url'];
-                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])) {
+                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])&& !$is_forum_thread) {
                                                                        $data['cc'][] = $actor_profile['followers'];
                                                                }
                                                        }
-                                               } elseif (!$exclusive) {
+                                               } elseif (!$exclusive && !$is_forum_thread) {
                                                        // Public thread parent post always are directed to the followers.
                                                        if ($item['private'] != Item::PRIVATE) {
                                                                $data['cc'][] = $actor_profile['followers'];
@@ -1456,10 +1467,28 @@ class Transmitter
                        return [];
                }
 
+               // We are treating posts differently when they are directed to a community.
+               // This is done to better support Lemmy. Most of the changes should work with other systems as well.
+               // But to not risk compatibility issues we currently perform the changes only for communities.
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       $isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
+                       $links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
+                       if ($isCommunityPost && (count($links) == 1)) {
+                               $link = $links[0]['url'];
+                       }
+               } else {
+                       $isCommunityPost = false;
+               }
+
                if ($item['event-type'] == 'event') {
                        $type = 'Event';
                } elseif (!empty($item['title'])) {
-                       $type = 'Article';
+                       if (!$isCommunityPost || empty($link)) {
+                               $type = 'Article';
+                       } else {
+                               // "Page" is used by Lemmy for posts that contain an external link
+                               $type = 'Page';
+                       }
                } else {
                        $type = 'Note';
                }
@@ -1491,7 +1520,7 @@ class Transmitter
                        $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
                }
 
-               $data['url'] = $item['plink'];
+               $data['url'] = $link ?? $item['plink'];
                $data['attributedTo'] = $item['author-link'];
                $data['sensitive'] = self::isSensitive($item['uri-id']);
                $data['context'] = self::fetchContextURLForItem($item);
@@ -1528,6 +1557,19 @@ class Transmitter
                if ($type == 'Event') {
                        $data = array_merge($data, self::createEvent($item));
                } else {
+                       if ($isCommunityPost) {
+                               // For community posts we remove the visible "!user@domain.tld".
+                               // This improves the look at systems like Lemmy.
+                               // Also in the future we should control the community delivery via other methods.
+                               $body = preg_replace("/!\[url\=[^\[\]]*\][^\[\]]*\[\/url\]/ism", '', $body);
+                       }
+
+                       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::setMentionsToNicknames($body);
 
                        $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);