3 * @file src/Protocol/ActivityPub/Processor.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\Database\DBA;
8 use Friendica\Core\Protocol;
9 use Friendica\Model\Conversation;
10 use Friendica\Model\Contact;
11 use Friendica\Model\APContact;
12 use Friendica\Model\Item;
13 use Friendica\Model\User;
14 use Friendica\Content\Text\HTML;
15 use Friendica\Util\JsonLD;
16 use Friendica\Core\Config;
17 use Friendica\Protocol\ActivityPub;
20 * ActivityPub Protocol class
25 * Converts mentions from Pleroma into the Friendica format
29 * @return converted body
31 private static function convertMentions($body)
33 $URLSearchString = "^\[\]";
34 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
40 * Constructs a string with tags for a given tag array
43 * @param boolean $sensitive
45 * @return string with tags
47 private static function constructTagList($tags, $sensitive)
54 foreach ($tags as $tag) {
55 if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) {
56 if (!empty($tag_text)) {
60 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
64 /// @todo add nsfw for $sensitive
77 private static function constructAttachList($attachments, $item)
79 if (empty($attachments)) {
83 foreach ($attachments as $attach) {
84 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
85 if ($filetype == 'image') {
86 $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
88 if (!empty($item["attach"])) {
89 $item["attach"] .= ',';
93 if (!isset($attach['length'])) {
94 $attach['length'] = "0";
96 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
106 * @param array $activity
109 public static function createItem($activity, $body)
112 $item['verb'] = ACTIVITY_POST;
113 $item['parent-uri'] = $activity['reply-to-id'];
115 if ($activity['reply-to-id'] == $activity['id']) {
116 $item['gravity'] = GRAVITY_PARENT;
117 $item['object-type'] = ACTIVITY_OBJ_NOTE;
119 $item['gravity'] = GRAVITY_COMMENT;
120 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
123 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
124 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
125 self::fetchMissingActivity($activity['reply-to-id'], $activity);
128 self::postItem($activity, $item, $body);
134 * @param array $activity
137 public static function likeItem($activity, $body)
140 $item['verb'] = ACTIVITY_LIKE;
141 $item['parent-uri'] = JsonLD::fetchElement($activity, 'object');
142 $item['gravity'] = GRAVITY_ACTIVITY;
143 $item['object-type'] = ACTIVITY_OBJ_NOTE;
145 self::postItem($activity, $item, $body);
151 * @param array $activity
154 public static function deleteItem($activity)
156 $owner = Contact::getIdForURL($activity['owner']);
157 $object = JsonLD::fetchElement($activity, 'object');
158 logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
159 Item::delete(['uri' => $object, 'owner-id' => $owner]);
165 * @param array $activity
168 public static function dislikeItem($activity, $body)
171 $item['verb'] = ACTIVITY_DISLIKE;
172 $item['parent-uri'] = JsonLD::fetchElement($activity, 'object');
173 $item['gravity'] = GRAVITY_ACTIVITY;
174 $item['object-type'] = ACTIVITY_OBJ_NOTE;
176 self::postItem($activity, $item, $body);
182 * @param array $activity
186 private static function postItem($activity, $item, $body)
188 /// @todo What to do with $activity['context']?
190 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
191 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
195 $item['network'] = Protocol::ACTIVITYPUB;
196 $item['private'] = !in_array(0, $activity['receiver']);
197 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
198 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
199 $item['uri'] = $activity['id'];
200 $item['created'] = $activity['published'];
201 $item['edited'] = $activity['updated'];
202 $item['guid'] = $activity['diaspora:guid'];
203 $item['title'] = HTML::toBBCode($activity['name']);
204 $item['content-warning'] = HTML::toBBCode($activity['summary']);
205 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
206 $item['location'] = $activity['location'];
207 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
208 $item['app'] = $activity['service'];
209 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
211 $item = self::constructAttachList($activity['attachments'], $item);
213 if (!empty($activity['source'])) {
214 $item['body'] = $activity['source'];
217 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
218 $item['source'] = $body;
219 $item['conversation-href'] = $activity['context'];
220 $item['conversation-uri'] = $activity['conversation'];
222 foreach ($activity['receiver'] as $receiver) {
223 $item['uid'] = $receiver;
224 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
226 if (($receiver != 0) && empty($item['contact-id'])) {
227 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
230 $item_id = Item::insert($item);
231 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
241 private static function fetchMissingActivity($url, $child)
243 if (Config::get('system', 'ostatus_full_threads')) {
247 $object = ActivityPub::fetchContent($url);
248 if (empty($object)) {
249 logger('Activity ' . $url . ' was not fetchable, aborting.');
254 $activity['@context'] = $object['@context'];
255 unset($object['@context']);
256 $activity['id'] = $object['id'];
257 $activity['to'] = defaults($object, 'to', []);
258 $activity['cc'] = defaults($object, 'cc', []);
259 $activity['actor'] = $child['author'];
260 $activity['object'] = $object;
261 $activity['published'] = $object['published'];
262 $activity['type'] = 'Create';
264 $ldactivity = JsonLD::compact($activity);
265 ActivityPub\Receiver::processActivity($activity, $ldactivity);
266 logger('Activity ' . $url . ' had been fetched and processed.');
270 * perform a "follow" request
272 * @param array $activity
274 public static function followUser($activity)
276 $actor = JsonLD::fetchElement($activity, 'object');
277 $uid = User::getIdForURL($actor);
282 $owner = User::getOwnerDataById($uid);
284 $cid = Contact::getIdForURL($activity['owner'], $uid);
286 $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
291 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
292 'author-link' => $activity['owner']];
294 Contact::addRelationship($owner, $contact, $item);
295 $cid = Contact::getIdForURL($activity['owner'], $uid);
300 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
301 if ($contact['network'] != Protocol::ACTIVITYPUB) {
302 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
305 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
306 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
310 * Update the given profile
312 * @param array $activity
314 public static function updatePerson($activity)
316 $actor = JsonLD::fetchElement($activity, 'object');
321 logger('Updating profile for ' . $actor, LOGGER_DEBUG);
322 APContact::getByURL($actor, true);
326 * Delete the given profile
328 * @param array $activity
330 public static function deletePerson($activity)
332 $id = JsonLD::fetchElement($activity, 'object');
333 $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
335 if (empty($id) || empty($actor)) {
336 logger('Empty object id or actor.', LOGGER_DEBUG);
341 logger('Object id does not match actor.', LOGGER_DEBUG);
345 $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($id)]);
346 while ($contact = DBA::fetch($contacts)) {
347 Contact::remove($contact['id']);
349 DBA::close($contacts);
351 logger('Deleted contact ' . $id, LOGGER_DEBUG);
355 * Accept a follow request
357 * @param array $activity
359 public static function acceptFollowUser($activity)
361 $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
362 $uid = User::getIdForURL($actor);
367 $owner = User::getOwnerDataById($uid);
369 $cid = Contact::getIdForURL($activity['owner'], $uid);
371 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
375 $fields = ['pending' => false];
377 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
378 if ($contact['rel'] == Contact::FOLLOWER) {
379 $fields['rel'] = Contact::FRIEND;
382 $condition = ['id' => $cid];
383 DBA::update('contact', $fields, $condition);
384 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
388 * Reject a follow request
390 * @param array $activity
392 public static function rejectFollowUser($activity)
394 $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
395 $uid = User::getIdForURL($actor);
400 $owner = User::getOwnerDataById($uid);
402 $cid = Contact::getIdForURL($activity['owner'], $uid);
404 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
408 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
409 Contact::remove($cid);
410 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
412 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
417 * Undo activity like "like" or "dislike"
419 * @param array $activity
421 public static function undoActivity($activity)
423 $activity_url = JsonLD::fetchElement($activity, 'object');
424 if (empty($activity_url)) {
428 $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
433 $author_id = Contact::getIdForURL($actor);
434 if (empty($author_id)) {
438 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
442 * Activity to remove a follower
444 * @param array $activity
446 public static function undoFollowUser($activity)
448 $object = JsonLD::fetchElement($activity, 'object', 'as:object');
449 $uid = User::getIdForURL($object);
454 $owner = User::getOwnerDataById($uid);
456 $cid = Contact::getIdForURL($activity['owner'], $uid);
458 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
462 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
463 if (!DBA::isResult($contact)) {
467 Contact::removeFollower($owner, $contact);
468 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);