]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Queue.php
Fetching of missing posts is reworked
[friendica.git] / src / Protocol / ActivityPub / Queue.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\Protocol\ActivityPub;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * This class handles the processing of incoming posts
31  */
32 class Queue
33 {
34         public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array
35         {
36                 $fields = [
37                         'activity-id' => $activity['id'],
38                         'object-id'   => $activity['object_id'],
39                         'type'        => $type,
40                         'object-type' => $activity['object_type'],
41                         'activity'    => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
42                         'received'    => DateTimeFormat::utcNow(),
43                         'push'        => $push,
44                 ];
45
46                 if (!empty($activity['reply-to-id'])) {
47                         $fields['in-reply-to-id'] = $activity['reply-to-id'];
48                 }
49
50                 if (!empty($activity['object_object_type'])) {
51                         $fields['object-object-type'] = $activity['object_object_type'];
52                 }
53
54                 if (!empty($http_signer)) {
55                         $fields['signer'] = $http_signer;
56                 }
57
58                 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
59
60                 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
61                 if (!empty($queue['id'])) {
62                         $activity['entry-id'] = $queue['id'];
63                         DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
64                 }
65                 return $activity;
66         }
67
68         public static function remove(array $activity = [])
69         {
70                 if (empty($activity['entry-id'])) {
71                         return;
72                 }
73                 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
74                 //echo "Delete ".$activity['entry-id']."\n";
75
76         }
77
78         public static function process(int $id)
79         {
80                 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
81                 if (empty($entry)) {
82                         return;
83                 }
84         
85                 Logger::debug('Processing queue entry', ['id' => $entry['id'], 'type' => $entry['type'], 'object-type' => $entry['object-type'], 'uri' => $entry['object-id'], 'in-reply-to' => $entry['in-reply-to-id']]);
86
87                 $activity   = json_decode($entry['activity'], true);
88                 $type       = $entry['type'];
89                 $push       = $entry['push'];
90
91                 $activity['entry-id'] = $entry['id'];
92
93                 if (!Receiver::routeActivities($activity, $type, $push)) {
94                         self::remove($activity);
95                 }
96         }
97
98         public static function processAll()
99         {
100                 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type'], [], ['order' => ['id' => true]]);
101                 while ($entry = DBA::fetch($entries)) {
102                         echo $entry['id'] . "\t" . $entry['type'] . "\t" . $entry['object-type'] . "\n";
103                         self::process($entry['id']);
104                 }
105         }
106
107         public static function processReplyByUri(string $uri)
108         {
109                 $entries = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $uri], ['order' => ['id' => true]]);
110                 while ($entry = DBA::fetch($entries)) {
111                         self::process($entry['id']);
112                 }
113         }
114 }