use Friendica\Model\Conversation;
use Friendica\Model\Group;
use Friendica\Model\Item;
-use Friendica\Model\ItemDeliveryData;
+use Friendica\Model\Post;
use Friendica\Model\PushSubscriber;
+use Friendica\Model\Tag;
use Friendica\Model\User;
-use Friendica\Network\Probe;
+use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
use Friendica\Protocol\Salmon;
-require_once 'include/items.php';
-
/*
* The notifier is typically called with:
*
if (!empty($target_item) && !empty($items)) {
$parent = $items[0];
- $fields = ['network', 'author-id', 'author-link', 'owner-id'];
+ $fields = ['network', 'author-id', 'author-link', 'author-network', 'owner-id'];
$condition = ['uri' => $target_item["thr-parent"], 'uid' => $target_item["uid"]];
$thr_parent = Item::selectFirst($fields, $condition);
if (empty($thr_parent)) {
// Only deliver threaded replies (comment to a comment) to Diaspora
// when the original comment author does support the Diaspora protocol.
- if ($target_item['parent-uri'] != $target_item['thr-parent']) {
+ if ($thr_parent['author-link'] && $target_item['parent-uri'] != $target_item['thr-parent']) {
$diaspora_delivery = Diaspora::isSupportedByContactUrl($thr_parent['author-link']);
Logger::info('Threaded comment', ['diaspora_delivery' => (int)$diaspora_delivery]);
}
// if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
// a delivery fork. private groups (forum_mode == 2) do not uplink
+ /// @todo Possibly we should not uplink when the author is the forum itself?
- if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK)) {
+ 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);
}
// Send a salmon to the parent author
$probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['author-id']]);
if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
- Logger::log('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
+ Logger::notice('Notify parent author', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon to the parent owner
$probed_contact = DBA::selectFirst('contact', ['url', 'notify'], ['id' => $thr_parent['owner-id']]);
if (DBA::isResult($probed_contact) && !empty($probed_contact["notify"])) {
- Logger::log('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
+ Logger::notice('Notify parent owner', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon notification to every person we mentioned in the post
- $arr = explode(',',$target_item['tag']);
- foreach ($arr as $x) {
- //Logger::log('Checking tag '.$x, Logger::DEBUG);
- $matches = null;
- if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
- $probed_contact = Probe::uri($matches[1]);
- if ($probed_contact["notify"] != "") {
- Logger::log('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
- $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
- }
+ foreach (Tag::getByURIId($target_item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]) as $tag) {
+ $probed_contact = Contact::getByURL($tag['url']);
+ if (!empty($probed_contact['notify'])) {
+ Logger::notice('Notify mentioned user', ['url' => $probed_contact["url"], 'notify' => $probed_contact["notify"]]);
+ $url_recipients[$probed_contact['notify']] = $probed_contact['notify'];
}
}
if ($followup) {
$recipients = $recipients_followup;
}
- $condition = ['id' => $recipients, 'self' => false,
+ $condition = ['id' => $recipients, 'self' => false, 'uid' => [0, $uid],
'blocked' => false, 'pending' => false, 'archive' => false];
if (!empty($networks)) {
$condition['network'] = $networks;
// Fetch the participation list
// The function will ensure that there are no duplicates
- $relay_list = Diaspora::participantsForThread($target_id, $relay_list);
+ $relay_list = Diaspora::participantsForThread($target_item, $relay_list);
// 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.
- if (!$followup && !Item::isForumPost($target_item, $owner)) {
+ if (!$followup && !Item::isForumPost($target_item, $owner) && !self::isForumPost($target_item)) {
$relay_list = Diaspora::relayList($target_id, $relay_list);
}
}
}
if (!empty($rr['addr']) && ($rr['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $rr['addr']])) {
- Logger::info('Contact is AP omly', ['target' => $target_id, 'contact' => $rr['url']]);
+ Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $rr['url']]);
continue;
}
if (!empty($rr['id']) && Contact::isArchived($rr['id'])) {
- Logger::info('Contact is archived', ['target' => $target_id, 'contact' => $rr['url']]);
+ Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $rr['url']]);
continue;
}
if (self::isRemovalActivity($cmd, $owner, $rr['network'])) {
- Logger::log('Skipping dropping for ' . $rr['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+ Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $rr['url']]);
continue;
}
- if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $cmd)) {
+ if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $owner, $cmd)) {
Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['id' => $target_id, 'url' => $rr['url']]);
continue;
}
+ if (self::skipActivityPubForDiaspora($rr, $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' => $rr['url']]);
+ continue;
+ }
+
$conversants[] = $rr['id'];
Logger::info('Public delivery', ['target' => $target_id, 'guid' => $target_item["guid"], 'to' => $rr]);
}
if (!empty($contact['addr']) && ($contact['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $contact['addr']])) {
- Logger::info('Contact is AP omly', ['target' => $target_id, 'contact' => $contact['url']]);
+ Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]);
continue;
}
if (!empty($contact['id']) && Contact::isArchived($contact['id'])) {
- Logger::info('Contact is archived', ['target' => $target_id, 'contact' => $contact['url']]);
+ Logger::info('Contact is archived, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
continue;
}
if (self::isRemovalActivity($cmd, $owner, $contact['network'])) {
- Logger::log('Skipping dropping for ' . $contact['url'] . ' since the network supports account removal commands.', Logger::DEBUG);
+ Logger::info('Contact does no supports account removal commands, so skip delivery', ['target' => $target_id, 'contact' => $contact['url']]);
continue;
}
- if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $cmd)) {
+ if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $owner, $cmd)) {
Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'url' => $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']]);
+ continue;
+ }
+
// Don't deliver to Diaspora if it already had been done as batch delivery
if (($contact['network'] == Protocol::DIASPORA) && $batch_delivery) {
Logger::log('Already delivered id ' . $target_id . ' via batch to ' . json_encode($contact), Logger::DEBUG);
/// @TODO Redeliver/queue these items on failure, though there is no contact record
$delivery_queue_count++;
Salmon::slapper($owner, $url, $slap);
- ItemDeliveryData::incrementQueueDone($target_id, ItemDeliveryData::OSTATUS);
+ Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Post\DeliveryData::OSTATUS);
}
}
// Workaround for pure connector posts
if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
if ($delivery_queue_count == 0) {
- ItemDeliveryData::incrementQueueDone($target_item['id']);
+ Post\DeliveryData::incrementQueueDone($target_item['uri-id']);
$delivery_queue_count = 1;
}
- ItemDeliveryData::incrementQueueCount($target_item['id'], $delivery_queue_count);
+ Post\DeliveryData::incrementQueueCount($target_item['uri-id'], $delivery_queue_count);
}
}
return;
}
+ /**
+ * Checks if the current delivery shouldn't be transported to Diaspora.
+ * This is done for posts from AP authors or posts that are comments to AP authors.
+ *
+ * @param array $contact Receiver of the post
+ * @param array $item The post
+ * @param array $thr_parent The thread parent
+ * @return bool
+ */
+ private static function skipActivityPubForDiaspora(array $contact, array $item, array $thr_parent)
+ {
+ // No skipping needs to be done when delivery isn't done to Diaspora
+ if ($contact['network'] != Protocol::DIASPORA) {
+ return false;
+ }
+
+ // Skip the delivery to Diaspora if the item is from an ActivityPub author
+ if (!empty($item['author-network']) && ($item['author-network'] == Protocol::ACTIVITYPUB)) {
+ return true;
+ }
+
+ // Skip the delivery to Diaspora if the thread parent is from an ActivityPub author
+ if (!empty($thr_parent['author-network']) && ($thr_parent['author-network'] == Protocol::ACTIVITYPUB)) {
+ return true;
+ }
+
+ return false;
+ }
+
/**
* Checks if the current delivery process needs to be transported via DFRN.
*
* @param array $item The post
* @param array $parent The parent
* @param array $thr_parent The thread parent
+ * @param array $owner Owner array
* @param string $cmd Notifier command
* @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- private static function skipDFRN($contact, $item, $parent, $thr_parent, $cmd)
+ private static function skipDFRN($contact, $item, $parent, $thr_parent, $owner, $cmd)
{
if (empty($parent['network'])) {
return false;
return true;
}
+ // For the time being we always deliver forum post via DFRN if possible
+ // This can be removed possible at the end of 2020 when hopefully most system can process AP forum posts
+ if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
+ return false;
+ }
+
// Skip DFRN when the item will be (forcefully) delivered via AP
if (DI::config()->get('debug', 'total_ap_delivery') && ($contact['network'] == Protocol::DFRN) && !empty(APContact::getByURL($contact['url'], false))) {
return true;
}
$inboxes = [];
+ $relay_inboxes = [];
$uid = $target_item['contact-uid'] ?: $target_item['uid'];
if ($target_item['origin']) {
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
+
+ if (in_array($target_item['private'], [Item::PUBLIC])) {
+ $relay_inboxes = ActivityPub\Transmitter::addRelayServerInboxes();
+ }
+
Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
} elseif (Item::isForumPost($target_item, $owner)) {
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true);
// Remote items are transmitted via the personal inboxes.
// Doing so ensures that the dedicated receiver will get the message.
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true, $target_item['id']);
+
+ if (in_array($target_item['private'], [Item::PUBLIC])) {
+ $relay_inboxes = ActivityPub\Transmitter::addRelayServerInboxes([]);
+ }
+
Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
}
- if (empty($inboxes)) {
+ if (empty($inboxes) && empty($relay_inboxes)) {
Logger::log('No inboxes found for item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', Logger::DEBUG);
return 0;
}
}
}
+ // We deliver posts to relay servers slightly delayed to priorize the direct delivery
+ 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)) {
+ $delivery_queue_count++;
+ }
+ }
+
return $delivery_queue_count;
}
+
+ /**
+ * Check if the delivered item is a forum post
+ *
+ * @param array $item
+ * @return boolean
+ */
+ public static function isForumPost(array $item)
+ {
+ return !empty($item['forum_mode']);
+ }
}