]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Receiver.php
Add and delete entries
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
index b55636a30ede1ee0e98ee612385e8e15688bb7a4..11e7151e1e9f57e7bff9b0cd1d86390bce6a8d12 100644 (file)
@@ -98,19 +98,6 @@ class Receiver
 
                $ldactivity = JsonLD::compact($activity);
 
-               $http_signer = HTTPSignature::getSigner($body, $header);
-               if ($http_signer === false) {
-                       Logger::warning('Invalid HTTP signature, message will be discarded.');
-                       return;
-               } elseif (empty($http_signer)) {
-                       Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
-                       return;
-               } else {
-                       Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
-               }
-
-               self::enqueuePost($ldactivity, $http_signer, $uid);
-
                $actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id') ?? '';
 
                $apcontact = APContact::getByURL($actor);
@@ -125,6 +112,17 @@ class Receiver
                        APContact::unmarkForArchival($apcontact);
                }
 
+               $http_signer = HTTPSignature::getSigner($body, $header);
+               if ($http_signer === false) {
+                       Logger::warning('Invalid HTTP signature, message will be discarded.');
+                       return;
+               } elseif (empty($http_signer)) {
+                       Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
+                       return;
+               } else {
+                       Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
+               }
+
                $signer = [$http_signer];
 
                Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
@@ -158,32 +156,45 @@ class Receiver
                }
 
                $fetchQueue = new FetchQueue();
-               self::processActivity($fetchQueue, $ldactivity, $body, $uid, $trust_source, true, $signer);
+               self::processActivity($fetchQueue, $ldactivity, $body, $uid, $trust_source, true, $signer, $http_signer);
                $fetchQueue->process();
        }
 
-       private static function enqueuePost(array $ldactivity = [], string $signer, int $uid)
+       private static function enqueuePost(array $ldactivity = [], string $type, int $uid, string $http_signer): array
        {
-               if (empty($ldactivity['as:object'])) {
-                       return;
-               }
-
-               $url = JsonLD::fetchElement($ldactivity, 'as:object', '@id');
                $fields = [
-                       'url' => $url,
-                       'in-reply-to-url' => JsonLD::fetchElement($ldactivity['as:object'], 'as:inReplyTo', '@id'),
-                       'signer' => $signer,
-                       'type' => JsonLD::fetchElement($ldactivity, '@type'),
-                       'object-type' => JsonLD::fetchElement($ldactivity['as:object'], '@type'),
+                       'activity-id' => $ldactivity['id'],
+                       'object-id' => $ldactivity['object_id'],
+                       'type' => $type,
+                       'object-type' => $ldactivity['object_type'],
                        'activity' => json_encode($ldactivity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
                        'received' => DateTimeFormat::utcNow(),
                ];
-               DBA::insert('inbox-queue', $fields, Database::INSERT_IGNORE);
 
-               $queue = DBA::selectFirst('inbox-queue', ['id'], ['url' => $url]);
+               if (!empty($ldactivity['object_object_type'])) {
+                       $fields['object-object-type'] = $ldactivity['object_object_type'];
+               }
+
+               if (!empty($http_signer)) {
+                       $fields['signer'] = $http_signer;
+               }
+
+               DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
+
+               $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $ldactivity['id']]);
                if (!empty($queue['id'])) {
-                       DBA::insert('inbox-queue-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
+                       $ldactivity['entry-id'] = $queue['id'];
+                       DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
                }
+               return $ldactivity;
+       }
+
+       public static function removeFromQueue(array $activity = [])
+       {
+               if (empty($activity['entry-id'])) {
+                       return;
+               }
+               DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
        }
 
        /**
@@ -517,7 +528,7 @@ class Receiver
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function processActivity(FetchQueue $fetchQueue, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
+       public static function processActivity(FetchQueue $fetchQueue, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [], string $http_signer = '')
        {
                $type = JsonLD::fetchElement($activity, '@type');
                if (!$type) {
@@ -591,6 +602,8 @@ class Receiver
                        $object_data['from-relay'] = $activity['from-relay'];
                }
 
+               $object_data = self::enqueuePost($object_data, $type, $uid, $http_signer);
+
                if (in_array('as:Question', [$object_data['object_type'] ?? '', $object_data['object_object_type'] ?? ''])) {
                        self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
                }