3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Protocol\ActivityPub;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
29 use Friendica\Model\ItemURI;
30 use Friendica\Model\Post;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\JsonLD;
35 * This class handles the processing of incoming posts
40 * Add activity to the queue
42 * @param array $activity
45 * @param string $http_signer
46 * @param boolean $push
49 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
52 'activity-id' => $activity['id'],
53 'object-id' => $activity['object_id'],
55 'object-type' => $activity['object_type'],
56 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
57 'received' => DateTimeFormat::utcNow(),
59 'trust' => $trust_source,
62 if (!empty($activity['reply-to-id'])) {
63 $fields['in-reply-to-id'] = $activity['reply-to-id'];
66 if (!empty($activity['context'])) {
67 $fields['conversation'] = $activity['context'];
68 } elseif (!empty($activity['conversation'])) {
69 $fields['conversation'] = $activity['conversation'];
72 if (!empty($activity['object_object_type'])) {
73 $fields['object-object-type'] = $activity['object_object_type'];
76 if (!empty($http_signer)) {
77 $fields['signer'] = $http_signer;
80 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
82 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
83 if (!empty($queue['id'])) {
84 $activity['entry-id'] = $queue['id'];
85 DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
91 * Checks if an entry for a given url and type already exists
97 public static function exists(string $url, string $type): bool
99 return DBA::exists('inbox-entry', ['type' => $type, 'object-id' => $url]);
103 * Remove activity from the queue
105 * @param array $activity
108 public static function remove(array $activity = [])
110 if (empty($activity['entry-id'])) {
113 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
117 * Delete all entries that depend on the given worker id
119 * @param integer $wid
122 public static function deleteByWorkerId(int $wid)
124 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
125 while ($entry = DBA::fetch($entries)) {
126 self::deleteById($entry['id']);
128 DBA::close($entries);
132 * Delete recursively an entry and all their children
137 public static function deleteById(int $id)
139 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
144 Logger::debug('Delete inbox-entry', ['id' => $entry['id']]);
146 DBA::delete('inbox-entry', ['id' => $entry['id']]);
148 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
149 while ($child = DBA::fetch($children)) {
150 self::deleteById($child['id']);
152 DBA::close($children);
156 * Set the worker id for the queue entry
158 * @param int $entry_id
162 public static function setWorkerId(int $entry_id, int $wid)
164 if (empty($entry_id) || empty($wid)) {
167 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $entry_id]);
171 * Check if there is an assigned worker task
177 public static function hasWorker(int $wid): bool
182 return DBA::exists('workerqueue', ['id' => $wid, 'done' => false]);
186 * Process the activity with the given id
189 * @param bool $fetch_parents
193 public static function process(int $id, bool $fetch_parents = true): bool
195 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
200 if (!self::isProcessable($id)) {
201 Logger::debug('Other queue entries need to be processed first.', ['id' => $id]);
205 if (!empty($entry['wid'])) {
206 $worker = DI::app()->getQueue();
207 $wid = $worker['id'] ?? 0;
208 if ($entry['wid'] != $wid) {
209 $workerqueue = DBA::selectFirst('workerqueue', ['pid'], ['id' => $entry['wid'], 'done' => false]);
210 if (!empty($workerqueue['pid']) && posix_kill($workerqueue['pid'], 0)) {
211 Logger::notice('Entry is already processed via another process.', ['current' => $wid, 'processor' => $entry['wid']]);
217 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'], 'callstack' => System::callstack(20)]);
219 $activity = json_decode($entry['activity'], true);
220 $type = $entry['type'];
221 $push = $entry['push'];
223 $activity['entry-id'] = $entry['id'];
224 $activity['worker-id'] = $entry['wid'];
225 $activity['recursion-depth'] = 0;
227 if (empty($activity['thread-children-type'])) {
228 $activity['thread-children-type'] = $type;
231 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ["`queue-id` = ? AND `uid` != ?", $entry['id'], 0]);
232 while ($receiver = DBA::fetch($receivers)) {
233 if (!in_array($receiver['uid'], $activity['receiver'])) {
234 $activity['receiver'][] = $receiver['uid'];
237 DBA::close($receivers);
239 if (!Receiver::routeActivities($activity, $type, $push, $fetch_parents, $activity['receiver'][0] ?? 0)) {
240 self::remove($activity);
247 * Process all activities
251 public static function processAll()
253 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
254 while ($entry = DBA::fetch($entries)) {
255 // Don't process entries of items that are answer to nonexistent posts
256 if (!empty($entry['in-reply-to-id']) && !Post::exists(['uri' => $entry['in-reply-to-id']])) {
259 // We don't need to process entries that depend on already existing entries.
260 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
263 Logger::debug('Process leftover entry', $entry);
264 self::process($entry['id'], false);
266 DBA::close($entries);
269 public static function isProcessable(int $id): bool
271 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
276 if (!empty($entry['object-id']) && Post::exists(['uri' => $entry['object-id']])) {
277 // The object already exists, so processing can be done
281 if (!empty($entry['conversation'])) {
282 $conv_id = ItemURI::getIdByURI($entry['conversation'], false);
283 if (DBA::exists('post-thread', ['conversation-id' => $conv_id])) {
284 // We have got the conversation in the system, so the post can be processed
289 if (!empty($entry['object-id']) && !empty($entry['in-reply-to-id']) && ($entry['object-id'] != $entry['in-reply-to-id']) && DBA::exists('inbox-entry', ['object-id' => $entry['in-reply-to-id']])) {
290 // This entry belongs to some other entry that should be processed first
298 * Clear old activities
302 public static function clear()
304 // We delete all entries that aren't associated with a worker entry after seven days.
305 // The other entries are deleted when the worker deferred for too long.
306 $entries = DBA::select('inbox-entry', ['id'], ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
307 while ($entry = DBA::fetch($entries)) {
308 self::deleteById($entry['id']);
310 DBA::close($entries);
312 // Optimizing this table only last seconds
313 if (DI::config()->get('system', 'optimize_tables')) {
314 Logger::info('Optimize start');
315 DBA::optimizeTable('inbox-entry');
316 Logger::info('Optimize end');
321 * Process all activities that are children of a given post url
326 public static function processReplyByUri(string $uri): int
329 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
330 while ($entry = DBA::fetch($entries)) {
332 self::process($entry['id'], false);
334 DBA::close($entries);
339 * Checks if there are children of the given uri
345 public static function hasChildren(string $uri): bool
347 return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
351 * Prepare the queue entry.
352 * This is a test function that is used solely for development.
357 public static function reprepareActivityById(int $id): array
359 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
364 $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
365 if (!empty($receiver)) {
366 $uid = $receiver['uid'];
371 $trust_source = $entry['trust'];
373 $data = json_decode($entry['activity'], true);
374 $activity = json_decode($data['raw'], true);
376 $ldactivity = JsonLD::compact($activity);
378 'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
379 'trust' => $trust_source
384 * Set the trust for all untrusted entries.
385 * This is a test function that is used solely for development.
389 public static function reprepareAll()
391 $entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
392 while ($entry = DBA::fetch($entries)) {
393 $data = self::reprepareActivityById($entry['id'], false);
394 if ($data['trust']) {
395 DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
398 DBA::close($entries);