]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Notifier.php
Merge remote-tracking branch 'upstream/develop' into no-item
[friendica.git] / src / Worker / Notifier.php
index 4ccfacd5cb63e4ca0bb0e82d3fe900721831ab76..f225adf11d4f7b1d034d4797061c1920fd59890e 100644 (file)
@@ -53,11 +53,30 @@ use Friendica\Protocol\Salmon;
 
 class Notifier
 {
-       public static function execute($cmd, $target_id)
+       public static function execute(string $cmd, int $post_uriid, int $sender_uid = 0)
        {
                $a = DI::app();
 
-               Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id]);
+               Logger::info('Invoked', ['cmd' => $cmd, 'target' => $post_uriid, 'sender_uid' => $sender_uid]);
+
+               if (!empty($sender_uid)) {
+                       $post = Post::selectFirst(['id'], ['uri-id' => $post_uriid, 'uid' => $sender_uid]);
+                       if (!DBA::isResult($post)) {
+                               Logger::warning('Post not found', ['uri-id' => $post_uriid, 'uid' => $sender_uid]);
+                               return;
+                       }
+                       $target_id = $post['id'];
+               } elseif (!in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
+                       $post = Post::selectFirst(['id'], ['item-id' => $post_uriid]);
+                       $post = Post::selectFirst(['id', 'uid', 'uri-id'], ['item-id' => $post_uriid]);
+                       if (DBA::isResult($post)) {
+                               $target_id = $post['id'];
+                               $sender_uid = $post['uid'];
+                               $post_uriid = $post['uri-id'];
+                       }
+               } else {
+                       $target_id = $post_uriid;
+               }
 
                $top_level = false;
                $recipients = [];
@@ -85,7 +104,7 @@ class Notifier
                                $ap_contacts = array_merge($ap_contacts, $receivers);
                                Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'target' => $target_id, 'inbox' => $inbox]);
                                Worker::add(['priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true],
-                                       'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers);
+                                       'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers, $post_uriid);
                        }
                } elseif ($cmd == Delivery::SUGGESTION) {
                        $suggest = DI::fsuggest()->getById($target_id);
@@ -100,8 +119,8 @@ class Notifier
                        $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'addr', 'network', 'protocol', 'batch'], $condition);
                } else {
                        // find ancestors
-                       $condition = ['id' => $target_id, 'visible' => true, 'moderated' => false];
-                       $target_item = Item::selectFirst([], $condition);
+                       $condition = ['id' => $target_id, 'visible' => true];
+                       $target_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
 
                        if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
                                Logger::info('No target item', ['cmd' => $cmd, 'target' => $target_id]);
@@ -117,15 +136,15 @@ class Notifier
                                return;
                        }
 
-                       $condition = ['parent' => $target_item['parent'], 'visible' => true, 'moderated' => false];
+                       $condition = ['parent' => $target_item['parent'], 'visible' => true];
                        $params = ['order' => ['id']];
-                       $items_stmt = Item::select([], $condition, $params);
+                       $items_stmt = Post::select(Item::DELIVER_FIELDLIST, $condition, $params);
                        if (!DBA::isResult($items_stmt)) {
                                Logger::info('No item found', ['cmd' => $cmd, 'target' => $target_id]);
                                return;
                        }
 
-                       $items = Item::inArray($items_stmt);
+                       $items = Post::toArray($items_stmt);
 
                        // avoid race condition with deleting entries
                        if ($items[0]['deleted']) {
@@ -165,7 +184,7 @@ class Notifier
 
                        $fields = ['network', 'author-id', 'author-link', 'author-network', 'owner-id'];
                        $condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
-                       $thr_parent = Item::selectFirst($fields, $condition);
+                       $thr_parent = Post::selectFirst($fields, $condition);
                        if (empty($thr_parent)) {
                                $thr_parent = $parent;
                        }
@@ -305,8 +324,8 @@ class Notifier
                                /// @todo Possibly we should not uplink when the author is the forum itself?
 
                                if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK)
-                                       && ($target_item['verb'] != Activity::ANNOUNCE)) {
-                                       Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $target_id);
+                                       && ($target_item['verb'] != Activity::ANNOUNCE)) {                                              
+                                       Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $post_uriid, $sender_uid);
                                }
 
                                foreach ($items as $item) {
@@ -452,13 +471,13 @@ class Notifier
 
                        $conversants = array_merge($contacts, $relay_list);
 
-                       $delivery_queue_count += self::delivery($cmd, $target_id, $target_item, $thr_parent, $owner, $batch_delivery, $conversants, $ap_contacts, []);
+                       $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, true, $conversants, $ap_contacts, []);
 
                        $push_notify = true;
                }
 
                $contacts = DBA::toArray($delivery_contacts_stmt);
