]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
Don't transmit to archived inboxes
[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\Item;
27 use Friendica\Model\Post;
28 use Friendica\Protocol\ActivityPub;
29 use Friendica\Util\HTTPSignature;
30
31 class APDelivery
32 {
33         /**
34          * Delivers ActivityPub messages
35          *
36          * @param string  $cmd
37          * @param integer $target_id
38          * @param string  $inbox
39          * @param integer $uid
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          * @throws \ImagickException
42          */
43         public static function execute($cmd, $target_id, $inbox, $uid)
44         {
45                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
46                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
47                         if (in_array($cmd, [Delivery::POST])) {
48                                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
49                                 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
50                         }
51                         return;
52                 }
53
54                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
55
56                 $success = true;
57
58                 if ($cmd == Delivery::MAIL) {
59                         $data = ActivityPub\Transmitter::createActivityFromMail($target_id);
60                         if (!empty($data)) {
61                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
62                         }
63                 } elseif ($cmd == Delivery::SUGGESTION) {
64                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $target_id);
65                 } elseif ($cmd == Delivery::RELOCATION) {
66                         // @todo Implementation pending
67                 } elseif ($cmd == Delivery::POKE) {
68                         // Implementation not planned
69                 } elseif ($cmd == Delivery::REMOVAL) {
70                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
71                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
72                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
73                 } else {
74                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($target_id);
75                         if (!empty($data)) {
76                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
77                         }
78                 }
79
80                 // This should never fail and is temporariy (until the move to the "post" structure)
81                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
82                 $uriid = $item['uri-id'] ?? 0;
83
84                 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
85                         Post\DeliveryData::incrementQueueFailed($uriid);
86                 } elseif ($success && in_array($cmd, [Delivery::POST])) {
87                         Post\DeliveryData::incrementQueueDone($uriid, Post\DeliveryData::ACTIVITYPUB);
88                 }
89         }
90 }