<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Protocol\ActivityPub;
use Friendica\Core\Logger;
+use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\GServer;
+use Friendica\Model\Item;
use Friendica\Model\Post;
+use Friendica\Model\User;
use Friendica\Protocol\ActivityPub;
+use Friendica\Protocol\Delivery as ProtocolDelivery;
use Friendica\Util\HTTPSignature;
-use Friendica\Worker\Delivery as WorkerDelivery;
class Delivery
{
$serverfail = false;
foreach ($posts as $post) {
+ $owner = User::getOwnerDataById($post['uid']);
+ if (!$owner) {
+ Post\Delivery::remove($post['uri-id'], $inbox);
+ Post\Delivery::incrementFailed($post['uri-id'], $inbox);
+ continue;
+ }
+
if (!$serverfail) {
- $result = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id']);
+ $result = self::deliverToInbox($post['command'], 0, $inbox, $owner, $post['receivers'], $post['uri-id']);
if ($result['serverfailure']) {
// In a timeout situation we assume that every delivery to that inbox will time out.
// So we set the flag and try all deliveries at a later time.
- Logger::info('Inbox delivery has a server failure', ['inbox' => $inbox]);
+ Logger::notice('Inbox delivery has a server failure', ['inbox' => $inbox]);
$serverfail = true;
}
+ Worker::coolDown();
}
if ($serverfail || (!$result['success'] && !$result['drop'])) {
* @param string $cmd
* @param integer $item_id
* @param string $inbox
- * @param integer $uid
+ * @param array $owner Sender owner-view record
* @param array $receivers
* @param integer $uri_id
* @return array
*/
- public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
+ public static function deliverToInbox(string $cmd, int $item_id, string $inbox, array $owner, array $receivers, int $uri_id): array
{
+ /** @var int $uid */
+ $uid = $owner['uid'];
+
if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
- $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
+ $item = Post::selectFirst(['id', 'parent', 'origin', 'gravity', 'verb'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
if (empty($item['id'])) {
- Logger::notice('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
+ Logger::warning('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
+ Post\Delivery::remove($uri_id, $inbox);
+ return ['success' => true, 'serverfailure' => false, 'drop' => false];
+ } elseif (!DI::config()->get('system', 'redistribute_activities') && !$item['origin'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
+ Logger::notice('Activities are not relayed, removing delivery', ['verb' => $item['verb'], 'uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
Post\Delivery::remove($uri_id, $inbox);
- return true;
+ return ['success' => true, 'serverfailure' => false, 'drop' => false];
} else {
$item_id = $item['id'];
}
$serverfail = false;
$drop = false;
- if ($cmd == WorkerDelivery::MAIL) {
+ if ($cmd == ProtocolDelivery::MAIL) {
$data = ActivityPub\Transmitter::createActivityFromMail($item_id);
if (!empty($data)) {
- $success = HTTPSignature::transmit($data, $inbox, $uid);
+ $success = HTTPSignature::transmit($data, $inbox, $owner);
}
- } elseif ($cmd == WorkerDelivery::SUGGESTION) {
- $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
- } elseif ($cmd == WorkerDelivery::RELOCATION) {
+ } elseif ($cmd == ProtocolDelivery::SUGGESTION) {
+ $success = ActivityPub\Transmitter::sendContactSuggestion($owner, $inbox, $item_id);
+ } elseif ($cmd == ProtocolDelivery::RELOCATION) {
// @todo Implementation pending
- } elseif ($cmd == WorkerDelivery::POKE) {
- // Implementation not planned
- } elseif ($cmd == WorkerDelivery::REMOVAL) {
- $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
- } elseif ($cmd == WorkerDelivery::PROFILEUPDATE) {
- $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
+ } elseif ($cmd == ProtocolDelivery::REMOVAL) {
+ $success = ActivityPub\Transmitter::sendProfileDeletion($owner, $inbox);
+ } elseif ($cmd == ProtocolDelivery::PROFILEUPDATE) {
+ $success = ActivityPub\Transmitter::sendProfileUpdate($owner, $inbox);
} else {
- $data = Post\Activity::getByURIId($uri_id);
- if (empty($data)) {
- $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
- }
+ $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
if (!empty($data)) {
$timestamp = microtime(true);
- $response = HTTPSignature::post($data, $inbox, $uid);
+ $response = HTTPSignature::post($data, $inbox, $owner);
$runtime = microtime(true) - $timestamp;
$success = $response->isSuccess();
$serverfail = $response->isTimeout();
if (!empty($actor)) {
$drop = !ActivityPub\Transmitter::sendRelayFollow($actor);
Logger::notice('Resubscribed to relay', ['url' => $actor, 'success' => !$drop]);
- } elseif ($cmd = WorkerDelivery::DELETION) {
+ } elseif ($cmd == ProtocolDelivery::DELETION) {
// Remote systems not always accept our deletion requests, so we drop them if rejected.
// Situation is: In Friendica we allow the thread owner to delete foreign comments to their thread.
// Most AP systems don't allow this, so they will reject the deletion request.
}
- Logger::info('Delivery failed', ['retcode' => $response->getReturnCode(), 'serverfailure' => $serverfail, 'drop' => $drop, 'runtime' => round($runtime, 3), 'uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox]);
+ Logger::notice('Delivery failed', ['retcode' => $response->getReturnCode(), 'serverfailure' => $serverfail, 'drop' => $drop, 'runtime' => round($runtime, 3), 'uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox]);
}
if ($uri_id) {
if ($success) {
Post\Delivery::incrementFailed($uri_id, $inbox);
}
}
+ } elseif ($uri_id) {
+ Post\Delivery::remove($uri_id, $inbox);
}
}
Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success, 'serverfailure' => $serverfail, 'drop' => $drop]);
- if (($success || $drop) && in_array($cmd, [WorkerDelivery::POST])) {
+ if (($success || $drop) && in_array($cmd, [ProtocolDelivery::POST])) {
Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
}
}
/**
- * mark or unmark the given receivers for archival upon succoess
+ * mark or unmark the given receivers for archival upon success
*
* @param array $receivers
* @param boolean $success
*/
private static function setSuccess(array $receivers, bool $success)
{
- $gsid = null;
+ $gsid = null;
+ $update_counter = 0;
foreach ($receivers as $receiver) {
+ // Only update the first 10 receivers to avoid flooding the remote system with requests
+ if ($success && ($update_counter < 10) && Contact::updateByIdIfNeeded($receiver)) {
+ $update_counter++;
+ }
+
$contact = Contact::getById($receiver);
if (empty($contact)) {
continue;