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