]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Merge pull request #13330 from annando/relay-tags
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index dc618370a01cd9bfbec785ba43cecb66499a7808..3d020a747223bf91a5e4b8c7da28e9211ba28885 100644 (file)
@@ -35,7 +35,6 @@ use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Event;
-use Friendica\Model\Group;
 use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
@@ -43,6 +42,7 @@ use Friendica\Model\Mail;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Model\Post;
+use Friendica\Moderation\Entity\Report;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Delivery;
@@ -94,7 +94,9 @@ class Processor
         */
        public static function normalizeMentionLinks(string $body): string
        {
-               return preg_replace('%\[url=([^\[\]]*)]([#@!])(.*?)\[/url]%ism', '$2[url=$1]$3[/url]', $body);
+               $body = preg_replace('%\[url=([^\[\]]*)]([#@!])(.*?)\[/url]%ism', '$2[url=$1]$3[/url]', $body);
+               $body = preg_replace('%([#@!])\[zrl=([^\[\]]*)](.*?)\[/zrl]%ism', '$1[url=$2]$3[/url]', $body);
+               return $body;
        }
 
        /**
@@ -243,6 +245,7 @@ class Processor
                $item['changed'] = DateTimeFormat::utcNow();
                $item['edited'] = DateTimeFormat::utc($activity['updated']);
 
+               Post\Media::deleteByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE, Post\Media::HTML]);
                $item = self::processContent($activity, $item);
                if (empty($item)) {
                        Queue::remove($activity);
@@ -413,13 +416,13 @@ class Processor
                        $item['post-type'] = Item::PT_NOTE;
                }
 
-               $item['isForum'] = false;
+               $item['isGroup'] = false;
 
                if (!empty($activity['thread-completion'])) {
                        if ($activity['thread-completion'] != $item['owner-id']) {
                                $actor = Contact::getById($activity['thread-completion'], ['url']);
                                $item['causer-link'] = $actor['url'];
-                               $item['causer-id'] = $activity['thread-completion'];
+                               $item['causer-id']   = $activity['thread-completion'];
                                Logger::info('Use inherited actor as causer.', ['id' => $item['owner-id'], 'activity' => $activity['thread-completion'], 'owner' => $item['owner-link'], 'actor' => $actor['url']]);
                        } else {
                                // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
@@ -429,10 +432,41 @@ class Processor
                        }
 
                        $item['owner-link'] = $item['author-link'];
-                       $item['owner-id'] = $item['author-id'];
+                       $item['owner-id']   = $item['author-id'];
+               }
+
+               if (!$item['isGroup'] && !empty($activity['receiver_urls']['as:audience'])) {
+                       foreach ($activity['receiver_urls']['as:audience'] as $audience) {
+                               $actor = APContact::getByURL($audience, false);
+                               if (($actor['type'] ?? 'Person') == 'Group') {
+                                       Logger::debug('Group post detected via audience.', ['audience' => $audience, 'actor' => $activity['actor'], 'author' => $activity['author']]);
+                                       $item['isGroup']    = true;
+                                       $item['group-link'] = $item['owner-link'] = $audience;
+                                       $item['owner-id']   = Contact::getIdForURL($audience);
+                                       break;
+                               }
+                       }
                } else {
-                       $actor = APContact::getByURL($item['owner-link'], false);
-                       $item['isForum'] = ($actor['type'] ?? 'Person') == 'Group';
+                       $owner = APContact::getByURL($item['owner-link'], false);
+               }
+
+               if (!$item['isGroup'] && (($owner['type'] ?? 'Person') == 'Group')) {
+                       Logger::debug('Group post detected via owner.', ['actor' => $activity['actor'], 'author' => $activity['author']]);
+                       $item['isGroup']    = true;
+                       $item['group-link'] = $item['owner-link'];
+               } elseif (!empty($item['causer-link'])) {
+                       $causer = APContact::getByURL($item['causer-link'], false);
+               }
+
+               if (!$item['isGroup'] && (($causer['type'] ?? 'Person') == 'Group')) {
+                       Logger::debug('Group post detected via causer.', ['actor' => $activity['actor'], 'author' => $activity['author'], 'causer' => $item['causer-link']]);
+                       $item['isGroup']    = true;
+                       $item['group-link'] = $item['causer-link'];
+               }
+
+               if (!empty($item['group-link']) && empty($item['causer-link'])) {
+                       $item['causer-link'] = $item['group-link'];
+                       $item['causer-id']   = Contact::getIdForURL($item['causer-link']);
                }
 
                $item['uri'] = $activity['id'];
@@ -506,7 +540,7 @@ class Processor
                $recursion_depth = $activity['recursion-depth'] ?? 0;
 
                if (!$in_background && ($recursion_depth < DI::config()->get('system', 'max_recursion_depth'))) {
-                       Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+                       Logger::info('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
                        $result = self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
                        if (empty($result) && self::isActivityGone($activity['reply-to-id'])) {
                                Logger::notice('The activity is gone, the queue entry will be deleted', ['parent' => $activity['reply-to-id']]);
@@ -517,10 +551,10 @@ class Processor
                        } elseif (!empty($result)) {
                                $exists = Post::exists(['uri' => [$result, $activity['reply-to-id']]]);
                                if ($exists) {
-                                       Logger::notice('The activity has been fetched and created.', ['parent' => $result]);
+                                       Logger::info('The activity has been fetched and created.', ['parent' => $result]);
                                        return $result;
                                } elseif (DI::config()->get('system', 'fetch_by_worker') || DI::config()->get('system', 'decoupled_receiver')) {
-                                       Logger::notice('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
+                                       Logger::info('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
                                } else {
                                        Logger::notice('The activity exists but has not been created, the queue entry will be deleted.', ['parent' => $result]);
                                        if (!empty($activity['entry-id'])) {
@@ -571,7 +605,12 @@ class Processor
         */
        public static function isActivityGone(string $url): bool
        {
-               $curlResult = HTTPSignature::fetchRaw($url, 0);
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, 0);
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
+                       return true;
+               }
 
                if (Network::isUrlBlocked($url)) {
                        return true;
@@ -833,6 +872,9 @@ class Processor
                        if ($id) {
                                $shared_item = Post::selectFirst(['uri-id'], ['id' => $id]);
                                $item['quote-uri-id'] = $shared_item['uri-id'];
+                       } elseif ($uri_id = ItemURI::getIdByURI($activity['quote-url'], false)) {
+                               Logger::info('Quote was not fetched but the uri-id existed', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'quote' => $activity['quote-url'], 'uri-id' => $uri_id]);
+                               $item['quote-uri-id'] = $uri_id;
                        } else {
                                Logger::info('Quote was not fetched', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'quote' => $activity['quote-url']]);
                        }
