3 * @copyright Copyright (C) 2020, Friendica
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\Item;
28 use Friendica\Model\Post;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Util\HTTPSignature;
35 * Delivers ActivityPub messages
37 * @param string $cmd One of the Worker\Delivery constant values
38 * @param integer $item_id 0 if no item is involved (like Delivery::REMOVAL and Delivery::PROFILEUPDATE)
39 * @param string $inbox The URL of the recipient profile
40 * @param integer $uid The ID of the user who triggered this delivery
41 * @param array $receivers The contact IDs related to the inbox URL for contact archival housekeeping
42 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43 * @throws \ImagickException
45 public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [])
47 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
48 Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]);
49 if (in_array($cmd, [Delivery::POST])) {
50 $item = Item::selectFirst(['uri-id'], ['id' => $item_id]);
51 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
56 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]);
60 if ($cmd == Delivery::MAIL) {
61 $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
63 $success = HTTPSignature::transmit($data, $inbox, $uid);
65 } elseif ($cmd == Delivery::SUGGESTION) {
66 $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
67 } elseif ($cmd == Delivery::RELOCATION) {
68 // @todo Implementation pending
69 } elseif ($cmd == Delivery::POKE) {
70 // Implementation not planned
71 } elseif ($cmd == Delivery::REMOVAL) {
72 $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
73 } elseif ($cmd == Delivery::PROFILEUPDATE) {
74 $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
76 $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
78 $success = HTTPSignature::transmit($data, $inbox, $uid);
82 // This should never fail and is temporariy (until the move to the "post" structure)
83 $item = Item::selectFirst(['uri-id'], ['id' => $item_id]);
84 $uriid = $item['uri-id'] ?? 0;
86 foreach ($receivers as $receiver) {
87 $contact = Contact::getById($receiver);
88 if (empty($contact)) {
93 Contact::unmarkForArchival($contact);
95 Contact::markForArchival($contact);
99 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
100 Post\DeliveryData::incrementQueueFailed($uriid);
101 } elseif ($success && in_array($cmd, [Delivery::POST])) {
102 Post\DeliveryData::incrementQueueDone($uriid, Post\DeliveryData::ACTIVITYPUB);