3 * @file src/Protocol/ActivityPub/Processor.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\Database\DBA;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Content\Text\HTML;
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Protocol;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Event;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Model\Mail;
21 use Friendica\Protocol\ActivityPub;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\JsonLD;
24 use Friendica\Util\Strings;
27 * ActivityPub Processor Protocol class
32 * Converts mentions from Pleroma into the Friendica format
36 * @return string converted body
38 private static function convertMentions($body)
40 $URLSearchString = "^\[\]";
41 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
47 * Replaces emojis in the body
49 * @param array $emojis
52 * @return string with replaced emojis
54 private static function replaceEmojis($body, array $emojis)
56 foreach ($emojis as $emoji) {
57 $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
58 $body = str_replace($emoji['name'], $replace, $body);
64 * Constructs a string with tags for a given tag array
67 * @param boolean $sensitive
68 * @return string with tags
70 private static function constructTagString(array $tags = null, $sensitive = false)
77 foreach ($tags as $tag) {
78 if (in_array($tag['type'] ?? '', ['Mention', 'Hashtag'])) {
79 if (!empty($tag_text)) {
83 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
87 /// @todo add nsfw for $sensitive
93 * Add attachment data to the item array
95 * @param array $attachments
97 * @param boolean $no_images
101 private static function constructAttachList($attachments, $item, $no_images)
103 if (empty($attachments)) {
107 foreach ($attachments as $attach) {
108 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
109 if ($filetype == 'image') {
114 if (empty($attach['name'])) {
115 $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
117 $item['body'] .= "\n[img=" . $attach['url'] . ']' . $attach['name'] . '[/img]';
120 if (!empty($item["attach"])) {
121 $item["attach"] .= ',';
123 $item["attach"] = '';
125 if (!isset($attach['length'])) {
126 $attach['length'] = "0";
128 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.($attach['name'] ?? '') .'"[/attach]';
138 * @param array $activity Activity array
139 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
141 public static function updateItem($activity)
143 $item = Item::selectFirst(['uri', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
144 if (!DBA::isResult($item)) {
145 Logger::warning('Unknown item', ['uri' => $activity['id']]);
149 $item['changed'] = DateTimeFormat::utcNow();
150 $item['edited'] = DateTimeFormat::utc($activity['updated']);
152 $item = self::processContent($activity, $item);
157 Item::update($item, ['uri' => $activity['id']]);
161 * Prepares data for a message
163 * @param array $activity Activity array
164 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
165 * @throws \ImagickException
167 public static function createItem($activity)
170 $item['verb'] = ACTIVITY_POST;
171 $item['thr-parent'] = $activity['reply-to-id'];
173 if ($activity['reply-to-id'] == $activity['id']) {
174 $item['gravity'] = GRAVITY_PARENT;
175 $item['object-type'] = ACTIVITY_OBJ_NOTE;
177 $item['gravity'] = GRAVITY_COMMENT;
178 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
181 if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
182 Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
183 self::fetchMissingActivity($activity['reply-to-id'], $activity);
186 $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
188 self::postItem($activity, $item);
194 * @param array $activity
195 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
196 * @throws \ImagickException
198 public static function deleteItem($activity)
200 $owner = Contact::getIdForURL($activity['actor']);
202 Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
203 Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
207 * Prepare the item array for an activity
209 * @param array $activity Activity array
210 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
211 * @throws \ImagickException
213 public static function addTag($activity)
215 if (empty($activity['object_content']) || empty($activity['object_id'])) {
219 foreach ($activity['receiver'] as $receiver) {
220 $item = Item::selectFirst(['id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
221 if (!DBA::isResult($item)) {
222 // We don't fetch missing content for this purpose
226 if (($item['author-link'] != $activity['actor']) && !$item['origin']) {
227 Logger::info('Not origin, not from the author, skipping update', ['id' => $item['id'], 'author' => $item['author-link'], 'actor' => $activity['actor']]);
232 // - Check if "blocktag" is set
233 // - Check if actor is a contact
235 if (!stristr($item['tag'], trim($activity['object_content']))) {
236 $tag = $item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . $activity['object_id'] . ']'. $activity['object_content'] . '[/url]';
237 Item::update(['tag' => $tag], ['id' => $item['id']]);
238 Logger::info('Tagged item', ['id' => $item['id'], 'tag' => $activity['object_content'], 'uri' => $activity['target_id'], 'actor' => $activity['actor']]);
244 * Prepare the item array for an activity
246 * @param array $activity Activity array
247 * @param string $verb Activity verb
248 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
249 * @throws \ImagickException
251 public static function createActivity($activity, $verb)
254 $item['verb'] = $verb;
255 $item['thr-parent'] = $activity['object_id'];
256 $item['gravity'] = GRAVITY_ACTIVITY;
257 $item['object-type'] = ACTIVITY_OBJ_NOTE;
259 $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
261 self::postItem($activity, $item);
267 * @param array $activity Activity array
271 public static function createEvent($activity, $item)
273 $event['summary'] = HTML::toBBCode($activity['name']);
274 $event['desc'] = HTML::toBBCode($activity['content']);
275 $event['start'] = $activity['start-time'];
276 $event['finish'] = $activity['end-time'];
277 $event['nofinish'] = empty($event['finish']);
278 $event['location'] = $activity['location'];
279 $event['adjust'] = true;
280 $event['cid'] = $item['contact-id'];
281 $event['uid'] = $item['uid'];
282 $event['uri'] = $item['uri'];
283 $event['edited'] = $item['edited'];
284 $event['private'] = $item['private'];
285 $event['guid'] = $item['guid'];
286 $event['plink'] = $item['plink'];
288 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
289 $ev = DBA::selectFirst('event', ['id'], $condition);
290 if (DBA::isResult($ev)) {
291 $event['id'] = $ev['id'];
294 $event_id = Event::store($event);
295 Logger::log('Event '.$event_id.' was stored', Logger::DEBUG);
299 * Process the content
301 * @param array $activity Activity array
303 * @return array|bool Returns the item array or false if there was an unexpected occurrence
306 private static function processContent($activity, $item)
308 $item['title'] = HTML::toBBCode($activity['name']);
310 if (!empty($activity['source'])) {
311 $item['body'] = $activity['source'];
313 $content = HTML::toBBCode($activity['content']);
315 if (!empty($activity['emojis'])) {
316 $content = self::replaceEmojis($content, $activity['emojis']);
319 $content = self::convertMentions($content);
321 if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
322 $item_private = !in_array(0, $activity['item_receiver']);
323 $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
324 if (!DBA::isResult($parent)) {
325 Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
328 if ($item_private && !$parent['private']) {
329 Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
333 $potential_implicit_mentions = self::getImplicitMentionList($parent);
334 $content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions);
335 $activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions);
337 $item['content-warning'] = HTML::toBBCode($activity['summary']);
338 $item['body'] = $content;
340 if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
341 $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
345 $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']);
347 $item['location'] = $activity['location'];
349 if (!empty($item['latitude']) && !empty($item['longitude'])) {
350 $item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
353 $item['app'] = $activity['generator'];
359 * Creates an item post
361 * @param array $activity Activity data
362 * @param array $item item array
363 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
364 * @throws \ImagickException
366 private static function postItem($activity, $item)
368 /// @todo What to do with $activity['context']?
369 if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) {
370 Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
374 $item['network'] = Protocol::ACTIVITYPUB;
375 $item['private'] = !in_array(0, $activity['receiver']);
376 $item['author-link'] = $activity['author'];
377 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
378 $item['owner-link'] = $activity['actor'];
379 $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
383 if (!empty($activity['thread-completion'])) {
384 // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
385 $item['causer-link'] = $item['owner-link'];
386 $item['causer-id'] = $item['owner-id'];
388 Logger::info('Ignoring actor because of thread completion.', ['actor' => $item['owner-link']]);
389 $item['owner-link'] = $item['author-link'];
390 $item['owner-id'] = $item['author-id'];
392 $actor = APContact::getByURL($item['owner-link'], false);
393 $isForum = ($actor['type'] == 'Group');
396 $item['uri'] = $activity['id'];
398 $item['created'] = DateTimeFormat::utc($activity['published']);
399 $item['edited'] = DateTimeFormat::utc($activity['updated']);
400 $item['guid'] = $activity['diaspora:guid'];
402 $item = self::processContent($activity, $item);
407 $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
409 $item = self::constructAttachList($activity['attachments'], $item, !empty($activity['source']));
413 foreach ($activity['receiver'] as $receiver) {
414 $item['uid'] = $receiver;
417 $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver, true);
419 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
422 if (($receiver != 0) && empty($item['contact-id'])) {
423 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
426 if (!empty($activity['directmessage'])) {
427 self::postMail($activity, $item);
431 if (PConfig::get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
432 $skip = !Contact::isSharingByURL($activity['author'], $receiver);
434 if ($skip && (($activity['type'] == 'as:Announce') || $isForum)) {
435 $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
439 Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
443 Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
446 if ($activity['object_type'] == 'as:Event') {
447 self::createEvent($activity, $item);
450 $item_id = Item::insert($item);
452 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
454 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
457 if ($item['uid'] == 0) {
462 // Store send a follow request for every reshare - but only when the item had been stored
463 if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
464 $author = APContact::getByURL($item['owner-link'], false);
465 // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
466 if ($author['type'] != 'Group') {
467 Logger::log('Send follow request for ' . $item['uri'] . ' (' . $stored . ') to ' . $item['author-link'], Logger::DEBUG);
468 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
474 * Creates an mail post
476 * @param array $activity Activity data
477 * @param array $item item array
478 * @return int|bool New mail table row id or false on error
479 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
481 private static function postMail($activity, $item)
483 if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
484 Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
488 Logger::info('Direct Message', $item);
491 $msg['uid'] = $item['uid'];
493 $msg['contact-id'] = $item['contact-id'];
495 $contact = Contact::getById($item['contact-id'], ['name', 'url', 'photo']);
496 $msg['from-name'] = $contact['name'];
497 $msg['from-url'] = $contact['url'];
498 $msg['from-photo'] = $contact['photo'];
500 $msg['uri'] = $item['uri'];
501 $msg['created'] = $item['created'];
503 $parent = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uri' => $item['thr-parent']]);
504 if (DBA::isResult($parent)) {
505 $msg['parent-uri'] = $parent['parent-uri'];
506 $msg['title'] = $parent['title'];
508 $msg['parent-uri'] = $item['thr-parent'];
510 if (!empty($item['title'])) {
511 $msg['title'] = $item['title'];
512 } elseif (!empty($item['content-warning'])) {
513 $msg['title'] = $item['content-warning'];
515 // Trying to generate a title out of the body
516 $title = $item['body'];
518 while (preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $title, $matches)) {
519 $title = $matches[3];
522 $title = trim(HTML::toPlaintext(BBCode::convert($title, false, 2, true), 0));
524 if (strlen($title) > 20) {
525 $title = substr($title, 0, 20) . '...';
528 $msg['title'] = $title;
531 $msg['body'] = $item['body'];
533 return Mail::insert($msg);
537 * Fetches missing posts
539 * @param string $url message URL
540 * @param array $child activity array with the child of this message
541 * @return boolean success
542 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
544 public static function fetchMissingActivity($url, $child = [])
546 if (!empty($child['receiver'])) {
547 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
552 $object = ActivityPub::fetchContent($url, $uid);
553 if (empty($object)) {
554 Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
558 if (empty($object['id'])) {
559 Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
563 if (!empty($child['author'])) {
564 $actor = $child['author'];
565 } elseif (!empty($object['actor'])) {
566 $actor = $object['actor'];
567 } elseif (!empty($object['attributedTo'])) {
568 $actor = $object['attributedTo'];
574 if (!empty($object['published'])) {
575 $published = $object['published'];
576 } elseif (!empty($child['published'])) {
577 $published = $child['published'];
579 $published = DateTimeFormat::utcNow();
583 $activity['@context'] = $object['@context'];
584 unset($object['@context']);
585 $activity['id'] = $object['id'];
586 $activity['to'] = $object['to'] ?? [];
587 $activity['cc'] = $object['cc'] ?? [];
588 $activity['actor'] = $actor;
589 $activity['object'] = $object;
590 $activity['published'] = $published;
591 $activity['type'] = 'Create';
593 $ldactivity = JsonLD::compact($activity);
595 $ldactivity['thread-completion'] = true;
597 ActivityPub\Receiver::processActivity($ldactivity);
598 Logger::log('Activity ' . $url . ' had been fetched and processed.');
604 * perform a "follow" request
606 * @param array $activity
607 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
608 * @throws \ImagickException
610 public static function followUser($activity)
612 $uid = User::getIdForURL($activity['object_id']);
617 $owner = User::getOwnerDataById($uid);
619 $cid = Contact::getIdForURL($activity['actor'], $uid);
621 self::switchContact($cid);
622 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
623 $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
628 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
629 'author-link' => $activity['actor']];
631 $note = Strings::escapeTags(trim($activity['content'] ?? ''));
633 // Ensure that the contact has got the right network type
634 self::switchContact($item['author-id']);
636 $result = Contact::addRelationship($owner, $contact, $item, false, $note);
637 if ($result === true) {
638 ActivityPub\Transmitter::sendContactAccept($item['author-link'], $item['author-id'], $owner['uid']);
641 $cid = Contact::getIdForURL($activity['actor'], $uid);
646 if (empty($contact)) {
647 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
650 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
654 * Update the given profile
656 * @param array $activity
659 public static function updatePerson($activity)
661 if (empty($activity['object_id'])) {
665 Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
666 Contact::updateFromProbeByURL($activity['object_id'], true);
670 * Delete the given profile
672 * @param array $activity
673 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
675 public static function deletePerson($activity)
677 if (empty($activity['object_id']) || empty($activity['actor'])) {
678 Logger::log('Empty object id or actor.', Logger::DEBUG);
682 if ($activity['object_id'] != $activity['actor']) {
683 Logger::log('Object id does not match actor.', Logger::DEBUG);
687 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
688 while ($contact = DBA::fetch($contacts)) {
689 Contact::remove($contact['id']);
691 DBA::close($contacts);
693 Logger::log('Deleted contact ' . $activity['object_id'], Logger::DEBUG);
697 * Accept a follow request
699 * @param array $activity
700 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
701 * @throws \ImagickException
703 public static function acceptFollowUser($activity)
705 $uid = User::getIdForURL($activity['object_actor']);
710 $cid = Contact::getIdForURL($activity['actor'], $uid);
712 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
716 self::switchContact($cid);
718 $fields = ['pending' => false];
720 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
721 if ($contact['rel'] == Contact::FOLLOWER) {
722 $fields['rel'] = Contact::FRIEND;
725 $condition = ['id' => $cid];
726 DBA::update('contact', $fields, $condition);
727 Logger::log('Accept contact request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
731 * Reject a follow request
733 * @param array $activity
734 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
735 * @throws \ImagickException
737 public static function rejectFollowUser($activity)
739 $uid = User::getIdForURL($activity['object_actor']);
744 $cid = Contact::getIdForURL($activity['actor'], $uid);
746 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
750 self::switchContact($cid);
752 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING])) {
753 Contact::remove($cid);
754 Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
756 Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', Logger::DEBUG);
761 * Undo activity like "like" or "dislike"
763 * @param array $activity
764 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
765 * @throws \ImagickException
767 public static function undoActivity($activity)
769 if (empty($activity['object_id'])) {
773 if (empty($activity['object_actor'])) {
777 $author_id = Contact::getIdForURL($activity['object_actor']);
778 if (empty($author_id)) {
782 Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
786 * Activity to remove a follower
788 * @param array $activity
789 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
790 * @throws \ImagickException
792 public static function undoFollowUser($activity)
794 $uid = User::getIdForURL($activity['object_object']);
799 $owner = User::getOwnerDataById($uid);
801 $cid = Contact::getIdForURL($activity['actor'], $uid);
803 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
807 self::switchContact($cid);
809 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
810 if (!DBA::isResult($contact)) {
814 Contact::removeFollower($owner, $contact);
815 Logger::log('Undo following request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
819 * Switches a contact to AP if needed
821 * @param integer $cid Contact ID
824 private static function switchContact($cid)
826 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
827 if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
831 Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
832 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
836 * Collects implicit mentions like:
837 * - the author of the parent item
838 * - all the mentioned conversants in the parent item
840 * @param array $parent Item array with at least ['id', 'author-link', 'alias']
842 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
844 private static function getImplicitMentionList(array $parent)
846 if (Config::get('system', 'disable_implicit_mentions')) {
850 $parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
852 $parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
854 $implicit_mentions = [];
855 if (empty($parent_author)) {
856 Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'item-id' => $parent['id']]);
858 $implicit_mentions[] = $parent_author['url'];
859 $implicit_mentions[] = $parent_author['nurl'];
860 $implicit_mentions[] = $parent_author['alias'];
863 if (!empty($parent['alias'])) {
864 $implicit_mentions[] = $parent['alias'];
867 foreach ($parent_terms as $term) {
868 $contact = Contact::getDetailsByURL($term['url'], 0);
869 if (!empty($contact)) {
870 $implicit_mentions[] = $contact['url'];
871 $implicit_mentions[] = $contact['nurl'];
872 $implicit_mentions[] = $contact['alias'];
876 return $implicit_mentions;
880 * Strips from the body prepended implicit mentions
882 * @param string $body
883 * @param array $potential_mentions
886 private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
888 if (Config::get('system', 'disable_implicit_mentions')) {
894 // Extract one prepended mention at a time from the body
895 while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
896 if (!in_array($matches[2], $potential_mentions)) {
897 $kept_mentions[] = $matches[1];
903 // Re-appending the kept mentions to the body after extraction
904 $kept_mentions[] = $body;
906 return implode('', $kept_mentions);
909 private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
911 if (Config::get('system', 'disable_implicit_mentions')) {
912 return $activity_tags;
915 foreach ($activity_tags as $index => $tag) {
916 if (in_array($tag['href'], $potential_mentions)) {
917 $activity_tags[$index]['name'] = preg_replace(
918 '/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/',
919 Term::TAG_CHARACTER[Term::IMPLICIT_MENTION],
920 $activity_tags[$index]['name'],
926 return $activity_tags;