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;
29 use Friendica\Util\JsonLD;
32 * This class handles the processing of incoming posts
37 * Add activity to the queue
39 * @param array $activity
42 * @param string $http_signer
43 * @param boolean $push
46 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array
49 'activity-id' => $activity['id'],
50 'object-id' => $activity['object_id'],
52 'object-type' => $activity['object_type'],
53 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
54 'received' => DateTimeFormat::utcNow(),
56 'trust' => $trust_source,
59 if (!empty($activity['reply-to-id'])) {
60 $fields['in-reply-to-id'] = $activity['reply-to-id'];
63 if (!empty($activity['context'])) {
64 $fields['conversation'] = $activity['context'];
65 } elseif (!empty($activity['conversation'])) {
66 $fields['conversation'] = $activity['conversation'];
69 if (!empty($activity['object_object_type'])) {
70 $fields['object-object-type'] = $activity['object_object_type'];
73 if (!empty($http_signer)) {
74 $fields['signer'] = $http_signer;
77 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
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);
88 * Remove activity from the queue
90 * @param array $activity
93 public static function remove(array $activity = [])
95 if (empty($activity['entry-id'])) {
98 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
102 * Delete all entries that depend on the given worker id
104 * @param integer $wid
107 public static function deleteByWorkerId(int $wid)
109 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
110 while ($entry = DBA::fetch($entries)) {
111 self::deleteById($entry['id']);
113 DBA::close($entries);
117 * Delete recursively an entry and all their children
122 public static function deleteById(int $id)
124 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
129 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
130 while ($child = DBA::fetch($children)) {
131 self::deleteById($child['id']);
133 DBA::close($children);
134 DBA::delete('inbox-entry', ['id' => $entry['id']]);
138 * Set the worker id for the queue entry
140 * @param array $activity
144 public static function setWorkerId(array $activity, int $wid)
146 if (empty($activity['entry-id']) || empty($wid)) {
149 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
153 * Check if there is an assigned worker task
155 * @param array $activity
158 public static function hasWorker(array $activity = []): bool
160 if (empty($activity['worker-id'])) {
163 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
167 * Process the activity with the given id
173 public static function process(int $id): bool
175 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
180 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']]);
182 $activity = json_decode($entry['activity'], true);
183 $type = $entry['type'];
184 $push = $entry['push'];
186 $activity['entry-id'] = $entry['id'];
187 $activity['worker-id'] = $entry['wid'];
188 $activity['recursion-depth'] = 0;
190 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
191 while ($receiver = DBA::fetch($receivers)) {
192 if (!in_array($receiver['uid'], $activity['receiver'])) {
193 $activity['receiver'][] = $receiver['uid'];
196 DBA::close($receivers);
198 if (!Receiver::routeActivities($activity, $type, $push)) {
199 self::remove($activity);
206 * Process all activities
210 public static function processAll()
212 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
213 while ($entry = DBA::fetch($entries)) {
214 // We don't need to process entries that depend on already existing entries.
215 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
218 Logger::debug('Process leftover entry', $entry);
219 self::process($entry['id']);
221 DBA::close($entries);
225 * Clear old activities
229 public static function clear()
231 // We delete all entries that aren't associated with a worker entry after seven days.
232 // The other entries are deleted when the worker deferred for too long.
233 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
235 // Optimizing this table only last seconds
236 if (DI::config()->get('system', 'optimize_tables')) {
237 Logger::info('Optimize start');
238 DBA::e("OPTIMIZE TABLE `inbox-entry`");
239 Logger::info('Optimize end');
244 * Process all activities that are children of a given post url
249 public static function processReplyByUri(string $uri): int
252 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
253 while ($entry = DBA::fetch($entries)) {
255 self::process($entry['id']);
257 DBA::close($entries);
262 * Checks if there are children of the given uri
268 public static function hasChildren(string $uri): bool
270 return DBA::exists('inbox-entry', ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
274 * Prepare the queue entry.
275 * This is a test function that is used solely for development.
280 public static function reprepareActivityById(int $id): array
282 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
287 $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
288 if (!empty($receiver)) {
289 $uid = $receiver['uid'];
294 $trust_source = $entry['trust'];
296 $data = json_decode($entry['activity'], true);
297 $activity = json_decode($data['raw'], true);
299 $ldactivity = JsonLD::compact($activity);
301 'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
302 'trust' => $trust_source
307 * Set the trust for all untrusted entries.
308 * This is a test function that is used solely for development.
312 public static function reprepareAll()
314 $entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
315 while ($entry = DBA::fetch($entries)) {
316 $data = self::reprepareActivityById($entry['id'], false);
317 if ($data['trust']) {
318 DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
321 DBA::close($entries);