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