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): 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(),
57 if (!empty($activity['reply-to-id'])) {
58 $fields['in-reply-to-id'] = $activity['reply-to-id'];
61 if (!empty($activity['context'])) {
62 $fields['conversation'] = $activity['context'];
63 } elseif (!empty($activity['conversation'])) {
64 $fields['conversation'] = $activity['conversation'];
67 if (!empty($activity['object_object_type'])) {
68 $fields['object-object-type'] = $activity['object_object_type'];
71 if (!empty($http_signer)) {
72 $fields['signer'] = $http_signer;
75 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
77 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
78 if (!empty($queue['id'])) {
79 $activity['entry-id'] = $queue['id'];
80 DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
86 * Remove activity from the queue
88 * @param array $activity
91 public static function remove(array $activity = [])
93 if (empty($activity['entry-id'])) {
96 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
100 * Delete all entries that depend on the given worker id
102 * @param integer $wid
105 public static function deleteByWorkerId(int $wid)
107 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
108 while ($entry = DBA::fetch($entries)) {
109 self::deleteById($entry['id']);
111 DBA::close($entries);
115 * Delete recursively an entry and all their children
120 public static function deleteById(int $id)
122 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
127 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
128 while ($child = DBA::fetch($children)) {
129 self::deleteById($child['id']);
131 DBA::close($children);
132 DBA::delete('inbox-entry', ['id' => $entry['id']]);
136 * Set the worker id for the queue entry
138 * @param array $activity
142 public static function setWorkerId(array $activity, int $wid)
144 if (empty($activity['entry-id']) || empty($wid)) {
147 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
151 * Check if there is an assigned worker task
153 * @param array $activity
156 public static function hasWorker(array $activity = []): bool
158 if (empty($activity['worker-id'])) {
161 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
165 * Process the activity with the given id
170 public static function process(int $id)
172 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
177 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']]);
179 $activity = json_decode($entry['activity'], true);
180 $type = $entry['type'];
181 $push = $entry['push'];
183 $activity['entry-id'] = $entry['id'];
184 $activity['worker-id'] = $entry['wid'];
185 $activity['recursion-depth'] = 0;
187 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
188 while ($receiver = DBA::fetch($receivers)) {
189 if (!in_array($receiver['uid'], $activity['receiver'])) {
190 $activity['receiver'][] = $receiver['uid'];
193 DBA::close($receivers);
195 if (!Receiver::routeActivities($activity, $type, $push)) {
196 self::remove($activity);
201 * Process all activities
205 public static function processAll()
207 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`wid` IS NULL"], ['order' => ['id' => true]]);
208 while ($entry = DBA::fetch($entries)) {
209 // We don't need to process entries that depend on already existing entries.
210 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
213 Logger::debug('Process leftover entry', $entry);
214 self::process($entry['id']);
219 * Clear old activities
223 public static function clear()
225 // We delete all entries that aren't associated with a worker entry after seven days.
226 // The other entries are deleted when the worker deferred for too long.
227 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
229 // Optimizing this table only last seconds
230 if (DI::config()->get('system', 'optimize_tables')) {
231 Logger::info('Optimize start');
232 DBA::e("OPTIMIZE TABLE `inbox-entry`");
233 Logger::info('Optimize end');
238 * Process all activities that are children of a given post url
243 public static function processReplyByUri(string $uri)
245 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
246 while ($entry = DBA::fetch($entries)) {
247 self::process($entry['id']);