]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Raw content is now stored with announce messages as well
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index cc5c90d3b8a05ba4d6aade8e1d8034c9ab7aec0c..4fa2d33f764429e6c73702914e7ef04420712acc 100644 (file)
@@ -1,18 +1,35 @@
 <?php
 /**
- * @file src/Protocol/ActivityPub/Processor.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Config;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\APContact;
 use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
 use Friendica\Model\Event;
 use Friendica\Model\Item;
 use Friendica\Model\Mail;
@@ -182,7 +199,7 @@ class Processor
                }
 
                if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
-                       Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
+                       Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
                        self::fetchMissingActivity($activity['reply-to-id'], $activity);
                }
 
@@ -359,7 +376,7 @@ class Processor
                                        Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
                                        return false;
                                }
-                               if ($item_private && !$parent['private']) {
+                               if ($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;
                                }
@@ -426,12 +443,30 @@ class Processor
                }
 
                $item['network'] = Protocol::ACTIVITYPUB;
-               $item['private'] = !in_array(0, $activity['receiver']);
                $item['author-link'] = $activity['author'];
                $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
                $item['owner-link'] = $activity['actor'];
                $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
 
+               if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) {
+                       $item['private'] = Item::UNLISTED;
+               } elseif (in_array(0, $activity['receiver'])) {
+                       $item['private'] = Item::PUBLIC;
+               } else {
+                       $item['private'] = Item::PRIVATE;
+               }
+
+               if (!empty($activity['raw'])) {
+                       $item['source'] = $activity['raw'];
+                       $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
+                       $item['conversation-href'] = $activity['context'] ?? '';
+                       $item['conversation-uri'] = $activity['conversation'] ?? '';
+
+                       if (isset($activity['push'])) {
+                               $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
+                       }
+               }
+
                $isForum = false;
 
                if (!empty($activity['thread-completion'])) {
@@ -465,6 +500,10 @@ class Processor
                $stored = false;
 
                foreach ($activity['receiver'] as $receiver) {
+                       if ($receiver == -1) {
+                               continue;
+                       }
+
                        $item['uid'] = $receiver;
 
                        if ($isForum) {
@@ -514,7 +553,7 @@ class Processor
                }
 
                // Store send a follow request for every reshare - but only when the item had been stored
-               if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
+               if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($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)
                        if ($author['type'] != 'Group') {
@@ -592,7 +631,7 @@ class Processor
         *
         * @param string $url message URL
         * @param array $child activity array with the child of this message
-        * @return boolean success
+        * @return string fetched message URL
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function fetchMissingActivity($url, $child = [])
@@ -606,12 +645,12 @@ class Processor
                $object = ActivityPub::fetchContent($url, $uid);
                if (empty($object)) {
                        Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
-                       return false;
+                       return '';
                }
 
                if (empty($object['id'])) {
                        Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
-                       return false;
+                       return '';
                }
 
                if (!empty($child['author'])) {
@@ -648,10 +687,11 @@ class Processor
 
                $ldactivity['thread-completion'] = true;
 
-               ActivityPub\Receiver::processActivity($ldactivity);
-               Logger::log('Activity ' . $url . ' had been fetched and processed.');
+               ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity));
+
+               Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
 
-               return true;
+               return $activity['id'];
        }
 
        /**
@@ -897,7 +937,7 @@ class Processor
         */
        private static function getImplicitMentionList(array $parent)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return [];
                }
 
@@ -939,7 +979,7 @@ class Processor
         */
        private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return $body;
                }
 
@@ -962,7 +1002,7 @@ class Processor
 
        private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return $activity_tags;
                }