]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
spelling: author
[friendica.git] / src / Worker / APDelivery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Post;
27 use Friendica\Model\User;
28 use Friendica\Protocol\ActivityPub;
29 class APDelivery
30 {
31         /**
32          * Delivers ActivityPub messages
33          *
34          * @param string  $cmd       One of the Worker\Delivery constant values
35          * @param integer $item_id   0 if no item is involved (like Delivery::REMOVAL and Delivery::PROFILEUPDATE)
36          * @param string  $inbox     The URL of the recipient profile
37          * @param integer $uid       The ID of the user who triggered this delivery
38          * @param array   $receivers The contact IDs related to the inbox URL for contact archival housekeeping
39          * @param int     $uri_id    URI-ID of item to be transmitted
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          * @throws \ImagickException
42          */
43         public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0)
44         {
45                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
46                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
47                         if (empty($uri_id) && !empty($item_id)) {
48                                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id]);
49                                 $uri_id = $item['uri-id'] ?? 0;
50                         }
51                         if (empty($uri_id)) {
52                                 $posts   = Post\Delivery::selectForInbox($inbox);
53                                 $uri_ids = array_column($posts, 'uri-id');
54                         } else {
55                                 $uri_ids = [$uri_id];
56                         }
57
58                         foreach ($uri_ids as $uri_id) {
59                                 Post\Delivery::remove($uri_id, $inbox);
60                                 Post\DeliveryData::incrementQueueFailed($uri_id);
61                         }
62                         return;
63                 }
64
65                 Logger::debug('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
66
67                 if (empty($uri_id)) {
68                         $result  = ActivityPub\Delivery::deliver($inbox);
69                         $success = $result['success'];
70                         $drop    = false;
71                         $uri_ids = $result['uri_ids'];
72                 } else {
73                         $owner = User::getOwnerDataById($uid);
74                         if (!$owner) {
75                                 Post\Delivery::remove($uri_id, $inbox);
76                                 Post\Delivery::incrementFailed($uri_id, $inbox);
77                                 return;
78                         }
79
80                         $result  = ActivityPub\Delivery::deliverToInbox($cmd, $item_id, $inbox, $owner, $receivers, $uri_id);
81                         $success = $result['success'];
82                         $drop    = $result['drop'];
83                         $uri_ids = [$uri_id];
84                 }
85
86                 if (!$drop && !$success && !Worker::defer() && !empty($uri_ids)) {
87                         foreach ($uri_ids as $uri_id) {
88                                 Post\Delivery::remove($uri_id, $inbox);
89                                 Post\DeliveryData::incrementQueueFailed($uri_id);
90                         }
91                 }
92         }
93 }