]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge branch 'post-reason' of github.com:annando/friendica into post-reason
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index d7995a8d1a37f53cf21e13c0cc273d7b5c90ad0d..ecd18431855713b3bc940d3db9179a0d5c13ee56 100644 (file)
@@ -31,7 +31,6 @@ use Friendica\Database\DBA;
 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\Photo;
@@ -1190,10 +1189,10 @@ class Transmitter
         *
         * @param integer $item_id Item id
         * @param boolean $force Force new cache entry
-        * @return array with the activity
+        * @return array|false activity or false on failure
         * @throws \Exception
         */
-       public static function createCachedActivityFromItem(int $item_id, bool $force = false): array
+       public static function createCachedActivityFromItem(int $item_id, bool $force = false)
        {
                $cachekey = 'APDelivery:createActivity:' . $item_id;
 
@@ -1232,30 +1231,22 @@ class Transmitter
                }
 
                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;
-                                       }
+                       $data = Post\Activity::getByURIId($item['uri-id']);
+                       if (!$item['origin'] && !empty($data)) {
+                               if ($object_mode) {
+                                       unset($data['@context']);
+                                       unset($data['signature']);
                                }
+                               Logger::info('Return stored conversation', ['item' => $item_id]);
+                               return $data;
                        }
                }
 
+               if (!$item['origin'] && empty($object)) {
+                       Logger::debug('Post is not ours and is not stored', ['id' => $item_id, 'uri-id' => $item['uri-id']]);
+                       return false;
+               }
+
                $type = self::getTypeOfItem($item);
 
                if (!$object_mode) {
@@ -1271,7 +1262,7 @@ class Transmitter
                }
 
                if ($type == 'Delete') {
-                       $data['id'] = Item::newURI($item['uid'], $item['guid']) . '/' . $type;;
+                       $data['id'] = Item::newURI($item['guid']) . '/' . $type;;
                } elseif (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) {
                        $data['id'] = $item['uri'];
                } else {
@@ -1515,26 +1506,6 @@ class Transmitter
                return $body;
        }
 
-       /**
-        * Fetches the "context" value for a givem item array from the "conversation" table
-        *
-        * @param array $item Item array
-        * @return string with context url
-        * @throws \Exception
-        */
-       private static function fetchContextURLForItem(array $item): string
-       {
-               $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
-               if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
-                       $context_uri = $conversation['conversation-href'];
-               } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
-                       $context_uri = $conversation['conversation-uri'];
-               } else {
-                       $context_uri = $item['parent-uri'] . '#context';
-               }
-               return $context_uri;
-       }
-
        /**
         * Returns if the post contains sensitive content ("nsfw")
         *
@@ -1646,7 +1617,7 @@ class Transmitter
                $data['url'] = $link ?? $item['plink'];
                $data['attributedTo'] = $item['author-link'];
                $data['sensitive'] = self::isSensitive($item['uri-id']);
-               $data['context'] = self::fetchContextURLForItem($item);
+               $data['conversation'] = $data['context'] = $item['conversation'];
 
                if (!empty($item['title'])) {
                        $data['name'] = BBCode::toPlaintext($item['title'], false);
@@ -2161,14 +2132,14 @@ class Transmitter
        /**
         * Reject a contact request or terminates the contact relation
         *
-        * @param string  $target Target profile
-        * @param integer $id Object id
-        * @param integer $uid    User ID
+        * @param string $target   Target profile
+        * @param string $objectId Object id
+        * @param int    $uid      User ID
         * @return bool Operation success
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function sendContactReject(string $target, int $id, int $uid): bool
+       public static function sendContactReject(string $target, string $objectId, int $uid): bool
        {
                $profile = APContact::getByURL($target);
                if (empty($profile['inbox'])) {
@@ -2181,9 +2152,9 @@ class Transmitter
                        '@context' => ActivityPub::CONTEXT,
                        'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Reject',
-                       'actor' => $owner['url'],
+                       'actor'  => $owner['url'],
                        'object' => [
-                               'id' => (string)$id,
+                               'id' => $objectId,
                                'type' => 'Follow',
                                'actor' => $profile['url'],
                                'object' => $owner['url']
@@ -2192,7 +2163,7 @@ class Transmitter
                        'to' => [$profile['url']],
                ];
 
-               Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
+               Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $objectId);
 
                $signed = LDSignature::sign($data, $owner);
                return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -2222,12 +2193,12 @@ class Transmitter
                        return false;
                }
 
-               $id = DI::baseUrl() . '/activity/' . System::createGUID();
+               $objectId = DI::baseUrl() . '/activity/' . System::createGUID();
 
                $owner = User::getOwnerDataById($uid);
                $data = [
                        '@context' => ActivityPub::CONTEXT,
-                       'id' => $id,
+                       'id' => $objectId,
                        'type' => 'Undo',
                        'actor' => $owner['url'],
                        'object' => [
@@ -2240,7 +2211,7 @@ class Transmitter
                        'to' => [$profile['url']],
                ];
 
-               Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id);
+               Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $objectId);
 
                $signed = LDSignature::sign($data, $owner);
                return HTTPSignature::transmit($signed, $profile['inbox'], $uid);