@@ -856,17 +898,25 @@ class Processor
                                        Logger::warning('Unknown parent item.', ['uri' => $parent_uri]);
                                        return false;
                                }
-                               if (!empty($activity['type']) && in_array($activity['type'], Receiver::CONTENT_TYPES) && ($item['private'] == Item::PRIVATE) && ($parent['private'] != Item::PRIVATE)) {
-                                       Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
-                                       return false;
-                               }
-
                                $content = self::removeImplicitMentionsFromBody($content, $parent);
                        }
                        $item['content-warning'] = HTML::toBBCode($activity['summary'] ?? '');
                        $item['raw-body'] = $item['body'] = $content;
                }
 
+               if (!empty($item['author-id']) && ($item['author-id'] == $item['owner-id'])) {
+                       foreach (Tag::getFromBody($item['body'], Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]) as $tag) {
+                               $actor = APContact::getByURL($tag[2], false);
+                               if (($actor['type'] ?? 'Person') == 'Group') {
+                                       Logger::debug('Group post detected via exclusive mention.', ['mention' => $actor['url'], 'actor' => $activity['actor'], 'author' => $activity['author']]);
+                                       $item['isGroup']    = true;
+                                       $item['group-link'] = $item['owner-link'] = $actor['url'];
+                                       $item['owner-id']   = Contact::getIdForURL($actor['url']);
+                                       break;
+                               }
+                       }
+               }
+
                self::storeFromBody($item);
                self::storeTags($item['uri-id'], $activity['tags']);
 
@@ -944,7 +994,7 @@ class Processor
                        return true;
                }
 
