3 * @copyright Copyright (C) 2020, Friendica
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\PageInfo;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Content\Text\Markdown;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Database\DBA;
32 use Friendica\Model\APContact;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Conversation;
35 use Friendica\Model\Event;
36 use Friendica\Model\Item;
37 use Friendica\Model\ItemURI;
38 use Friendica\Model\Mail;
39 use Friendica\Model\Tag;
40 use Friendica\Model\User;
41 use Friendica\Model\Post;
42 use Friendica\Protocol\Activity;
43 use Friendica\Protocol\ActivityPub;
44 use Friendica\Protocol\Relay;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\JsonLD;
47 use Friendica\Util\Strings;
50 * ActivityPub Processor Protocol class
55 * Converts mentions from Pleroma into the Friendica format
59 * @return string converted body
61 private static function convertMentions($body)
63 $URLSearchString = "^\[\]";
64 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
70 * Convert the language array into a language JSON
72 * @param array $languages
73 * @return string language JSON
75 private static function processLanguages(array $languages)
77 $codes = array_keys($languages);
79 foreach ($codes as $code) {
87 return json_encode($lang);
90 * Replaces emojis in the body
92 * @param array $emojis
95 * @return string with replaced emojis
97 private static function replaceEmojis($body, array $emojis)
101 array_column($emojis, 'name'),
102 array_map(function ($emoji) {
103 return '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
112 * Store attached media files in the post-media table
115 * @param array $attachment
118 private static function storeAttachmentAsMedia(int $uriid, array $attachment)
120 if (empty($attachment['url'])) {
124 $data = ['uri-id' => $uriid];
126 $filetype = strtolower(substr($attachment['mediaType'], 0, strpos($attachment['mediaType'], '/')));
127 if ($filetype == 'image') {
128 $data['type'] = Post\Media::IMAGE;
129 } elseif ($filetype == 'video') {
130 $data['type'] = Post\Media::VIDEO;
131 } elseif ($filetype == 'audio') {
132 $data['type'] = Post\Media::AUDIO;
133 } elseif (in_array($attachment['mediaType'], ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
134 $data['type'] = Post\Media::TORRENT;
136 Logger::info('Unknown type', ['attachment' => $attachment]);
140 $data['url'] = $attachment['url'];
141 $data['mimetype'] = $attachment['mediaType'];
142 $data['height'] = $attachment['height'] ?? null;
143 $data['size'] = $attachment['size'] ?? null;
144 $data['preview'] = $attachment['image'] ?? null;
145 $data['description'] = $attachment['name'] ?? null;
147 Post\Media::insert($data);
151 * Add attachment data to the item array
153 * @param array $activity
156 * @return array array
158 private static function constructAttachList($activity, $item)
160 if (empty($activity['attachments'])) {
164 foreach ($activity['attachments'] as $attach) {
165 switch ($attach['type']) {
168 'url' => $attach['url'],
169 'type' => $attach['type'],
170 'title' => $attach['title'] ?? '',
171 'text' => $attach['desc'] ?? '',
172 'image' => $attach['image'] ?? '',
176 $item['body'] = PageInfo::appendDataToBody($item['body'], $data);
179 self::storeAttachmentAsMedia($item['uri-id'], $attach);
181 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
182 if ($filetype == 'image') {
183 if (!empty($activity['source'])) {
184 foreach ([0, 1, 2] as $size) {
185 if (preg_match('#/photo/.*-' . $size . '\.#ism', $attach['url']) &&
186 strpos(preg_replace('#(/photo/.*)-[012]\.#ism', '$1-' . $size . '.', $activity['source']), $attach['url'])) {
190 if (strpos($activity['source'], $attach['url'])) {
195 $item['body'] .= "\n";
197 // image is the preview/thumbnail URL
198 if (!empty($attach['image'])) {
199 $item['body'] .= '[url=' . $attach['url'] . ']';
200 $attach['url'] = $attach['image'];
203 if (empty($attach['name'])) {
204 $item['body'] .= '[img]' . $attach['url'] . '[/img]';
206 $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
209 if (!empty($attach['image'])) {
210 $item['body'] .= '[/url]';
212 } elseif ($filetype == 'audio') {
213 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
217 $item['body'] = '[audio]' . $attach['url'] . "[/audio]\n" . $item['body'];
218 } elseif ($filetype == 'video') {
219 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
223 $item['body'] = '[video]' . $attach['url'] . "[/video]\n" . $item['body'];
234 * @param array $activity Activity array
235 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237 public static function updateItem($activity)
239 $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
240 if (!DBA::isResult($item)) {
241 Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
242 $item = self::createItem($activity);
243 self::postItem($activity, $item);
247 $item['changed'] = DateTimeFormat::utcNow();
248 $item['edited'] = DateTimeFormat::utc($activity['updated']);
250 $item = self::processContent($activity, $item);
252 $item = self::constructAttachList($activity, $item);
258 Item::update($item, ['uri' => $activity['id']]);
262 * Prepares data for a message
264 * @param array $activity Activity array
265 * @return array Internal item
266 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
267 * @throws \ImagickException
269 public static function createItem($activity)
272 $item['verb'] = Activity::POST;
273 $item['thr-parent'] = $activity['reply-to-id'];
275 if ($activity['reply-to-id'] == $activity['id']) {
276 $item['gravity'] = GRAVITY_PARENT;
277 $item['object-type'] = Activity\ObjectType::NOTE;
279 $item['gravity'] = GRAVITY_COMMENT;
280 $item['object-type'] = Activity\ObjectType::COMMENT;
283 if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
284 Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
285 self::fetchMissingActivity($activity['reply-to-id'], $activity);
288 $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
290 /// @todo What to do with $activity['context']?
291 if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
292 Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
296 $item['network'] = Protocol::ACTIVITYPUB;
297 $item['author-link'] = $activity['author'];
298 $item['author-id'] = Contact::getIdForURL($activity['author']);
299 $item['owner-link'] = $activity['actor'];
300 $item['owner-id'] = Contact::getIdForURL($activity['actor']);
302 if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) {
303 $item['private'] = Item::UNLISTED;
304 } elseif (in_array(0, $activity['receiver'])) {
305 $item['private'] = Item::PUBLIC;
307 $item['private'] = Item::PRIVATE;
310 if (!empty($activity['raw'])) {
311 $item['source'] = $activity['raw'];
312 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
313 $item['conversation-href'] = $activity['context'] ?? '';
314 $item['conversation-uri'] = $activity['conversation'] ?? '';
316 if (isset($activity['push'])) {
317 $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
321 if (!empty($activity['from-relay'])) {
322 $item['direction'] = Conversation::RELAY;
325 $item['isForum'] = false;
327 if (!empty($activity['thread-completion'])) {
328 if ($activity['thread-completion'] != $item['owner-id']) {
329 $actor = Contact::getById($activity['thread-completion'], ['url']);
330 $item['causer-link'] = $actor['url'];
331 $item['causer-id'] = $activity['thread-completion'];
332 Logger::info('Use inherited actor as causer.', ['id' => $item['owner-id'], 'activity' => $activity['thread-completion'], 'owner' => $item['owner-link'], 'actor' => $actor['url']]);
334 // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
335 $item['causer-link'] = $item['owner-link'];
336 $item['causer-id'] = $item['owner-id'];
337 Logger::info('Use actor as causer.', ['id' => $item['owner-id'], 'actor' => $item['owner-link']]);
340 $item['owner-link'] = $item['author-link'];
341 $item['owner-id'] = $item['author-id'];
343 $actor = APContact::getByURL($item['owner-link'], false);
344 $item['isForum'] = ($actor['type'] == 'Group');
347 $item['uri'] = $activity['id'];
349 $item['created'] = DateTimeFormat::utc($activity['published']);
350 $item['edited'] = DateTimeFormat::utc($activity['updated']);
351 $guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
352 $item['guid'] = $activity['diaspora:guid'] ?: $guid;
354 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
355 if (empty($item['uri-id'])) {
356 Logger::warning('Unable to get a uri-id for an item uri', ['uri' => $item['uri'], 'guid' => $item['guid']]);
360 $item = self::processContent($activity, $item);
362 Logger::info('Message was not processed');
366 $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
368 $item = self::constructAttachList($activity, $item);
376 * @param array $activity
377 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
378 * @throws \ImagickException
380 public static function deleteItem($activity)
382 $owner = Contact::getIdForURL($activity['actor']);
384 Logger::info('Deleting item', ['object' => $activity['object_id'], 'owner' => $owner]);
385 Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
389 * Prepare the item array for an activity
391 * @param array $activity Activity array
392 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
393 * @throws \ImagickException
395 public static function addTag($activity)
397 if (empty($activity['object_content']) || empty($activity['object_id'])) {
401 foreach ($activity['receiver'] as $receiver) {
402 $item = Post::selectFirst(['id', 'uri-id', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
403 if (!DBA::isResult($item)) {
404 // We don't fetch missing content for this purpose
408 if (($item['author-link'] != $activity['actor']) && !$item['origin']) {
409 Logger::info('Not origin, not from the author, skipping update', ['id' => $item['id'], 'author' => $item['author-link'], 'actor' => $activity['actor']]);
413 Tag::store($item['uri-id'], Tag::HASHTAG, $activity['object_content'], $activity['object_id']);
414 Logger::info('Tagged item', ['id' => $item['id'], 'tag' => $activity['object_content'], 'uri' => $activity['target_id'], 'actor' => $activity['actor']]);
419 * Prepare the item array for an activity
421 * @param array $activity Activity array
422 * @param string $verb Activity verb
423 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
424 * @throws \ImagickException
426 public static function createActivity($activity, $verb)
428 $item = self::createItem($activity);
429 $item['verb'] = $verb;
430 $item['thr-parent'] = $activity['object_id'];
431 $item['gravity'] = GRAVITY_ACTIVITY;
432 $item['object-type'] = Activity\ObjectType::NOTE;
434 $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
436 self::postItem($activity, $item);
442 * @param array $activity Activity array
446 public static function createEvent($activity, $item)
448 $event['summary'] = HTML::toBBCode($activity['name']);
449 $event['desc'] = HTML::toBBCode($activity['content']);
450 $event['start'] = $activity['start-time'];
451 $event['finish'] = $activity['end-time'];
452 $event['nofinish'] = empty($event['finish']);
453 $event['location'] = $activity['location'];
454 $event['adjust'] = true;
455 $event['cid'] = $item['contact-id'];
456 $event['uid'] = $item['uid'];
457 $event['uri'] = $item['uri'];
458 $event['edited'] = $item['edited'];
459 $event['private'] = $item['private'];
460 $event['guid'] = $item['guid'];
461 $event['plink'] = $item['plink'];
462 $event['network'] = $item['network'];
463 $event['protocol'] = $item['protocol'];
464 $event['direction'] = $item['direction'];
465 $event['source'] = $item['source'];
467 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
468 $ev = DBA::selectFirst('event', ['id'], $condition);
469 if (DBA::isResult($ev)) {
470 $event['id'] = $ev['id'];
473 $event_id = Event::store($event);
474 Logger::info('Event was stored', ['id' => $event_id]);
478 * Process the content
480 * @param array $activity Activity array
482 * @return array|bool Returns the item array or false if there was an unexpected occurrence
485 private static function processContent($activity, $item)
487 if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) {
488 $item['title'] = Markdown::toBBCode($activity['name']);
489 $content = Markdown::toBBCode($activity['content']);
490 } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) {
491 $item['title'] = $activity['name'];
492 $content = $activity['content'];
494 // By default assume "text/html"
495 $item['title'] = HTML::toBBCode($activity['name']);
496 $content = HTML::toBBCode($activity['content']);
499 if (!empty($activity['languages'])) {
500 $item['language'] = self::processLanguages($activity['languages']);
503 if (!empty($activity['emojis'])) {
504 $content = self::replaceEmojis($content, $activity['emojis']);
507 $content = self::convertMentions($content);
509 if (!empty($activity['source'])) {
510 $item['body'] = $activity['source'];
511 $item['raw-body'] = $content;
513 if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
514 $item_private = !in_array(0, $activity['item_receiver']);
515 $parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
516 if (!DBA::isResult($parent)) {
517 Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
520 if ($item_private && ($parent['private'] != Item::PRIVATE)) {
521 Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
525 $content = self::removeImplicitMentionsFromBody($content, $parent);
527 $item['content-warning'] = HTML::toBBCode($activity['summary']);
528 $item['raw-body'] = $item['body'] = $content;
531 self::storeFromBody($item);
532 self::storeTags($item['uri-id'], $activity['tags']);
534 $item['location'] = $activity['location'];
536 if (!empty($activity['latitude']) && !empty($activity['longitude'])) {
537 $item['coord'] = $activity['latitude'] . ' ' . $activity['longitude'];
540 $item['app'] = $activity['generator'];
546 * Store hashtags and mentions
550 private static function storeFromBody(array $item)
552 // Make sure to delete all existing tags (can happen when called via the update functionality)
553 DBA::delete('post-tag', ['uri-id' => $item['uri-id']]);
555 Tag::storeFromBody($item['uri-id'], $item['body'], '@!');
559 * Generate a GUID out of an URL
561 * @param string $url message URL
562 * @return string with GUID
564 private static function getGUIDByURL(string $url)
566 $parsed = parse_url($url);
568 $host_hash = hash('crc32', $parsed['host']);
570 unset($parsed["scheme"]);
571 unset($parsed["host"]);
573 $path = implode("/", $parsed);
575 return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path);
579 * Creates an item post
581 * @param array $activity Activity data
582 * @param array $item item array
583 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
584 * @throws \ImagickException
586 public static function postItem(array $activity, array $item)
593 ksort($activity['receiver']);
595 foreach ($activity['receiver'] as $receiver) {
596 if ($receiver == -1) {
600 $item['uid'] = $receiver;
602 $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN;
604 case Receiver::TARGET_TO:
605 $item['post-type'] = Item::PT_TO;
607 case Receiver::TARGET_CC:
608 $item['post-type'] = Item::PT_CC;
610 case Receiver::TARGET_BTO:
611 $item['post-type'] = Item::PT_BTO;
613 case Receiver::TARGET_BCC:
614 $item['post-type'] = Item::PT_BCC;
616 case Receiver::TARGET_FOLLOWER:
617 $item['post-type'] = Item::PT_FOLLOWER;
619 case Receiver::TARGET_ANSWER:
620 $item['post-type'] = Item::PT_COMMENT;
622 case Receiver::TARGET_GLOBAL:
623 $item['post-type'] = Item::PT_GLOBAL;
626 $item['post-type'] = Item::PT_ARTICLE;
629 if (!empty($activity['from-relay'])) {
630 $item['post-type'] = Item::PT_RELAY;
631 } elseif (!empty($activity['thread-completion'])) {
632 $item['post-type'] = Item::PT_FETCHED;
635 if ($item['isForum'] ?? false) {
636 $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver);
638 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver);
641 if (($receiver != 0) && empty($item['contact-id'])) {
642 $item['contact-id'] = Contact::getIdForURL($activity['author']);
645 if (!empty($activity['directmessage'])) {
646 self::postMail($activity, $item);
650 if (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
651 $skip = !Contact::isSharingByURL($activity['author'], $receiver);
653 if ($skip && (($activity['type'] == 'as:Announce') || ($item['isForum'] ?? false))) {
654 $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
658 Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
662 Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
665 if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
666 self::createEvent($activity, $item);
669 $item_id = Item::insert($item);
671 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
673 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
676 if ($item['uid'] == 0) {
681 // Store send a follow request for every reshare - but only when the item had been stored
682 if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
683 $author = APContact::getByURL($item['owner-link'], false);
684 // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
685 if ($author['type'] != 'Group') {
686 Logger::info('Send follow request', ['uri' => $item['uri'], 'stored' => $stored, 'to' => $item['author-link']]);
687 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
693 * Store tags and mentions into the tag table
695 * @param integer $uriid
698 private static function storeTags(int $uriid, array $tags = null)
700 foreach ($tags as $tag) {
701 if (empty($tag['name']) || empty($tag['type']) || !in_array($tag['type'], ['Mention', 'Hashtag'])) {
705 $hash = substr($tag['name'], 0, 1);
707 if ($tag['type'] == 'Mention') {
708 if (in_array($hash, [Tag::TAG_CHARACTER[Tag::MENTION],
709 Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION],
710 Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION]])) {
711 $tag['name'] = substr($tag['name'], 1);
713 $type = Tag::IMPLICIT_MENTION;
715 if (!empty($tag['href'])) {
716 $apcontact = APContact::getByURL($tag['href']);
717 if (!empty($apcontact['name']) || !empty($apcontact['nick'])) {
718 $tag['name'] = $apcontact['name'] ?: $apcontact['nick'];
721 } elseif ($tag['type'] == 'Hashtag') {
722 if ($hash == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
723 $tag['name'] = substr($tag['name'], 1);
725 $type = Tag::HASHTAG;
728 if (empty($tag['name'])) {
732 Tag::store($uriid, $type, $tag['name'], $tag['href']);
737 * Creates an mail post
739 * @param array $activity Activity data
740 * @param array $item item array
741 * @return int|bool New mail table row id or false on error
742 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
744 private static function postMail($activity, $item)
746 if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
747 Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
751 Logger::info('Direct Message', $item);
754 $msg['uid'] = $item['uid'];
756 $msg['contact-id'] = $item['contact-id'];
758 $contact = Contact::getById($item['contact-id'], ['name', 'url', 'photo']);
759 $msg['from-name'] = $contact['name'];
760 $msg['from-url'] = $contact['url'];
761 $msg['from-photo'] = $contact['photo'];
763 $msg['uri'] = $item['uri'];
764 $msg['created'] = $item['created'];
766 $parent = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uri' => $item['thr-parent']]);
767 if (DBA::isResult($parent)) {
768 $msg['parent-uri'] = $parent['parent-uri'];
769 $msg['title'] = $parent['title'];
771 $msg['parent-uri'] = $item['thr-parent'];
773 if (!empty($item['title'])) {
774 $msg['title'] = $item['title'];
775 } elseif (!empty($item['content-warning'])) {
776 $msg['title'] = $item['content-warning'];
778 // Trying to generate a title out of the body
779 $title = $item['body'];
781 while (preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $title, $matches)) {
782 $title = $matches[3];
785 $title = trim(HTML::toPlaintext(BBCode::convert($title, false, BBCode::API, true), 0));
787 if (strlen($title) > 20) {
788 $title = substr($title, 0, 20) . '...';
791 $msg['title'] = $title;
794 $msg['body'] = $item['body'];
796 return Mail::insert($msg);
800 * Fetches missing posts
802 * @param string $url message URL
803 * @param array $child activity array with the child of this message
804 * @param string $relay_actor Relay actor
805 * @return string fetched message URL
806 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
808 public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '')
810 if (!empty($child['receiver'])) {
811 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
816 $object = ActivityPub::fetchContent($url, $uid);
817 if (empty($object)) {
818 Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
822 if (empty($object['id'])) {
823 Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
827 if (!empty($object['actor'])) {
828 $object_actor = $object['actor'];
829 } elseif (!empty($object['attributedTo'])) {
830 $object_actor = $object['attributedTo'];
831 if (is_array($object_actor)) {
832 $compacted = JsonLD::compact($object);
833 $object_actor = JsonLD::fetchElement($compacted, 'as:attributedTo', '@id');
840 $signer = [$object_actor];
842 if (!empty($child['author'])) {
843 $actor = $child['author'];
846 $actor = $object_actor;
849 if (!empty($object['published'])) {
850 $published = $object['published'];
851 } elseif (!empty($child['published'])) {
852 $published = $child['published'];
854 $published = DateTimeFormat::utcNow();
858 $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
859 unset($object['@context']);
860 $activity['id'] = $object['id'];
861 $activity['to'] = $object['to'] ?? [];
862 $activity['cc'] = $object['cc'] ?? [];
863 $activity['actor'] = $actor;
864 $activity['object'] = $object;
865 $activity['published'] = $published;
866 $activity['type'] = 'Create';
868 $ldactivity = JsonLD::compact($activity);
870 if (!empty($relay_actor)) {
871 $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor);
872 } elseif (!empty($child['thread-completion'])) {
873 $ldactivity['thread-completion'] = $child['thread-completion'];
875 $ldactivity['thread-completion'] = Contact::getIdForURL($actor);
878 if (!empty($relay_actor) && !self::acceptIncomingMessage($ldactivity, $object['id'])) {
882 ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer);
884 Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
886 return $activity['id'];
890 * Test if incoming relay messages should be accepted
892 * @param array $activity activity array
893 * @param string $id object ID
894 * @return boolean true if message is accepted
896 private static function acceptIncomingMessage(array $activity, string $id)
898 if (empty($activity['as:object'])) {
899 Logger::info('No object field in activity - accepted', ['id' => $id]);
903 $replyto = JsonLD::fetchElement($activity['as:object'], 'as:inReplyTo', '@id');
904 $uriid = ItemURI::getIdByURI($replyto);
905 if (Post::exists(['uri-id' => $uriid])) {
906 Logger::info('Post is a reply to an existing post - accepted', ['id' => $id, 'uri-id' => $uriid, 'replyto' => $replyto]);
910 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
911 $authorid = Contact::getIdForURL($attributed_to);
913 $body = HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value'));
916 $tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
918 foreach ($tags as $tag) {
919 if ($tag['type'] != 'Hashtag') {
922 $messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
926 return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB);
930 * perform a "follow" request
932 * @param array $activity
933 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
934 * @throws \ImagickException
936 public static function followUser($activity)
938 $uid = User::getIdForURL($activity['object_id']);
943 $owner = User::getOwnerDataById($uid);
948 $cid = Contact::getIdForURL($activity['actor'], $uid);
950 self::switchContact($cid);
951 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
954 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
955 'author-link' => $activity['actor']];
957 // Ensure that the contact has got the right network type
958 self::switchContact($item['author-id']);
960 $result = Contact::addRelationship($owner, [], $item, false, $activity['content'] ?? '');
961 if ($result === true) {
962 ActivityPub\Transmitter::sendContactAccept($item['author-link'], $activity['id'], $owner['uid']);
965 $cid = Contact::getIdForURL($activity['actor'], $uid);
970 if (empty($contact)) {
971 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
974 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
978 * Update the given profile
980 * @param array $activity
983 public static function updatePerson($activity)
985 if (empty($activity['object_id'])) {
989 Logger::info('Updating profile', ['object' => $activity['object_id']]);
990 Contact::updateFromProbeByURL($activity['object_id']);
994 * Delete the given profile
996 * @param array $activity
997 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
999 public static function deletePerson($activity)
1001 if (empty($activity['object_id']) || empty($activity['actor'])) {
1002 Logger::info('Empty object id or actor.');
1006 if ($activity['object_id'] != $activity['actor']) {
1007 Logger::info('Object id does not match actor.');
1011 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
1012 while ($contact = DBA::fetch($contacts)) {
1013 Contact::remove($contact['id']);
1015 DBA::close($contacts);
1017 Logger::info('Deleted contact', ['object' => $activity['object_id']]);
1021 * Accept a follow request
1023 * @param array $activity
1024 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1025 * @throws \ImagickException
1027 public static function acceptFollowUser($activity)
1029 $uid = User::getIdForURL($activity['object_actor']);
1034 $cid = Contact::getIdForURL($activity['actor'], $uid);
1036 Logger::info('No contact found', ['actor' => $activity['actor']]);
1040 self::switchContact($cid);
1042 $fields = ['pending' => false];
1044 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1045 if ($contact['rel'] == Contact::FOLLOWER) {
1046 $fields['rel'] = Contact::FRIEND;
1049 $condition = ['id' => $cid];
1050 DBA::update('contact', $fields, $condition);
1051 Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]);
1055 * Reject a follow request
1057 * @param array $activity
1058 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1059 * @throws \ImagickException
1061 public static function rejectFollowUser($activity)
1063 $uid = User::getIdForURL($activity['object_actor']);
1068 $cid = Contact::getIdForURL($activity['actor'], $uid);
1070 Logger::info('No contact found', ['actor' => $activity['actor']]);
1074 self::switchContact($cid);
1076 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING])) {
1077 Contact::remove($cid);
1078 Logger::info('Rejected contact request - contact removed', ['contact' => $cid, 'user' => $uid]);
1080 Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]);
1085 * Undo activity like "like" or "dislike"
1087 * @param array $activity
1088 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1089 * @throws \ImagickException
1091 public static function undoActivity($activity)
1093 if (empty($activity['object_id'])) {
1097 if (empty($activity['object_actor'])) {
1101 $author_id = Contact::getIdForURL($activity['object_actor']);
1102 if (empty($author_id)) {
1106 Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1110 * Activity to remove a follower
1112 * @param array $activity
1113 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1114 * @throws \ImagickException
1116 public static function undoFollowUser($activity)
1118 $uid = User::getIdForURL($activity['object_object']);
1123 $owner = User::getOwnerDataById($uid);
1124 if (empty($owner)) {
1128 $cid = Contact::getIdForURL($activity['actor'], $uid);
1130 Logger::info('No contact found', ['actor' => $activity['actor']]);
1134 self::switchContact($cid);
1136 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1137 if (!DBA::isResult($contact)) {
1141 Contact::removeFollower($owner, $contact);
1142 Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]);
1146 * Switches a contact to AP if needed
1148 * @param integer $cid Contact ID
1149 * @throws \Exception
1151 private static function switchContact($cid)
1153 $contact = DBA::selectFirst('contact', ['network', 'url'], ['id' => $cid]);
1154 if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN]) || Contact::isLocal($contact['url'])) {
1158 Logger::info('Change existing contact', ['cid' => $cid, 'previous' => $contact['network']]);
1159 Contact::updateFromProbe($cid);
1163 * Collects implicit mentions like:
1164 * - the author of the parent item
1165 * - all the mentioned conversants in the parent item
1167 * @param array $parent Item array with at least ['id', 'author-link', 'alias']
1169 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1171 private static function getImplicitMentionList(array $parent)
1173 $parent_terms = Tag::getByURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1175 $parent_author = Contact::getByURL($parent['author-link'], false, ['url', 'nurl', 'alias']);
1177 $implicit_mentions = [];
1178 if (empty($parent_author['url'])) {
1179 Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'parent-id' => $parent['id']]);
1181 $implicit_mentions[] = $parent_author['url'];
1182 $implicit_mentions[] = $parent_author['nurl'];
1183 $implicit_mentions[] = $parent_author['alias'];
1186 if (!empty($parent['alias'])) {
1187 $implicit_mentions[] = $parent['alias'];
1190 foreach ($parent_terms as $term) {
1191 $contact = Contact::getByURL($term['url'], false, ['url', 'nurl', 'alias']);
1192 if (!empty($contact['url'])) {
1193 $implicit_mentions[] = $contact['url'];
1194 $implicit_mentions[] = $contact['nurl'];
1195 $implicit_mentions[] = $contact['alias'];
1199 return $implicit_mentions;
1203 * Strips from the body prepended implicit mentions
1205 * @param string $body
1206 * @param array $parent
1209 private static function removeImplicitMentionsFromBody(string $body, array $parent)
1211 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1215 $potential_mentions = self::getImplicitMentionList($parent);
1217 $kept_mentions = [];
1219 // Extract one prepended mention at a time from the body
1220 while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
1221 if (!in_array($matches[2], $potential_mentions)) {
1222 $kept_mentions[] = $matches[1];
1225 $body = $matches[3];
1228 // Re-appending the kept mentions to the body after extraction
1229 $kept_mentions[] = $body;
1231 return implode('', $kept_mentions);