]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Notifier.php
Ensure author-link key has a value in Worker\Notifier
[friendica.git] / src / Worker / Notifier.php
index 5585546ce87bdabf1b1f303b6701b9f481c3102b..1bcad1a73bfa756878eafcf7cbf21a30e8c053ef 100644 (file)
@@ -1,10 +1,26 @@
 <?php
 /**
- * @file src/Worker/Notifier.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Worker;
 
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -16,17 +32,15 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
-use Friendica\Model\ItemDeliveryData;
+use Friendica\Model\Post;
 use Friendica\Model\PushSubscriber;
+use Friendica\Model\Tag;
 use Friendica\Model\User;
-use Friendica\Network\Probe;
+use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\Salmon;
-use Friendica\Util\ACLFormatter;
-
-require_once 'include/items.php';
 
 /*
  * The notifier is typically called with:
@@ -72,12 +86,9 @@ class Notifier
                                        'APDelivery', $cmd, $target_id, $inbox, $uid);
                        }
                } elseif ($cmd == Delivery::SUGGESTION) {
-                       $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]);
-                       if (!DBA::isResult($suggest)) {
-                               return;
-                       }
-                       $uid = $suggest['uid'];
-                       $recipients[] = $suggest['cid'];
+                       $suggest = DI::fsuggest()->getById($target_id);
+                       $uid = $suggest->uid;
+                       $recipients[] = $suggest->cid;
                } elseif ($cmd == Delivery::REMOVAL) {
                        return self::notifySelfRemoval($target_id, $a->queue['priority'], $a->queue['created']);
                } elseif ($cmd == Delivery::RELOCATION) {
@@ -139,6 +150,8 @@ class Notifier
                // If this is a public conversation, notify the feed hub
                $public_message = true;
 
+               $unlisted = false;
+
                // Do a PuSH
                $push_notify = false;
 
@@ -151,7 +164,7 @@ class Notifier
                if (!empty($target_item) && !empty($items)) {
                        $parent = $items[0];
 
-                       $fields = ['network', 'author-id', 'author-link', 'owner-id'];
+                       $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);
                        if (empty($thr_parent)) {
@@ -166,11 +179,13 @@ class Notifier
 
                        // Only deliver threaded replies (comment to a comment) to Diaspora
                        // when the original comment author does support the Diaspora protocol.
-                       if ($target_item['parent-uri'] != $target_item['thr-parent']) {
+                       if ($thr_parent['author-link'] && $target_item['parent-uri'] != $target_item['thr-parent']) {
                                $diaspora_delivery = Diaspora::isSupportedByContactUrl($thr_parent['author-link']);
                                Logger::info('Threaded comment', ['diaspora_delivery' => (int)$diaspora_delivery]);
                        }
 
+                       $unlisted = $target_item['private'] == Item::UNLISTED;
+
                        // This is IMPORTANT!!!!
 
                        // We will only send a "notify owner to relay" or followup message if the referenced post
@@ -233,8 +248,7 @@ class Notifier
 
                                Logger::info('Followup', ['target' => $target_id, 'guid' => $target_item['guid'], 'to' => $parent['contact-id']]);
 
-                               //if (!$target_item['private'] && $target_item['wall'] &&
-                               if (!$target_item['private'] &&
+                               if (($target_item['private'] != Item::PRIVATE) &&
                                        (strlen($target_item['allow_cid'].$target_item['allow_gid'].
                                                $target_item['deny_cid'].$target_item['deny_gid']) == 0))
                                        $push_notify = true;
@@ -287,8 +301,10 @@ class Notifier
 
                                // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
                                // a delivery fork. private groups (forum_mode == 2) do not uplink
+                               /// @todo Possibly we should not uplink when the author is the forum itself?
 
-                               if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK)) {
+                               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);
                                }
 
@@ -340,28 +356,23 @@ class Notifier
                                // Send a salmon to the parent author
                                $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['author-id']]);
                                if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
-                                       Logger::log('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
+                                       Logger::notice('Notify parent author', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
                                        $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
                                }
 
                                // Send a salmon to the parent owner
                                $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['owner-id']]);
                                if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
-                                       Logger::log('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
+                                       Logger::notice('Notify parent owner', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
                                        $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
                                }
 
                                // Send a salmon notification to every person we mentioned in the post
-                               $arr = explode(',',$target_item['tag']);
-                               foreach ($arr as $x) {
-                                       //Logger::log('Checking tag '.$x, Logger::DEBUG);
-                                       $matches = null;
-                                       if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
-                                                       $probed_contact = Probe::uri($matches[1]);
-                                               if ($probed_contact["notify"] != "") {
-                                                       Logger::log('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
-                                                       $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
-                                               }
+                               foreach (Tag::getByURIId($target_item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]) as $tag) {
+                                       $probed_contact = Contact::getByURL($tag['url']);
+                                       if (!empty($probed_contact['notify'])) {
+                                               Logger::notice('Notify mentioned user', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
+                                               $url_recipients[$probed_contact['notify']] = $probed_contact['notify'];
                                        }
                                }
 
@@ -384,7 +395,7 @@ class Notifier
                        if ($followup) {
                                $recipients = $recipients_followup;
                        }
-                       $condition = ['id' => $recipients, 'self' => false,
+                       $condition = ['id' => $recipients, 'self' => false, 'uid' => [0, $uid],
                                'blocked' => false, 'pending' => false, 'archive' => false];
                        if (!empty($networks)) {
                                $condition['network'] = $networks;
@@ -398,7 +409,7 @@ class Notifier
                if ($public_message && !in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION]) && !$followup) {
                        $relay_list = [];
 
-                       if ($diaspora_delivery) {
+                       if ($diaspora_delivery && !$unlisted) {
                                $batch_delivery = true;
 
                                $relay_list_stmt = DBA::p(
@@ -426,11 +437,11 @@ class Notifier
 
                                // Fetch the participation list
                                // The function will ensure that there are no duplicates
-                               $relay_list = Diaspora::participantsForThread($target_id, $relay_list);
+                               $relay_list = Diaspora::participantsForThread($target_item, $relay_list);
 
                                // 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 && !Item::isForumPost($target_item, $owner)) {
+                               if (!$followup && !Item::isForumPost($target_item, $owner) && !self::isForumPost($target_item)) {
                                        $relay_list = Diaspora::relayList($target_id, $relay_list);
                                }
                        }
@@ -450,25 +461,30 @@ class Notifier
                                        }
 
                                        if (!empty($rr['addr']) && ($rr['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $rr['addr']])) {
-                                               Logger::info('Contact is AP omly', ['target' => $target_id, 'contact' => $rr['url']]);
+                                               Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $rr['url']]);
                                                continue;
                                        }
 
                                        if (!empty($rr['id']) && Contact::isArchived($rr['id'])) {
-                                               Logger::info('Contact is archived', ['target' => $target_id, 'contact' => $rr['url']]);
+                                               Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $rr['url']]);
                                                continue;
                                        }
 
                                        if (self::isRemovalActivity($cmd, $owner, $rr['network'])) {
-                                               Logger::log('Skipping dropping for ' . $rr['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+                                               Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $rr['url']]);
                                                continue;
                                        }
 
-                                       if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $cmd)) {
+                                       if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $owner, $cmd)) {
                                                Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['id' => $target_id, 'url' => $rr['url']]);
                                                continue;
                                        }
 
+                                       if (self::skipActivityPubForDiaspora($rr, $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' => $rr['url']]);
+                                               continue;
+                                       }
+
                                        $conversants[] = $rr['id'];
 
                                        Logger::info('Public delivery', ['target' => $target_id, 'guid' => $target_item["guid"], 'to' => $rr]);
@@ -500,25 +516,30 @@ class Notifier
                        }
 
                        if (!empty($contact['addr']) && ($contact['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $contact['addr']])) {
-                               Logger::info('Contact is AP omly', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]);
                                continue;
                        }
 
                        if (!empty($contact['id']) && Contact::isArchived($contact['id'])) {
-                               Logger::info('Contact is archived', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
                                continue;
                        }
 
                        if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
-                               Logger::log('Skipping dropping for ' . $contact['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+                               Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
                                continue;
                        }
 
-                       if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $cmd)) {
+                       if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $owner, $cmd)) {
                                Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'url' => $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']]);
+                               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);
@@ -558,7 +579,7 @@ class Notifier
                                /// @TODO Redeliver/queue these items on failure, though there is no contact record
                                $delivery_queue_count++;
                                Salmon::slapper($owner, $url, $slap);
-                               ItemDeliveryData::incrementQueueDone($target_id, ItemDeliveryData::OSTATUS);
+                               Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Post\DeliveryData::OSTATUS);
                        }
                }
 
@@ -580,17 +601,46 @@ class Notifier
                        // Workaround for pure connector posts
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                if ($delivery_queue_count == 0) {
-                                       ItemDeliveryData::incrementQueueDone($target_item['id']);
+                                       Post\DeliveryData::incrementQueueDone($target_item['uri-id']);
                                        $delivery_queue_count = 1;
                                }
 
-                               ItemDeliveryData::incrementQueueCount($target_item['id'], $delivery_queue_count);
+                               Post\DeliveryData::incrementQueueCount($target_item['uri-id'], $delivery_queue_count);
                        }
                }
 
                return;
        }
 
+       /**
+        * Checks if the current delivery shouldn't be transported to Diaspora.
+        * This is done for posts from AP authors or posts that are comments to AP authors.
+        *
+        * @param array  $contact    Receiver of the post
+        * @param array  $item       The post
+        * @param array  $thr_parent The thread parent
+        * @return bool
+        */
+       private static function skipActivityPubForDiaspora(array $contact, array $item, array $thr_parent)
+       {
+               // No skipping needs to be done when delivery isn't done to Diaspora
+               if ($contact['network'] != Protocol::DIASPORA) {
+                       return false;
+               }
+
+               // Skip the delivery to Diaspora if the item is from an ActivityPub author
+               if ($item['author-network'] == Protocol::ACTIVITYPUB) {
+                       return true;
+               }
+               
+               // Skip the delivery to Diaspora if the thread parent is from an ActivityPub author
+               if ($thr_parent['author-network'] == Protocol::ACTIVITYPUB) {
+                       return true;
+               }
+
+               return false;
+       }
+
        /**
         * Checks if the current delivery process needs to be transported via DFRN.
         *
@@ -598,12 +648,13 @@ class Notifier
         * @param array  $item       The post
         * @param array  $parent     The parent
         * @param array  $thr_parent The thread parent
+        * @param array  $owner      Owner array
         * @param string $cmd        Notifier command
         * @return bool
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function skipDFRN($contact, $item, $parent, $thr_parent, $cmd)
+       private static function skipDFRN($contact, $item, $parent, $thr_parent, $owner, $cmd)
        {
                if (empty($parent['network'])) {
                        return false;
@@ -639,6 +690,12 @@ class Notifier
                        return true;
                }
 
+               // For the time being we always deliver forum post via DFRN if possible
+               // This can be removed possible at the end of 2020 when hopefully most system can process AP forum posts
+               if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
+                       return false;
+               }
+
                // Skip DFRN when the item will be (forcefully) delivered via AP
                if (DI::config()->get('debug', 'total_ap_delivery') && ($contact['network'] == Protocol::DFRN) && !empty(APContact::getByURL($contact['url'], false))) {
                        return true;
@@ -729,6 +786,11 @@ class Notifier
 
                if ($target_item['origin']) {
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
+
+                       if (in_array($target_item['private'], [Item::PUBLIC])) {
+                               $inboxes = ActivityPub\Transmitter::addRelayServerInboxes($inboxes);
+                       }
+
                        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);
@@ -764,4 +826,15 @@ class Notifier
 
                return $delivery_queue_count;
        }
+
+       /**
+        * Check if the delivered item is a forum post
+        *
+        * @param array $item
+        * @return boolean
+        */
+       public static function isForumPost(array $item)
+       {
+               return !empty($item['forum_mode']);
+       }
 }