+
+ /**
+ * Collects implicit mentions like:
+ * - the author of the parent item
+ * - all the mentioned conversants in the parent item
+ *
+ * @param array $parent Item array with at least ['id', 'author-link', 'alias']
+ * @return array
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ private static function getImplicitMentionList(array $parent)
+ {
+ if (Config::get('system', 'disable_implicit_mentions')) {
+ return [];
+ }
+
+ $parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
+
+ $parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
+
+ $implicit_mentions = [];
+ if (empty($parent_author)) {
+ Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'item-id' => $parent['id']]);
+ } else {
+ $implicit_mentions[] = $parent_author['url'];
+ $implicit_mentions[] = $parent_author['nurl'];
+ $implicit_mentions[] = $parent_author['alias'];
+ }
+
+ if (!empty($parent['alias'])) {
+ $implicit_mentions[] = $parent['alias'];
+ }
+
+ foreach ($parent_terms as $term) {
+ $contact = Contact::getDetailsByURL($term['url'], 0);
+ if (!empty($contact)) {
+ $implicit_mentions[] = $contact['url'];
+ $implicit_mentions[] = $contact['nurl'];
+ $implicit_mentions[] = $contact['alias'];
+ }
+ }
+
+ return $implicit_mentions;
+ }
+
+ /**
+ * Strips from the body prepended implicit mentions
+ *
+ * @param string $body
+ * @param array $potential_mentions
+ * @return string
+ */
+ private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
+ {
+ if (Config::get('system', 'disable_implicit_mentions')) {
+ return $body;
+ }
+
+ $kept_mentions = [];
+
+ // Extract one prepended mention at a time from the body
+ while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
+ if (!in_array($matches[2], $potential_mentions) ) {
+ $kept_mentions[] = $matches[1];
+ }
+
+ $body = $matches[3];
+ }
+
+ // Re-appending the kept mentions to the body after extraction
+ $kept_mentions[] = $body;
+
+ return implode('', $kept_mentions);
+ }
+
+ private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
+ {
+ if (Config::get('system', 'disable_implicit_mentions')) {
+ return $activity_tags;
+ }
+
+ foreach ($activity_tags as $index => $tag) {
+ if (in_array($tag['href'], $potential_mentions)) {
+ $activity_tags[$index]['name'] = preg_replace(
+ '/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/',
+ Term::TAG_CHARACTER[Term::IMPLICIT_MENTION],
+ $activity_tags[$index]['name'],
+ 1
+ );
+ }
+ }
+
+ return $activity_tags;
+ }