]> git.mxchange.org Git - friendica.git/blob - src/Worker/Notifier.php
Forums now are working with AP as well
[friendica.git] / src / Worker / Notifier.php
1 <?php
2 /**
3  * @file src/Worker/Notifier.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Model\APContact;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Conversation;
17 use Friendica\Model\Group;
18 use Friendica\Model\Item;
19 use Friendica\Model\ItemDeliveryData;
20 use Friendica\Model\PushSubscriber;
21 use Friendica\Model\User;
22 use Friendica\Network\Probe;
23 use Friendica\Protocol\ActivityPub;
24 use Friendica\Protocol\Diaspora;
25 use Friendica\Protocol\OStatus;
26 use Friendica\Protocol\Salmon;
27
28 require_once 'include/items.php';
29
30 /*
31  * The notifier is typically called with:
32  *
33  *              Worker::add(PRIORITY_HIGH, "Notifier", COMMAND, ITEM_ID);
34  *
35  * where COMMAND is one of the following:
36  *
37  *              activity                                (in diaspora.php, dfrn_confirm.php, profiles.php)
38  *              comment-import                  (in diaspora.php, items.php)
39  *              comment-new                             (in item.php)
40  *              drop                                    (in diaspora.php, items.php, photos.php)
41  *              edit_post                               (in item.php)
42  *              event                                   (in events.php)
43  *              like                                    (in like.php, poke.php)
44  *              mail                                    (in message.php)
45  *              suggest                                 (in fsuggest.php)
46  *              tag                                             (in photos.php, poke.php, tagger.php)
47  *              tgroup                                  (in items.php)
48  *              wall-new                                (in photos.php, item.php)
49  *              removeme                                (in Contact.php)
50  *              relocate                                (in uimport.php)
51  *
52  * and ITEM_ID is the id of the item in the database that needs to be sent to others.
53  */
54
55 class Notifier
56 {
57         public static function execute($cmd, $target_id)
58         {
59                 $a = BaseObject::getApp();
60
61                 Logger::log('Invoked: ' . $cmd . ': ' . $target_id, Logger::DEBUG);
62
63                 $top_level = false;
64                 $recipients = [];
65                 $url_recipients = [];
66
67                 $delivery_contacts_stmt = null;
68                 $target_item = [];
69                 $items = [];
70                 $delivery_queue_count = 0;
71
72                 if ($cmd == Delivery::MAIL) {
73                         $message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $target_id]);
74                         if (!DBA::isResult($message)) {
75                                 return;
76                         }
77                         $uid = $message['uid'];
78                         $recipients[] = $message['contact-id'];
79                 } elseif ($cmd == Delivery::SUGGESTION) {
80                         $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]);
81                         if (!DBA::isResult($suggest)) {
82                                 return;
83                         }
84                         $uid = $suggest['uid'];
85                         $recipients[] = $suggest['cid'];
86                 } elseif ($cmd == Delivery::REMOVAL) {
87                         return self::notifySelfRemoval($target_id, $a->queue['priority'], $a->queue['created']);
88                 } elseif ($cmd == Delivery::RELOCATION) {
89                         $uid = $target_id;
90
91                         $condition = ['uid' => $target_id, 'self' => false, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
92                         $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'network', 'batch'], $condition);
93                 } else {
94                         // find ancestors
95                         $condition = ['id' => $target_id, 'visible' => true, 'moderated' => false];
96                         $target_item = Item::selectFirst([], $condition);
97
98                         if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
99                                 return;
100                         }
101
102                         if (!empty($target_item['contact-uid'])) {
103                                 $uid = $target_item['contact-uid'];
104                         } elseif (!empty($target_item['uid'])) {
105                                 $uid = $target_item['uid'];
106                         } else {
107                                 Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
108                                 return;
109                         }
110
111                         $condition = ['parent' => $target_item['parent'], 'visible' => true, 'moderated' => false];
112                         $params = ['order' => ['id']];
113                         $items_stmt = Item::select([], $condition, $params);
114                         if (!DBA::isResult($items_stmt)) {
115                                 return;
116                         }
117
118                         $items = Item::inArray($items_stmt);
119
120                         // avoid race condition with deleting entries
121                         if ($items[0]['deleted']) {
122                                 foreach ($items as $item) {
123                                         $item['deleted'] = 1;
124                                 }
125                         }
126
127                         if ((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
128                                 Logger::log('Top level post');
129                                 $top_level = true;
130                         }
131                 }
132
133                 $owner = User::getOwnerDataById($uid);
134                 if (!$owner) {
135                         return;
136                 }
137
138                 // Should the post be transmitted to Diaspora?
139                 $diaspora_delivery = true;
140
141                 // If this is a public conversation, notify the feed hub
142                 $public_message = true;
143
144                 // Do a PuSH
145                 $push_notify = false;
146
147                 // Deliver directly to a forum, don't PuSH
148                 $direct_forum_delivery = false;
149
150                 $followup = false;
151                 $recipients_followup = [];
152
153                 if (!empty($target_item) && !empty($items)) {
154                         $parent = $items[0];
155
156                         if (!self::isRemovalActivity($cmd, $owner, Protocol::ACTIVITYPUB)) {
157                                 $delivery_queue_count += self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], $a->queue['created'], $owner);
158                         }
159
160                         $fields = ['network', 'author-id', 'owner-id'];
161                         $condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
162                         $thr_parent = Item::selectFirst($fields, $condition);
163
164                         Logger::log('GUID: ' . $target_item["guid"] . ': Parent is ' . $parent['network'] . '. Thread parent is ' . $thr_parent['network'], Logger::DEBUG);
165
166                         // This is IMPORTANT!!!!
167
168                         // We will only send a "notify owner to relay" or followup message if the referenced post
169                         // originated on our system by virtue of having our hostname somewhere
170                         // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
171
172                         // if $parent['wall'] == 1 we will already have the parent message in our array
173                         // and we will relay the whole lot.
174
175                         $localhost = str_replace('www.','',$a->getHostName());
176                         if (strpos($localhost,':')) {
177                                 $localhost = substr($localhost,0,strpos($localhost,':'));
178                         }
179                         /**
180                          *
181                          * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
182                          * have been known to cause runaway conditions which affected several servers, along with
183                          * permissions issues.
184                          *
185                          */
186
187                         $relay_to_owner = false;
188
189                         if (!$top_level && ($parent['wall'] == 0) && (stristr($target_item['uri'],$localhost))) {
190                                 $relay_to_owner = true;
191                         }
192
193
194                         if (($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
195                                 $relay_to_owner = true;
196                         }
197
198                         // until the 'origin' flag has been in use for several months
199                         // we will just use it as a fallback test
200                         // later we will be able to use it as the primary test of whether or not to relay.
201
202                         if (!$target_item['origin']) {
203                                 $relay_to_owner = false;
204                         }
205                         if ($parent['origin']) {
206                                 $relay_to_owner = false;
207                         }
208
209                         // Special treatment for forum posts
210                         if (Item::isForumPost($target_item, $owner)) {
211                                 $relay_to_owner = true;
212                                 $direct_forum_delivery = true;
213                         }
214
215                         // Avoid that comments in a forum thread are sent to OStatus
216                         if (Item::isForumPost($parent, $owner)) {
217                                 $direct_forum_delivery = true;
218                         }
219
220                         if ($relay_to_owner) {
221                                 // local followup to remote post
222                                 $followup = true;
223                                 $public_message = false; // not public
224                                 $recipients = [$parent['contact-id']];
225                                 $recipients_followup  = [$parent['contact-id']];
226
227                                 Logger::log('Followup ' . $target_item['guid'] . ' to ' . $parent['contact-id'], Logger::DEBUG);
228
229                                 //if (!$target_item['private'] && $target_item['wall'] &&
230                                 if (!$target_item['private'] &&
231                                         (strlen($target_item['allow_cid'].$target_item['allow_gid'].
232                                                 $target_item['deny_cid'].$target_item['deny_gid']) == 0))
233                                         $push_notify = true;
234
235                                 if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
236                                         $push_notify = true;
237
238                                         if ($parent["network"] == Protocol::OSTATUS) {
239                                                 // Distribute the message to the DFRN contacts as if this wasn't a followup since OStatus can't relay comments
240                                                 // Currently it is work at progress
241                                                 $condition = ['uid' => $uid, 'network' => Protocol::DFRN, 'blocked' => false, 'pending' => false, 'archive' => false];
242                                                 $followup_contacts_stmt = DBA::select('contact', ['id'], $condition);
243                                                 while($followup_contact = DBA::fetch($followup_contacts_stmt)) {
244                                                         $recipients_followup[] = $followup_contact['id'];
245                                                 }
246                                                 DBA::close($followup_contacts_stmt);
247                                         }
248                                 }
249
250                                 if ($direct_forum_delivery) {
251                                         $push_notify = false;
252                                 }
253
254                                 Logger::log('Notify ' . $target_item["guid"] .' via PuSH: ' . ($push_notify ? "Yes":"No"), Logger::DEBUG);
255                         } else {
256                                 $followup = false;
257
258                                 Logger::log('Distributing directly ' . $target_item["guid"], Logger::DEBUG);
259
260                                 // don't send deletions onward for other people's stuff
261
262                                 if ($target_item['deleted'] && !intval($target_item['wall'])) {
263                                         Logger::log('Ignoring delete notification for non-wall item');
264                                         return;
265                                 }
266
267                                 if (strlen($parent['allow_cid'])
268                                         || strlen($parent['allow_gid'])
269                                         || strlen($parent['deny_cid'])
270                                         || strlen($parent['deny_gid'])) {
271                                         $public_message = false; // private recipients, not public
272                                 }
273
274                                 $allow_people = expand_acl($parent['allow_cid']);
275                                 $allow_groups = Group::expand(expand_acl($parent['allow_gid']),true);
276                                 $deny_people  = expand_acl($parent['deny_cid']);
277                                 $deny_groups  = Group::expand(expand_acl($parent['deny_gid']));
278
279                                 // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
280                                 // a delivery fork. private groups (forum_mode == 2) do not uplink
281
282                                 if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== 'uplink')) {
283                                         Worker::add($a->queue['priority'], 'Notifier', 'uplink', $target_id);
284                                 }
285
286                                 foreach ($items as $item) {
287                                         $recipients[] = $item['contact-id'];
288                                         // pull out additional tagged people to notify (if public message)
289                                         if ($public_message && strlen($item['inform'])) {
290                                                 $people = explode(',',$item['inform']);
291                                                 foreach ($people as $person) {
292                                                         if (substr($person,0,4) === 'cid:') {
293                                                                 $recipients[] = intval(substr($person,4));
294                                                         } else {
295                                                                 $url_recipients[] = substr($person,4);
296                                                         }
297                                                 }
298                                         }
299                                 }
300
301                                 if (count($url_recipients)) {
302                                         Logger::log('Deliver ' . $target_item["guid"] . ' to _recipients ' . json_encode($url_recipients));
303                                 }
304
305                                 $recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups));
306                                 $deny = array_unique(array_merge($deny_people, $deny_groups));
307                                 $recipients = array_diff($recipients, $deny);
308
309                                 // If this is a public message and pubmail is set on the parent, include all your email contacts
310                                 if (
311                                         function_exists('imap_open')
312                                         && !Config::get('system','imap_disabled')
313                                         && $public_message
314                                         && intval($target_item['pubmail'])
315                                 ) {
316                                         $mail_contacts_stmt = DBA::select('contact', ['id'], ['uid' => $uid, 'network' => Protocol::MAIL]);
317                                         while ($mail_contact = DBA::fetch($mail_contacts_stmt)) {
318                                                 $recipients[] = $mail_contact['id'];
319                                         }
320                                         DBA::close($mail_contacts_stmt);
321                                 }
322                         }
323
324                         // If the thread parent is OStatus then do some magic to distribute the messages.
325                         // We have not only to look at the parent, since it could be a Friendica thread.
326                         if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
327                                 $diaspora_delivery = false;
328
329                                 Logger::log('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent['author-id']." - Owner: ".$thr_parent['owner-id'], Logger::DEBUG);
330
331                                 // Send a salmon to the parent author
332                                 $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['author-id']]);
333                                 if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
334                                         Logger::log('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
335                                         $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
336                                 }
337
338                                 // Send a salmon to the parent owner
339                                 $probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['owner-id']]);
340                                 if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
341                                         Logger::log('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
342                                         $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
343                                 }
344
345                                 // Send a salmon notification to every person we mentioned in the post
346                                 $arr = explode(',',$target_item['tag']);
347                                 foreach ($arr as $x) {
348                                         //Logger::log('Checking tag '.$x, Logger::DEBUG);
349                                         $matches = null;
350                                         if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
351                                                         $probed_contact = Probe::uri($matches[1]);
352                                                 if ($probed_contact["notify"] != "") {
353                                                         Logger::log('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
354                                                         $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
355                                                 }
356                                         }
357                                 }
358
359                                 // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
360                                 $networks = [Protocol::OSTATUS, Protocol::DFRN];
361                         } else {
362                                 $networks = [Protocol::OSTATUS, Protocol::DFRN, Protocol::DIASPORA, Protocol::MAIL];
363                         }
364                 } else {
365                         $public_message = false;
366                 }
367
368                 if (empty($delivery_contacts_stmt)) {
369                         if ($followup) {
370                                 $recipients = $recipients_followup;
371                         }
372                         $condition = ['id' => $recipients, 'self' => false,
373                                 'blocked' => false, 'pending' => false, 'archive' => false];
374                         if (!empty($networks)) {
375                                 $condition['network'] = $networks;
376                         }
377                         $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'network', 'batch'], $condition);
378                 }
379
380                 $conversants = [];
381                 $batch_delivery = false;
382
383                 if ($public_message && !in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION]) && !$followup) {
384                         $relay_list = [];
385
386                         if ($diaspora_delivery) {
387                                 $batch_delivery = true;
388
389                                 $relay_list_stmt = DBA::p(
390                                         "SELECT
391                                         `batch`,
392                                         ANY_VALUE(`id`) AS `id`,
393                                         ANY_VALUE(`name`) AS `name`,
394                                         ANY_VALUE(`network`) AS `network`
395                                         FROM `contact`
396                                         WHERE `network` = ?
397                                         AND `batch` != ''
398                                         AND `uid` = ?
399                                         AND `rel` != ?
400                                         AND NOT `blocked`
401                                         AND NOT `pending`
402                                         AND NOT `archive`
403                                         GROUP BY `batch`",
404                                         Protocol::DIASPORA,
405                                         $owner['uid'],
406                                         Contact::SHARING
407                                 );
408                                 $relay_list = DBA::toArray($relay_list_stmt);
409
410                                 // Fetch the participation list
411                                 // The function will ensure that there are no duplicates
412                                 $relay_list = Diaspora::participantsForThread($target_id, $relay_list);
413
414                                 // Add the relay to the list, avoid duplicates.
415                                 // Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly.
416                                 if (!$followup && !Item::isForumPost($target_item, $owner)) {
417                                         $relay_list = Diaspora::relayList($target_id, $relay_list);
418                                 }
419                         }
420
421                         $condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
422                                 'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
423
424                         $r2 = DBA::toArray(DBA::select('contact', ['id', 'url', 'name', 'network'], $condition));
425
426                         $r = array_merge($r2, $relay_list);
427
428                         if (DBA::isResult($r)) {
429                                 foreach ($r as $rr) {
430                                         if (self::isRemovalActivity($cmd, $owner, $rr['network'])) {
431                                                 Logger::log('Skipping dropping for ' . $rr['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
432                                                 continue;
433                                         }
434
435                                         if (Config::get('debug', 'total_ap_delivery') && !empty($rr['url']) && ($rr['network'] == Protocol::DFRN) && !empty(APContact::getByURL($rr['url'], false))) {
436                                                 Logger::log('Skipping contact ' . $rr['url'] . ' since it will be delivered via AP', Logger::DEBUG);
437                                                 continue;
438                                         }
439
440                                         $conversants[] = $rr['id'];
441
442                                         $delivery_queue_count++;
443
444                                         Logger::log('Public delivery of item ' . $target_item["guid"] . ' (' . $target_id . ') to ' . json_encode($rr), Logger::DEBUG);
445
446                                         // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
447                                         // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
448                                         // The conversion in Markdown reduces the formatting, so these posts should arrive after the Friendica posts.
449                                         // This is only important for high and medium priority tasks and not for Low priority jobs like deletions.
450                                         if (($rr['network'] == Protocol::DIASPORA) && in_array($a->queue['priority'], [PRIORITY_HIGH, PRIORITY_MEDIUM])) {
451                                                 $deliver_options = ['priority' => $a->queue['priority'], 'dont_fork' => true];
452                                         } else {
453                                                 $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
454                                         }
455                                         Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$rr['id']);
456                                 }
457                         }
458
459                         $push_notify = true;
460                 }
461
462                 // delivery loop
463                 while ($contact = DBA::fetch($delivery_contacts_stmt)) {
464                         if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
465                                 Logger::log('Skipping dropping for ' . $contact['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
466                                 continue;
467                         }
468
469                         if (Config::get('debug', 'total_ap_delivery') && ($contact['network'] == Protocol::DFRN) && !empty(APContact::getByURL($contact['url'], false))) {
470                                 Logger::log('Skipping contact ' . $contact['url'] . ' since it will be delivered via AP', Logger::DEBUG);
471                                 continue;
472                         }
473
474                         // Don't deliver to Diaspora if it already had been done as batch delivery
475                         if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) {
476                                 Logger::log('Already delivered  id ' . $target_id . ' via batch to ' . json_encode($contact), Logger::DEBUG);
477                                 continue;
478                         }
479
480                         // Don't deliver to folks who have already been delivered to
481                         if (in_array($contact['id'], $conversants)) {
482                                 Logger::log('Already delivered id ' . $target_id. ' to ' . json_encode($contact), Logger::DEBUG);
483                                 continue;
484                         }
485
486                         $delivery_queue_count++;
487
488                         Logger::log('Delivery of item ' . $target_id . ' to ' . json_encode($contact), Logger::DEBUG);
489
490                         // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
491                         // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
492                         // The conversion in Markdown reduces the formatting, so these posts should arrive after the Friendica posts.
493                         if ($contact['network'] == Protocol::DIASPORA) {
494                                 $deliver_options = ['priority' => $a->queue['priority'], 'dont_fork' => true];
495                         } else {
496                                 $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
497                         }
498                         Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$contact['id']);
499                 }
500                 DBA::close($delivery_contacts_stmt);
501
502                 $url_recipients = array_filter($url_recipients);
503                 // send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
504                 // They are especially used for notifications to OStatus users that don't follow us.
505                 if (!Config::get('system', 'dfrn_only') && count($url_recipients) && ($public_message || $push_notify) && !empty($target_item)) {
506                         $delivery_queue_count += count($url_recipients);
507                         $slap = OStatus::salmon($target_item, $owner);
508                         foreach ($url_recipients as $url) {
509                                 Logger::log('Salmon delivery of item ' . $target_id . ' to ' . $url);
510                                 /// @TODO Redeliver/queue these items on failure, though there is no contact record
511                                 Salmon::slapper($owner, $url, $slap);
512                                 ItemDeliveryData::incrementQueueDone($target_id);
513                         }
514                 }
515
516                 // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
517                 if ($push_notify) {
518                         Logger::log('Activating internal PuSH for item '.$target_id, Logger::DEBUG);
519
520                         // Handling the pubsubhubbub requests
521                         PushSubscriber::publishFeed($owner['uid'], $a->queue['priority']);
522                 }
523
524                 if (!empty($target_item)) {
525                         Logger::log('Calling hooks for ' . $cmd . ' ' . $target_id, Logger::DEBUG);
526
527                         if (in_array($cmd, [Delivery::POST, Delivery::COMMENT])) {
528                                 ItemDeliveryData::update($target_item['id'], ['queue_count' => $delivery_queue_count]);
529                         }
530
531                         Hook::fork($a->queue['priority'], 'notifier_normal', $target_item);
532
533                         Hook::callAll('notifier_end', $target_item);
534                 }
535
536                 return;
537         }
538
539         /**
540          * Checks if the current action is a deletion command of a account removal activity
541          * For Diaspora and ActivityPub we don't need to send single item deletion calls.
542          * These protocols do have a dedicated command for deleting a whole account.
543          *
544          * @param string $cmd     Notifier command
545          * @param array  $owner   Sender of the post
546          * @param string $network Receiver network
547          * @return bool
548          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
549          * @throws \ImagickException
550          */
551         private static function isRemovalActivity($cmd, $owner, $network)
552         {
553                 return ($cmd == Delivery::DELETION) && $owner['account_removed'] && in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DIASPORA]);
554         }
555
556         /**
557          * @param int    $self_user_id
558          * @param int    $priority The priority the Notifier queue item was created with
559          * @param string $created  The date the Notifier queue item was created on
560          * @return bool
561          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
562          * @throws \ImagickException
563          */
564         private static function notifySelfRemoval($self_user_id, $priority, $created)
565         {
566                 $owner = User::getOwnerDataById($self_user_id);
567                 if (!$owner) {
568                         return false;
569                 }
570
571                 $contacts_stmt = DBA::select('contact', [], ['self' => false, 'uid' => $self_user_id]);
572                 if (!DBA::isResult($contacts_stmt)) {
573                         return false;
574                 }
575
576                 while($contact = DBA::fetch($contacts_stmt)) {
577                         Contact::terminateFriendship($owner, $contact, true);
578                 }
579                 DBA::close($contacts_stmt);
580
581                 $inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser(0);
582                 foreach ($inboxes as $inbox) {
583                         Logger::log('Account removal for user ' . $self_user_id . ' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
584                         Worker::add(['priority' => PRIORITY_NEGLIGIBLE, 'created' => $created, 'dont_fork' => true],
585                                 'APDelivery', Delivery::REMOVAL, '', $inbox, $self_user_id);
586                 }
587
588                 return true;
589         }
590
591         /**
592          * @param string $cmd
593          * @param array  $target_item
594          * @param array  $parent
595          * @param int    $priority The priority the Notifier queue item was created with
596          * @param string $created  The date the Notifier queue item was created on
597          * @return int The number of delivery tasks created
598          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
599          * @throws \ImagickException
600          */
601         private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created, $owner)
602         {
603                 $inboxes = [];
604
605                 $uid = $target_item['contact-uid'] ?: $target_item['uid'];
606
607                 if ($target_item['origin']) {
608                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
609                         Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
610                 } elseif (Item::isForumPost($target_item, $owner)) {
611                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true);
612                         Logger::log('Forum item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
613                 } elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) {
614                         Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', Logger::DEBUG);
615                         return 0;
616                 } elseif ($parent['origin']) {
617                         // Remote items are transmitted via the personal inboxes.
618                         // Doing so ensures that the dedicated receiver will get the message.
619                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true, $target_item['id']);
620                         Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
621                 }
622
623                 if (empty($inboxes)) {
624                         Logger::log('No inboxes found for item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', Logger::DEBUG);
625                         return 0;
626                 }
627
628                 // Fill the item cache
629                 ActivityPub\Transmitter::createCachedActivityFromItem($target_item['id'], true);
630
631                 foreach ($inboxes as $inbox) {
632                         Logger::log('Deliver ' . $target_item['id'] .' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
633
634                         Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
635                                         'APDelivery', $cmd, $target_item['id'], $inbox, $uid);
636                 }
637
638                 return count($inboxes);
639         }
640 }