-               $delivery_queue_count += self::delivery($cmd, $target_id, $target_item, $thr_parent, $owner, $batch_delivery, $contacts, $ap_contacts, $conversants);
+               $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, false, $contacts, $ap_contacts, $conversants);
 
                $delivery_queue_count += self::deliverOStatus($target_id, $target_item, $owner, $url_recipients, $public_message, $push_notify);
 
@@ -487,7 +506,8 @@ class Notifier
         * Deliver the message to the contacts
         *
         * @param string $cmd 
-        * @param int $target_id 
+        * @param int $post_uriid 
+        * @param int $sender_uid
         * @param array $target_item 
         * @param array $thr_parent 
         * @param array $owner 
@@ -499,7 +519,7 @@ class Notifier
         * @throws InternalServerErrorException 
         * @throws Exception 
         */
-       private static function delivery(string $cmd, int $target_id, array $target_item, array $thr_parent, array $owner, bool $batch_delivery, array $contacts, array $ap_contacts, array $conversants = [])
+       private static function delivery(string $cmd, int $post_uriid, int $sender_uid, array $target_item, array $thr_parent, array $owner, bool $batch_delivery, bool $in_batch, array $contacts, array $ap_contacts, array $conversants = [])
        {
                $a = DI::app(); 
                $delivery_queue_count = 0;
@@ -511,38 +531,38 @@ class Notifier
                        }
 
                        if (in_array($contact['id'], $ap_contacts)) {
-                               Logger::info('Contact is already delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact is already delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
                                continue;
                        }
 
                        if (!empty($contact['id']) && Contact::isArchived($contact['id'])) {
-                               Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact is archived, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
                                continue;
                        }
 
                        if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
-                               Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
                                continue;
                        }
 
                        if (self::skipActivityPubForDiaspora($contact, $target_item, $thr_parent)) {
-                               Logger::info('Contact is from Diaspora, but the replied author is from ActivityPub, so skip delivery via Diaspora', ['id' => $target_id, 'url' => $contact['url']]);
+                               Logger::info('Contact is from Diaspora, but the replied author is from ActivityPub, so skip delivery via Diaspora', ['id' => $post_uriid, 'uid' => $sender_uid, 'url' => $contact['url']]);
                                continue;
                        }
 
                        // Don't deliver to Diaspora if it already had been done as batch delivery
-                       if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) {
-                               Logger::info('Diaspora contact is already delivered via batch', ['id' => $target_id, 'contact' => $contact]);
+                       if (!$in_batch && $batch_delivery && ($contact['network'] == Protocol::DIASPORA)) {
+                               Logger::info('Diaspora contact is already delivered via batch', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
                                continue;
                        }
 
                        // Don't deliver to folks who have already been delivered to
                        if (in_array($contact['id'], $conversants)) {
-                               Logger::info('Already delivery', ['id' => $target_id, 'contact' => $contact]);
+                               Logger::info('Already delivery', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
                                continue;
                        }
 
-                       Logger::info('Delivery', ['target' => $target_id, 'guid' => $target_item['guid'] ?? '', 'to' => $contact]);
+                       Logger::info('Delivery', ['batch' => $in_batch, 'target' => $post_uriid, 'uid' => $sender_uid, 'guid' => $target_item['guid'] ?? '', 'to' => $contact]);
 
                        // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
                        // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
@@ -554,7 +574,7 @@ class Notifier
                                $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
                        }
 
-                       if (Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$contact['id'])) {
+                       if (Worker::add($deliver_options, 'Delivery', $cmd, $post_uriid, (int)$contact['id'], $sender_uid)) {
                                $delivery_queue_count++;
                        }
                }
@@ -767,7 +787,7 @@ class Notifier
                        Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
 
                        if (Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
-                                       'APDelivery', $cmd, $target_item['id'], $inbox, $uid, $receivers)) {
+                                       'APDelivery', $cmd, $target_item['id'], $inbox, $uid, $receivers, $target_item['uri-id'])) {
                                $delivery_queue_count++;
                        }
                }
@@ -776,7 +796,7 @@ class Notifier
                foreach ($relay_inboxes as $inbox) {
                        Logger::info('Delivery to relay servers via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
 
-                       if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid)) {
+                       if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, [], $target_item['uri-id'])) {
                                $delivery_queue_count++;
                        }
                }
@@ -792,6 +812,6 @@ class Notifier
         */
        public static function isForumPost(array $item)
        {
-               return !empty($item['forum_mode']);
+               return ($item['gravity'] == GRAVITY_PARENT) && !empty($item['forum_mode']);
        }
 }