-               if (in_array($activity['completion-mode'] ?? Receiver::COMPLETION_NONE, [Receiver::COMPLETION_MANUAL, Receiver::COMPLETION_ANNOUCE])) {
+               if (in_array($activity['completion-mode'] ?? Receiver::COMPLETION_NONE, [Receiver::COMPLETION_MANUAL, Receiver::COMPLETION_ANNOUNCE])) {
                        // Manual completions and completions caused by reshares are allowed without any further checks.
                        Logger::debug('Message is in completion mode - accepted', ['mode' => $activity['completion-mode'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]);
                        return true;
@@ -1021,6 +1071,9 @@ class Processor
                                case Receiver::TARGET_BCC:
                                        $item['post-reason'] = Item::PR_BCC;
                                        break;
+                               case Receiver::TARGET_AUDIENCE:
+                                       $item['post-reason'] = Item::PR_AUDIENCE;
+                                       break;
                                case Receiver::TARGET_FOLLOWER:
                                        $item['post-reason'] = Item::PR_FOLLOWER;
                                        break;
@@ -1051,10 +1104,10 @@ class Processor
                                $item['causer-id'] = ($item['gravity'] == Item::GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
                        }
 
-                       if ($item['isForum'] ?? false) {
-                               $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver);
+                       if ($item['isGroup']) {
+                               $item['contact-id'] = Contact::getIdForURL($item['group-link'], $receiver);
                        } else {
-                               $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver);
+                               $item['contact-id'] = Contact::getIdForURL($item['author-link'], $receiver);
                        }
 
                        if (($receiver != 0) && empty($item['contact-id'])) {
@@ -1066,8 +1119,8 @@ class Processor
                                continue;
                        }
 
-                       if (($receiver != 0) && ($item['gravity'] == Item::GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC])) {
-                               if (!($item['isForum'] ?? false)) {
+                       if (($receiver != 0) && ($item['gravity'] == Item::GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC, Item::PR_AUDIENCE])) {
+                               if (!$item['isGroup']) {
                                        if ($item['post-reason'] == Item::PR_BCC) {
                                                Logger::info('Top level post via BCC from a non sharer, ignoring', ['uid' => $receiver, 'contact' => $item['contact-id'], 'url' => $item['uri']]);
                                                continue;
@@ -1081,16 +1134,16 @@ class Processor
                                        }
                                }
 
-                               $is_forum = false;
+                               $isGroup = false;
                                $user = User::getById($receiver, ['account-type']);
                                if (!empty($user['account-type'])) {
-                                       $is_forum = ($user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
+                                       $isGroup = ($user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
                                }
 
                                if ((DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') == Item::COMPLETION_NONE)
-                                       && ((!$is_forum && !($item['isForum'] ?? false) && ($activity['type'] != 'as:Announce'))
+                                       && ((!$isGroup && !$item['isGroup'] && ($activity['type'] != 'as:Announce'))
                                        || !Contact::isSharingByURL($activity['actor'], $receiver))) {
-                                       Logger::info('Actor is a non sharer, is no forum or it is no announce', ['uid' => $receiver, 'actor' => $activity['actor'], 'url' => $item['uri'], 'type' => $activity['type']]);
+                                       Logger::info('Actor is a non sharer, is no group or it is no announce', ['uid' => $receiver, 'actor' => $activity['actor'], 'url' => $item['uri'], 'type' => $activity['type']]);
                                        continue;
                                }
 
@@ -1133,7 +1186,7 @@ class Processor
                // Store send a follow request for every reshare - but only when the item had been stored
                if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == Item::GRAVITY_PARENT) && !empty($item['author-link']) && ($item['author-link'] != $item['owner-link'])) {
                        $author = APContact::getByURL($item['owner-link'], false);
-                       // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
+                       // We send automatic follow requests for reshared messages. (We don't need though for group posts)
                        if ($author['type'] != 'Group') {
                                Logger::info('Send follow request', ['uri' => $item['uri'], 'stored' => $stored, 'to' => $item['author-link']]);
                                ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
@@ -1269,15 +1322,17 @@ class Processor
 
        public static function storeReceivers(int $uriid, array $receivers)
        {
-               foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC] as $element => $type) {
+               foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC, 'as:audience' => Tag::AUDIENCE, 'as:attributedTo' => Tag::ATTRIBUTED] as $element => $type) {
                        if (!empty($receivers[$element])) {
                                foreach ($receivers[$element] as $receiver) {
                                        if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
                                                $name = Receiver::PUBLIC_COLLECTION;
                                        } elseif ($path = parse_url($receiver, PHP_URL_PATH)) {
                                                $name = trim($path, '/');
+                                       } elseif ($host = parse_url($receiver, PHP_URL_HOST)) {
+                                               $name = $host;
                                        } else {
-                                               Logger::warning('Unable to coerce name from receiver', ['receiver' => $receiver]);
+                                               Logger::warning('Unable to coerce name from receiver', ['element' => $element, 'type' => $type, 'receiver' => $receiver]);
                                                $name = '';
                                        }
 
@@ -1523,6 +1578,7 @@ class Processor
                $activity['id'] = $object['id'];
                $activity['to'] = $object['to'] ?? [];
                $activity['cc'] = $object['cc'] ?? [];
+               $activity['audience'] = $object['audience'] ?? [];
                $activity['actor'] = $actor;
                $activity['object'] = $object;
                $activity['published'] = $published;
@@ -1562,11 +1618,11 @@ class Processor
                }
 
                if (($completion == Receiver::COMPLETION_RELAY) && Queue::exists($url, 'as:Create')) {
-                       Logger::notice('Activity has already been queued.', ['url' => $url, 'object' => $activity['id']]);
+                       Logger::info('Activity has already been queued.', ['url' => $url, 'object' => $activity['id']]);
                } elseif (ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer, '', $completion)) {
-                       Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
+                       Logger::info('Activity had been fetched and processed.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
                } else {
-                       Logger::notice('Activity had been fetched and will be processed later.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
+                       Logger::info('Activity had been fetched and will be processed later.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
                }
 
                return $activity['id'];
@@ -1602,7 +1658,7 @@ class Processor
                $tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
                if (!empty($tags)) {
                        foreach ($tags as $tag) {
-                               if ($tag['type'] != 'Hashtag') {
+                               if (($tag['type'] != 'Hashtag') && !strpos($tag['type'], ':Hashtag')) {
                                        continue;
                                }
                                $messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
@@ -1838,8 +1894,8 @@ class Processor
         */
        public static function ReportAccount(array $activity)
        {
-               $account_id = Contact::getIdForURL($activity['object_id']);
-               if (empty($account_id)) {
+               $account = Contact::getByURL($activity['object_id'], null, ['id', 'gsid']);
+               if (empty($account)) {
                        Logger::info('Unknown account', ['activity' => $activity]);
                        Queue::remove($activity);
                        return;
@@ -1860,10 +1916,10 @@ class Processor
                        }
                }
 
-               $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids);
+               $report = DI::reportFactory()->createFromReportsRequest(System::getRules(true), $reporter_id, $account['id'], $account['gsid'], $activity['content'], 'other', false, $uri_ids);
                DI::report()->save($report);
 
-               Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
+               Logger::info('Stored report', ['reporter' => $reporter_id, 'account' => $account, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
        }
 
        /**
@@ -2144,70 +2200,4 @@ class Processor
 
                return $body;
        }
-
-       /**
-        * Create an item array from client to server object data
-        *
-        * @param array $object_data
-        * @param array $application
-        * @param integer $uid
-        * @return array
-        */
-       public static function processC2SContent(array $object_data, array $application, int $uid): array
-       {
-               $owner = User::getOwnerDataById($uid);
-
-               $item = [];
-
-               $item['network']    = Protocol::DFRN;
-               $item['uid']        = $uid;
-               $item['verb']       = Activity::POST;
-               $item['contact-id'] = $owner['id'];
-               $item['author-id']  = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
-               $item['title']      = $object_data['name'];
-               $item['body']       = Markdown::toBBCode($object_data['content']);
-               $item['app']        = $application['name'] ?? 'API';
-
-               if (!empty($object_data['target'][Receiver::TARGET_GLOBAL])) {
-                       $item['allow_cid'] = '';
-                       $item['allow_gid'] = '';
-                       $item['deny_cid']  = '';
-                       $item['deny_gid']  = '';
-                       $item['private']   = Item::PUBLIC;
-               } elseif (isset($object_data['target'][Receiver::TARGET_GLOBAL])) {
-                       $item['allow_cid'] = '';
-                       $item['allow_gid'] = '';
-                       $item['deny_cid']  = '';
-                       $item['deny_gid']  = '';
-                       $item['private']   = Item::UNLISTED;
-               } elseif (!empty($object_data['target'][Receiver::TARGET_FOLLOWER])) {
-                       $item['allow_cid'] = '';
-                       $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
-                       $item['deny_cid']  = '';
-                       $item['deny_gid']  = '';
-                       $item['private'] = Item::PRIVATE;
-               } else {
-                       // @todo Set permissions via the $object_data['target'] array
-                       $item['allow_cid'] = '<' . $owner['id'] . '>';
-                       $item['allow_gid'] = '';
-                       $item['deny_cid']  = '';
-                       $item['deny_gid']  = '';
-                       $item['private'] = Item::PRIVATE;
-               }
-
-               if (!empty($object_data['summary'])) {
-                       $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $object_data['summary'] . "[/abstract]\n" . $item['body'];
-               }
-
-               if ($object_data['reply-to-id']) {
-                       $item['thr-parent'] = $object_data['reply-to-id'];
-                       $item['gravity'] = Item::GRAVITY_COMMENT;
-               } else {
-                       $item['gravity'] = Item::GRAVITY_PARENT;
-               }
-
-               $item = DI::contentItem()->expandTags($item);
-
-               return $item;
-       }
 }