3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Model\Contact;
27 use Friendica\Model\GServer;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\HTTPSignature;
36 * Delivers ActivityPub messages
38 * @param string $cmd One of the Worker\Delivery constant values
39 * @param integer $item_id 0 if no item is involved (like Delivery::REMOVAL and Delivery::PROFILEUPDATE)
40 * @param string $inbox The URL of the recipient profile
41 * @param integer $uid The ID of the user who triggered this delivery
42 * @param array $receivers The contact IDs related to the inbox URL for contact archival housekeeping
43 * @param int $uri_id URI-ID of item to be transmitted
44 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
45 * @throws \ImagickException
47 public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0)
49 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
50 Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]);
51 if (in_array($cmd, [Delivery::POST])) {
52 $item = Post::selectFirst(['uri-id'], ['id' => $item_id]);
53 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
58 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
61 $result = self::deliver($inbox);
62 $success = $result['success'];
63 $uri_ids = $result['uri_ids'];
66 if (empty($uri_ids)) {
67 $success = self::deliverToInbox($cmd, $item_id, $inbox, $uid, $receivers, $uri_id);
70 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
71 if (!empty($uri_id)) {
72 Post\Delivery::remove($uri_id, $inbox);
73 Post\DeliveryData::incrementQueueFailed($uri_id);
74 } elseif (!empty($uri_ids)) {
75 foreach ($uri_ids as $uri_id) {
76 Post\Delivery::remove($uri_id, $inbox);
77 Post\DeliveryData::incrementQueueFailed($uri_id);
80 } elseif ($success && in_array($cmd, [Delivery::POST])) {
81 if (!empty($uri_id)) {
82 Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
83 } elseif (!empty($uri_ids)) {
84 foreach ($uri_ids as $uri_id) {
85 Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
91 private static function deliver(string $inbox)
96 $posts = Post\Delivery::selectForInbox($inbox);
97 foreach ($posts as $post) {
98 $uri_ids[] = $post['uri-id'];
100 $success = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], [], $post['uri-id']);
104 return ['success' => $success, 'uri_ids' => $uri_ids];
107 private static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id)
109 if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
110 $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => $uid]);
111 $item_id = $item['id'] ?? 0;
112 if (empty($receivers) && !empty($item)) {
113 $parent = Post::selectFirst(Item::DELIVER_FIELDLIST, ['id' => $item['parent']]);
115 $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid);
116 $receivers = $inboxes[$inbox] ?? [];
118 // When we haven't fetched the receiver list, it can be a personal inbox
119 if (empty($receivers)) {
120 $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true);
121 $receivers = $inboxes[$inbox] ?? [];
128 if ($cmd == Delivery::MAIL) {
129 $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
131 $success = HTTPSignature::transmit($data, $inbox, $uid);
133 } elseif ($cmd == Delivery::SUGGESTION) {
134 $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
135 } elseif ($cmd == Delivery::RELOCATION) {
136 // @todo Implementation pending
137 } elseif ($cmd == Delivery::POKE) {
138 // Implementation not planned
139 } elseif ($cmd == Delivery::REMOVAL) {
140 $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
141 } elseif ($cmd == Delivery::PROFILEUPDATE) {
142 $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
144 $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
146 $success = HTTPSignature::transmit($data, $inbox, $uid);
147 if ($success && $uri_id) {
148 Post\Delivery::remove($uri_id, $inbox);
153 self::setSuccess($receivers, $success);
155 Logger::info('Delivered', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid, 'success' => $success]);
160 private static function setSuccess(array $receivers, bool $success)
164 foreach ($receivers as $receiver) {
165 $contact = Contact::getById($receiver);
166 if (empty($contact)) {
170 $gsid = $gsid ?: $contact['gsid'];
173 Contact::unmarkForArchival($contact);
175 Contact::markForArchival($contact);
180 GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);