]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Queue.php
We are now setting the corresponding worker id
[friendica.git] / src / Protocol / ActivityPub / Queue.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Protocol\ActivityPub;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * This class handles the processing of incoming posts
31  */
32 class Queue
33 {
34         /**
35          * Add activity to the queue
36          *
37          * @param array $activity
38          * @param string $type
39          * @param integer $uid
40          * @param string $http_signer
41          * @param boolean $push
42          * @return array
43          */
44         public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array
45         {
46                 $fields = [
47                         'activity-id' => $activity['id'],
48                         'object-id'   => $activity['object_id'],
49                         'type'        => $type,
50                         'object-type' => $activity['object_type'],
51                         'activity'    => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
52                         'received'    => DateTimeFormat::utcNow(),
53                         'push'        => $push,
54                 ];
55
56                 if (!empty($activity['reply-to-id'])) {
57                         $fields['in-reply-to-id'] = $activity['reply-to-id'];
58                 }
59
60                 if (!empty($activity['object_object_type'])) {
61                         $fields['object-object-type'] = $activity['object_object_type'];
62                 }
63
64                 if (!empty($http_signer)) {
65                         $fields['signer'] = $http_signer;
66                 }
67
68                 DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
69
70                 $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $activity['id']]);
71                 if (!empty($queue['id'])) {
72                         $activity['entry-id'] = $queue['id'];
73                         DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
74                 }
75                 return $activity;
76         }
77
78         /**
79          * Remove activity from the queue
80          *
81          * @param array $activity
82          * @return void
83          */
84         public static function remove(array $activity = [])
85         {
86                 if (empty($activity['entry-id'])) {
87                         return;
88                 }
89                 DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
90         }
91
92         /**
93          * Set the worker id for the queue entry
94          *
95          * @param array $activity
96          * @param int   $wid
97          * @return void
98          */
99         public static function setWorkerId(array $activity, int $wid)
100         {
101                 if (empty($activity['entry-id']) || empty($wid)) {
102                         return;
103                 }
104                 DBA::update('inbox-entry', ['wid' => $wid], ['id' => $activity['entry-id']]);
105         }
106
107         /**
108          * Process the activity with the given id
109          *
110          * @param integer $id
111          * @return void
112          */
113         public static function process(int $id)
114         {
115                 $entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
116                 if (empty($entry)) {
117                         return;
118                 }
119
120                 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']]);
121
122                 $activity = json_decode($entry['activity'], true);
123                 $type     = $entry['type'];
124                 $push     = $entry['push'];
125
126                 $activity['entry-id'] = $entry['id'];
127
128                 if (!Receiver::routeActivities($activity, $type, $push)) {
129                         self::remove($activity);
130                 }
131         }
132
133         /**
134          * Process all activities
135          *
136          * @return void
137          */
138         public static function processAll()
139         {
140                 $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type'], [], ['order' => ['id' => true]]);
141                 while ($entry = DBA::fetch($entries)) {
142                         self::process($entry['id']);
143                 }
144         }
145
146         /**
147          * Clear old activities
148          *
149          * @return void
150          */
151         public static function clear()
152         {
153                 DBA::delete('inbox-entry', ["`received` < ?", DateTimeFormat::utc('now - 2 days')]);
154         }
155
156         /**
157          * Process all activities that are children of a given post url
158          *
159          * @param string $uri
160          * @return void
161          */
162         public static function processReplyByUri(string $uri)
163         {
164                 $entries = DBA::select('inbox-entry', ['id'], ['in-reply-to-id' => $uri], ['order' => ['id' => true]]);
165                 while ($entry = DBA::fetch($entries)) {
166                         self::process($entry['id']);
167                 }
168         }
169 }