3 * @copyright Copyright (C) 2010-2022, 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\Database\Database;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Post;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\JsonLD;
33 * This class handles the processing of incoming posts
38 * Add activity to the queue
40 * @param array $activity
43 * @param string $http_signer
44 * @param boolean $push
47 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
50 'activity-id' => $activity['id'],
51 'object-id' => $activity['object_id'],
53 'object-type' => $activity['object_type'],
54 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
55 'received' => DateTimeFormat::utcNow(),
57 'trust' => $trust_source,
60 if (!empty($activity['reply-to-id'])) {
61 $fields['in-reply-to-id'] = $activity['reply-to-id'];
64 if (!empty($activity['context'])) {
65 $fields['conversation'] = $activity['context'];
66 } elseif (!empty($activity['conversation'])) {
67 $fields['conversation'] = $activity['conversation'];
70 if (!empty($activity['object_object_type'])) {
71 $fields['object-object-type'] = $activity['object_object_type'];
74 if (!empty($http_signer)) {
75 $fields['signer'] = $http_signer;
78 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
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);
89 * Checks if an entry for a given url and type already exists
95 public static function exists(string $url, string $type): bool
97 return DBA::exists('inbox-entry', ['type' => $type, 'object-id' => $url]);
101 * Remove activity from the queue
103 * @param array $activity
106 public static function remove(array $activity = [])
108 if (empty($activity['entry-id'])) {
111 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
115 * Delete all entries that depend on the given worker id
117 * @param integer $wid
120 public static function deleteByWorkerId(int $wid)
122 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
123 while ($entry = DBA::fetch($entries)) {
124 self::deleteById($entry['id']);
126 DBA::close($entries);
130 * Delete recursively an entry and all their children
135 public static function deleteById(int $id)
137 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
142 DBA::delete('inbox-entry', ['id' => $entry['id']]);
146 * Set the worker id for the queue entry
148 * @param int $entry_id
152 public static function setWorkerId(int $entry_id, int $wid)
154 if (empty($entry_id) || empty($wid)) {
157 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $entry_id]);
161 * Check if there is an assigned worker task
167 public static function hasWorker(int $wid): bool
172 return DBA::exists('workerqueue', ['id' => $wid, 'done' => false]);
176 * Process the activity with the given id
179 * @param bool $fetch_parents
183 public static function process(int $id, bool $fetch_parents = true): bool
185 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
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']]);
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']]);
204 $activity = json_decode($entry['activity'], true);
205 $type = $entry['type'];
206 $push = $entry['push'];
208 $activity['entry-id'] = $entry['id'];
209 $activity['worker-id'] = $entry['wid'];
210 $activity['recursion-depth'] = 0;
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'];
218 DBA::close($receivers);
220 if (!Receiver::routeActivities($activity, $type, $push, $fetch_parents)) {
221 self::remove($activity);
228 * Process all activities
232 public static function processAll()
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']])) {
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']])) {
244 Logger::debug('Process leftover entry', $entry);
245 self::process($entry['id'], false);
247 DBA::close($entries);
251 * Clear old activities
255 public static function clear()
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')]);
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');
270 * Process all activities that are children of a given post url
275 public static function processReplyByUri(string $uri): int
278 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
279 while ($entry = DBA::fetch($entries)) {
281 self::process($entry['id'], false);
283 DBA::close($entries);
288 * Checks if there are children of the given uri
294 public static function hasChildren(string $uri): bool
296 return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
300 * Prepare the queue entry.
301 * This is a test function that is used solely for development.
306 public static function reprepareActivityById(int $id): array
308 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
313 $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
314 if (!empty($receiver)) {
315 $uid = $receiver['uid'];
320 $trust_source = $entry['trust'];
322 $data = json_decode($entry['activity'], true);
323 $activity = json_decode($data['raw'], true);
325 $ldactivity = JsonLD::compact($activity);
327 'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
328 'trust' => $trust_source
333 * Set the trust for all untrusted entries.
334 * This is a test function that is used solely for development.
338 public static function reprepareAll()
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']]);
347 DBA::close($entries);