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\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Contact;
14 use Friendica\Model\APContact;
15 use Friendica\Model\Item;
16 use Friendica\Model\Event;
17 use Friendica\Model\Term;
18 use Friendica\Model\User;
19 use Friendica\Model\Mail;
20 use Friendica\Protocol\ActivityPub;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\JsonLD;
23 use Friendica\Util\Strings;
26 * ActivityPub Processor Protocol class
31 * Converts mentions from Pleroma into the Friendica format
35 * @return string converted body
37 private static function convertMentions($body)
39 $URLSearchString = "^\[\]";
40 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
46 * Replaces emojis in the body
48 * @param array $emojis
51 * @return string with replaced emojis
53 private static function replaceEmojis($body, array $emojis)
55 foreach ($emojis as $emoji) {
56 $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
57 $body = str_replace($emoji['name'], $replace, $body);
63 * Constructs a string with tags for a given tag array
66 * @param boolean $sensitive
67 * @return string with tags
69 private static function constructTagString(array $tags = null, $sensitive = false)
76 foreach ($tags as $tag) {
77 if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) {
78 if (!empty($tag_text)) {
82 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
86 /// @todo add nsfw for $sensitive
92 * Add attachment data to the item array
94 * @param array $attachments
96 * @param boolean $no_images
100 private static function constructAttachList($attachments, $item, $no_images)
102 if (empty($attachments)) {
106 foreach ($attachments as $attach) {
107 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
108 if ($filetype == 'image') {
113 $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
115 if (!empty($item["attach"])) {
116 $item["attach"] .= ',';
118 $item["attach"] = '';
120 if (!isset($attach['length'])) {
121 $attach['length'] = "0";
123 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
133 * @param array $activity Activity array
134 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136 public static function updateItem($activity)
138 $item = Item::selectFirst(['uri', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
139 if (!DBA::isResult($item)) {
140 Logger::warning('Unknown item', ['uri' => $activity['id']]);
144 $item['changed'] = DateTimeFormat::utcNow();
145 $item['edited'] = DateTimeFormat::utc($activity['updated']);
147 $item = self::processContent($activity, $item);
152 Item::update($item, ['uri' => $activity['id']]);
156 * Prepares data for a message
158 * @param array $activity Activity array
159 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160 * @throws \ImagickException
162 public static function createItem($activity)
165 $item['verb'] = ACTIVITY_POST;
166 $item['thr-parent'] = $activity['reply-to-id'];
168 if ($activity['reply-to-id'] == $activity['id']) {
169 $item['gravity'] = GRAVITY_PARENT;
170 $item['object-type'] = ACTIVITY_OBJ_NOTE;
172 $item['gravity'] = GRAVITY_COMMENT;
173 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
176 if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
177 Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
178 self::fetchMissingActivity($activity['reply-to-id'], $activity);
181 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
183 self::postItem($activity, $item);
189 * @param array $activity
190 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191 * @throws \ImagickException
193 public static function deleteItem($activity)
195 $owner = Contact::getIdForURL($activity['actor']);
197 Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
198 Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
202 * Prepare the item array for an activity
204 * @param array $activity Activity array
205 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
206 * @throws \ImagickException
208 public static function addTag($activity)
210 if (empty($activity['object_content']) || empty($activity['object_id'])) {
214 foreach ($activity['receiver'] as $receiver) {
215 $item = Item::selectFirst(['id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
216 if (!DBA::isResult($item)) {
217 // We don't fetch missing content for this purpose
221 if (($item['author-link'] != $activity['actor']) && !$item['origin']) {
222 Logger::info('Not origin, not from the author, skipping update', ['id' => $item['id'], 'author' => $item['author-link'], 'actor' => $activity['actor']]);
227 // - Check if "blocktag" is set
228 // - Check if actor is a contact
230 if (!stristr($item['tag'], trim($activity['object_content']))) {
231 $tag = $item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . $activity['object_id'] . ']'. $activity['object_content'] . '[/url]';
232 Item::update(['tag' => $tag], ['id' => $item['id']]);
233 Logger::info('Tagged item', ['id' => $item['id'], 'tag' => $activity['object_content'], 'uri' => $activity['target_id'], 'actor' => $activity['actor']]);
239 * Prepare the item array for an activity
241 * @param array $activity Activity array
242 * @param string $verb Activity verb
243 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
244 * @throws \ImagickException
246 public static function createActivity($activity, $verb)
249 $item['verb'] = $verb;
250 $item['thr-parent'] = $activity['object_id'];
251 $item['gravity'] = GRAVITY_ACTIVITY;
252 $item['object-type'] = ACTIVITY_OBJ_NOTE;
254 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:like', '');
256 self::postItem($activity, $item);
262 * @param array $activity Activity array
266 public static function createEvent($activity, $item)
268 $event['summary'] = HTML::toBBCode($activity['name']);
269 $event['desc'] = HTML::toBBCode($activity['content']);
270 $event['start'] = $activity['start-time'];
271 $event['finish'] = $activity['end-time'];
272 $event['nofinish'] = empty($event['finish']);
273 $event['location'] = $activity['location'];
274 $event['adjust'] = true;
275 $event['cid'] = $item['contact-id'];
276 $event['uid'] = $item['uid'];
277 $event['uri'] = $item['uri'];
278 $event['edited'] = $item['edited'];
279 $event['private'] = $item['private'];
280 $event['guid'] = $item['guid'];
281 $event['plink'] = $item['plink'];
283 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
284 $ev = DBA::selectFirst('event', ['id'], $condition);
285 if (DBA::isResult($ev)) {
286 $event['id'] = $ev['id'];
289 $event_id = Event::store($event);
290 Logger::log('Event '.$event_id.' was stored', Logger::DEBUG);
294 * Process the content
296 * @param array $activity Activity array
298 * @return array|bool Returns the item array or false if there was an unexpected occurrence
301 private static function processContent($activity, $item)
303 $item['title'] = HTML::toBBCode($activity['name']);
305 if (!empty($activity['source'])) {
306 $item['body'] = $activity['source'];
308 $content = HTML::toBBCode($activity['content']);
310 if (!empty($activity['emojis'])) {
311 $content = self::replaceEmojis($content, $activity['emojis']);
314 $content = self::convertMentions($content);
316 if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
317 $item_private = !in_array(0, $activity['item_receiver']);
318 $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
319 if (!DBA::isResult($parent)) {
320 Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
323 if ($item_private && !$parent['private']) {
324 Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
328 $potential_implicit_mentions = self::getImplicitMentionList($parent);
329 $content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions);
330 $activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions);
332 $item['content-warning'] = HTML::toBBCode($activity['summary']);
333 $item['body'] = $content;
335 if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
336 $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
340 $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']);
342 $item['location'] = $activity['location'];
344 if (!empty($item['latitude']) && !empty($item['longitude'])) {
345 $item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
348 $item['app'] = $activity['generator'];
354 * Creates an item post
356 * @param array $activity Activity data
357 * @param array $item item array
358 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
359 * @throws \ImagickException
361 private static function postItem($activity, $item)
363 /// @todo What to do with $activity['context']?
364 if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) {
365 Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
369 $item['network'] = Protocol::ACTIVITYPUB;
370 $item['private'] = !in_array(0, $activity['receiver']);
371 $item['author-link'] = $activity['author'];
372 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
373 $item['owner-link'] = $activity['actor'];
374 $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
378 if (!empty($activity['thread-completion'])) {
379 // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
380 $item['causer-link'] = $item['owner-link'];
381 $item['causer-id'] = $item['owner-id'];
383 Logger::info('Ignoring actor because of thread completion.', ['actor' => $item['owner-link']]);
384 $item['owner-link'] = $item['author-link'];
385 $item['owner-id'] = $item['author-id'];
387 $actor = APContact::getByURL($item['owner-link'], false);
388 $isForum = ($actor['type'] == 'Group');
391 $item['uri'] = $activity['id'];
393 $item['created'] = DateTimeFormat::utc($activity['published']);
394 $item['edited'] = DateTimeFormat::utc($activity['updated']);
395 $item['guid'] = $activity['diaspora:guid'];
397 $item = self::processContent($activity, $item);
402 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
404 $item = self::constructAttachList($activity['attachments'], $item, !empty($activity['source']));
408 foreach ($activity['receiver'] as $receiver) {
409 $item['uid'] = $receiver;
412 $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver, true);
415 if (empty($item['contact-id'])) {
416 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
419 if (($receiver != 0) && empty($item['contact-id'])) {
420 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
423 if (!empty($activity['directmessage'])) {
424 self::postMail($activity, $item);
428 if (PConfig::get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
429 $skip = !Contact::isSharingByURL($activity['author'], $receiver);
431 if ($skip && (($activity['type'] == 'as:Announce') || $isForum)) {
432 $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
436 Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
440 Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
443 if ($activity['object_type'] == 'as:Event') {
444 self::createEvent($activity, $item);
447 $item_id = Item::insert($item);
449 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
451 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
454 if ($item['uid'] == 0) {
459 // Store send a follow request for every reshare - but only when the item had been stored
460 if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
461 $author = APContact::getByURL($item['owner-link'], false);
462 // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
463 if ($author['type'] != 'Group') {
464 Logger::log('Send follow request for ' . $item['uri'] . ' (' . $stored . ') to ' . $item['author-link'], Logger::DEBUG);
465 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
471 * Creates an mail post
473 * @param array $activity Activity data
474 * @param array $item item array
475 * @return int|bool New mail table row id or false on error
476 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
478 private static function postMail($activity, $item)
480 if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
481 Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
485 Logger::info('Direct Message', $item);
488 $msg['uid'] = $item['uid'];
490 $msg['contact-id'] = $item['contact-id'];
492 $contact = Contact::getById($item['contact-id'], ['name', 'url', 'photo']);
493 $msg['from-name'] = $contact['name'];
494 $msg['from-url'] = $contact['url'];
495 $msg['from-photo'] = $contact['photo'];
497 $msg['uri'] = $item['uri'];
498 $msg['created'] = $item['created'];
500 $parent = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uri' => $item['thr-parent']]);
501 if (DBA::isResult($parent)) {
502 $msg['parent-uri'] = $parent['parent-uri'];
503 $msg['title'] = $parent['title'];
505 $msg['parent-uri'] = $item['thr-parent'];
507 if (!empty($item['title'])) {
508 $msg['title'] = $item['title'];
509 } elseif (!empty($item['content-warning'])) {
510 $msg['title'] = $item['content-warning'];
512 // Trying to generate a title out of the body
513 $title = $item['body'];
515 while (preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $title, $matches)) {
516 $title = $matches[3];
519 $title = trim(HTML::toPlaintext(BBCode::convert($title, false, 2, true), 0));
521 if (strlen($title) > 20) {
522 $title = substr($title, 0, 20) . '...';
525 $msg['title'] = $title;
528 $msg['body'] = $item['body'];
530 return Mail::insert($msg);
534 * Fetches missing posts
538 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
540 public static function fetchMissingActivity($url, $child = [])
542 if (!empty($child['receiver'])) {
543 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
548 $object = ActivityPub::fetchContent($url, $uid);
549 if (empty($object)) {
550 Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
554 if (empty($object['id'])) {
555 Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
559 if (!empty($child['author'])) {
560 $actor = $child['author'];
561 } elseif (!empty($object['actor'])) {
562 $actor = $object['actor'];
563 } elseif (!empty($object['attributedTo'])) {
564 $actor = $object['attributedTo'];
570 if (!empty($object['published'])) {
571 $published = $object['published'];
572 } elseif (!empty($child['published'])) {
573 $published = $child['published'];
575 $published = DateTimeFormat::utcNow();
579 $activity['@context'] = $object['@context'];
580 unset($object['@context']);
581 $activity['id'] = $object['id'];
582 $activity['to'] = defaults($object, 'to', []);
583 $activity['cc'] = defaults($object, 'cc', []);
584 $activity['actor'] = $actor;
585 $activity['object'] = $object;
586 $activity['published'] = $published;
587 $activity['type'] = 'Create';
589 $ldactivity = JsonLD::compact($activity);
591 $ldactivity['thread-completion'] = true;
593 ActivityPub\Receiver::processActivity($ldactivity);
594 Logger::log('Activity ' . $url . ' had been fetched and processed.');
598 * perform a "follow" request
600 * @param array $activity
601 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
602 * @throws \ImagickException
604 public static function followUser($activity)
606 $uid = User::getIdForURL($activity['object_id']);
611 $owner = User::getOwnerDataById($uid);
613 $cid = Contact::getIdForURL($activity['actor'], $uid);
615 self::switchContact($cid);
616 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
617 $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
622 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
623 'author-link' => $activity['actor']];
625 $note = Strings::escapeTags(trim(defaults($activity, 'content', '')));
627 // Ensure that the contact has got the right network type
628 self::switchContact($item['author-id']);
630 $result = Contact::addRelationship($owner, $contact, $item, false, $note);
631 if ($result === true) {
632 ActivityPub\Transmitter::sendContactAccept($item['author-link'], $item['author-id'], $owner['uid']);
635 $cid = Contact::getIdForURL($activity['actor'], $uid);
640 if (empty($contact)) {
641 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
644 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
648 * Update the given profile
650 * @param array $activity
653 public static function updatePerson($activity)
655 if (empty($activity['object_id'])) {
659 Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
660 Contact::updateFromProbeByURL($activity['object_id'], true);
664 * Delete the given profile
666 * @param array $activity
667 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
669 public static function deletePerson($activity)
671 if (empty($activity['object_id']) || empty($activity['actor'])) {
672 Logger::log('Empty object id or actor.', Logger::DEBUG);
676 if ($activity['object_id'] != $activity['actor']) {
677 Logger::log('Object id does not match actor.', Logger::DEBUG);
681 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
682 while ($contact = DBA::fetch($contacts)) {
683 Contact::remove($contact['id']);
685 DBA::close($contacts);
687 Logger::log('Deleted contact ' . $activity['object_id'], Logger::DEBUG);
691 * Accept a follow request
693 * @param array $activity
694 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
695 * @throws \ImagickException
697 public static function acceptFollowUser($activity)
699 $uid = User::getIdForURL($activity['object_actor']);
704 $cid = Contact::getIdForURL($activity['actor'], $uid);
706 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
710 self::switchContact($cid);
712 $fields = ['pending' => false];
714 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
715 if ($contact['rel'] == Contact::FOLLOWER) {
716 $fields['rel'] = Contact::FRIEND;
719 $condition = ['id' => $cid];
720 DBA::update('contact', $fields, $condition);
721 Logger::log('Accept contact request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
725 * Reject a follow request
727 * @param array $activity
728 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
729 * @throws \ImagickException
731 public static function rejectFollowUser($activity)
733 $uid = User::getIdForURL($activity['object_actor']);
738 $cid = Contact::getIdForURL($activity['actor'], $uid);
740 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
744 self::switchContact($cid);
746 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING])) {
747 Contact::remove($cid);
748 Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
750 Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', Logger::DEBUG);
755 * Undo activity like "like" or "dislike"
757 * @param array $activity
758 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
759 * @throws \ImagickException
761 public static function undoActivity($activity)
763 if (empty($activity['object_id'])) {
767 if (empty($activity['object_actor'])) {
771 $author_id = Contact::getIdForURL($activity['object_actor']);
772 if (empty($author_id)) {
776 Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
780 * Activity to remove a follower
782 * @param array $activity
783 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
784 * @throws \ImagickException
786 public static function undoFollowUser($activity)
788 $uid = User::getIdForURL($activity['object_object']);
793 $owner = User::getOwnerDataById($uid);
795 $cid = Contact::getIdForURL($activity['actor'], $uid);
797 Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
801 self::switchContact($cid);
803 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
804 if (!DBA::isResult($contact)) {
808 Contact::removeFollower($owner, $contact);
809 Logger::log('Undo following request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
813 * Switches a contact to AP if needed
815 * @param integer $cid Contact ID
818 private static function switchContact($cid)
820 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
821 if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
825 Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
826 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
830 * Collects implicit mentions like:
831 * - the author of the parent item
832 * - all the mentioned conversants in the parent item
834 * @param array $parent Item array with at least ['id', 'author-link', 'alias']
836 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
838 private static function getImplicitMentionList(array $parent)
840 if (Config::get('system', 'disable_implicit_mentions')) {
844 $parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
846 $parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
848 $implicit_mentions = [];
849 if (empty($parent_author)) {
850 Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'item-id' => $parent['id']]);
852 $implicit_mentions[] = $parent_author['url'];
853 $implicit_mentions[] = $parent_author['nurl'];
854 $implicit_mentions[] = $parent_author['alias'];
857 if (!empty($parent['alias'])) {
858 $implicit_mentions[] = $parent['alias'];
861 foreach ($parent_terms as $term) {
862 $contact = Contact::getDetailsByURL($term['url'], 0);
863 if (!empty($contact)) {
864 $implicit_mentions[] = $contact['url'];
865 $implicit_mentions[] = $contact['nurl'];
866 $implicit_mentions[] = $contact['alias'];
870 return $implicit_mentions;
874 * Strips from the body prepended implicit mentions
876 * @param string $body
877 * @param array $potential_mentions
880 private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
882 if (Config::get('system', 'disable_implicit_mentions')) {
888 // Extract one prepended mention at a time from the body
889 while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
890 if (!in_array($matches[2], $potential_mentions) ) {
891 $kept_mentions[] = $matches[1];
897 // Re-appending the kept mentions to the body after extraction
898 $kept_mentions[] = $body;
900 return implode('', $kept_mentions);
903 private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
905 if (Config::get('system', 'disable_implicit_mentions')) {
906 return $activity_tags;
909 foreach ($activity_tags as $index => $tag) {
910 if (in_array($tag['href'], $potential_mentions)) {
911 $activity_tags[$index]['name'] = preg_replace(
912 '/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/',
913 Term::TAG_CHARACTER[Term::IMPLICIT_MENTION],
914 $activity_tags[$index]['name'],
920 return $activity_tags;