]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Issue 12977: Ensure to fetch quoted posts
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index 5a51d31c4140dbaae9254eebeb4c14b3565f19be..1963d7e057495d60c169bd82ffa424a14f184767 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;
@@ -94,7 +93,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 +244,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);
@@ -506,7 +508,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 +519,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 +573,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;
@@ -856,11 +863,6 @@ 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'] ?? '');
@@ -944,7 +946,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;
@@ -1276,8 +1278,10 @@ class Processor
                                                $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 = '';
                                        }
 
@@ -1562,11 +1566,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'];
@@ -2144,61 +2148,4 @@ class Processor
 
                return $body;
        }
-
-       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['gravity'] = Item::GRAVITY_COMMENT;
-               } else {
-                       $item['gravity'] = Item::GRAVITY_PARENT;
-               }
-
-               $item = DI::contentItem()->expandTags($item);
-
-               return $item;
-       }
 }