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