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
34 public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array
37 'activity-id' => $activity['id'],
38 'object-id' => $activity['object_id'],
40 'object-type' => $activity['object_type'],
41 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
42 'received' => DateTimeFormat::utcNow(),
46 if (!empty($activity['reply-to-id'])) {
47 $fields['in-reply-to-id'] = $activity['reply-to-id'];
50 if (!empty($activity['object_object_type'])) {
51 $fields['object-object-type'] = $activity['object_object_type'];
54 if (!empty($http_signer)) {
55 $fields['signer'] = $http_signer;
58 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
60 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
61 if (!empty($queue['id'])) {
62 $activity['entry-id'] = $queue['id'];
63 DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
68 public static function remove(array $activity = [])
70 if (empty($activity['entry-id'])) {
73 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
74 //echo "Delete ".$activity['entry-id']."\n";
78 public static function process(int $id)
80 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
85 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']]);
87 $activity = json_decode($entry['activity'], true);
88 $type = $entry['type'];
89 $push = $entry['push'];
91 $activity['entry-id'] = $entry['id'];
93 if (!Receiver::routeActivities($activity, $type, $push)) {
94 self::remove($activity);
98 public static function processAll()
100 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type'], [], ['order' => ['id' => true]]);
101 while ($entry = DBA::fetch($entries)) {
102 echo $entry['id'] . "\t" . $entry['type'] . "\t" . $entry['object-type'] . "\n";
103 self::process($entry['id']);
107 public static function processReplyByUri(string $uri)
109 $entries = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $uri], ['order' => ['id' => true]]);
110 while ($entry = DBA::fetch($entries)) {
111 self::process($entry['id']);