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\Protocol\ActivityPub;
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 * Remove activity from the queue
91 * @param array $activity
94 public static function remove(array $activity = [])
96 if (empty($activity['entry-id'])) {
99 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
103 * Delete all entries that depend on the given worker id
105 * @param integer $wid
108 public static function deleteByWorkerId(int $wid)
110 $entries = DBA::select('inbox-entry', ['id'], ['wid' => $wid]);
111 while ($entry = DBA::fetch($entries)) {
112 self::deleteById($entry['id']);
114 DBA::close($entries);
118 * Delete recursively an entry and all their children
123 public static function deleteById(int $id)
125 $entry = DBA::selectFirst('inbox-entry', ['id', 'object-id'], ['id' => $id]);
130 $children = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $entry['object-id']]);
131 while ($child = DBA::fetch($children)) {
132 self::deleteById($child['id']);
134 DBA::close($children);
135 DBA::delete('inbox-entry', ['id' => $entry['id']]);
139 * Set the worker id for the queue entry
141 * @param array $activity
145 public static function setWorkerId(array $activity, int $wid)
147 if (empty($activity['entry-id']) || empty($wid)) {
150 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
154 * Check if there is an assigned worker task
156 * @param array $activity
159 public static function hasWorker(array $activity = []): bool
161 if (empty($activity['worker-id'])) {
164 return DBA::exists('workerqueue', ['id' => $activity['worker-id'], 'done' => false]);
168 * Process the activity with the given id
173 public static function process(int $id)
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);
204 * Process all activities
208 public static function processAll()
210 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
211 while ($entry = DBA::fetch($entries)) {
212 // We don't need to process entries that depend on already existing entries.
213 if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) {
216 Logger::debug('Process leftover entry', $entry);
217 self::process($entry['id']);
219 DBA::close($entries);
223 * Clear old activities
227 public static function clear()
229 // We delete all entries that aren't associated with a worker entry after seven days.
230 // The other entries are deleted when the worker deferred for too long.
231 DBA::delete('inbox-entry', ["`wid` IS NULL AND `received` < ?", DateTimeFormat::utc('now - 7 days')]);
233 // Optimizing this table only last seconds
234 if (DI::config()->get('system', 'optimize_tables')) {
235 Logger::info('Optimize start');
236 DBA::e("OPTIMIZE TABLE `inbox-entry`");
237 Logger::info('Optimize end');
242 * Process all activities that are children of a given post url
247 public static function processReplyByUri(string $uri)
249 $entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
250 while ($entry = DBA::fetch($entries)) {
251 self::process($entry['id']);
253 DBA::close($entries);
257 * Prepare the queue entry.
258 * This is a test function that is used solely for development.
263 public static function reprepareActivityById(int $id): array
265 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
270 $receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
271 if (!empty($receiver)) {
272 $uid = $receiver['uid'];
277 $trust_source = $entry['trust'];
279 $data = json_decode($entry['activity'], true);
280 $activity = json_decode($data['raw'], true);
282 $ldactivity = JsonLD::compact($activity);
284 'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
285 'trust' => $trust_source
290 * Set the trust for all untrusted entries.
291 * This is a test function that is used solely for development.
295 public static function reprepareAll()
297 $entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
298 while ($entry = DBA::fetch($entries)) {
299 $data = self::reprepareActivityById($entry['id'], false);
300 if ($data['trust']) {
301 DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
304 DBA::close($entries);