]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Queue.php
Merge branch 'post-reason' of github.com:annando/friendica into post-reason
[friendica.git] / src / Protocol / ActivityPub / Queue.php
index faaf0aa3bc905b7deab933139ef6ef0dab4275e9..95a4cfa0d4557ecd5a34e33ef8ae5d413ac751dc 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\JsonLD;
 
 /**
  * This class handles the processing of incoming posts
@@ -125,11 +126,6 @@ class Queue
                        return;
                }
 
-               $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
-               while ($child = DBA::fetch($children)) {
-                       self::deleteById($child['id']);
-               }
-               DBA::close($children);
                DBA::delete('inbox-entry', ['id' => $entry['id']]);
        }
 
@@ -166,13 +162,14 @@ class Queue
         * Process the activity with the given id
         *
         * @param integer $id
-        * @return void
+        *
+        * @return bool
         */
-       public static function process(int $id)
+       public static function process(int $id): bool
        {
                $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
                if (empty($entry)) {
-                       return;
+                       return false;
                }
 
                Logger::debug('Processing queue entry', ['id' => $entry['id'], 'type' => $entry['type'], 'object-type' => $entry['object-type'], 'uri' => $entry['object-id'], 'in-reply-to' => $entry['in-reply-to-id']]);
@@ -196,6 +193,8 @@ class Queue
                if (!Receiver::routeActivities($activity, $type, $push)) {
                        self::remove($activity);
                }
+
+               return true;
        }
 
        /**
@@ -214,6 +213,7 @@ class Queue
                        Logger::debug('Process leftover entry', $entry);
                        self::process($entry['id']);
                }
+               DBA::close($entries);
        }
 
        /**
@@ -239,13 +239,81 @@ class Queue
         * Process all activities that are children of a given post url
         *
         * @param string $uri
-        * @return void
+        * @return int
         */
-       public static function processReplyByUri(string $uri)
+       public static function processReplyByUri(string $uri): int
        {
+               $count = 0;
                $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
                while ($entry = DBA::fetch($entries)) {
+                       $count += 1;
                        self::process($entry['id']);
                }
+               DBA::close($entries);
+               return $count;
+       }
+
+       /**
+        * Checks if there are children of the given uri
+        *
+        * @param string $uri
+        *
+        * @return bool
+        */
+       public static function hasChildren(string $uri): bool
+       {
+               return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
+       }
+
+       /**
+        * Prepare the queue entry.
+        * This is a test function that is used solely for development.
+        *
+        * @param integer $id
+        * @return array
+        */
+       public static function reprepareActivityById(int $id): array
+       {
+               $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
+               if (empty($entry)) {
+                       return [];
+               }
+
+               $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
+               if (!empty($receiver)) {
+                       $uid = $receiver['uid'];
+               } else {
+                       $uid = 0;
+               }
+
+               $trust_source = $entry['trust'];
+
+               $data     = json_decode($entry['activity'], true);
+               $activity = json_decode($data['raw'], true);
+
+               $ldactivity = JsonLD::compact($activity);
+               return [
+                       'data'  => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
+                       'trust' => $trust_source
+               ];
+       }
+
+       /**
+        * Set the trust for all untrusted entries.
+        * This is a test function that is used solely for development.
+        *
+        * @return void
+        */
+       public static function reprepareAll()
+       {
+               $entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
+               while ($entry = DBA::fetch($entries)) {
+                       $data = self::reprepareActivityById($entry['id'], false);
+                       if ($data['trust']) {
+                               DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
+                       }
+               }
+               DBA::close($entries);
+
        }
 }