]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Processor.php
5899d7f25f0f913a83e56e81db4b3fdf64bfd8ab
[friendica.git] / src / Protocol / ActivityPub / Processor.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub/Processor.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
7 use Friendica\Database\DBA;
8 use Friendica\Content\Text\HTML;
9 use Friendica\Core\Config;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Model\Contact;
13 use Friendica\Model\APContact;
14 use Friendica\Model\Item;
15 use Friendica\Model\Event;
16 use Friendica\Model\Term;
17 use Friendica\Model\User;
18 use Friendica\Protocol\ActivityPub;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Util\JsonLD;
21 use Friendica\Util\Strings;
22
23 /**
24  * ActivityPub Processor Protocol class
25  */
26 class Processor
27 {
28         /**
29          * Converts mentions from Pleroma into the Friendica format
30          *
31          * @param string $body
32          *
33          * @return string converted body
34          */
35         private static function convertMentions($body)
36         {
37                 $URLSearchString = "^\[\]";
38                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
39
40                 return $body;
41         }
42
43         /**
44          * Replaces emojis in the body
45          *
46          * @param array $emojis
47          * @param string $body
48          *
49          * @return string with replaced emojis
50          */
51         public static function replaceEmojis($body, array $emojis)
52         {
53                 foreach ($emojis as $emoji) {
54                         $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
55                         $body = str_replace($emoji['name'], $replace, $body);
56                 }
57                 return $body;
58         }
59
60         /**
61          * Constructs a string with tags for a given tag array
62          *
63          * @param array   $tags
64          * @param boolean $sensitive
65          * @param array   $implicit_mentions List of profile URLs to skip
66          * @return string with tags
67          */
68         private static function constructTagString($tags, $sensitive, array $implicit_mentions)
69         {
70                 if (empty($tags)) {
71                         return '';
72                 }
73
74                 $tag_text = '';
75                 foreach ($tags as $tag) {
76                         if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag']) && !in_array($tag['href'], $implicit_mentions)) {
77                                 if (!empty($tag_text)) {
78                                         $tag_text .= ',';
79                                 }
80
81                                 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
82                         }
83                 }
84
85                 /// @todo add nsfw for $sensitive
86
87                 return $tag_text;
88         }
89
90         /**
91          * Add attachment data to the item array
92          *
93          * @param array $attachments
94          * @param array $item
95          *
96          * @return array array
97          */
98         private static function constructAttachList($attachments, $item)
99         {
100                 if (empty($attachments)) {
101                         return $item;
102                 }
103
104                 foreach ($attachments as $attach) {
105                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
106                         if ($filetype == 'image') {
107                                 $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
108                         } else {
109                                 if (!empty($item["attach"])) {
110                                         $item["attach"] .= ',';
111                                 } else {
112                                         $item["attach"] = '';
113                                 }
114                                 if (!isset($attach['length'])) {
115                                         $attach['length'] = "0";
116                                 }
117                                 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
118                         }
119                 }
120
121                 return $item;
122         }
123
124         /**
125          * Updates a message
126          *
127          * @param array $activity Activity array
128          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
129          */
130         public static function updateItem($activity)
131         {
132                 $item = Item::selectFirst(['uri', 'parent-uri', 'gravity'], ['uri' => $activity['id']]);
133                 if (!DBA::isResult($item)) {
134                         Logger::warning('Unknown item', ['uri' => $activity['id']]);
135                         return;
136                 }
137
138                 $item['changed'] = DateTimeFormat::utcNow();
139                 $item['edited'] = $activity['updated'];
140                 $item['title'] = HTML::toBBCode($activity['name']);
141                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
142
143                 $content = HTML::toBBCode($activity['content']);
144                 $content = self::replaceEmojis($content, $activity['emojis']);
145                 $content = self::convertMentions($content);
146
147                 $implicit_mentions = [];
148                 if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
149                         $parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['parent-uri']]);
150                         if (!DBA::isResult($parent)) {
151                                 Logger::warning('Unknown parent item.', ['uri' => $item['parent-uri']]);
152                                 return;
153                         }
154
155                         $implicit_mentions = self::getImplicitMentionList($parent);
156                         $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions);
157                 }
158
159                 $item['body'] = $content;
160                 $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions);
161
162                 Item::update($item, ['uri' => $activity['id']]);
163         }
164
165         /**
166          * Prepares data for a message
167          *
168          * @param array $activity Activity array
169          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
170          * @throws \ImagickException
171          */
172         public static function createItem($activity)
173         {
174                 $item = [];
175                 $item['verb'] = ACTIVITY_POST;
176                 $item['parent-uri'] = $activity['reply-to-id'];
177
178                 if ($activity['reply-to-id'] == $activity['id']) {
179                         $item['gravity'] = GRAVITY_PARENT;
180                         $item['object-type'] = ACTIVITY_OBJ_NOTE;
181                 } else {
182                         $item['gravity'] = GRAVITY_COMMENT;
183                         $item['object-type'] = ACTIVITY_OBJ_COMMENT;
184                 }
185
186                 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
187                         Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
188                         self::fetchMissingActivity($activity['reply-to-id'], $activity);
189                 }
190
191                 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
192
193                 self::postItem($activity, $item);
194         }
195
196         /**
197          * Delete items
198          *
199          * @param array $activity
200          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
201          * @throws \ImagickException
202          */
203         public static function deleteItem($activity)
204         {
205                 $owner = Contact::getIdForURL($activity['actor']);
206
207                 Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
208                 Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
209         }
210
211         /**
212          * Prepare the item array for an activity
213          *
214          * @param array  $activity Activity array
215          * @param string $verb     Activity verb
216          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217          * @throws \ImagickException
218          */
219         public static function createActivity($activity, $verb)
220         {
221                 $item = [];
222                 $item['verb'] = $verb;
223                 $item['parent-uri'] = $activity['object_id'];
224                 $item['gravity'] = GRAVITY_ACTIVITY;
225                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
226
227                 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:like', '');
228
229                 self::postItem($activity, $item);
230         }
231
232         /**
233          * Create an event
234          *
235          * @param array $activity Activity array
236          * @param array $item
237          * @throws \Exception
238          */
239         public static function createEvent($activity, $item)
240         {
241                 $event['summary']  = HTML::toBBCode($activity['name']);
242                 $event['desc']     = HTML::toBBCode($activity['content']);
243                 $event['start']    = $activity['start-time'];
244                 $event['finish']   = $activity['end-time'];
245                 $event['nofinish'] = empty($event['finish']);
246                 $event['location'] = $activity['location'];
247                 $event['adjust']   = true;
248                 $event['cid']      = $item['contact-id'];
249                 $event['uid']      = $item['uid'];
250                 $event['uri']      = $item['uri'];
251                 $event['edited']   = $item['edited'];
252                 $event['private']  = $item['private'];
253                 $event['guid']     = $item['guid'];
254                 $event['plink']    = $item['plink'];
255
256                 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
257                 $ev = DBA::selectFirst('event', ['id'], $condition);
258                 if (DBA::isResult($ev)) {
259                         $event['id'] = $ev['id'];
260                 }
261
262                 $event_id = Event::store($event);
263                 Logger::log('Event '.$event_id.' was stored', Logger::DEBUG);
264         }
265
266         /**
267          * Creates an item post
268          *
269          * @param array $activity Activity data
270          * @param array $item     item array
271          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
272          * @throws \ImagickException
273          */
274         private static function postItem($activity, $item)
275         {
276                 /// @todo What to do with $activity['context']?
277
278                 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
279                         Logger::log('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', Logger::DEBUG);
280                         return;
281                 }
282
283                 $item['network'] = Protocol::ACTIVITYPUB;
284                 $item['private'] = !in_array(0, $activity['receiver']);
285                 $item['author-link'] = $activity['author'];
286                 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
287
288                 if (empty($activity['thread-completion'])) {
289                         $item['owner-link'] = $activity['actor'];
290                         $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
291                 } else {
292                         Logger::info('Ignoring actor because of thread completion.');
293                         $item['owner-link'] = $item['author-link'];
294                         $item['owner-id'] = $item['author-id'];
295                 }
296
297                 $item['uri'] = $activity['id'];
298                 $content = HTML::toBBCode($activity['content']);
299                 $content = self::replaceEmojis($content, $activity['emojis']);
300                 $content = self::convertMentions($content);
301
302                 $implicit_mentions = [];
303
304                 if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
305                         $item_private = !in_array(0, $activity['item_receiver']);
306                         $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['parent-uri']]);
307                         if (!DBA::isResult($parent)) {
308                                 return;
309                         }
310                         if ($item_private && !$parent['private']) {
311                                 Logger::log('Item ' . $item['uri'] . ' is private but the parent ' . $item['parent-uri'] . ' is not. So we drop it.');
312                                 return;
313                         }
314
315                         $implicit_mentions = self::getImplicitMentionList($parent);
316                         $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions);
317                 }
318
319                 $item['created'] = $activity['published'];
320                 $item['edited'] = $activity['updated'];
321                 $item['guid'] = $activity['diaspora:guid'];
322                 $item['title'] = HTML::toBBCode($activity['name']);
323                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
324                 $item['body'] = $content;
325
326                 if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
327                         $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
328                 }
329
330                 $item['location'] = $activity['location'];
331
332                 if (!empty($item['latitude']) && !empty($item['longitude'])) {
333                         $item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
334                 }
335
336                 $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions);
337                 $item['app'] = $activity['generator'];
338                 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
339
340                 $item = self::constructAttachList($activity['attachments'], $item);
341
342                 if (!empty($activity['source'])) {
343                         $item['body'] = $activity['source'];
344                 }
345
346                 $stored = false;
347
348                 foreach ($activity['receiver'] as $receiver) {
349                         $item['uid'] = $receiver;
350                         $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
351
352                         if (($receiver != 0) && empty($item['contact-id'])) {
353                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
354                         }
355
356                         if ($activity['object_type'] == 'as:Event') {
357                                 self::createEvent($activity, $item);
358                         }
359
360                         $item_id = Item::insert($item);
361                         if ($item_id) {
362                                 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
363                         } else {
364                                 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
365                         }
366
367                         if ($item['uid'] == 0) {
368                                 $stored = $item_id;
369                         }
370                 }
371
372                 // Store send a follow request for every reshare - but only when the item had been stored
373                 if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
374                         $author = APContact::getByURL($item['owner-link'], false);
375                         // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
376                         if ($author['type'] != 'Group') {
377                                 Logger::log('Send follow request for ' . $item['uri'] . ' (' . $stored . ') to ' . $item['author-link'], Logger::DEBUG);
378                                 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
379                         }
380                 }
381         }
382
383         /**
384          * Fetches missing posts
385          *
386          * @param $url
387          * @param $child
388          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
389          */
390         private static function fetchMissingActivity($url, $child)
391         {
392                 if (Config::get('system', 'ostatus_full_threads')) {
393                         return;
394                 }
395
396                 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
397
398                 $object = ActivityPub::fetchContent($url, $uid);
399                 if (empty($object)) {
400                         Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
401                         return;
402                 }
403
404                 if (empty($object['id'])) {
405                         Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
406                         return;
407                 }
408
409                 $activity = [];
410                 $activity['@context'] = $object['@context'];
411                 unset($object['@context']);
412                 $activity['id'] = $object['id'];
413                 $activity['to'] = defaults($object, 'to', []);
414                 $activity['cc'] = defaults($object, 'cc', []);
415                 $activity['actor'] = $child['author'];
416                 $activity['object'] = $object;
417                 $activity['published'] = defaults($object, 'published', $child['published']);
418                 $activity['type'] = 'Create';
419
420                 $ldactivity = JsonLD::compact($activity);
421
422                 $ldactivity['thread-completion'] = true;
423
424                 ActivityPub\Receiver::processActivity($ldactivity);
425                 Logger::log('Activity ' . $url . ' had been fetched and processed.');
426         }
427
428         /**
429          * perform a "follow" request
430          *
431          * @param array $activity
432          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
433          * @throws \ImagickException
434          */
435         public static function followUser($activity)
436         {
437                 $uid = User::getIdForURL($activity['object_id']);
438                 if (empty($uid)) {
439                         return;
440                 }
441
442                 $owner = User::getOwnerDataById($uid);
443
444                 $cid = Contact::getIdForURL($activity['actor'], $uid);
445                 if (!empty($cid)) {
446                         self::switchContact($cid);
447                         DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
448                         $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
449                 } else {
450                         $contact = false;
451                 }
452
453                 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
454                         'author-link' => $activity['actor']];
455
456                 // Ensure that the contact has got the right network type
457                 self::switchContact($item['author-id']);
458
459                 Contact::addRelationship($owner, $contact, $item);
460                 $cid = Contact::getIdForURL($activity['actor'], $uid);
461                 if (empty($cid)) {
462                         return;
463                 }
464
465                 if (empty($contact)) {
466                         DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
467                 }
468
469                 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
470         }
471
472         /**
473          * Update the given profile
474          *
475          * @param array $activity
476          * @throws \Exception
477          */
478         public static function updatePerson($activity)
479         {
480                 if (empty($activity['object_id'])) {
481                         return;
482                 }
483
484                 Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
485                 APContact::getByURL($activity['object_id'], true);
486         }
487
488         /**
489          * Delete the given profile
490          *
491          * @param array $activity
492          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
493          */
494         public static function deletePerson($activity)
495         {
496                 if (empty($activity['object_id']) || empty($activity['actor'])) {
497                         Logger::log('Empty object id or actor.', Logger::DEBUG);
498                         return;
499                 }
500
501                 if ($activity['object_id'] != $activity['actor']) {
502                         Logger::log('Object id does not match actor.', Logger::DEBUG);
503                         return;
504                 }
505
506                 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
507                 while ($contact = DBA::fetch($contacts)) {
508                         Contact::remove($contact['id']);
509                 }
510                 DBA::close($contacts);
511
512                 Logger::log('Deleted contact ' . $activity['object_id'], Logger::DEBUG);
513         }
514
515         /**
516          * Accept a follow request
517          *
518          * @param array $activity
519          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
520          * @throws \ImagickException
521          */
522         public static function acceptFollowUser($activity)
523         {
524                 $uid = User::getIdForURL($activity['object_actor']);
525                 if (empty($uid)) {
526                         return;
527                 }
528
529                 $cid = Contact::getIdForURL($activity['actor'], $uid);
530                 if (empty($cid)) {
531                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
532                         return;
533                 }
534
535                 self::switchContact($cid);
536
537                 $fields = ['pending' => false];
538
539                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
540                 if ($contact['rel'] == Contact::FOLLOWER) {
541                         $fields['rel'] = Contact::FRIEND;
542                 }
543
544                 $condition = ['id' => $cid];
545                 DBA::update('contact', $fields, $condition);
546                 Logger::log('Accept contact request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
547         }
548
549         /**
550          * Reject a follow request
551          *
552          * @param array $activity
553          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
554          * @throws \ImagickException
555          */
556         public static function rejectFollowUser($activity)
557         {
558                 $uid = User::getIdForURL($activity['object_actor']);
559                 if (empty($uid)) {
560                         return;
561                 }
562
563                 $cid = Contact::getIdForURL($activity['actor'], $uid);
564                 if (empty($cid)) {
565                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
566                         return;
567                 }
568
569                 self::switchContact($cid);
570
571                 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
572                         Contact::remove($cid);
573                         Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
574                 } else {
575                         Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', Logger::DEBUG);
576                 }
577         }
578
579         /**
580          * Undo activity like "like" or "dislike"
581          *
582          * @param array $activity
583          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
584          * @throws \ImagickException
585          */
586         public static function undoActivity($activity)
587         {
588                 if (empty($activity['object_id'])) {
589                         return;
590                 }
591
592                 if (empty($activity['object_actor'])) {
593                         return;
594                 }
595
596                 $author_id = Contact::getIdForURL($activity['object_actor']);
597                 if (empty($author_id)) {
598                         return;
599                 }
600
601                 Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
602         }
603
604         /**
605          * Activity to remove a follower
606          *
607          * @param array $activity
608          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
609          * @throws \ImagickException
610          */
611         public static function undoFollowUser($activity)
612         {
613                 $uid = User::getIdForURL($activity['object_object']);
614                 if (empty($uid)) {
615                         return;
616                 }
617
618                 $owner = User::getOwnerDataById($uid);
619
620                 $cid = Contact::getIdForURL($activity['actor'], $uid);
621                 if (empty($cid)) {
622                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
623                         return;
624                 }
625
626                 self::switchContact($cid);
627
628                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
629                 if (!DBA::isResult($contact)) {
630                         return;
631                 }
632
633                 Contact::removeFollower($owner, $contact);
634                 Logger::log('Undo following request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
635         }
636
637         /**
638          * Switches a contact to AP if needed
639          *
640          * @param integer $cid Contact ID
641          * @throws \Exception
642          */
643         private static function switchContact($cid)
644         {
645                 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
646                 if (!DBA::isResult($contact) || ($contact['network'] == Protocol::ACTIVITYPUB)) {
647                         return;
648                 }
649
650                 Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
651                 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
652         }
653
654         /**
655          * Collects implicit mentions like:
656          * - the author of the parent item
657          * - all the mentioned conversants in the parent item
658          *
659          * @param array $parent Item array with at least ['id', 'author-link', 'alias']
660          * @return array
661          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
662          */
663         private static function getImplicitMentionList(array $parent)
664         {
665                 $parent_terms = Term::tagArrayFromItemId($parent['id'], [TERM_MENTION]);
666
667                 $implicit_mentions = [
668                         $parent['author-link']
669                 ];
670
671                 if ($parent['alias']) {
672                         $implicit_mentions[] = $parent['alias'];
673                 }
674
675                 foreach ($parent_terms as $term) {
676                         $contact = Contact::getDetailsByURL($term['url'], 0);
677                         if (!empty($contact)) {
678                                 $implicit_mentions[] = $contact['url'];
679                                 $implicit_mentions[] = $contact['nurl'];
680                                 $implicit_mentions[] = $contact['alias'];
681                         }
682                 }
683
684                 return $implicit_mentions;
685         }
686
687         /**
688          * Strips from the body prepended implicit mentions
689          *
690          * @param string $body
691          * @param array  $implicit_mentions List of profile URLs
692          * @return string
693          */
694         private static function removeImplicitMentionsFromBody($body, array $implicit_mentions)
695         {
696                 if (Config::get('system', 'disable_mentions_removal')) {
697                         return $body;
698                 }
699
700                 $kept_mentions = [];
701
702                 // Extract one prepended mention at a time from the body
703                 while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#mis', $body, $matches)) {
704                         if (!in_array($matches[2], $implicit_mentions) ) {
705                                 $kept_mentions[] = $matches[1];
706                         }
707
708                         $body = $matches[3];
709                 }
710
711                 // Re-appending the kept mentions to the body after extraction
712                 $kept_mentions[] = $body;
713
714                 return implode('', $kept_mentions);
715         }
716 }