]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
Inverted condition
[friendica.git] / src / Worker / APDelivery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Model\Contact;
27 use Friendica\Model\GServer;
28 use Friendica\Model\Post;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Util\HTTPSignature;
31
32 class APDelivery
33 {
34         /**
35          * Delivers ActivityPub messages
36          *
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          * @param int     $uri_id    URI-ID of item to be transmitted
43          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44          * @throws \ImagickException
45          */
46         public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0)
47         {
48                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
49                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uid' => $uid]);
50                         if (in_array($cmd, [Delivery::POST])) {
51                                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id]);
52                                 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
53                         }
54                         return;
55                 }
56
57                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
58
59                 $success = true;
60
61                 if ($cmd == Delivery::MAIL) {
62                         $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
63                         if (!empty($data)) {
64                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
65                         }
66                 } elseif ($cmd == Delivery::SUGGESTION) {
67                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
68                 } elseif ($cmd == Delivery::RELOCATION) {
69                         // @todo Implementation pending
70                 } elseif ($cmd == Delivery::POKE) {
71                         // Implementation not planned
72                 } elseif ($cmd == Delivery::REMOVAL) {
73                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
74                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
75                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
76                 } else {
77                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
78                         if (!empty($data)) {
79                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
80                         }
81                 }
82
83                 $gsid = null;
84
85                 foreach ($receivers as $receiver) {
86                         $contact = Contact::getById($receiver);
87                         if (empty($contact)) {
88                                 continue;
89                         }
90
91                         $gsid = $gsid ?: $contact['gsid'];
92
93                         if ($success) {
94                                 Contact::unmarkForArchival($contact);
95                         } else {
96                                 Contact::markForArchival($contact);
97                         }
98                 }
99
100                 if (!empty($gsid)) {
101                         GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
102                 }
103
104                 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
105                         Post\DeliveryData::incrementQueueFailed($uri_id);
106                 } elseif ($success && in_array($cmd, [Delivery::POST])) {
107                         Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
108                 }
109         }
110 }