Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git/.git] / src / Worker / Notifier.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Conversation;
32 use Friendica\Model\Circle;
33 use Friendica\Model\GServer;
34 use Friendica\Model\Item;
35 use Friendica\Model\Post;
36 use Friendica\Model\PushSubscriber;
37 use Friendica\Model\Tag;
38 use Friendica\Model\User;
39 use Friendica\Protocol\Activity;
40 use Friendica\Protocol\ActivityPub;
41 use Friendica\Protocol\Diaspora;
42 use Friendica\Protocol\Delivery;
43 use Friendica\Protocol\OStatus;
44 use Friendica\Protocol\Salmon;
45 use Friendica\Util\LDSignature;
46 use Friendica\Util\Network;
47 use Friendica\Util\Strings;
48
49 /*
50  * The notifier is typically called with:
51  *
52  *              Worker::add(PRIORITY_HIGH, "Notifier", COMMAND, ITEM_ID);
53  *
54  * where COMMAND is one of the constants that are defined in Worker/Delivery.php
55  * and ITEM_ID is the id of the item in the database that needs to be sent to others.
56  */
57
58 class Notifier
59 {
60         public static function execute(string $cmd, int $post_uriid, int $sender_uid = 0)
61         {
62                 $a = DI::app();
63
64                 Logger::info('Invoked', ['cmd' => $cmd, 'target' => $post_uriid, 'sender_uid' => $sender_uid]);
65
66                 $target_id = $post_uriid;
67                 $top_level = false;
68                 $recipients = [];
69                 $url_recipients = [];
70
71                 $delivery_contacts_stmt = null;
72                 $target_item = [];
73                 $parent = [];
74                 $thr_parent = [];
75                 $items = [];
76                 $delivery_queue_count = 0;
77                 $ap_contacts = [];
78
79                 if ($cmd == Delivery::MAIL) {
80                         $message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $target_id]);
81                         if (!DBA::isResult($message)) {
82                                 return;
83                         }
84                         $uid = $message['uid'];
85                         $recipients[] = $message['contact-id'];
86
87                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxesFromMail($target_id);
88                         foreach ($inboxes as $inbox => $receivers) {
89                                 $ap_contacts = array_merge($ap_contacts, $receivers);
90                                 Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'target' => $target_id, 'inbox' => $inbox]);
91                                 Worker::add(['priority' => Worker::PRIORITY_HIGH, 'created' => $a->getQueueValue('created'), 'dont_fork' => true],
92                                         'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers, $post_uriid);
93                         }
94                 } elseif ($cmd == Delivery::SUGGESTION) {
95                         $suggest = DI::fsuggest()->selectOneById($target_id);
96                         $uid = $suggest->uid;
97                         $recipients[] = $suggest->cid;
98                 } elseif ($cmd == Delivery::REMOVAL) {
99                         return self::notifySelfRemoval($target_id, $a->getQueueValue('priority'), $a->getQueueValue('created'));
100                 } elseif ($cmd == Delivery::RELOCATION) {
101                         $uid = $target_id;
102
103                         $condition = ['uid' => $target_id, 'self' => false, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
104                         $delivery_contacts_stmt = DBA::select('contact', ['id', 'uri-id', 'url', 'addr', 'network', 'protocol', 'baseurl', 'gsid', 'batch'], $condition);
105                 } else {
106                         $post = Post::selectFirst(['id'], ['uri-id' => $post_uriid, 'uid' => $sender_uid]);
107                         if (!DBA::isResult($post)) {
108                                 Logger::warning('Post not found', ['uri-id' => $post_uriid, 'uid' => $sender_uid]);
109                                 return;
110                         }
111                         $target_id = $post['id'];
112
113                         // find ancestors
114                         $condition = ['id' => $target_id, 'visible' => true];
115                         $target_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
116                         $target_item = Post\Media::addHTMLAttachmentToItem($target_item);
117
118                         if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
119                                 Logger::info('No target item', ['cmd' => $cmd, 'target' => $target_id]);
120                                 return;
121                         }
122
123                         if (!empty($target_item['contact-uid'])) {
124                                 $uid = $target_item['contact-uid'];
125                         } elseif (!empty($target_item['uid'])) {
126                                 $uid = $target_item['uid'];
127                         } else {
128                                 Logger::info('Only public users, quitting', ['target' => $target_id]);
129                                 return;
130                         }
131
132                         $condition = ['parent' => $target_item['parent'], 'visible' => true];
133                         $params = ['order' => ['id']];
134                         $items_stmt = Post::select(Item::DELIVER_FIELDLIST, $condition, $params);
135                         if (!DBA::isResult($items_stmt)) {
136                                 Logger::info('No item found', ['cmd' => $cmd, 'target' => $target_id]);
137                                 return;
138                         }
139
140                         $items = Post::toArray($items_stmt);
141
142                         // avoid race condition with deleting entries
143                         if ($items[0]['deleted']) {
144                                 foreach ($items as $item) {
145                                         $item['deleted'] = 1;
146                                 }
147                         }
148
149                         $top_level = $target_item['gravity'] == Item::GRAVITY_PARENT;
150                 }
151
152                 $owner = User::getOwnerDataById($uid);
153                 if (!$owner) {
154                         Logger::info('Owner not found', ['cmd' => $cmd, 'target' => $target_id]);
155                         return;
156                 }
157
158                 // Should the post be transmitted to Diaspora?
159                 $diaspora_delivery = ($owner['account-type'] != User::ACCOUNT_TYPE_COMMUNITY);
160
161                 // If this is a public conversation, notify the feed hub
162                 $public_message = true;
163
164                 $unlisted = false;
165
166                 // Do a PuSH
167                 $push_notify = false;
168
169                 // Deliver directly to a group, don't PuSH
170                 $direct_group_delivery = false;
171
172                 $only_ap_delivery = false;
173
174                 $followup = false;
175                 $recipients_followup = [];
176
177                 if (!empty($target_item) && !empty($items)) {
178                         $parent = $items[0];
179
180                         $fields = ['network', 'private', 'author-id', 'author-link', 'author-network', 'owner-id'];
181                         $condition = ['uri' => $target_item['thr-parent'], 'uid' => $target_item['uid']];
182                         $thr_parent = Post::selectFirst($fields, $condition);
183                         if (empty($thr_parent)) {
184                                 $thr_parent = $parent;
185                         }
186
187                         Logger::info('Got post', ['guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'network' => $target_item['network'], 'parent-network' => $parent['network'], 'thread-parent-network' => $thr_parent['network']]);
188
189                         if (!self::isRemovalActivity($cmd, $owner, Protocol::ACTIVITYPUB)) {
190                                 $apdelivery = self::activityPubDelivery($cmd, $target_item, $parent, $thr_parent, $a->getQueueValue('priority'), $a->getQueueValue('created'), $owner);
191                                 $ap_contacts = $apdelivery['contacts'];
192                                 $delivery_queue_count += $apdelivery['count'];
193                                 // Restrict distribution to AP, when there are no permissions.
194                                 if (($target_item['private'] == Item::PRIVATE) && empty($target_item['allow_cid']) && empty($target_item['allow_gid']) && empty($target_item['deny_cid']) && empty($target_item['deny_gid'])) {
195                                         $only_ap_delivery   = true;
196                                         $public_message     = false;
197                                         $diaspora_delivery  = false;
198                                 }
199                         }
200
201                         // Only deliver threaded replies (comment to a comment) to Diaspora
202                         // when the original comment author does support the Diaspora protocol.
203                         if ($thr_parent['author-link'] && $target_item['parent-uri'] != $target_item['thr-parent']) {
204                                 $diaspora_delivery = Diaspora::isSupportedByContactUrl($thr_parent['author-link']);
205                                 if ($diaspora_delivery && empty($target_item['signed_text'])) {
206                                         Logger::debug('Post has got no Diaspora signature, so there will be no Diaspora delivery', ['guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id']]);
207                                         $diaspora_delivery = false;
208                                 }
209                                 Logger::info('Threaded comment', ['diaspora_delivery' => (int)$diaspora_delivery]);
210                         }
211
212                         $unlisted = $target_item['private'] == Item::UNLISTED;
213
214                         // This is IMPORTANT!!!!
215
216                         // We will only send a "notify owner to relay" or followup message if the referenced post
217                         // originated on our system by virtue of having our hostname somewhere
218                         // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
219
220                         // if $parent['wall'] == 1 we will already have the parent message in our array
221                         // and we will relay the whole lot.
222
223                         $localhost = str_replace('www.','', DI::baseUrl()->getHost());
224                         if (strpos($localhost,':')) {
225                                 $localhost = substr($localhost,0,strpos($localhost,':'));
226                         }
227                         /**
228                          *
229                          * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
230                          * have been known to cause runaway conditions which affected several servers, along with
231                          * permissions issues.
232                          *
233                          */
234
235                         $relay_to_owner = false;
236
237                         if (!$top_level && ($parent['wall'] == 0) && (stristr($target_item['uri'],$localhost))) {
238                                 $relay_to_owner = true;
239                         }
240
241                         // until the 'origin' flag has been in use for several months
242                         // we will just use it as a fallback test
243                         // later we will be able to use it as the primary test of whether or not to relay.
244
245                         if (!$target_item['origin']) {
246                                 $relay_to_owner = false;
247                         }
248                         if ($parent['origin']) {
249                                 $relay_to_owner = false;
250                         }
251
252                         // Special treatment for group posts
253                         if (Item::isGroupPost($target_item['uri-id'])) {
254                                 $relay_to_owner = true;
255                                 $direct_group_delivery = true;
256                         }
257
258                         // Avoid that comments in a group thread are sent to OStatus
259                         if (Item::isGroupPost($parent['uri-id'])) {
260                                 $direct_group_delivery = true;
261                         }
262
263                         $exclusive_delivery = false;
264
265                         $exclusive_targets = Tag::getByURIId($parent['uri-id'], [Tag::EXCLUSIVE_MENTION]);
266                         if (!empty($exclusive_targets)) {
267                                 $exclusive_delivery = true;
268                                 Logger::info('Possible Exclusively delivering', ['uid' => $target_item['uid'], 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id']]);
269                                 foreach ($exclusive_targets as $target) {
270                                         if (Strings::compareLink($owner['url'], $target['url'])) {
271                                                 $exclusive_delivery = false;
272                                                 Logger::info('False Exclusively delivering', ['uid' => $target_item['uid'], 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'url' => $target['url']]);
273                                         }
274                                 }
275                         }
276
277                         if ($relay_to_owner) {
278                                 // local followup to remote post
279                                 $followup = true;
280                                 $public_message = false; // not public
281                                 $recipients = [$parent['contact-id']];
282                                 $recipients_followup  = [$parent['contact-id']];
283
284                                 Logger::info('Followup', ['target' => $target_id, 'guid' => $target_item['guid'], 'to' => $parent['contact-id']]);
285
286                                 if (($target_item['private'] != Item::PRIVATE) &&
287                                         (strlen($target_item['allow_cid'].$target_item['allow_gid'].
288                                                 $target_item['deny_cid'].$target_item['deny_gid']) == 0))
289                                         $push_notify = true;
290
291                                 if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
292                                         $push_notify = true;
293
294                                         if ($parent["network"] == Protocol::OSTATUS) {
295                                                 // Distribute the message to the DFRN contacts as if this wasn't a followup since OStatus can't relay comments
296                                                 // Currently it is work at progress
297                                                 $condition = ['uid' => $uid, 'network' => Protocol::DFRN, 'blocked' => false, 'pending' => false, 'archive' => false];
298                                                 $followup_contacts_stmt = DBA::select('contact', ['id'], $condition);
299                                                 while($followup_contact = DBA::fetch($followup_contacts_stmt)) {
300                                                         $recipients_followup[] = $followup_contact['id'];
301                                                 }
302                                                 DBA::close($followup_contacts_stmt);
303                                         }
304                                 }
305
306                                 if ($direct_group_delivery) {
307                                         $push_notify = false;
308                                 }
309
310                                 Logger::info('Notify ' . $target_item["guid"] .' via PuSH: ' . ($push_notify ? "Yes":"No"));
311                         } elseif ($exclusive_delivery) {
312                                 $followup = true;
313
314                                 foreach ($exclusive_targets as $target) {
315                                         $cid = Contact::getIdForURL($target['url'], $uid, false);
316                                         if ($cid) {
317                                                 $recipients_followup[] = $cid;
318                                                 Logger::info('Exclusively delivering', ['uid' => $target_item['uid'], 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'url' => $target['url']]);
319                                         }
320                                 }
321                         } else {
322                                 $followup = false;
323
324                                 Logger::info('Distributing directly', ['target' => $target_id, 'guid' => $target_item['guid']]);
325
326                                 // don't send deletions onward for other people's stuff
327
328                                 if ($target_item['deleted'] && !intval($target_item['wall'])) {
329                                         Logger::notice('Ignoring delete notification for non-wall item');
330                                         return;
331                                 }
332
333                                 if (strlen($parent['allow_cid'])
334                                         || strlen($parent['allow_gid'])
335                                         || strlen($parent['deny_cid'])
336                                         || strlen($parent['deny_gid'])) {
337                                         $public_message = false; // private recipients, not public
338                                 }
339
340                                 $aclFormatter = DI::aclFormatter();
341
342                                 $allow_people = $aclFormatter->expand($parent['allow_cid']);
343                                 $allow_circles = Circle::expand($uid, $aclFormatter->expand($parent['allow_gid']),true);
344                                 $deny_people  = $aclFormatter->expand($parent['deny_cid']);
345                                 $deny_circles  = Circle::expand($uid, $aclFormatter->expand($parent['deny_gid']));
346
347                                 foreach ($items as $item) {
348                                         $recipients[] = $item['contact-id'];
349                                         // pull out additional tagged people to notify (if public message)
350                                         if ($public_message && $item['inform']) {
351                                                 $people = explode(',',$item['inform']);
352                                                 foreach ($people as $person) {
353                                                         if (substr($person,0,4) === 'cid:') {
354                                                                 $recipients[] = intval(substr($person,4));
355                                                         } else {
356                                                                 $url_recipients[] = substr($person,4);
357                                                         }
358                                                 }
359                                         }
360                                 }
361
362                                 if (count($url_recipients)) {
363                                         Logger::notice('Deliver', ['target' => $target_id, 'guid' => $target_item['guid'], 'recipients' => $url_recipients]);
364                                 }
365
366                                 $recipients = array_unique(array_merge($recipients, $allow_people, $allow_circles));
367                                 $deny = array_unique(array_merge($deny_people, $deny_circles));
368                                 $recipients = array_diff($recipients, $deny);
369
370                                 // If this is a public message and pubmail is set on the parent, include all your email contacts
371                                 if (
372                                         function_exists('imap_open')
373                                         && !DI::config()->get('system','imap_disabled')
374                                         && $public_message
375                                         && intval($target_item['pubmail'])
376                                 ) {
377                                         $mail_contacts_stmt = DBA::select('contact', ['id'], ['uid' => $uid, 'network' => Protocol::MAIL]);
378                                         while ($mail_contact = DBA::fetch($mail_contacts_stmt)) {
379                                                 $recipients[] = $mail_contact['id'];
380                                         }
381                                         DBA::close($mail_contacts_stmt);
382                                 }
383                         }
384
385                         // If the thread parent is OStatus then do some magic to distribute the messages.
386                         // We have not only to look at the parent, since it could be a Friendica thread.
387                         if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
388                                 $diaspora_delivery = false;
389
390                                 Logger::info('Some parent is OStatus for ' . $target_item['guid'] . ' - Author: ' . $thr_parent['author-id'] . ' - Owner: ' . $thr_parent['owner-id']);
391
392                                 // Send a salmon to the parent author
393                                 $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['author-id']]);
394                                 if (DBA::isResult($probed_contact) && !empty($probed_contact['notify'])) {
395                                         Logger::notice('Notify parent author', ['url' => $probed_contact['url'], 'notify' => $probed_contact['notify']]);
396                                         $url_recipients[$probed_contact['notify']] = $probed_contact['notify'];
397                                 }
398
399                                 // Send a salmon to the parent owner
400                                 $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['owner-id']]);
401                                 if (DBA::isResult($probed_contact) && !empty($probed_contact['notify'])) {
402                                         Logger::notice('Notify parent owner', ['url' => $probed_contact['url'], 'notify' => $probed_contact['notify']]);
403                                         $url_recipients[$probed_contact['notify']] = $probed_contact['notify'];
404                                 }
405
406                                 // Send a salmon notification to every person we mentioned in the post
407                                 foreach (Tag::getByURIId($target_item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]) as $tag) {
408                                         $probed_contact = Contact::getByURL($tag['url']);
409                                         if (!empty($probed_contact['notify'])) {
410                                                 Logger::notice('Notify mentioned user', ['url' => $probed_contact['url'], 'notify' => $probed_contact['notify']]);
411                                                 $url_recipients[$probed_contact['notify']] = $probed_contact['notify'];
412                                         }
413                                 }
414
415                                 // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
416                                 $networks = [Protocol::DFRN];
417                         } elseif ($diaspora_delivery) {
418                                 $networks = [Protocol::DFRN, Protocol::DIASPORA, Protocol::MAIL];
419                                 if (($parent['network'] == Protocol::DIASPORA) || ($thr_parent['network'] == Protocol::DIASPORA)) {
420                                         Logger::info('Add AP contacts', ['target' => $target_id, 'guid' => $target_item['guid']]);
421                                         $networks[] = Protocol::ACTIVITYPUB;
422                                 }
423                         } else {
424                                 $networks = [Protocol::DFRN, Protocol::MAIL];
425                         }
426                 } else {
427                         $public_message = false;
428                 }
429
430                 if (empty($delivery_contacts_stmt)) {
431                         if ($only_ap_delivery) {
432                                 $recipients = $ap_contacts;
433                         } elseif ($followup) {
434                                 $recipients = $recipients_followup;
435                         }
436                         $condition = ['id' => $recipients, 'self' => false, 'uid' => [0, $uid],
437                                 'blocked' => false, 'pending' => false, 'archive' => false];
438                         if (!empty($networks)) {
439                                 $condition['network'] = $networks;
440                         }
441                         $delivery_contacts_stmt = DBA::select('contact', ['id', 'uri-id', 'addr', 'url', 'network', 'protocol', 'baseurl', 'gsid', 'batch'], $condition);
442                 }
443
444                 $conversants = [];
445                 $batch_delivery = false;
446
447                 if ($public_message && !in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION]) && !$followup) {
448                         $participants = [];
449
450                         if ($diaspora_delivery && !$unlisted) {
451                                 $batch_delivery = true;
452
453                                 $participants = DBA::selectToArray('contact', ['batch', 'network', 'protocol', 'baseurl', 'gsid', 'id', 'url', 'name'],
454                                         ["`network` = ? AND `batch` != '' AND `uid` = ? AND `rel` != ? AND NOT `blocked` AND NOT `pending` AND NOT `archive`", Protocol::DIASPORA, $owner['uid'], Contact::SHARING],
455                                         ['group_by' => ['batch', 'network', 'protocol']]);
456
457                                 // Fetch the participation list
458                                 // The function will ensure that there are no duplicates
459                                 $participants = Diaspora::participantsForThread($target_item, $participants);
460                         }
461
462                         $condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
463                                 'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
464
465                         $contacts = DBA::selectToArray('contact', ['id', 'uri-id', 'url', 'addr', 'name', 'network', 'protocol', 'baseurl', 'gsid'], $condition);
466
467                         $conversants = array_merge($contacts, $participants);
468
469                         $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, true, $conversants, $ap_contacts, []);
470
471                         $push_notify = true;
472                 }
473
474                 $contacts = DBA::toArray($delivery_contacts_stmt);
475                 $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, false, $contacts, $ap_contacts, $conversants);
476
477                 $delivery_queue_count += self::deliverOStatus($target_id, $target_item, $owner, $url_recipients, $public_message, $push_notify);
478
479                 if (!empty($target_item)) {
480                         Logger::info('Calling hooks for ' . $cmd . ' ' . $target_id);
481
482                         Hook::fork($a->getQueueValue('priority'), 'notifier_normal', $target_item);
483
484                         Hook::callAll('notifier_end', $target_item);
485
486                         // Workaround for pure connector posts
487                         if ($cmd == Delivery::POST) {
488                                 if ($delivery_queue_count == 0) {
489                                         Post\DeliveryData::incrementQueueDone($target_item['uri-id']);
490                                         $delivery_queue_count = 1;
491                                 }
492
493                                 Post\DeliveryData::incrementQueueCount($target_item['uri-id'], $delivery_queue_count);
494                         }
495                 }
496
497                 return;
498         }
499
500         /**
501          * Deliver the message to the contacts
502          *
503          * @param string $cmd
504          * @param int $post_uriid
505          * @param int $sender_uid
506          * @param array $target_item
507          * @param array $thr_parent
508          * @param array $owner
509          * @param bool $batch_delivery
510          * @param array $contacts
511          * @param array $ap_contacts
512          * @param array $conversants
513          *
514          * @return int Count of delivery queue
515          * @throws InternalServerErrorException
516          * @throws Exception
517          */
518         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 = []): int
519         {
520                 $a = DI::app();
521                 $delivery_queue_count = 0;
522
523                 if (!empty($target_item['verb']) && ($target_item['verb'] == Activity::ANNOUNCE)) {
524                         Logger::notice('Announces are only delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
525                         return 0;
526                 }
527
528                 foreach ($contacts as $contact) {
529                         // Direct delivery of local contacts
530                         if (!in_array($cmd, [Delivery::RELOCATION, Delivery::SUGGESTION, Delivery::MAIL]) && $target_uid = User::getIdForURL($contact['url'])) {
531                                 if ($cmd == Delivery::DELETION) {
532                                         Logger::info('No need to deliver deletions internally', ['uid' => $target_uid, 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
533                                         continue;
534                                 }
535                                 if ($target_item['origin'] || ($target_item['network'] != Protocol::ACTIVITYPUB)) {
536                                         if ($target_uid != $target_item['uid']) {
537                                                 $fields = ['protocol' => Conversation::PARCEL_LOCAL_DFRN, 'direction' => Conversation::PUSH, 'post-reason' => Item::PR_DIRECT];
538                                                 Item::storeForUserByUriId($target_item['uri-id'], $target_uid, $fields, $target_item['uid']);
539                                                 Logger::info('Delivered locally', ['cmd' => $cmd, 'id' => $target_item['id'], 'target' => $target_uid]);
540                                         } else {
541                                                 Logger::info('No need to deliver to myself', ['uid' => $target_uid, 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
542                                         }
543                                 } else {
544                                         Logger::info('Remote item does not need to be delivered locally', ['guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
545                                 }
546                                 continue;
547                         }
548
549                         // Deletions are always sent via DFRN as well.
550                         // This is done until we can perform deletions of foreign comments on our own threads via AP.
551                         if (($cmd != Delivery::DELETION) && in_array($contact['id'], $ap_contacts)) {
552                                 Logger::info('Contact is already delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
553                                 continue;
554                         }
555
556                         if (!empty($contact['id']) && Contact::isArchived($contact['id'])) {
557                                 Logger::info('Contact is archived, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
558                                 continue;
559                         }
560
561                         if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
562                                 Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]);
563                                 continue;
564                         }
565
566                         if (self::skipActivityPubForDiaspora($contact, $target_item, $thr_parent)) {
567                                 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']]);
568                                 continue;
569                         }
570
571                         // Don't deliver to Diaspora if it already had been done as batch delivery
572                         if (!$in_batch && $batch_delivery && ($contact['network'] == Protocol::DIASPORA)) {
573                                 Logger::info('Diaspora contact is already delivered via batch', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
574                                 continue;
575                         }
576
577                         // Don't deliver to folks who have already been delivered to
578                         if (in_array($contact['id'], $conversants)) {
579                                 Logger::info('Already delivery', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
580                                 continue;
581                         }
582
583                         if (empty($contact['gsid'])) {
584                                 $reachable = GServer::reachable($contact);
585                         } elseif (!DI::config()->get('system', 'bulk_delivery')) {
586                                 $reachable = GServer::isReachableById($contact['gsid']);
587                         } else {
588                                 $reachable = !GServer::isDefunctById($contact['gsid']);
589                         }
590
591                         if (!$reachable) {
592                                 Logger::info('Server is not reachable', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
593                                 continue;
594                         }
595
596                         if (($contact['network'] == Protocol::ACTIVITYPUB) && !DI::dsprContact()->existsByUriId($contact['uri-id'])) {
597                                 Logger::info('The ActivityPub contact does not support Diaspora, so skip delivery via Diaspora', ['id' => $post_uriid, 'uid' => $sender_uid, 'url' => $contact['url']]);
598                                 continue;
599                         }
600
601                         Logger::info('Delivery', ['batch' => $in_batch, 'target' => $post_uriid, 'uid' => $sender_uid, 'guid' => $target_item['guid'] ?? '', 'to' => $contact]);
602
603                         // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
604                         // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
605                         // The conversion in Markdown reduces the formatting, so these posts should arrive after the Friendica posts.
606                         // This is only important for high and medium priority tasks and not for Low priority jobs like deletions.
607                         if (($contact['network'] == Protocol::DIASPORA) && in_array($a->getQueueValue('priority'), [Worker::PRIORITY_HIGH, Worker::PRIORITY_MEDIUM])) {
608                                 $deliver_options = ['priority' => $a->getQueueValue('priority'), 'dont_fork' => true];
609                         } else {
610                                 $deliver_options = ['priority' => $a->getQueueValue('priority'), 'created' => $a->getQueueValue('created'), 'dont_fork' => true];
611                         }
612
613                         if (!empty($contact['gsid']) && DI::config()->get('system', 'bulk_delivery')) {
614                                 $delivery_queue_count++;
615                                 $deliveryQueueItem = DI::deliveryQueueItemFactory()->createFromDelivery($cmd, $post_uriid, new \DateTimeImmutable($target_item['created']), $contact['id'], $contact['gsid'], $sender_uid);
616                                 DI::deliveryQueueItemRepo()->save($deliveryQueueItem);
617                                 Worker::add(['priority' => Worker::PRIORITY_HIGH, 'dont_fork' => true], 'BulkDelivery', $contact['gsid']);
618                         } else {
619                                 if (Worker::add($deliver_options, 'Delivery', $cmd, $post_uriid, (int)$contact['id'], $sender_uid)) {
620                                         $delivery_queue_count++;
621                                 }
622                         }
623
624                         Worker::coolDown();
625                 }
626                 return $delivery_queue_count;
627         }
628
629         /**
630          * Deliver the message via OStatus
631          *
632          * @param int $target_id
633          * @param array $target_item
634          * @param array $owner
635          * @param array $url_recipients
636          * @param bool $public_message
637          * @param bool $push_notify
638          *
639          * @return int Count of sent Salmon notifications
640          * @throws InternalServerErrorException
641          * @throws Exception
642          */
643         private static function deliverOStatus(int $target_id, array $target_item, array $owner, array $url_recipients, bool $public_message, bool $push_notify): int
644         {
645                 $a = DI::app();
646                 $delivery_queue_count = 0;
647
648                 $url_recipients = array_filter($url_recipients);
649                 // send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
650                 // They are especially used for notifications to OStatus users that don't follow us.
651                 if (count($url_recipients) && ($public_message || $push_notify) && !empty($target_item)) {
652                         $slap = OStatus::salmon($target_item, $owner);
653                         foreach ($url_recipients as $url) {
654                                 Logger::info('Salmon delivery', ['item' => $target_id, 'to' => $url]);
655
656                                 $delivery_queue_count++;
657                                 Salmon::slapper($owner, $url, $slap);
658                                 Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Post\DeliveryData::OSTATUS);
659                         }
660                 }
661
662                 // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
663                 if ($push_notify) {
664                         Logger::info('Activating internal PuSH', ['uid' => $owner['uid']]);
665
666                         // Handling the pubsubhubbub requests
667                         PushSubscriber::publishFeed($owner['uid'], $a->getQueueValue('priority'));
668                 }
669                 return $delivery_queue_count;
670         }
671
672         /**
673          * Checks if the current delivery shouldn't be transported to Diaspora.
674          * This is done for posts from AP authors or posts that are comments to AP authors.
675          *
676          * @param array  $contact    Receiver of the post
677          * @param array  $item       The post
678          * @param array  $thr_parent The thread parent
679          *
680          * @return bool
681          */
682         private static function skipActivityPubForDiaspora(array $contact, array $item, array $thr_parent): bool
683         {
684                 // No skipping needs to be done when delivery isn't done to Diaspora
685                 if ($contact['network'] != Protocol::DIASPORA) {
686                         return false;
687                 }
688
689                 // Skip the delivery to Diaspora if the item is from an ActivityPub author
690                 if (!empty($item['author-network']) && ($item['author-network'] == Protocol::ACTIVITYPUB)) {
691                         return true;
692                 }
693
694                 // Skip the delivery to Diaspora if the thread parent is from an ActivityPub author
695                 if (!empty($thr_parent['author-network']) && ($thr_parent['author-network'] == Protocol::ACTIVITYPUB)) {
696                         return true;
697                 }
698
699                 return false;
700         }
701
702         /**
703          * Checks if the current action is a deletion command of a account removal activity
704          * For Diaspora and ActivityPub we don't need to send single item deletion calls.
705          * These protocols do have a dedicated command for deleting a whole account.
706          *
707          * @param string $cmd     Notifier command
708          * @param array  $owner   Sender of the post
709          * @param string $network Receiver network
710          *
711          * @return bool
712          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
713          * @throws \ImagickException
714          */
715         private static function isRemovalActivity(string $cmd, array $owner, string $network): bool
716         {
717                 return ($cmd == Delivery::DELETION) && $owner['account_removed'] && in_array($network, [Protocol::ACTIVITYPUB, Protocol::DIASPORA]);
718         }
719
720         /**
721          * @param int    $self_user_id
722          * @param int    $priority The priority the Notifier queue item was created with
723          * @param string $created  The date the Notifier queue item was created on
724          *
725          * @return bool
726          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
727          * @throws \ImagickException
728          */
729         private static function notifySelfRemoval(int $self_user_id, int $priority, string $created): bool
730         {
731                 $owner = User::getOwnerDataById($self_user_id);
732                 if (empty($self_user_id) || empty($owner)) {
733                         return false;
734                 }
735
736                 $contacts_stmt = DBA::select('contact', [], ['self' => false, 'uid' => $self_user_id]);
737                 if (!DBA::isResult($contacts_stmt)) {
738                         return false;
739                 }
740
741                 while($contact = DBA::fetch($contacts_stmt)) {
742                         Contact::terminateFriendship($contact);
743                 }
744                 DBA::close($contacts_stmt);
745
746                 $inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser($self_user_id);
747                 foreach ($inboxes as $inbox => $receivers) {
748                         Logger::info('Account removal via ActivityPub', ['uid' => $self_user_id, 'inbox' => $inbox]);
749                         Worker::add(['priority' => Worker::PRIORITY_NEGLIGIBLE, 'created' => $created, 'dont_fork' => true],
750                                 'APDelivery', Delivery::REMOVAL, 0, $inbox, $self_user_id, $receivers);
751                         Worker::coolDown();
752                 }
753
754                 return true;
755         }
756
757         /**
758          * @param string $cmd
759          * @param array  $target_item
760          * @param array  $parent
761          * @param array  $thr_parent
762          * @param int    $priority The priority the Notifier queue item was created with
763          * @param string $created  The date the Notifier queue item was created on
764          *
765          * @return array 'count' => The number of delivery tasks created, 'contacts' => their contact ids
766          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
767          * @throws \ImagickException
768          * @todo Unused parameter $owner
769          */
770         private static function activityPubDelivery($cmd, array $target_item, array $parent, array $thr_parent, int $priority, string $created, $owner): array
771         {
772                 // Don't deliver via AP when the starting post isn't from a federated network
773                 if (!in_array($parent['network'], Protocol::FEDERATED)) {
774                         Logger::info('Parent network is no federated network, so no AP delivery', ['network' => $parent['network']]);
775                         return ['count' => 0, 'contacts' => []];
776                 }
777
778                 // Don't deliver via AP when the starting post is delivered via Diaspora
779                 if ($parent['network'] == Protocol::DIASPORA) {
780                         Logger::info('Parent network is Diaspora, so no AP delivery');
781                         return ['count' => 0, 'contacts' => []];
782                 }
783
784                 // Also don't deliver when the direct thread parent was delivered via Diaspora
785                 if ($thr_parent['network'] == Protocol::DIASPORA) {
786                         Logger::info('Thread parent network is Diaspora, so no AP delivery');
787                         return ['count' => 0, 'contacts' => []];
788                 }
789
790                 // Posts from Diaspora contacts are transmitted via Diaspora
791                 if ($target_item['network'] == Protocol::DIASPORA) {
792                         Logger::info('Post network is Diaspora, so no AP delivery');
793                         return ['count' => 0, 'contacts' => []];
794                 }
795
796                 $inboxes = [];
797                 $relay_inboxes = [];
798
799                 $uid = $target_item['contact-uid'] ?: $target_item['uid'];
800
801                 // Update the locally stored follower list when we deliver to a group
802                 foreach (Tag::getByURIId($target_item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $tag) {
803                         $target_contact = Contact::getByURL(Strings::normaliseLink($tag['url']), null, [], $uid);
804                         if ($target_contact && $target_contact['contact-type'] == Contact::TYPE_COMMUNITY && $target_contact['manually-approve']) {
805                                 Circle::updateMembersForGroup($target_contact['id']);
806                         }
807                 }
808
809                 if ($target_item['origin']) {
810                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
811
812                         if (in_array($target_item['private'], [Item::PUBLIC])) {
813                                 $inboxes = ActivityPub\Transmitter::addRelayServerInboxesForItem($target_item['id'], $inboxes);
814                                 $relay_inboxes = ActivityPub\Transmitter::addRelayServerInboxes();
815                         }
816
817                         Logger::info('Origin item will be distributed', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
818                         $check_signature = false;
819                 } elseif (!Post\Activity::exists($target_item['uri-id'])) {
820                         Logger::info('Remote item is no AP post. It will not be distributed.', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
821                         return ['count' => 0, 'contacts' => []];
822                 } elseif ($parent['origin'] && (($target_item['gravity'] != Item::GRAVITY_ACTIVITY) || DI::config()->get('system', 'redistribute_activities'))) {
823                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid);
824
825                         if (in_array($target_item['private'], [Item::PUBLIC])) {
826                                 $inboxes = ActivityPub\Transmitter::addRelayServerInboxesForItem($parent['id'], $inboxes);
827                         }
828
829                         Logger::info('Remote item will be distributed', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
830                         $check_signature = ($target_item['gravity'] == Item::GRAVITY_ACTIVITY);
831                 } else {
832                         Logger::info('Remote activity will not be distributed', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
833                         return ['count' => 0, 'contacts' => []];
834                 }
835
836                 if (empty($inboxes) && empty($relay_inboxes)) {
837                         Logger::info('No inboxes found for item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . '. It will not be distributed.');
838                         return ['count' => 0, 'contacts' => []];
839                 }
840
841                 // Fill the item cache
842                 $activity = ActivityPub\Transmitter::createCachedActivityFromItem($target_item['id'], true);
843                 if (empty($activity)) {
844                         Logger::info('Item cache was not created. The post will not be distributed.', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
845                         return ['count' => 0, 'contacts' => []];
846                 }
847
848                 if ($check_signature && !LDSignature::isSigned($activity)) {
849                         Logger::info('Unsigned remote activity will not be distributed', ['id' => $target_item['id'], 'url' => $target_item['uri'], 'verb' => $target_item['verb']]);
850                         return ['count' => 0, 'contacts' => []];
851                 }
852
853                 $delivery_queue_count = 0;
854                 $contacts = [];
855
856                 foreach ($inboxes as $inbox => $receivers) {
857                         $contacts = array_merge($contacts, $receivers);
858
859                         if ((count($receivers) == 1) && DI::baseUrl()->isLocalUrl($inbox)) {
860                                 $contact = Contact::getById($receivers[0], ['url']);
861                                 if (!in_array($cmd, [Delivery::RELOCATION, Delivery::SUGGESTION, Delivery::MAIL]) && ($target_uid = User::getIdForURL($contact['url']))) {
862                                         if ($cmd == Delivery::DELETION) {
863                                                 Logger::info('No need to deliver deletions internally', ['uid' => $target_uid, 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
864                                                 continue;
865                                         }
866                                         if ($target_item['origin'] || ($target_item['network'] != Protocol::ACTIVITYPUB)) {
867                                                 if ($target_uid != $target_item['uid']) {
868                                                         $fields = ['protocol' => Conversation::PARCEL_LOCAL_DFRN, 'direction' => Conversation::PUSH, 'post-reason' => Item::PR_BCC];
869                                                         Item::storeForUserByUriId($target_item['uri-id'], $target_uid, $fields, $target_item['uid']);
870                                                         Logger::info('Delivered locally', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
871                                                 } else {
872                                                         Logger::info('No need to deliver to myself', ['uid' => $target_uid, 'guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
873                                                 }
874                                         } else {
875                                                 Logger::info('Remote item does not need to be delivered locally', ['guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
876                                         }
877                                         continue;
878                                 }
879                         } elseif ((count($receivers) >= 1) && DI::baseUrl()->isLocalUrl($inbox)) {
880                                 Logger::info('Is this a thing?', ['guid' => $target_item['guid'], 'uri-id' => $target_item['uri-id'], 'uri' => $target_item['uri']]);
881                         }
882
883                         Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
884
885                         if (DI::config()->get('system', 'bulk_delivery')) {
886                                 $delivery_queue_count++;
887                                 Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd, $receivers);
888                                 Worker::add([Worker::PRIORITY_HIGH, 'dont_fork' => true], 'APDelivery', '', 0, $inbox, 0);
889                         } else {
890                                 if (Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
891                                                 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, $receivers, $target_item['uri-id'])) {
892                                         $delivery_queue_count++;
893                                 }
894                         }
895                         Worker::coolDown();
896                 }
897
898                 // We deliver posts to relay servers slightly delayed to prioritize the direct delivery
899                 foreach ($relay_inboxes as $inbox) {
900                         Logger::info('Delivery to relay servers via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
901
902                         if (DI::config()->get('system', 'bulk_delivery')) {
903                                 $delivery_queue_count++;
904                                 Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd, []);
905                                 Worker::add([Worker::PRIORITY_MEDIUM, 'dont_fork' => true], 'APDelivery', '', 0, $inbox, 0);
906                         } else {
907                                 if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, [], $target_item['uri-id'])) {
908                                         $delivery_queue_count++;
909                                 }
910                         }
911                         Worker::coolDown();
912                 }
913
914                 return ['count' => $delivery_queue_count, 'contacts' => $contacts];
915         }
916 }