]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Queue.php
Funkwhale context file moved
[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         /**
35          * Add activity to the queue
36          *
37          * @param array $activity
38          * @param string $type
39          * @param integer $uid
40          * @param string $http_signer
41          * @param boolean $push
42          * @return array
43          */
44         public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array
45         {
46                 $fields = [
47                         'activity-id' => $activity['id'],
48                         'object-id'   => $activity['object_id'],
49                         'type'        => $type,
50                         'object-type' => $activity['object_type'],
51                         'activity'    => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
52                         'received'    => DateTimeFormat::utcNow(),
53                         'push'        => $push,
54                 ];
55
56                 if (!empty($activity['reply-to-id'])) {
57                         $fields['in-reply-to-id'] = $activity['reply-to-id'];
58                 }
59
60                 if (!empty($activity['context'])) {
61                         $fields['conversation'] = $activity['context'];
62                 } elseif (!empty($activity['conversation'])) {
63                         $fields['conversation'] = $activity['conversation'];
64                 }
65
66                 if (!empty($activity['object_object_type'])) {
67                         $fields['object-object-type'] = $activity['object_object_type'];
68                 }
69
70                 if (!empty($http_signer)) {
71                         $fields['signer'] = $http_signer;
72                 }
73
74                 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
75
76                 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
77                 if (!empty($queue['id'])) {
78                         $activity['entry-id'] = $queue['id'];
79                         DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
80                 }
81                 return $activity;
82         }
83
84         /**
85          * Remove activity from the queue
86          *
87          * @param array $activity
88          * @return void
89          */
90         public static function remove(array $activity = [])
91         {
92                 if (empty($activity['entry-id'])) {
93                         return;
94                 }
95                 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
96         }
97
98         /**
99          * Set the worker id for the queue entry
100          *
101          * @param array $activity
102          * @param int   $wid
103          * @return void
104          */
105         public static function setWorkerId(array $activity, int $wid)
106         {
107                 if (empty($activity['entry-id']) || empty($wid)) {
108                         return;
109                 }
110                 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
111         }
112
113         /**
114          * Check if there is an assigned worker task
115          *
116          * @param array $activity
117          * @return bool
118          */
119         public static function hasWorker(array $activity = []): bool
120         {
121                 if (empty($activity['worker-id'])) {
122                         return false;
123                 }
124                 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
125         }
126
127         /**
128          * Process the activity with the given id
129          *
130          * @param integer $id
131          * @return void
132          */
133         public static function process(int $id)
134         {
135                 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
136                 if (empty($entry)) {
137                         return;
138                 }
139
140                 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']]);
141
142                 $activity = json_decode($entry['activity'], true);
143                 $type     = $entry['type'];
144                 $push     = $entry['push'];
145
146                 $activity['entry-id']  = $entry['id'];
147                 $activity['worker-id'] = $entry['wid'];
148
149                 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
150                 while ($receiver = DBA::fetch($receivers)) {
151                         if (!in_array($receiver['uid'], $activity['receiver'])) {
152                                 $activity['receiver'][] = $receiver['uid'];
153                         }
154                 }
155                 DBA::close($receivers);
156
157                 if (!Receiver::routeActivities($activity, $type, $push)) {
158                         self::remove($activity);
159                 }
160         }
161
162         /**
163          * Process all activities
164          *
165          * @return void
166          */
167         public static function processAll()
168         {
169                 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type'], [], ['order' => ['id' => true]]);
170                 while ($entry = DBA::fetch($entries)) {
171                         self::process($entry['id']);
172                 }
173         }
174
175         /**
176          * Clear old activities
177          *
178          * @return void
179          */
180         public static function clear()
181         {
182                 // We delete all entries that aren't associated with a worker entry after seven days.
183                 // The other entries are deleted when the worker deferred for too long.
184                 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
185         }
186
187         /**
188          * Process all activities that are children of a given post url
189          *
190          * @param string $uri
191          * @return void
192          */
193         public static function processReplyByUri(string $uri)
194         {
195                 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
196                 while ($entry = DBA::fetch($entries)) {
197                         self::process($entry['id']);
198                 }
199         }
200 }