]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Queue.php
Changes after code review
[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\DI;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\JsonLD;
30
31 /**
32  * This class handles the processing of incoming posts
33  */
34 class Queue
35 {
36         /**
37          * Add activity to the queue
38          *
39          * @param array $activity
40          * @param string $type
41          * @param integer $uid
42          * @param string $http_signer
43          * @param boolean $push
44          * @return array
45          */
46         public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
47         {
48                 $fields = [
49                         'activity-id' => $activity['id'],
50                         'object-id'   => $activity['object_id'],
51                         'type'        => $type,
52                         'object-type' => $activity['object_type'],
53                         'activity'    => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
54                         'received'    => DateTimeFormat::utcNow(),
55                         'push'        => $push,
56                         'trust'       => $trust_source,
57                 ];
58
59                 if (!empty($activity['reply-to-id'])) {
60                         $fields['in-reply-to-id'] = $activity['reply-to-id'];
61                 }
62
63                 if (!empty($activity['context'])) {
64                         $fields['conversation'] = $activity['context'];
65                 } elseif (!empty($activity['conversation'])) {
66                         $fields['conversation'] = $activity['conversation'];
67                 }
68
69                 if (!empty($activity['object_object_type'])) {
70                         $fields['object-object-type'] = $activity['object_object_type'];
71                 }
72
73                 if (!empty($http_signer)) {
74                         $fields['signer'] = $http_signer;
75                 }
76
77                 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
78
79                 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
80                 if (!empty($queue['id'])) {
81                         $activity['entry-id'] = $queue['id'];
82                         DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
83                 }
84                 return $activity;
85         }
86
87         /**
88          * Checks if an entry for a given url and type already exists
89          *
90          * @param string $url
91          * @param string $type
92          * @return boolean
93          */
94         public static function exists(string $url, string $type): bool
95         {
96                 return DBA::exists('inbox-entry', ['type' => $type, 'object-id' => $url]);
97         }
98
99         /**
100          * Remove activity from the queue
101          *
102          * @param array $activity
103          * @return void
104          */
105         public static function remove(array $activity = [])
106         {
107                 if (empty($activity['entry-id'])) {
108                         return;
109                 }
110                 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
111         }
112
113         /**
114          * Delete all entries that depend on the given worker id
115          *
116          * @param integer $wid
117          * @return void
118          */
119         public static function deleteByWorkerId(int $wid)
120         {
121                 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
122                 while ($entry = DBA::fetch($entries)) {
123                         self::deleteById($entry['id']);
124                 }
125                 DBA::close($entries);
126         }
127
128         /**
129          * Delete recursively an entry and all their children
130          *
131          * @param integer $id
132          * @return void
133          */
134         public static function deleteById(int $id)
135         {
136                 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
137                 if (empty($entry)) {
138                         return;
139                 }
140
141                 DBA::delete('inbox-entry', ['id' => $entry['id']]);
142         }
143
144         /**
145          * Set the worker id for the queue entry
146          *
147          * @param int $entry_id
148          * @param int $wid
149          * @return void
150          */
151         public static function setWorkerId(int $entry_id, int $wid)
152         {
153                 if (empty($entry_id) || empty($wid)) {
154                         return;
155                 }
156                 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $entry_id]);
157         }
158
159         /**
160          * Check if there is an assigned worker task
161          *
162          * @param int $wid
163          *
164          * @return bool
165          */
166         public static function hasWorker(int $wid): bool
167         {
168                 if (empty($wid)) {
169                         return false;
170                 }
171                 return DBA::exists('workerqueue', ['id' => $wid, 'done' => false]);
172         }
173
174         /**
175          * Process the activity with the given id
176          *
177          * @param integer $id
178          *
179          * @return bool
180          */
181         public static function process(int $id): bool
182         {
183                 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
184                 if (empty($entry)) {
185                         return false;
186                 }
187
188                 if (!empty($entry['wid'])) {
189                         $worker = DI::app()->getQueue();
190                         $wid = $worker['id'] ?? 0;
191                         if ($entry['wid'] != $wid) {
192                                 $workerqueue = DBA::selectFirst('workerqueue', ['pid'], ['id' => $entry['wid'], 'done' => false]);
193                                 if (!empty($workerqueue['pid']) && posix_kill($workerqueue['pid'], 0)) {
194                                         Logger::notice('Entry is already processed via another process.', ['current' => $wid, 'processor' => $entry['wid']]);
195                                         return false;
196                                 }
197                         }
198                 }
199
200                 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']]);
201
202                 $activity = json_decode($entry['activity'], true);
203                 $type     = $entry['type'];
204                 $push     = $entry['push'];
205
206                 $activity['entry-id']        = $entry['id'];
207                 $activity['worker-id']       = $entry['wid'];
208                 $activity['recursion-depth'] = 0;
209
210                 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ["`queue-id` = ? AND `uid` != ?", $entry['id'], 0]);
211                 while ($receiver = DBA::fetch($receivers)) {
212                         if (!in_array($receiver['uid'], $activity['receiver'])) {
213                                 $activity['receiver'][] = $receiver['uid'];
214                         }
215                 }
216                 DBA::close($receivers);
217
218                 if (!Receiver::routeActivities($activity, $type, $push)) {
219                         self::remove($activity);
220                 }
221
222                 return true;
223         }
224
225         /**
226          * Process all activities
227          *
228          * @return void
229          */
230         public static function processAll()
231         {
232                 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
233                 while ($entry = DBA::fetch($entries)) {
234                         // We don't need to process entries that depend on already existing entries.
235                         if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
236                                 continue;
237                         }
238                         Logger::debug('Process leftover entry', $entry);
239                         self::process($entry['id']);
240                 }
241                 DBA::close($entries);
242         }
243
244         /**
245          * Clear old activities
246          *
247          * @return void
248          */
249         public static function clear()
250         {
251                 // We delete all entries that aren't associated with a worker entry after seven days.
252                 // The other entries are deleted when the worker deferred for too long.
253                 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
254
255                 // Optimizing this table only last seconds
256                 if (DI::config()->get('system', 'optimize_tables')) {
257                         Logger::info('Optimize start');
258                         DBA::e("OPTIMIZE TABLE `inbox-entry`");
259                         Logger::info('Optimize end');
260                 }
261         }
262
263         /**
264          * Process all activities that are children of a given post url
265          *
266          * @param string $uri
267          * @return int
268          */
269         public static function processReplyByUri(string $uri): int
270         {
271                 $count = 0;
272                 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
273                 while ($entry = DBA::fetch($entries)) {
274                         $count += 1;
275                         self::process($entry['id']);
276                 }
277                 DBA::close($entries);
278                 return $count;
279         }
280
281         /**
282          * Checks if there are children of the given uri
283          *
284          * @param string $uri
285          *
286          * @return bool
287          */
288         public static function hasChildren(string $uri): bool
289         {
290                 return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
291         }
292
293         /**
294          * Prepare the queue entry.
295          * This is a test function that is used solely for development.
296          *
297          * @param integer $id
298          * @return array
299          */
300         public static function reprepareActivityById(int $id): array
301         {
302                 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
303                 if (empty($entry)) {
304                         return [];
305                 }
306
307                 $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
308                 if (!empty($receiver)) {
309                         $uid = $receiver['uid'];
310                 } else {
311                         $uid = 0;
312                 }
313
314                 $trust_source = $entry['trust'];
315
316                 $data     = json_decode($entry['activity'], true);
317                 $activity = json_decode($data['raw'], true);
318
319                 $ldactivity = JsonLD::compact($activity);
320                 return [
321                         'data'  => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
322                         'trust' => $trust_source
323                 ];
324         }
325
326         /**
327          * Set the trust for all untrusted entries.
328          * This is a test function that is used solely for development.
329          *
330          * @return void
331          */
332         public static function reprepareAll()
333         {
334                 $entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
335                 while ($entry = DBA::fetch($entries)) {
336                         $data = self::reprepareActivityById($entry['id'], false);
337                         if ($data['trust']) {
338                                 DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
339                         }
340                 }
341                 DBA::close($entries);
342         }
343 }