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;
27 use Friendica\Util\DateTimeFormat;
30 * This class handles the processing of incoming posts
35 * Add activity to the queue
37 * @param array $activity
40 * @param string $http_signer
41 * @param boolean $push
44 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array
47 'activity-id' => $activity['id'],
48 'object-id' => $activity['object_id'],
50 'object-type' => $activity['object_type'],
51 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
52 'received' => DateTimeFormat::utcNow(),
56 if (!empty($activity['reply-to-id'])) {
57 $fields['in-reply-to-id'] = $activity['reply-to-id'];
60 if (!empty($activity['context'])) {
61 $fields['conversation'] = $activity['context'];
62 } elseif (!empty($activity['conversation'])) {
63 $fields['conversation'] = $activity['conversation'];
66 if (!empty($activity['object_object_type'])) {
67 $fields['object-object-type'] = $activity['object_object_type'];
70 if (!empty($http_signer)) {
71 $fields['signer'] = $http_signer;
74 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
76 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
77 if (!empty($queue['id'])) {
78 $activity['entry-id'] = $queue['id'];
79 DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
85 * Remove activity from the queue
87 * @param array $activity
90 public static function remove(array $activity = [])
92 if (empty($activity['entry-id'])) {
95 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
99 * Delete all entries that depend on the given worker id
101 * @param integer $wid
104 public static function deleteByWorkerId(int $wid)
106 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
107 while ($entry = DBA::fetch($entries)) {
108 self::deleteById($entry['id']);
110 DBA::close($entries);
114 * Delete recursively an entry and all their children
119 private static function deleteById(int $id)
121 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
126 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
127 while ($child = DBA::fetch($children)) {
128 self::deleteById($child['id']);
130 DBA::close($children);
131 DBA::delete('inbox-entry', ['id' => $entry['id']]);
135 * Set the worker id for the queue entry
137 * @param array $activity
141 public static function setWorkerId(array $activity, int $wid)
143 if (empty($activity['entry-id']) || empty($wid)) {
146 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
150 * Check if there is an assigned worker task
152 * @param array $activity
155 public static function hasWorker(array $activity = []): bool
157 if (empty($activity['worker-id'])) {
160 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
164 * Process the activity with the given id
169 public static function process(int $id)
171 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
176 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']]);
178 $activity = json_decode($entry['activity'], true);
179 $type = $entry['type'];
180 $push = $entry['push'];
182 $activity['entry-id'] = $entry['id'];
183 $activity['worker-id'] = $entry['wid'];
184 $activity['recursion-depth'] = 0;
186 $receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
187 while ($receiver = DBA::fetch($receivers)) {
188 if (!in_array($receiver['uid'], $activity['receiver'])) {
189 $activity['receiver'][] = $receiver['uid'];
192 DBA::close($receivers);
194 if (!Receiver::routeActivities($activity, $type, $push)) {
195 self::remove($activity);
200 * Process all activities
204 public static function processAll()
206 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`wid` IS NULL"], ['order' => ['id' => true]]);
207 while ($entry = DBA::fetch($entries)) {
208 // We don't need to process entries that depend on already existing entries.
209 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ['object-id' => $entry['in-reply-to-id']])) {
212 Logger::debug('Process leftover entry', $entry);
213 self::process($entry['id']);
218 * Clear old activities
222 public static function clear()
224 // We delete all entries that aren't associated with a worker entry after seven days.
225 // The other entries are deleted when the worker deferred for too long.
226 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
230 * Process all activities that are children of a given post url
235 public static function processReplyByUri(string $uri)
237 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
238 while ($entry = DBA::fetch($entries)) {
239 self::process($entry['id']);