]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Notifier.php
Merge pull request #6703 from tobiasd/2019.03-CHANGELOG
[friendica.git] / src / Worker / Notifier.php
index ef4e8824922ec03926a3f683265208ee8354dc0e..baae33f7e0f3d8f2015e7756e582957005a00660 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
+use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Group;
@@ -152,7 +153,9 @@ class Notifier
                if (!empty($target_item) && !empty($items)) {
                        $parent = $items[0];
 
-                       $delivery_queue_count += self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], defaults($a->query_string, 'created', ''));
+                       if (!self::isRemovalActivity($cmd, $owner, Protocol::ACTIVITYPUB)) {
+                               $delivery_queue_count += self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], $a->queue['created'], $owner);
+                       }
 
                        $fields = ['network', 'author-id', 'owner-id'];
                        $condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
@@ -204,13 +207,13 @@ class Notifier
                        }
 
                        // Special treatment for forum posts
-                       if (self::isForumPost($target_item, $owner)) {
+                       if (Item::isForumPost($target_item, $owner)) {
                                $relay_to_owner = true;
                                $direct_forum_delivery = true;
                        }
 
                        // Avoid that comments in a forum thread are sent to OStatus
-                       if (self::isForumPost($parent, $owner)) {
+                       if (Item::isForumPost($parent, $owner)) {
                                $direct_forum_delivery = true;
                        }
 
@@ -410,7 +413,7 @@ class Notifier
 
                                // Add the relay to the list, avoid duplicates.
                                // Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly.
-                               if (!$followup && !self::isForumPost($target_item, $owner)) {
+                               if (!$followup && !Item::isForumPost($target_item, $owner)) {
                                        $relay_list = Diaspora::relayList($target_id, $relay_list);
                                }
                        }
@@ -418,12 +421,22 @@ class Notifier
                        $condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
                                'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
 
-                       $r2 = DBA::toArray(DBA::select('contact', ['id', 'name', 'network'], $condition));
+                       $r2 = DBA::toArray(DBA::select('contact', ['id', 'url', 'name', 'network'], $condition));
 
                        $r = array_merge($r2, $relay_list);
 
                        if (DBA::isResult($r)) {
                                foreach ($r as $rr) {
+                                       if (self::isRemovalActivity($cmd, $owner, $rr['network'])) {
+                                               Logger::log('Skipping dropping for ' . $rr['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+                                               continue;
+                                       }
+
+                                       if (Config::get('debug', 'total_ap_delivery') && !empty($rr['url']) && ($rr['network'] == Protocol::DFRN) && !empty(APContact::getByURL($rr['url'], false))) {
+                                               Logger::log('Skipping contact ' . $rr['url'] . ' since it will be delivered via AP', Logger::DEBUG);
+                                               continue;
+                                       }
+
                                        $conversants[] = $rr['id'];
 
                                        $delivery_queue_count++;
@@ -433,7 +446,8 @@ class Notifier
                                        // 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.
                                        // The conversion in Markdown reduces the formatting, so these posts should arrive after the Friendica posts.
-                                       if ($rr['network'] == Protocol::DIASPORA) {
+                                       // This is only important for high and medium priority tasks and not for Low priority jobs like deletions.
+                                       if (($rr['network'] == Protocol::DIASPORA) && in_array($a->queue['priority'], [PRIORITY_HIGH, PRIORITY_MEDIUM])) {
                                                $deliver_options = ['priority' => $a->queue['priority'], 'dont_fork' => true];
                                        } else {
                                                $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
@@ -447,6 +461,16 @@ class Notifier
 
                // delivery loop
                while ($contact = DBA::fetch($delivery_contacts_stmt)) {
+                       if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
+                               Logger::log('Skipping dropping for ' . $contact['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+                               continue;
+                       }
+
+                       if (Config::get('debug', 'total_ap_delivery') && ($contact['network'] == Protocol::DFRN) && !empty(APContact::getByURL($contact['url'], false))) {
+                               Logger::log('Skipping contact ' . $contact['url'] . ' since it will be delivered via AP', Logger::DEBUG);
+                               continue;
+                       }
+
                        // Don't deliver to Diaspora if it already had been done as batch delivery
                        if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) {
                                Logger::log('Already delivered  id ' . $target_id . ' via batch to ' . json_encode($contact), Logger::DEBUG);
@@ -475,7 +499,6 @@ class Notifier
                }
                DBA::close($delivery_contacts_stmt);
 
-
                $url_recipients = array_filter($url_recipients);
                // send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
                // They are especially used for notifications to OStatus users that don't follow us.
@@ -513,6 +536,23 @@ class Notifier
                return;
        }
 
+       /**
+        * Checks if the current action is a deletion command of a account removal activity
+        * For Diaspora and ActivityPub we don't need to send single item deletion calls.
+        * These protocols do have a dedicated command for deleting a whole account.
+        *
+        * @param string $cmd     Notifier command
+        * @param array  $owner   Sender of the post
+        * @param string $network Receiver network
+        * @return bool
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function isRemovalActivity($cmd, $owner, $network)
+       {
+               return ($cmd == Delivery::DELETION) && $owner['account_removed'] && in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DIASPORA]);
+       }
+
        /**
         * @param int    $self_user_id
         * @param int    $priority The priority the Notifier queue item was created with
@@ -558,7 +598,7 @@ class Notifier
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created)
+       private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created, $owner)
        {
                $inboxes = [];
 
@@ -567,6 +607,9 @@ class Notifier
                if ($target_item['origin']) {
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
                        Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
+               } elseif (Item::isForumPost($target_item, $owner)) {
+                       $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true);
+                       Logger::log('Forum item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
                } elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) {
                        Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', Logger::DEBUG);
                        return 0;
@@ -594,24 +637,4 @@ class Notifier
 
                return count($inboxes);
        }
-
-       private static function isForumPost(array $item, array $owner)
-       {
-               if (($item['author-id'] == $item['owner-id']) ||
-                       ($owner['id'] == $item['contact-id']) ||
-                       ($item['uri'] != $item['parent-uri'])) {
-                       return false;
-               }
-
-               $fields = ['forum', 'prv'];
-               $condition = ['id' => $item['contact-id']];
-               $contact = DBA::selectFirst('contact', $fields, $condition);
-               if (!DBA::isResult($contact)) {
-                       // Should never happen
-                       return false;
-               }
-
-               // Is the post from a forum?
-               return ($contact['forum'] || $contact['prv']);
-       }
 }