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\Util\DateTimeFormat;
31 * This class handles the processing of incoming posts
36 * Add activity to the queue
38 * @param array $activity
41 * @param string $http_signer
42 * @param boolean $push
45 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
48 'activity-id' => $activity['id'],
49 'object-id' => $activity['object_id'],
51 'object-type' => $activity['object_type'],
52 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
53 'received' => DateTimeFormat::utcNow(),
55 'trust' => $trust_source,
58 if (!empty($activity['reply-to-id'])) {
59 $fields['in-reply-to-id'] = $activity['reply-to-id'];
62 if (!empty($activity['context'])) {
63 $fields['conversation'] = $activity['context'];
64 } elseif (!empty($activity['conversation'])) {
65 $fields['conversation'] = $activity['conversation'];
68 if (!empty($activity['object_object_type'])) {
69 $fields['object-object-type'] = $activity['object_object_type'];
72 if (!empty($http_signer)) {
73 $fields['signer'] = $http_signer;
76 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
78 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
79 if (!empty($queue['id'])) {
80 $activity['entry-id'] = $queue['id'];
81 DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
87 * Remove activity from the queue
89 * @param array $activity
92 public static function remove(array $activity = [])
94 if (empty($activity['entry-id'])) {
97 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
101 * Delete all entries that depend on the given worker id
103 * @param integer $wid
106 public static function deleteByWorkerId(int $wid)
108 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
109 while ($entry = DBA::fetch($entries)) {
110 self::deleteById($entry['id']);
112 DBA::close($entries);
116 * Delete recursively an entry and all their children
121 public static function deleteById(int $id)
123 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
128 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
129 while ($child = DBA::fetch($children)) {
130 self::deleteById($child['id']);
132 DBA::close($children);
133 DBA::delete('inbox-entry', ['id' => $entry['id']]);
137 * Set the worker id for the queue entry
139 * @param array $activity
143 public static function setWorkerId(array $activity, int $wid)
145 if (empty($activity['entry-id']) || empty($wid)) {
148 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
152 * Check if there is an assigned worker task
154 * @param array $activity
157 public static function hasWorker(array $activity = []): bool
159 if (empty($activity['worker-id'])) {
162 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
166 * Process the activity with the given id
171 public static function process(int $id)
173 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
178 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']]);
180 $activity = json_decode($entry['activity'], true);
181 $type = $entry['type'];
182 $push = $entry['push'];
184 $activity['entry-id'] = $entry['id'];
185 $activity['worker-id'] = $entry['wid'];
186 $activity['recursion-depth'] = 0;
188 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
189 while ($receiver = DBA::fetch($receivers)) {
190 if (!in_array($receiver['uid'], $activity['receiver'])) {
191 $activity['receiver'][] = $receiver['uid'];
194 DBA::close($receivers);
196 if (!Receiver::routeActivities($activity, $type, $push)) {
197 self::remove($activity);
202 * Process all activities
206 public static function processAll()
208 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
209 while ($entry = DBA::fetch($entries)) {
210 // We don't need to process entries that depend on already existing entries.
211 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
214 Logger::debug('Process leftover entry', $entry);
215 self::process($entry['id']);
220 * Clear old activities
224 public static function clear()
226 // We delete all entries that aren't associated with a worker entry after seven days.
227 // The other entries are deleted when the worker deferred for too long.
228 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
230 // Optimizing this table only last seconds
231 if (DI::config()->get('system', 'optimize_tables')) {
232 Logger::info('Optimize start');
233 DBA::e("OPTIMIZE TABLE `inbox-entry`");
234 Logger::info('Optimize end');
239 * Process all activities that are children of a given post url
244 public static function processReplyByUri(string $uri)
246 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
247 while ($entry = DBA::fetch($entries)) {
248 self::process($entry['id']);