]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
caf89781e524025e5f233335c105850f9d77f0cd
[friendica.git] / src / Worker / APDelivery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Item;
29 use Friendica\Model\Post;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\HTTPSignature;
32
33 class APDelivery
34 {
35         /**
36          * Delivers ActivityPub messages
37          *
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
46          */
47         public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0)
48         {
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);
54                         }
55                         return;
56                 }
57
58                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
59
60                 if (empty($uri_id)) {
61                         $result = self::deliver($inbox);
62                         $success = $result['success'];
63                         $uri_ids = $result['uri_ids'];
64                 } else {
65                         $success = self::deliverToInbox($cmd, $item_id, $inbox, $uid, $receivers, $uri_id);
66                         $uri_ids = [$uri_id];
67                 }
68
69                 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
70                         foreach ($uri_ids as $uri_id) {
71                                 Post\Delivery::remove($uri_id, $inbox);
72                                 Post\DeliveryData::incrementQueueFailed($uri_id);
73                         }
74                 }
75         }
76
77         private static function deliver(string $inbox)
78         {
79                 Post\Delivery::removeFailed($inbox);
80                 
81                 $uri_ids = [];
82                 $posts   = Post\Delivery::selectForInbox($inbox);
83
84                 foreach ($posts as $post) {
85                         if (!self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], [], $post['uri-id'])) {
86                                 $uri_ids[] = $post['uri-id'];
87                         }
88                 }
89
90                 return ['success' => empty($uri_ids), 'uri_ids' => $uri_ids];
91         }
92
93         private static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id)
94         {
95                 if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
96                         $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => $uid]);
97                         $item_id = $item['id'] ?? 0;
98                         if (empty($receivers) && !empty($item)) {
99                                 $parent = Post::selectFirst(Item::DELIVER_FIELDLIST, ['id' => $item['parent']]);
100
101                                 $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid);
102                                 $receivers = $inboxes[$inbox] ?? [];
103
104                                 // When we haven't fetched the receiver list, it can be a personal inbox
105                                 if (empty($receivers)) {
106                                         $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true);
107                                         $receivers = $inboxes[$inbox] ?? [];
108                                 }
109                         }
110                 }
111
112                 $success = true;
113
114                 if ($cmd == Delivery::MAIL) {
115                         $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
116                         if (!empty($data)) {
117                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
118                         }
119                 } elseif ($cmd == Delivery::SUGGESTION) {
120                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
121                 } elseif ($cmd == Delivery::RELOCATION) {
122                         // @todo Implementation pending
123                 } elseif ($cmd == Delivery::POKE) {
124                         // Implementation not planned
125                 } elseif ($cmd == Delivery::REMOVAL) {
126                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
127                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
128                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
129                 } else {
130                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
131                         if (!empty($data)) {
132                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
133                                 if ($uri_id) {
134                                         if ($success) {
135                                                 Post\Delivery::remove($uri_id, $inbox);
136                                         } else {
137                                                 Post\Delivery::incrementFailed($uri_id, $inbox);
138                                         }
139                                 }
140                         }
141                 }
142
143                 self::setSuccess($receivers, $success);
144
145                 Logger::info('Delivered', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid, 'success' => $success]);
146
147                 if ($success && in_array($cmd, [Delivery::POST])) {
148                         Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
149                 }
150
151                 return $success;
152         }
153
154         private static function setSuccess(array $receivers, bool $success)
155         {
156                 $gsid = null;
157
158                 foreach ($receivers as $receiver) {
159                         $contact = Contact::getById($receiver);
160                         if (empty($contact)) {
161                                 continue;
162                         }
163
164                         $gsid = $gsid ?: $contact['gsid'];
165
166                         if ($success) {
167                                 Contact::unmarkForArchival($contact);
168                         } else {
169                                 Contact::markForArchival($contact);
170                         }
171                 }
172
173                 if (!empty($gsid)) {
174                         GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
175                 }
176         }
177 }