]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
Merge pull request #9652 from annando/issue-9584
[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  $inbox
40          * @param integer $uid
41          * @param array   $receivers
42          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43          * @throws \ImagickException
44          */
45         public static function execute($cmd, $target_id, $inbox, $uid, $receivers = [])
46         {
47                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
48                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
49                         if (in_array($cmd, [Delivery::POST])) {
50                                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
51                                 Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? 0);
52                         }
53                         return;
54                 }
55
56                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $target_id, 'uid' => $uid]);
57
58                 $success = true;
59
60                 if ($cmd == Delivery::MAIL) {
61                         $data = ActivityPub\Transmitter::createActivityFromMail($target_id);
62                         if (!empty($data)) {
63                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
64                         }
65                 } elseif ($cmd == Delivery::SUGGESTION) {
66                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $target_id);
67                 } elseif ($cmd == Delivery::RELOCATION) {
68                         // @todo Implementation pending
69                 } elseif ($cmd == Delivery::POKE) {
70                         // Implementation not planned
71                 } elseif ($cmd == Delivery::REMOVAL) {
72                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
73                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
74                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
75                 } else {
76                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($target_id);
77                         if (!empty($data)) {
78                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
79                         }
80                 }
81
82                 // This should never fail and is temporariy (until the move to the "post" structure)
83                 $item = Item::selectFirst(['uri-id'], ['id' => $target_id]);
84                 $uriid = $item['uri-id'] ?? 0;
85
86                 foreach ($receivers as $receiver) {
87                         $contact = Contact::getById($receiver);
88                         if (empty($contact)) {
89                                 continue;
90                         }
91
92                         if ($success) {
93                                 Contact::unmarkForArchival($contact);
94                         } else {
95                                 Contact::markForArchival($contact);
96                         }
97                 }
98
99                 if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
100                         Post\DeliveryData::incrementQueueFailed($uriid);
101                 } elseif ($success && in_array($cmd, [Delivery::POST])) {
102                         Post\DeliveryData::incrementQueueDone($uriid, Post\DeliveryData::ACTIVITYPUB);
103                 }
104         }
105 }