]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Notifier.php
Merge pull request #5903 from nupplaphil/move_module_acctlink
[friendica.git] / src / Worker / Notifier.php
index 401178ec26664090aa6478314d8812d9470113ae..ad33c3245d4be9d33e1381cb9d1cb00b40851842 100644 (file)
@@ -16,9 +16,11 @@ use Friendica\Model\Item;
 use Friendica\Model\PushSubscriber;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
+use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\Salmon;
+use Friendica\Model\Conversation;
 
 require_once 'include/dba.php';
 require_once 'include/items.php';
@@ -96,8 +98,16 @@ class Notifier
                                return;
                        }
                        foreach ($r as $contact) {
-                               Contact::terminateFriendship($user, $contact);
+                               Contact::terminateFriendship($user, $contact, true);
                        }
+
+                       $inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser(0);
+                       foreach ($inboxes as $inbox) {
+                               logger('Account removal for user ' . $item_id . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+                               Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
+                                       'APDelivery', Delivery::REMOVAL, '', $inbox, $item_id);
+                       }
+
                        return;
                } elseif ($cmd == Delivery::RELOCATION) {
                        $normal_mode = false;
@@ -167,6 +177,8 @@ class Notifier
                if (!in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION, Delivery::RELOCATION])) {
                        $parent = $items[0];
 
+                       self::activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent);
+
                        $fields = ['network', 'author-id', 'owner-id'];
                        $condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
                        $thr_parent = Item::selectFirst($fields, $condition);
@@ -182,7 +194,7 @@ class Notifier
                        // if $parent['wall'] == 1 we will already have the parent message in our array
                        // and we will relay the whole lot.
 
-                       $localhost = str_replace('www.','',$a->get_hostname());
+                       $localhost = str_replace('www.','',$a->getHostName());
                        if (strpos($localhost,':')) {
                                $localhost = substr($localhost,0,strpos($localhost,':'));
                        }
@@ -217,24 +229,16 @@ class Notifier
                        }
 
                        // Special treatment for forum posts
-                       if (($target_item['author-id'] != $target_item['owner-id']) &&
-                               ($owner['id'] != $target_item['contact-id']) &&
-                               ($target_item['uri'] === $target_item['parent-uri'])) {
-
-                               $fields = ['forum', 'prv'];
-                               $condition = ['id' => $target_item['contact-id']];
-                               $contact = DBA::selectFirst('contact', $fields, $condition);
-                               if (!DBA::isResult($contact)) {
-                                       // Should never happen
-                                       return false;
-                               }
+                       if (self::isForumPost($target_item, $owner)) {
+                               $relay_to_owner = true;
+                               $direct_forum_delivery = true;
+                       }
 
-                               // Is the post from a forum?
-                               if ($contact['forum'] || $contact['prv']) {
-                                       $relay_to_owner = true;
-                                       $direct_forum_delivery = true;
-                               }
+                       // Avoid that comments in a forum thread are sent to OStatus
+                       if (self::isForumPost($parent, $owner)) {
+                               $direct_forum_delivery = true;
                        }
+
                        if ($relay_to_owner) {
                                // local followup to remote post
                                $followup = true;
@@ -504,4 +508,58 @@ class Notifier
 
                return;
        }
+
+       private static function activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent)
+       {
+               $inboxes = [];
+
+               if ($target_item['origin']) {
+                       $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
+                       logger('Origin item ' . $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('Remote item ' . $item_id . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', LOGGER_DEBUG);
+                       return;
+               } else {
+                       logger('Remote item ' . $item_id . ' with URL ' . $target_item['uri'] . ' will be distributed.', LOGGER_DEBUG);
+               }
+
+               if ($parent['origin']) {
+                       $parent_inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid);
+                       $inboxes = array_merge($inboxes, $parent_inboxes);
+               }
+
+               if (empty($inboxes)) {
+                       logger('No inboxes found for item ' . $item_id . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', LOGGER_DEBUG);
+                       return;
+               }
+
+               // Fill the item cache
+               ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
+
+               foreach ($inboxes as $inbox) {
+                       logger('Deliver ' . $item_id .' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+
+                       Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
+                                       'APDelivery', $cmd, $item_id, $inbox, $uid);
+               }
+       }
+
+       private static function isForumPost($item, $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']);
+       }
 }