3 * @file src/Worker/Notifier.php
5 namespace Friendica\Worker;
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;
28 require_once 'include/items.php';
31 * The notifier is typically called with:
33 * Worker::add(PRIORITY_HIGH, "Notifier", COMMAND, ITEM_ID);
35 * where COMMAND is one of the following:
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)
52 * and ITEM_ID is the id of the item in the database that needs to be sent to others.
57 public static function execute($cmd, $target_id)
59 $a = BaseObject::getApp();
61 Logger::log('Invoked: ' . $cmd . ': ' . $target_id, Logger::DEBUG);
67 $delivery_contacts_stmt = null;
70 $delivery_queue_count = 0;
72 if ($cmd == Delivery::MAIL) {
73 $message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $target_id]);
74 if (!DBA::isResult($message)) {
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)) {
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) {
91 $condition = ['uid' => $target_id, 'self' => false, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
92 $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'network', 'batch'], $condition);
95 $condition = ['id' => $target_id, 'visible' => true, 'moderated' => false];
96 $target_item = Item::selectFirst([], $condition);
98 if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
102 if (!empty($target_item['contact-uid'])) {
103 $uid = $target_item['contact-uid'];
104 } elseif (!empty($target_item['uid'])) {
105 $uid = $target_item['uid'];
107 Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
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)) {
118 $items = Item::inArray($items_stmt);
120 // avoid race condition with deleting entries
121 if ($items[0]['deleted']) {
122 foreach ($items as $item) {
123 $item['deleted'] = 1;
127 if ((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
128 Logger::log('Top level post');
133 $owner = User::getOwnerDataById($uid);
138 // Should the post be transmitted to Diaspora?
139 $diaspora_delivery = true;
141 // If this is a public conversation, notify the feed hub
142 $public_message = true;
145 $push_notify = false;
147 // Deliver directly to a forum, don't PuSH
148 $direct_forum_delivery = false;
151 $recipients_followup = [];
153 if (!empty($target_item) && !empty($items)) {
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);
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);
164 Logger::log('GUID: ' . $target_item["guid"] . ': Parent is ' . $parent['network'] . '. Thread parent is ' . $thr_parent['network'], Logger::DEBUG);
166 // This is IMPORTANT!!!!
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.
172 // if $parent['wall'] == 1 we will already have the parent message in our array
173 // and we will relay the whole lot.
175 $localhost = str_replace('www.','',$a->getHostName());
176 if (strpos($localhost,':')) {
177 $localhost = substr($localhost,0,strpos($localhost,':'));
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.
187 $relay_to_owner = false;
189 if (!$top_level && ($parent['wall'] == 0) && (stristr($target_item['uri'],$localhost))) {
190 $relay_to_owner = true;
194 if (($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
195 $relay_to_owner = true;
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.
202 if (!$target_item['origin']) {
203 $relay_to_owner = false;
205 if ($parent['origin']) {
206 $relay_to_owner = false;
209 // Special treatment for forum posts
210 if (Item::isForumPost($target_item, $owner)) {
211 $relay_to_owner = true;
212 $direct_forum_delivery = true;
215 // Avoid that comments in a forum thread are sent to OStatus
216 if (Item::isForumPost($parent, $owner)) {
217 $direct_forum_delivery = true;
220 if ($relay_to_owner) {
221 // local followup to remote post
223 $public_message = false; // not public
224 $recipients = [$parent['contact-id']];
225 $recipients_followup = [$parent['contact-id']];
227 Logger::log('Followup ' . $target_item['guid'] . ' to ' . $parent['contact-id'], Logger::DEBUG);
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))
235 if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
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'];
246 DBA::close($followup_contacts_stmt);
250 if ($direct_forum_delivery) {
251 $push_notify = false;
254 Logger::log('Notify ' . $target_item["guid"] .' via PuSH: ' . ($push_notify ? "Yes":"No"), Logger::DEBUG);
258 Logger::log('Distributing directly ' . $target_item["guid"], Logger::DEBUG);
260 // don't send deletions onward for other people's stuff
262 if ($target_item['deleted'] && !intval($target_item['wall'])) {
263 Logger::log('Ignoring delete notification for non-wall item');
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
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']));
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
282 if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== 'uplink')) {
283 Worker::add($a->queue['priority'], 'Notifier', 'uplink', $target_id);
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));
295 $url_recipients[] = substr($person,4);
301 if (count($url_recipients)) {
302 Logger::log('Deliver ' . $target_item["guid"] . ' to _recipients ' . json_encode($url_recipients));
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);
309 // If this is a public message and pubmail is set on the parent, include all your email contacts
311 function_exists('imap_open')
312 && !Config::get('system','imap_disabled')
314 && intval($target_item['pubmail'])
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'];
320 DBA::close($mail_contacts_stmt);
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;
329 Logger::log('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent['author-id']." - Owner: ".$thr_parent['owner-id'], Logger::DEBUG);
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"];
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"];
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);
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"];
359 // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
360 $networks = [Protocol::OSTATUS, Protocol::DFRN];
362 $networks = [Protocol::OSTATUS, Protocol::DFRN, Protocol::DIASPORA, Protocol::MAIL];
365 $public_message = false;
368 if (empty($delivery_contacts_stmt)) {
370 $recipients = $recipients_followup;
372 $condition = ['id' => $recipients, 'self' => false,
373 'blocked' => false, 'pending' => false, 'archive' => false];
374 if (!empty($networks)) {
375 $condition['network'] = $networks;
377 $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'network', 'batch'], $condition);
381 $batch_delivery = false;
383 if ($public_message && !in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION]) && !$followup) {
386 if ($diaspora_delivery) {
387 $batch_delivery = true;
389 $relay_list_stmt = DBA::p(
392 ANY_VALUE(`id`) AS `id`,
393 ANY_VALUE(`name`) AS `name`,
394 ANY_VALUE(`network`) AS `network`
408 $relay_list = DBA::toArray($relay_list_stmt);
410 // Fetch the participation list
411 // The function will ensure that there are no duplicates
412 $relay_list = Diaspora::participantsForThread($target_id, $relay_list);
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);
421 $condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
422 'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
424 $r2 = DBA::toArray(DBA::select('contact', ['id', 'url', 'name', 'network'], $condition));
426 $r = array_merge($r2, $relay_list);
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);
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);
440 $conversants[] = $rr['id'];
442 $delivery_queue_count++;
444 Logger::log('Public delivery of item ' . $target_item["guid"] . ' (' . $target_id . ') to ' . json_encode($rr), Logger::DEBUG);
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];
453 $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
455 Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$rr['id']);
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);
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);
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);
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);
486 $delivery_queue_count++;
488 Logger::log('Delivery of item ' . $target_id . ' to ' . json_encode($contact), Logger::DEBUG);
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];
496 $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
498 Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$contact['id']);
500 DBA::close($delivery_contacts_stmt);
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);
516 // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
518 Logger::log('Activating internal PuSH for item '.$target_id, Logger::DEBUG);
520 // Handling the pubsubhubbub requests
521 PushSubscriber::publishFeed($owner['uid'], $a->queue['priority']);
524 if (!empty($target_item)) {
525 Logger::log('Calling hooks for ' . $cmd . ' ' . $target_id, Logger::DEBUG);
527 if (in_array($cmd, [Delivery::POST, Delivery::COMMENT])) {
528 ItemDeliveryData::update($target_item['id'], ['queue_count' => $delivery_queue_count]);
531 Hook::fork($a->queue['priority'], 'notifier_normal', $target_item);
533 Hook::callAll('notifier_end', $target_item);
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.
544 * @param string $cmd Notifier command
545 * @param array $owner Sender of the post
546 * @param string $network Receiver network
548 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
549 * @throws \ImagickException
551 private static function isRemovalActivity($cmd, $owner, $network)
553 return ($cmd == Delivery::DELETION) && $owner['account_removed'] && in_array($network, [Protocol::ACTIVITYPUB, Protocol::DIASPORA]);
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
561 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
562 * @throws \ImagickException
564 private static function notifySelfRemoval($self_user_id, $priority, $created)
566 $owner = User::getOwnerDataById($self_user_id);
571 $contacts_stmt = DBA::select('contact', [], ['self' => false, 'uid' => $self_user_id]);
572 if (!DBA::isResult($contacts_stmt)) {
576 while($contact = DBA::fetch($contacts_stmt)) {
577 Contact::terminateFriendship($owner, $contact, true);
579 DBA::close($contacts_stmt);
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);
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
601 private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created, $owner)
605 $uid = $target_item['contact-uid'] ?: $target_item['uid'];
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);
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);
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);
628 // Fill the item cache
629 ActivityPub\Transmitter::createCachedActivityFromItem($target_item['id'], true);
631 foreach ($inboxes as $inbox) {
632 Logger::log('Deliver ' . $target_item['id'] .' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
634 Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
635 'APDelivery', $cmd, $target_item['id'], $inbox, $uid);
638 return count($inboxes);