3 * @file src/Worker/Notifier.php
5 namespace Friendica\Worker;
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;
28 * The notifier is typically called with:
30 * Worker::add(PRIORITY_HIGH, "Notifier", COMMAND, ITEM_ID);
32 * where COMMAND is one of the following:
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)
49 * and ITEM_ID is the id of the item in the database that needs to be sent to others.
54 public static function execute($cmd, $item_id)
56 $a = BaseObject::getApp();
58 Logger::log('Invoked: ' . $cmd . ': ' . $item_id, Logger::DEBUG);
65 $recipients_relocate = [];
67 if ($cmd == Delivery::MAIL) {
69 $message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $item_id]);
70 if (!DBA::isResult($message)) {
73 $uid = $message['uid'];
74 $recipients[] = $message['contact-id'];
75 } elseif ($cmd == Delivery::SUGGESTION) {
77 $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $item_id]);
78 if (!DBA::isResult($suggest)) {
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",
95 $r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($item_id));
99 foreach ($r as $contact) {
100 Contact::terminateFriendship($user, $contact, true);
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);
111 } elseif ($cmd == Delivery::RELOCATION) {
112 $normal_mode = false;
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);
119 $condition = ['id' => $item_id, 'visible' => true, 'moderated' => false];
120 $target_item = Item::selectFirst([], $condition);
122 if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
126 $parent_id = intval($target_item['parent']);
128 if (!empty($target_item['contact-uid'])) {
129 $uid = $target_item['contact-uid'];
130 } elseif (!empty($target_item['uid'])) {
131 $uid = $target_item['uid'];
133 Logger::log('Only public users for item ' . $item_id, Logger::DEBUG);
137 $updated = $target_item['edited'];
139 $condition = ['parent' => $parent_id, 'visible' => true, 'moderated' => false];
140 $params = ['order' => ['id']];
141 $ret = Item::select([], $condition, $params);
143 if (!DBA::isResult($ret)) {
147 $items = Item::inArray($ret);
149 // avoid race condition with deleting entries
150 if ($items[0]['deleted']) {
151 foreach ($items as $item) {
152 $item['deleted'] = 1;
156 if ((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
157 Logger::log('Top level post');
162 $owner = User::getOwnerDataById($uid);
167 $walltowall = ($top_level && ($owner['id'] != $items[0]['contact-id']) ? true : false);
169 // Should the post be transmitted to Diaspora?
170 $diaspora_delivery = true;
172 // If this is a public conversation, notify the feed hub
173 $public_message = true;
176 $push_notify = false;
178 // Deliver directly to a forum, don't PuSH
179 $direct_forum_delivery = false;
182 $recipients_followup = [];
184 if (!in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION, Delivery::RELOCATION])) {
187 self::activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent);
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);
193 Logger::log('GUID: ' . $target_item["guid"] . ': Parent is ' . $parent['network'] . '. Thread parent is ' . $thr_parent['network'], Logger::DEBUG);
195 // This is IMPORTANT!!!!
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.
201 // if $parent['wall'] == 1 we will already have the parent message in our array
202 // and we will relay the whole lot.
204 $localhost = str_replace('www.','',$a->getHostName());
205 if (strpos($localhost,':')) {
206 $localhost = substr($localhost,0,strpos($localhost,':'));
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.
216 $relay_to_owner = false;
218 if (!$top_level && ($parent['wall'] == 0) && (stristr($target_item['uri'],$localhost))) {
219 $relay_to_owner = true;
223 if (($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
224 $relay_to_owner = true;
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.
231 if (!$target_item['origin']) {
232 $relay_to_owner = false;
234 if ($parent['origin']) {
235 $relay_to_owner = false;
238 // Special treatment for forum posts
239 if (self::isForumPost($target_item, $owner)) {
240 $relay_to_owner = true;
241 $direct_forum_delivery = true;
244 // Avoid that comments in a forum thread are sent to OStatus
245 if (self::isForumPost($parent, $owner)) {
246 $direct_forum_delivery = true;
249 if ($relay_to_owner) {
250 // local followup to remote post
252 $public_message = false; // not public
253 $recipients = [$parent['contact-id']];
254 $recipients_followup = [$parent['contact-id']];
256 Logger::log('Followup ' . $target_item['guid'] . ' to ' . $parent['contact-id'], Logger::DEBUG);
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))
264 if (($thr_parent && ($thr_parent['network'] == Protocol::OSTATUS)) || ($parent['network'] == Protocol::OSTATUS)) {
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`",
272 DBA::escape(Protocol::DFRN)
274 if (DBA::isResult($r)) {
275 foreach ($r as $rr) {
276 $recipients_followup[] = $rr['id'];
282 if ($direct_forum_delivery) {
283 $push_notify = false;
286 Logger::log('Notify ' . $target_item["guid"] .' via PuSH: ' . ($push_notify ? "Yes":"No"), Logger::DEBUG);
290 Logger::log('Distributing directly ' . $target_item["guid"], Logger::DEBUG);
292 // don't send deletions onward for other people's stuff
294 if ($target_item['deleted'] && !intval($target_item['wall'])) {
295 Logger::log('Ignoring delete notification for non-wall item');
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
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']));
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
314 if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== 'uplink')) {
315 Worker::add($a->queue['priority'], 'Notifier', 'uplink', $item_id);
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));
327 $url_recipients[] = substr($person,4);
333 if (count($url_recipients)) {
334 Logger::log('Deliver ' . $target_item["guid"] . ' to _recipients ' . json_decode($url_recipients));
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);
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;
347 Logger::log('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent['author-id']." - Owner: ".$thr_parent['owner-id'], Logger::DEBUG);
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"];
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"];
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);
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"];
377 // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
378 $networks = [Protocol::OSTATUS, Protocol::DFRN];
380 $networks = [Protocol::OSTATUS, Protocol::DFRN, Protocol::DIASPORA, Protocol::MAIL];
383 $public_message = false;
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'",
393 DBA::escape(Protocol::MAIL)
395 if (DBA::isResult($r)) {
396 foreach ($r as $rr) {
397 $recipients[] = $rr['id'];
403 if (($cmd == Delivery::RELOCATION)) {
404 $contacts = $recipients_relocate;
407 $recipients = $recipients_followup;
409 $condition = ['id' => $recipients, 'self' => false,
410 'blocked' => false, 'pending' => false, 'archive' => false];
411 if (!empty($networks)) {
412 $condition['network'] = $networks;
414 $result = DBA::select('contact', ['id', 'url', 'network', 'batch'], $condition);
415 $contacts = DBA::toArray($result);
419 $batch_delivery = false;
421 if ($public_message && !in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION]) && !$followup) {
424 if ($diaspora_delivery) {
425 $batch_delivery = true;
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)
435 // Fetch the participation list
436 // The function will ensure that there are no duplicates
437 $r1 = Diaspora::participantsForThread($item_id, $r1);
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);
446 $condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
447 'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
449 $r2 = DBA::toArray(DBA::select('contact', ['id', 'name', 'network'], $condition));
451 $r = array_merge($r2, $r1);
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);
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];
464 $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
466 Worker::add($deliver_options, 'Delivery', $cmd, $item_id, (int)$rr['id']);
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);
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);
488 Logger::log('Delivery of item ' . $item_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, $item_id, (int)$contact['id']);
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) {
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
515 // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
517 Logger::log('Activating internal PuSH for item '.$item_id, Logger::DEBUG);
519 // Handling the pubsubhubbub requests
520 PushSubscriber::publishFeed($owner['uid'], $a->queue['priority']);
523 Logger::log('Calling hooks for ' . $cmd . ' ' . $item_id, Logger::DEBUG);
526 Hook::fork($a->queue['priority'], 'notifier_normal', $target_item);
529 Addon::callHooks('notifier_end',$target_item);
534 private static function activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent)
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);
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);
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);
556 // Fill the item cache
557 ActivityPub\Transmitter::createCachedActivityFromItem($item_id, true);
559 foreach ($inboxes as $inbox) {
560 Logger::log('Deliver ' . $item_id .' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
562 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
563 'APDelivery', $cmd, $item_id, $inbox, $uid);
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'])) {
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
582 // Is the post from a forum?
583 return ($contact['forum'] || $contact['prv']);