1
1
Fork 0

Refactor Worker/Notifier part 1

- Rename variables
- Remove orphan, duplicate and one-use intermediary variables
- Initialize potentially missing variables
- Replace obsolete Addon::callHooks call
- Simplify Notifier::activityPubDelivery() signature
This commit is contained in:
Hypolite Petovan 2018-12-04 23:49:48 -05:00
parent 061ad380f6
commit 507c215c76

View file

@ -5,7 +5,6 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Addon;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
@ -51,11 +50,11 @@ use Friendica\Protocol\Salmon;
class Notifier class Notifier
{ {
public static function execute($cmd, $item_id) public static function execute($cmd, $target_id)
{ {
$a = BaseObject::getApp(); $a = BaseObject::getApp();
Logger::log('Invoked: ' . $cmd . ': ' . $item_id, Logger::DEBUG); Logger::log('Invoked: ' . $cmd . ': ' . $target_id, Logger::DEBUG);
$top_level = false; $top_level = false;
$recipients = []; $recipients = [];
@ -63,10 +62,12 @@ class Notifier
$normal_mode = true; $normal_mode = true;
$recipients_relocate = []; $recipients_relocate = [];
$target_item = [];
$items = [];
if ($cmd == Delivery::MAIL) { if ($cmd == Delivery::MAIL) {
$normal_mode = false; $normal_mode = false;
$message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $item_id]); $message = DBA::selectFirst('mail', ['uid', 'contact-id'], ['id' => $target_id]);
if (!DBA::isResult($message)) { if (!DBA::isResult($message)) {
return; return;
} }
@ -74,7 +75,7 @@ class Notifier
$recipients[] = $message['contact-id']; $recipients[] = $message['contact-id'];
} elseif ($cmd == Delivery::SUGGESTION) { } elseif ($cmd == Delivery::SUGGESTION) {
$normal_mode = false; $normal_mode = false;
$suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $item_id]); $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]);
if (!DBA::isResult($suggest)) { if (!DBA::isResult($suggest)) {
return; return;
} }
@ -86,13 +87,13 @@ class Notifier
`user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`, `user`.`guid` `user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`, `user`.`guid`
FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
WHERE `contact`.`uid` = %d AND `contact`.`self` LIMIT 1", WHERE `contact`.`uid` = %d AND `contact`.`self` LIMIT 1",
intval($item_id)); intval($target_id));
if (!$r) { if (!$r) {
return; return;
} }
$user = $r[0]; $user = $r[0];
$r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($item_id)); $r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($target_id));
if (!$r) { if (!$r) {
return; return;
} }
@ -102,49 +103,44 @@ class Notifier
$inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser(0); $inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser(0);
foreach ($inboxes as $inbox) { foreach ($inboxes as $inbox) {
Logger::log('Account removal for user ' . $item_id . ' to ' . $inbox .' via ActivityPub', Logger::DEBUG); Logger::log('Account removal for user ' . $target_id . ' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
Worker::add(['priority' => PRIORITY_NEGLIGIBLE, 'created' => $a->queue['created'], 'dont_fork' => true], Worker::add(['priority' => PRIORITY_NEGLIGIBLE, 'created' => $a->queue['created'], 'dont_fork' => true],
'APDelivery', Delivery::REMOVAL, '', $inbox, $item_id); 'APDelivery', Delivery::REMOVAL, '', $inbox, $target_id);
} }
return; return;
} elseif ($cmd == Delivery::RELOCATION) { } elseif ($cmd == Delivery::RELOCATION) {
$normal_mode = false; $normal_mode = false;
$uid = $item_id; $uid = $target_id;
$recipients_relocate = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND `network` IN ('%s', '%s')", $recipients_relocate = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND `network` IN ('%s', '%s')",
intval($uid), Protocol::DFRN, Protocol::DIASPORA); intval($uid), Protocol::DFRN, Protocol::DIASPORA);
} else { } else {
// find ancestors // find ancestors
$condition = ['id' => $item_id, 'visible' => true, 'moderated' => false]; $condition = ['id' => $target_id, 'visible' => true, 'moderated' => false];
$target_item = Item::selectFirst([], $condition); $target_item = Item::selectFirst([], $condition);
if (!DBA::isResult($target_item) || !intval($target_item['parent'])) { if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
return; return;
} }
$parent_id = intval($target_item['parent']);
if (!empty($target_item['contact-uid'])) { if (!empty($target_item['contact-uid'])) {
$uid = $target_item['contact-uid']; $uid = $target_item['contact-uid'];
} elseif (!empty($target_item['uid'])) { } elseif (!empty($target_item['uid'])) {
$uid = $target_item['uid']; $uid = $target_item['uid'];
} else { } else {
Logger::log('Only public users for item ' . $item_id, Logger::DEBUG); Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
return; return;
} }
$updated = $target_item['edited']; $condition = ['parent' => $target_item['parent'], 'visible' => true, 'moderated' => false];
$condition = ['parent' => $parent_id, 'visible' => true, 'moderated' => false];
$params = ['order' => ['id']]; $params = ['order' => ['id']];
$ret = Item::select([], $condition, $params); $items_stmt = Item::select([], $condition, $params);
if (!DBA::isResult($items_stmt)) {
if (!DBA::isResult($ret)) {
return; return;
} }
$items = Item::inArray($ret); $items = Item::inArray($items_stmt);
// avoid race condition with deleting entries // avoid race condition with deleting entries
if ($items[0]['deleted']) { if ($items[0]['deleted']) {
@ -164,8 +160,6 @@ class Notifier
return; return;
} }
$walltowall = ($top_level && ($owner['id'] != $items[0]['contact-id']) ? true : false);
// Should the post be transmitted to Diaspora? // Should the post be transmitted to Diaspora?
$diaspora_delivery = true; $diaspora_delivery = true;
@ -181,10 +175,10 @@ class Notifier
$followup = false; $followup = false;
$recipients_followup = []; $recipients_followup = [];
if (!in_array($cmd, [Delivery::MAIL, Delivery::SUGGESTION, Delivery::RELOCATION])) { if (!empty($target_item) && !empty($items)) {
$parent = $items[0]; $parent = $items[0];
self::activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent); self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], $a->query_string['created']);
$fields = ['network', 'author-id', 'owner-id']; $fields = ['network', 'author-id', 'owner-id'];
$condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]]; $condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
@ -312,7 +306,7 @@ class Notifier
// a delivery fork. private groups (forum_mode == 2) do not uplink // a delivery fork. private groups (forum_mode == 2) do not uplink
if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== 'uplink')) { if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== 'uplink')) {
Worker::add($a->queue['priority'], 'Notifier', 'uplink', $item_id); Worker::add($a->queue['priority'], 'Notifier', 'uplink', $target_id);
} }
foreach ($items as $item) { foreach ($items as $item) {
@ -331,7 +325,7 @@ class Notifier
} }
if (count($url_recipients)) { if (count($url_recipients)) {
Logger::log('Deliver ' . $target_item["guid"] . ' to _recipients ' . json_decode($url_recipients)); Logger::log('Deliver ' . $target_item["guid"] . ' to _recipients ' . json_encode($url_recipients));
} }
$recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups)); $recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups));
@ -400,7 +394,7 @@ class Notifier
} }
} }
if (($cmd == Delivery::RELOCATION)) { if ($cmd == Delivery::RELOCATION) {
$contacts = $recipients_relocate; $contacts = $recipients_relocate;
} else { } else {
if ($followup) { if ($followup) {
@ -434,12 +428,12 @@ class Notifier
// Fetch the participation list // Fetch the participation list
// The function will ensure that there are no duplicates // The function will ensure that there are no duplicates
$r1 = Diaspora::participantsForThread($item_id, $r1); $r1 = Diaspora::participantsForThread($target_id, $r1);
// Add the relay to the list, avoid duplicates. // Add the relay to the list, avoid duplicates.
// Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly. // Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly.
if (!$followup && !self::isForumPost($target_item, $owner)) { if (!$followup && !self::isForumPost($target_item, $owner)) {
$r1 = Diaspora::relayList($item_id, $r1); $r1 = Diaspora::relayList($target_id, $r1);
} }
} }
@ -453,7 +447,7 @@ class Notifier
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
foreach ($r as $rr) { foreach ($r as $rr) {
$conversants[] = $rr['id']; $conversants[] = $rr['id'];
Logger::log('Public delivery of item ' . $target_item["guid"] . ' (' . $item_id . ') to ' . json_encode($rr), Logger::DEBUG); Logger::log('Public delivery of item ' . $target_item["guid"] . ' (' . $target_id . ') to ' . json_encode($rr), Logger::DEBUG);
// Ensure that posts with our own protocol arrives before Diaspora posts arrive. // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
// Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first. // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
@ -463,7 +457,7 @@ class Notifier
} else { } else {
$deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true]; $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
} }
Worker::add($deliver_options, 'Delivery', $cmd, $item_id, (int)$rr['id']); Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$rr['id']);
} }
} }
@ -475,17 +469,17 @@ class Notifier
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
// Don't deliver to Diaspora if it already had been done as batch delivery // Don't deliver to Diaspora if it already had been done as batch delivery
if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) { if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) {
Logger::log('Already delivered id ' . $item_id . ' via batch to ' . json_encode($contact), Logger::DEBUG); Logger::log('Already delivered id ' . $target_id . ' via batch to ' . json_encode($contact), Logger::DEBUG);
continue; continue;
} }
// Don't deliver to folks who have already been delivered to // Don't deliver to folks who have already been delivered to
if (in_array($contact['id'], $conversants)) { if (in_array($contact['id'], $conversants)) {
Logger::log('Already delivered id ' . $item_id. ' to ' . json_encode($contact), Logger::DEBUG); Logger::log('Already delivered id ' . $target_id. ' to ' . json_encode($contact), Logger::DEBUG);
continue; continue;
} }
Logger::log('Delivery of item ' . $item_id . ' to ' . json_encode($contact), Logger::DEBUG); Logger::log('Delivery of item ' . $target_id . ' to ' . json_encode($contact), Logger::DEBUG);
// Ensure that posts with our own protocol arrives before Diaspora posts arrive. // Ensure that posts with our own protocol arrives before Diaspora posts arrive.
// Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first. // Situation is that sometimes Friendica servers receive Friendica posts over the Diaspora protocol first.
@ -495,7 +489,7 @@ class Notifier
} else { } else {
$deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true]; $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true];
} }
Worker::add($deliver_options, 'Delivery', $cmd, $item_id, (int)$contact['id']); Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$contact['id']);
} }
} }
@ -505,66 +499,74 @@ class Notifier
$slap = OStatus::salmon($target_item, $owner); $slap = OStatus::salmon($target_item, $owner);
foreach ($url_recipients as $url) { foreach ($url_recipients as $url) {
if ($url) { if ($url) {
Logger::log('Salmon delivery of item ' . $item_id . ' to ' . $url); Logger::log('Salmon delivery of item ' . $target_id . ' to ' . $url);
$deliver_status = Salmon::slapper($owner, $url, $slap);
/// @TODO Redeliver/queue these items on failure, though there is no contact record /// @TODO Redeliver/queue these items on failure, though there is no contact record
Salmon::slapper($owner, $url, $slap);
} }
} }
} }
// Notify PuSH subscribers (Used for OStatus distribution of regular posts) // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
if ($push_notify) { if ($push_notify) {
Logger::log('Activating internal PuSH for item '.$item_id, Logger::DEBUG); Logger::log('Activating internal PuSH for item '.$target_id, Logger::DEBUG);
// Handling the pubsubhubbub requests // Handling the pubsubhubbub requests
PushSubscriber::publishFeed($owner['uid'], $a->queue['priority']); PushSubscriber::publishFeed($owner['uid'], $a->queue['priority']);
} }
Logger::log('Calling hooks for ' . $cmd . ' ' . $item_id, Logger::DEBUG); Logger::log('Calling hooks for ' . $cmd . ' ' . $target_id, Logger::DEBUG);
if ($normal_mode) { if ($normal_mode) {
Hook::fork($a->queue['priority'], 'notifier_normal', $target_item); Hook::fork($a->queue['priority'], 'notifier_normal', $target_item);
} }
Addon::callHooks('notifier_end',$target_item); Hook::callAll('notifier_end',$target_item);
return; return;
} }
private static function activityPubDelivery($a, $cmd, $item_id, $uid, $target_item, $parent) /**
* @param string $cmd
* @param array $target_item
* @param array $parent
* @param int $priority The priority the Notifier queue item was created with
* @param string $created The date the Notifier queue item was created on
*/
private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created)
{ {
$inboxes = []; $inboxes = [];
if ($target_item['origin']) { if ($target_item['origin']) {
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid); $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $target_item['contact-uid']);
Logger::log('Origin item ' . $item_id . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG); Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
} elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) { } elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) {
Logger::log('Remote item ' . $item_id . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', Logger::DEBUG); Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', Logger::DEBUG);
return; return;
} elseif ($parent['origin']) { } elseif ($parent['origin']) {
// Remote items are transmitted via the personal inboxes. // Remote items are transmitted via the personal inboxes.
// Doing so ensures that the dedicated receiver will get the message. // Doing so ensures that the dedicated receiver will get the message.
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true, $item_id); $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $target_item['contact-uid'], true, $target_item['id']);
Logger::log('Remote item ' . $item_id . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG); Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
} }
if (empty($inboxes)) { if (empty($inboxes)) {
Logger::log('No inboxes found for item ' . $item_id . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', Logger::DEBUG); Logger::log('No inboxes found for item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', Logger::DEBUG);
return; return;
} }
// Fill the item cache // Fill the item cache
ActivityPub\Transmitter::createCachedActivityFromItem($item_id, true); ActivityPub\Transmitter::createCachedActivityFromItem($target_item['id'], true);
foreach ($inboxes as $inbox) { foreach ($inboxes as $inbox) {
Logger::log('Deliver ' . $item_id .' to ' . $inbox .' via ActivityPub', Logger::DEBUG); Logger::log('Deliver ' . $target_item['id'] .' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true], Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
'APDelivery', $cmd, $item_id, $inbox, $uid); 'APDelivery', $cmd, $target_item['id'], $inbox, $target_item['contact-uid']);
} }
} }
private static function isForumPost($item, $owner) { private static function isForumPost(array $item, array $owner)
{
if (($item['author-id'] == $item['owner-id']) || if (($item['author-id'] == $item['owner-id']) ||
($owner['id'] == $item['contact-id']) || ($owner['id'] == $item['contact-id']) ||
($item['uri'] != $item['parent-uri'])) { ($item['uri'] != $item['parent-uri'])) {