3 * @copyright Copyright (C) 2010-2023, 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\Content\Text\Markdown;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
28 use Friendica\Model\APContact;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Group;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Protocol\Activity;
35 use Friendica\Util\JsonLD;
38 * ActivityPub Client To Server class
43 * Process client to server activities
45 * @param array $activity
47 * @param array $application
50 public static function processActivity(array $activity, int $uid, array $application): array
52 $ldactivity = JsonLD::compact($activity);
53 if (empty($ldactivity)) {
54 Logger::notice('Invalid activity', ['activity' => $activity, 'uid' => $uid]);
58 $type = JsonLD::fetchElement($ldactivity, '@type');
60 Logger::notice('Empty type', ['activity' => $ldactivity, 'uid' => $uid]);
64 $object_id = JsonLD::fetchElement($ldactivity, 'as:object', '@id') ?? '';
65 $object_type = Receiver::fetchObjectType($ldactivity, $object_id, $uid);
66 if (!$object_type && !$object_id) {
67 Logger::notice('Empty object type or id', ['activity' => $ldactivity, 'uid' => $uid]);
71 Logger::debug('Processing activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'activity' => $ldactivity]);
72 return self::routeActivities($type, $object_type, $object_id, $uid, $application, $ldactivity);
76 * Route client to server activities
79 * @param string $object_type
80 * @param string $object_id
82 * @param array $application
83 * @param array $ldactivity
86 private static function routeActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity): array
90 if (in_array($object_type, Receiver::CONTENT_TYPES)) {
91 return self::createContent($uid, $application, $ldactivity);
95 if (in_array($object_type, Receiver::CONTENT_TYPES) && !empty($object_id)) {
96 return self::updateContent($uid, $object_id, $application, $ldactivity);
100 if (in_array($object_type, Receiver::ACCOUNT_TYPES) && !empty($object_id)) {
101 return self::followAccount($uid, $object_id, $ldactivity);
109 * Create a new post or comment
111 * @param integer $uid
112 * @param array $application
113 * @param array $ldactivity
116 private static function createContent(int $uid, array $application, array $ldactivity): array
118 $object_data = self::processObject($ldactivity['as:object']);
119 $item = ClientToServer::processContent($object_data, $application, $uid);
120 Logger::debug('Got data', ['item' => $item, 'object' => $object_data]);
122 $id = Item::insert($item, true);
124 $item = Post::selectFirst(['uri-id'], ['id' => $id]);
125 if (!empty($item['uri-id'])) {
126 return Transmitter::createActivityFromItem($id);
133 * Update an existing post or comment
135 * @param integer $uid
136 * @param string $object_id
137 * @param array $application
138 * @param array $ldactivity
141 private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array
143 $id = Item::fetchByLink($object_id, $uid);
144 $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]);
145 if (empty($original_post)) {
146 Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]);
150 $object_data = self::processObject($ldactivity['as:object']);
151 $item = ClientToServer::processContent($object_data, $application, $uid);
152 if (empty($item['title']) && empty($item['body'])) {
153 Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]);
156 $post = ['title' => $item['title'], 'body' => $item['body']];
157 Logger::debug('Got data', ['id' => $id, 'uid' => $uid, 'item' => $post]);
158 Item::update($post, ['id' => $id]);
159 Item::updateDisplayCache($original_post['uri-id']);
161 return Transmitter::createActivityFromItem($id);
165 * Follow a given account
166 * @todo Check the expected return value
168 * @param integer $uid
169 * @param string $object_id
170 * @param array $ldactivity
173 private static function followAccount(int $uid, string $object_id, array $ldactivity): array
179 * Fetches data from the object part of an client to server activity
181 * @param array $object
183 * @return array Object data
185 private static function processObject(array $object): array
187 $object_data = Receiver::getObjectDataFromActivity($object);
189 $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? '');
190 $object_data['receiver'] = [];
196 * Accumulate the targets and visibility of this post
198 * @param array $object
199 * @param string $actor
202 private static function getTargets(array $object, string $actor): array
204 $profile = APContact::getByURL($actor);
205 $followers = $profile['followers'];
209 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
212 $type = Receiver::TARGET_TO;
215 $type = Receiver::TARGET_CC;
218 $type = Receiver::TARGET_BTO;
221 $type = Receiver::TARGET_BCC;
224 $receiver_list = JsonLD::fetchElementArray($object, $element, '@id');
225 if (empty($receiver_list)) {
229 foreach ($receiver_list as $receiver) {
230 if ($receiver == Receiver::PUBLIC_COLLECTION) {
231 $targets[Receiver::TARGET_GLOBAL] = ($element == 'as:to');
235 if ($receiver == $followers) {
236 $targets[Receiver::TARGET_FOLLOWER] = true;
239 $targets[$type][] = Contact::getIdForURL($receiver);
246 * Create an item array from client to server object data
248 * @param array $object_data
249 * @param array $application
250 * @param integer $uid
253 private static function processContent(array $object_data, array $application, int $uid): array
255 $owner = User::getOwnerDataById($uid);
259 $item['network'] = Protocol::DFRN;
261 $item['verb'] = Activity::POST;
262 $item['contact-id'] = $owner['id'];
263 $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
264 $item['title'] = $object_data['name'];
265 $item['body'] = Markdown::toBBCode($object_data['content']);
266 $item['app'] = $application['name'] ?? 'API';
268 if (!empty($object_data['target'][Receiver::TARGET_GLOBAL])) {
269 $item['allow_cid'] = '';
270 $item['allow_gid'] = '';
271 $item['deny_cid'] = '';
272 $item['deny_gid'] = '';
273 $item['private'] = Item::PUBLIC;
274 } elseif (isset($object_data['target'][Receiver::TARGET_GLOBAL])) {
275 $item['allow_cid'] = '';
276 $item['allow_gid'] = '';
277 $item['deny_cid'] = '';
278 $item['deny_gid'] = '';
279 $item['private'] = Item::UNLISTED;
280 } elseif (!empty($object_data['target'][Receiver::TARGET_FOLLOWER])) {
281 $item['allow_cid'] = '';
282 $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
283 $item['deny_cid'] = '';
284 $item['deny_gid'] = '';
285 $item['private'] = Item::PRIVATE;
287 // @todo Set permissions via the $object_data['target'] array
288 $item['allow_cid'] = '<' . $owner['id'] . '>';
289 $item['allow_gid'] = '';
290 $item['deny_cid'] = '';
291 $item['deny_gid'] = '';
292 $item['private'] = Item::PRIVATE;
295 if (!empty($object_data['summary'])) {
296 $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $object_data['summary'] . "[/abstract]\n" . $item['body'];
299 if ($object_data['reply-to-id']) {
300 $item['thr-parent'] = $object_data['reply-to-id'];
301 $item['gravity'] = Item::GRAVITY_COMMENT;
303 $item['gravity'] = Item::GRAVITY_PARENT;
306 $item = DI::contentItem()->expandTags($item);