From 6e1483545e432355178c4dfabf9db601f3b856c0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 14 Feb 2021 14:24:48 +0000 Subject: [PATCH] Support for message delivering via uri-id --- mod/events.php | 4 ++-- mod/item.php | 2 +- mod/tagger.php | 4 +++- src/Core/System.php | 6 +++++ src/Model/Event.php | 27 +++++++++++---------- src/Model/Item.php | 9 +++---- src/Model/Post.php | 9 +------ src/Worker/APDelivery.php | 24 +++++++++++++------ src/Worker/Delivery.php | 16 +++++++++++-- src/Worker/Notifier.php | 50 ++++++++++++++++++++++++--------------- 10 files changed, 95 insertions(+), 56 deletions(-) diff --git a/mod/events.php b/mod/events.php index 291535df34..50ffd41a42 100644 --- a/mod/events.php +++ b/mod/events.php @@ -216,10 +216,10 @@ function events_post(App $a) exit(); } - $item_id = Event::store($datarray); + $uri_id = Event::store($datarray); if (!$cid) { - Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $item_id); + Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$uri_id, (int)$uid); } DI::baseUrl()->redirect('events'); diff --git a/mod/item.php b/mod/item.php index d180cb08ad..8c256cfabd 100644 --- a/mod/item.php +++ b/mod/item.php @@ -781,7 +781,7 @@ function item_post(App $a) { // When we are doing some forum posting via ! we have to start the notifier manually. // These kind of posts don't initiate the notifier call in the item class. if ($only_to_forum) { - Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => false], "Notifier", Delivery::POST, $post_id); + Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => false], "Notifier", Delivery::POST, (int)$datarray['uri-id'], (int)$datarray['uid']); } Logger::info('post_complete'); diff --git a/mod/tagger.php b/mod/tagger.php index 38575a2159..5e989c7cd1 100644 --- a/mod/tagger.php +++ b/mod/tagger.php @@ -176,7 +176,9 @@ EOT; Hook::callAll('post_local_end', $arr); - Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id); + $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]); + + Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post['uri-id'], $post['uid']); exit(); } diff --git a/src/Core/System.php b/src/Core/System.php index e84fcb5737..38db75d893 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -53,6 +53,12 @@ class System while ($func = array_pop($trace)) { if (!empty($func['class'])) { + if (in_array($previous['function'], ['insert', 'fetch', 'toArray', 'exists', 'count', 'selectFirst', 'selectToArray', + 'select', 'update', 'delete', 'selectFirstForUser', 'selectForUser']) + && (substr($previous['class'], 0, 15) === 'Friendica\Model')) { + continue; + } + // Don't show multiple calls from the Database classes to show the essential parts of the callstack $func['database'] = in_array($func['class'], ['Friendica\Database\DBA', 'Friendica\Database\Database']); if (!$previous['database'] || !$func['database']) { diff --git a/src/Model/Event.php b/src/Model/Event.php index c5a8ef7880..e6c6092832 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -173,32 +173,32 @@ class Event { $ev = []; - $match = ''; + $match = []; if (preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is", $text, $match)) { $ev['summary'] = $match[1]; } - $match = ''; + $match = []; if (preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is", $text, $match)) { $ev['desc'] = $match[1]; } - $match = ''; + $match = []; if (preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is", $text, $match)) { $ev['start'] = $match[1]; } - $match = ''; + $match = []; if (preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is", $text, $match)) { $ev['finish'] = $match[1]; } - $match = ''; + $match = []; if (preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is", $text, $match)) { $ev['location'] = $match[1]; } - $match = ''; + $match = []; if (preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is", $text, $match)) { $ev['adjust'] = $match[1]; } @@ -330,7 +330,7 @@ class Event DBA::update('event', $updated_fields, ['id' => $event['id'], 'uid' => $event['uid']]); - $item = Post::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]); + $item = Post::selectFirst(['id', 'uri-id'], ['event-id' => $event['id'], 'uid' => $event['uid']]); if (DBA::isResult($item)) { $object = '' . XML::escape(Activity\ObjectType::EVENT) . '' . XML::escape($event['uri']) . ''; $object .= '' . XML::escape(self::getBBCode($event)) . ''; @@ -339,9 +339,9 @@ class Event $fields = ['body' => self::getBBCode($event), 'object' => $object, 'edited' => $event['edited']]; Item::update($fields, ['id' => $item['id']]); - $item_id = $item['id']; + $uriid = $item['uri-id']; } else { - $item_id = 0; + $uriid = 0; } Hook::callAll('event_updated', $event['id']); @@ -349,7 +349,7 @@ class Event // New event. Store it. DBA::insert('event', $event); - $item_id = 0; + $uriid = 0; // Don't create an item for birthday events if ($event['type'] == 'event') { @@ -360,6 +360,7 @@ class Event $item_arr['uid'] = $event['uid']; $item_arr['contact-id'] = $event['cid']; $item_arr['uri'] = $event['uri']; + $item_arr['uri-id'] = ItemURI::getIdByURI($event['uri']); $item_arr['guid'] = $event['guid']; $item_arr['plink'] = $arr['plink'] ?? ''; $item_arr['post-type'] = Item::PT_EVENT; @@ -392,13 +393,15 @@ class Event $item_arr['object'] .= '' . XML::escape(self::getBBCode($event)) . ''; $item_arr['object'] .= '' . "\n"; - $item_id = Item::insert($item_arr); + if (Item::insert($item_arr)) { + $uriid = $item_arr['uri-id']; + } } Hook::callAll("event_created", $event['id']); } - return $item_id; + return $uriid; } /** diff --git a/src/Model/Item.php b/src/Model/Item.php index 814696a49d..28e0e25f4c 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -212,7 +212,8 @@ class Item DBA::close($items); foreach ($notify_items as $notify_item) { - Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $notify_item); + $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $notify_item]); + Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$post['uri-id'], (int)$post['uid']); } return $rows; @@ -354,7 +355,7 @@ class Item // send the notification upstream/downstream if ($priority) { - Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id'])); + Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, (int)$item['uri-id'], (int)$item['uid']); } } elseif ($item['uid'] != 0) { Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]); @@ -1142,7 +1143,7 @@ class Item } if ($transmit) { - Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, $current_post); + Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$item['uri-id'], (int)$item['uid']); } return $current_post; @@ -1866,7 +1867,7 @@ class Item 'owner-id' => $owner_id, 'private' => $private, 'psid' => $psid]; self::update($fields, ['id' => $item_id]); - Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id); + Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, (int)$item['uri-id'], (int)$item['uid']); self::performActivity($item_id, 'announce', $uid); diff --git a/src/Model/Post.php b/src/Model/Post.php index 4f06babf9a..fe2171d294 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -246,13 +246,7 @@ class Post */ public static function select(array $selected = [], array $condition = [], $params = []) { - $timestamp = microtime(true); - $data = self::selectView('post-view', $selected, $condition, $params); - - $duration = microtime(true) - $timestamp;; - if ($duration > 0.1) - Logger::info('Blubb', ['duration' => $duration, 'selected' => $selected, 'condition' => $condition, 'params' => $params, 'callstack' => System::callstack(20)]); - return $data; + return self::selectView('post-view', $selected, $condition, $params); } /** @@ -335,7 +329,6 @@ class Post */ public static function selectForUser($uid, array $selected = [], array $condition = [], $params = []) { - //Logger::info('Blubb', ['uid' => $uid, 'selected' => $selected, 'condition' => $condition, 'params' => $params]); return self::selectViewForUser('post-view', $uid, $selected, $condition, $params); } diff --git a/src/Worker/APDelivery.php b/src/Worker/APDelivery.php index 9f0af133e6..099fdc397a 100644 --- a/src/Worker/APDelivery.php +++ b/src/Worker/APDelivery.php @@ -25,7 +25,6 @@ use Friendica\Core\Logger; use Friendica\Core\Worker; use Friendica\Model\Contact; use Friendica\Model\GServer; -use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Protocol\ActivityPub; use Friendica\Util\HTTPSignature; @@ -40,10 +39,11 @@ class APDelivery * @param string $inbox The URL of the recipient profile * @param integer $uid The ID of the user who triggered this delivery * @param array $receivers The contact IDs related to the inbox URL for contact archival housekeeping + * @param int $uri_id URI-ID of item to be transmitted * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = []) + public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0) { if (ActivityPub\Transmitter::archivedInbox($inbox)) { Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]); @@ -54,7 +54,19 @@ class APDelivery return; } - Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]); + if (empty($uri_id) && !empty($item_id)) { + $item = Post::selectFirst(['uri-id'], ['id' => $item_id]); + if (!empty($item['uri-id'])) { + $uri_id = $item['uri-id']; + } + } elseif (!empty($uri_id) && !empty($item_id)) { + $item = Post::selectFirst(['id'], ['uri-id' => $uri_id, 'uid' => $uid]); + if (!empty($item['uri-id'])) { + $item_id = $item['id']; + } + } + + Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]); $success = true; @@ -81,8 +93,6 @@ class APDelivery } // This should never fail and is temporariy (until the move to the "post" structure) - $item = Post::selectFirst(['uri-id'], ['id' => $item_id]); - $uriid = $item['uri-id'] ?? 0; $gsid = null; foreach ($receivers as $receiver) { @@ -105,9 +115,9 @@ class APDelivery } if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) { - Post\DeliveryData::incrementQueueFailed($uriid); + Post\DeliveryData::incrementQueueFailed($uri_id); } elseif ($success && in_array($cmd, [Delivery::POST])) { - Post\DeliveryData::incrementQueueDone($uriid, Post\DeliveryData::ACTIVITYPUB); + Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB); } } } diff --git a/src/Worker/Delivery.php b/src/Worker/Delivery.php index d5a3e8f5cb..faba263e92 100644 --- a/src/Worker/Delivery.php +++ b/src/Worker/Delivery.php @@ -36,6 +36,7 @@ use Friendica\Core\Worker; use Friendica\Model\Conversation; use Friendica\Model\FContact; use Friendica\Model\Item; +use Friendica\Model\Post; use Friendica\Protocol\Relay; class Delivery @@ -50,9 +51,20 @@ class Delivery const REMOVAL = 'removeme'; const PROFILEUPDATE = 'profileupdate'; - public static function execute($cmd, $target_id, $contact_id) + public static function execute(string $cmd, int $post_uriid, int $contact_id, int $sender_uid = 0) { - Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id, 'contact' => $contact_id]); + Logger::info('Invoked', ['cmd' => $cmd, 'target' => $post_uriid, 'sender_uid' => $sender_uid, 'contact' => $contact_id]); + + if (!empty($sender_uid)) { + $post = Post::selectFirst(['id'], ['uri-id' => $post_uriid, 'uid' => $sender_uid]); + if (!DBA::isResult($post)) { + Logger::warning('Post not found', ['uri-id' => $post_uriid, 'uid' => $sender_uid]); + return; + } + $target_id = $post['id']; + } else { + $target_id = $post_uriid; + } $top_level = false; $followup = false; diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 312dcc0f98..836e4c6803 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -53,11 +53,22 @@ use Friendica\Protocol\Salmon; class Notifier { - public static function execute(string $cmd, int $target_id) + public static function execute(string $cmd, int $post_uriid, int $sender_uid = 0) { $a = DI::app(); - Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id]); + Logger::info('Invoked', ['cmd' => $cmd, 'target' => $post_uriid, 'sender_uid' => $sender_uid]); + + if (!empty($sender_uid)) { + $post = Post::selectFirst(['id'], ['uri-id' => $post_uriid, 'uid' => $sender_uid]); + if (!DBA::isResult($post)) { + Logger::warning('Post not found', ['uri-id' => $post_uriid, 'uid' => $sender_uid]); + return; + } + $target_id = $post['id']; + } else { + $target_id = $post_uriid; + } $top_level = false; $recipients = []; @@ -85,7 +96,7 @@ class Notifier $ap_contacts = array_merge($ap_contacts, $receivers); Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'target' => $target_id, 'inbox' => $inbox]); Worker::add(['priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true], - 'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers); + 'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers, $post_uriid); } } elseif ($cmd == Delivery::SUGGESTION) { $suggest = DI::fsuggest()->getById($target_id); @@ -305,8 +316,8 @@ class Notifier /// @todo Possibly we should not uplink when the author is the forum itself? if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK) - && ($target_item['verb'] != Activity::ANNOUNCE)) { - Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $target_id); + && ($target_item['verb'] != Activity::ANNOUNCE)) { + Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $post_uriid, $sender_uid); } foreach ($items as $item) { @@ -452,13 +463,13 @@ class Notifier $conversants = array_merge($contacts, $relay_list); - $delivery_queue_count += self::delivery($cmd, $target_id, $target_item, $thr_parent, $owner, $batch_delivery, true, $conversants, $ap_contacts, []); + $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, true, $conversants, $ap_contacts, []); $push_notify = true; } $contacts = DBA::toArray($delivery_contacts_stmt); - $delivery_queue_count += self::delivery($cmd, $target_id, $target_item, $thr_parent, $owner, $batch_delivery, false, $contacts, $ap_contacts, $conversants); + $delivery_queue_count += self::delivery($cmd, $post_uriid, $sender_uid, $target_item, $thr_parent, $owner, $batch_delivery, false, $contacts, $ap_contacts, $conversants); $delivery_queue_count += self::deliverOStatus($target_id, $target_item, $owner, $url_recipients, $public_message, $push_notify); @@ -487,7 +498,8 @@ class Notifier * Deliver the message to the contacts * * @param string $cmd - * @param int $target_id + * @param int $post_uriid + * @param int $sender_uid * @param array $target_item * @param array $thr_parent * @param array $owner @@ -499,7 +511,7 @@ class Notifier * @throws InternalServerErrorException * @throws Exception */ - private static function delivery(string $cmd, int $target_id, array $target_item, array $thr_parent, array $owner, bool $batch_delivery, bool $in_batch, array $contacts, array $ap_contacts, array $conversants = []) + private static function delivery(string $cmd, int $post_uriid, int $sender_uid, array $target_item, array $thr_parent, array $owner, bool $batch_delivery, bool $in_batch, array $contacts, array $ap_contacts, array $conversants = []) { $a = DI::app(); $delivery_queue_count = 0; @@ -511,38 +523,38 @@ class Notifier } if (in_array($contact['id'], $ap_contacts)) { - Logger::info('Contact is already delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]); + Logger::info('Contact is already delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]); continue; } if (!empty($contact['id']) && Contact::isArchived($contact['id'])) { - Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]); + Logger::info('Contact is archived, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]); continue; } if (self::isRemovalActivity($cmd, $owner, $contact['network'])) { - Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]); + Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact['url']]); continue; } if (self::skipActivityPubForDiaspora($contact, $target_item, $thr_parent)) { - Logger::info('Contact is from Diaspora, but the replied author is from ActivityPub, so skip delivery via Diaspora', ['id' => $target_id, 'url' => $contact['url']]); + Logger::info('Contact is from Diaspora, but the replied author is from ActivityPub, so skip delivery via Diaspora', ['id' => $post_uriid, 'uid' => $sender_uid, 'url' => $contact['url']]); continue; } // Don't deliver to Diaspora if it already had been done as batch delivery if (!$in_batch && $batch_delivery && ($contact['network'] == Protocol::DIASPORA)) { - Logger::info('Diaspora contact is already delivered via batch', ['id' => $target_id, 'contact' => $contact]); + Logger::info('Diaspora contact is already delivered via batch', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]); continue; } // Don't deliver to folks who have already been delivered to if (in_array($contact['id'], $conversants)) { - Logger::info('Already delivery', ['id' => $target_id, 'contact' => $contact]); + Logger::info('Already delivery', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]); continue; } - Logger::info('Delivery', ['batch' => $in_batch, 'target' => $target_id, 'guid' => $target_item['guid'] ?? '', 'to' => $contact]); + Logger::info('Delivery', ['batch' => $in_batch, 'target' => $post_uriid, 'uid' => $sender_uid, 'guid' => $target_item['guid'] ?? '', 'to' => $contact]); // 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. @@ -554,7 +566,7 @@ class Notifier $deliver_options = ['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true]; } - if (Worker::add($deliver_options, 'Delivery', $cmd, $target_id, (int)$contact['id'])) { + if (Worker::add($deliver_options, 'Delivery', $cmd, $post_uriid, (int)$contact['id'], $sender_uid)) { $delivery_queue_count++; } } @@ -767,7 +779,7 @@ class Notifier Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]); if (Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true], - 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, $receivers)) { + 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, $receivers, $target_item['uri-id'])) { $delivery_queue_count++; } } @@ -776,7 +788,7 @@ class Notifier foreach ($relay_inboxes as $inbox) { Logger::info('Delivery to relay servers via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]); - if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid)) { + if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, [], $target_item['uri-id'])) { $delivery_queue_count++; } }