]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
c98f1e399322e9d02725f08e45e41fe36fa99f9b
[friendica.git] / src / Worker / APDelivery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Item;
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
38          * @param integer      $target_id
39          * @param string|array $inboxes
40          * @param integer      $uid
41          * @param array        $receivers
42          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43          * @throws \ImagickException
44          */
45         public static function execute(string $cmd, int $target_id, $inboxes, int $uid, array $receivers = [])
46         {
47                 if (is_string($inboxes)) {
48                         $inboxes = [$inboxes];
49                 }
50
51                 foreach ($inboxes as $inbox) {
52                         self::perform($cmd, $target_id, $inbox, $uid, $receivers);
53                 }
54         }
55
56         /**
57          * Delivers ActivityPub messages
58          *
59          * @param string  $cmd
60          * @param integer $target_id
61          * @param string  $inbox
62          * @param integer $uid
63          * @param array   $receivers
64          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
65          * @throws \ImagickException
66          */
67         private static function perform(string $cmd, int $target_id, string $inbox, int $uid, array $receivers = [])
68         {
69                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
70                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
71                         if (in_array($cmd, [Delivery::POST])) {
72                                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
73                                 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
74                         }
75                         return;
76                 }
77
78                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
79
80                 $success = true;
81
82                 if ($cmd == Delivery::MAIL) {
83                         $data = ActivityPub\Transmitter::createActivityFromMail($target_id);
84                         if (!empty($data)) {
85                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
86                         }
87                 } elseif ($cmd == Delivery::SUGGESTION) {
88                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $target_id);
89                 } elseif ($cmd == Delivery::RELOCATION) {
90                         // @todo Implementation pending
91                 } elseif ($cmd == Delivery::POKE) {
92                         // Implementation not planned
93                 } elseif ($cmd == Delivery::REMOVAL) {
94                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
95                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
96                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
97                 } else {
98                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($target_id);
99                         if (!empty($data)) {
100                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
101                         }
102                 }
103
104                 // This should never fail and is temporariy (until the move to the "post" structure)
105                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
106                 $uriid = $item['uri-id'] ?? 0;
107
108                 foreach ($receivers as $receiver) {
109                         $contact = Contact::getById($receiver);
110                         if (empty($contact)) {
111                                 continue;
112                         }
113
114                         if ($success) {
115                                 Contact::unmarkForArchival($contact);
116                         } else {
117                                 Contact::markForArchival($contact);
118                         }
119                 }
120
121                 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
122                         Post\DeliveryData::incrementQueueFailed($uriid);
123                 } elseif ($success && in_array($cmd, [Delivery::POST])) {
124                         Post\DeliveryData::incrementQueueDone($uriid, Post\DeliveryData::ACTIVITYPUB);
125                 }
126         }
127 }