From d5f64e11560b34e2b2c3195bb3bd6527315a4440 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 06:33:59 +0000 Subject: [PATCH 1/9] Issue 8610: Implicit mentions work again --- mod/item.php | 31 +++++++----------------- src/Model/Tag.php | 21 ++++++++++++++++ src/Protocol/ActivityPub/Transmitter.php | 12 ++++----- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/mod/item.php b/mod/item.php index 10ad3ff05..acce4ecee 100644 --- a/mod/item.php +++ b/mod/item.php @@ -747,6 +747,7 @@ function item_post(App $a) { } Tag::storeFromBody($datarray['uri-id'], $datarray['body']); + Tag::createImplicitMentions($datarray['uri-id'], $datarray['thr-parent-id']); // update filetags in pconfig FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category'); @@ -1002,29 +1003,15 @@ function handle_tag(&$body, &$inform, $profile_uid, $tag, $network = "") function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $thread_parent_uriid) { - if (DI::config()->get('system', 'disable_implicit_mentions')) { - // Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them) - if (in_array($thread_parent_contact['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) { - $contact = Tag::TAG_CHARACTER[Tag::MENTION] . '[url=' . $thread_parent_contact['url'] . ']' . $thread_parent_contact['nick'] . '[/url]'; - if (!stripos(implode($tags), '[url=' . $thread_parent_contact['url'] . ']')) { - $tags[] = $contact; - } - } - } else { - $implicit_mentions = [ - $thread_parent_contact['url'] => $thread_parent_contact['nick'] - ]; + if (!DI::config()->get('system', 'disable_implicit_mentions')) { + return $tags; + } - $parent_terms = Tag::getByURIId($thread_parent_uriid, [Tag::MENTION, Tag::IMPLICIT_MENTION]); - - foreach ($parent_terms as $parent_term) { - $implicit_mentions[$parent_term['url']] = $parent_term['name']; - } - - foreach ($implicit_mentions as $url => $label) { - if ($url != \Friendica\Model\Profile::getMyURL() && !stripos(implode($tags), '[url=' . $url . ']')) { - $tags[] = Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION] . '[url=' . $url . ']' . $label . '[/url]'; - } + // Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them) + if (in_array($thread_parent_contact['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) { + $contact = Tag::TAG_CHARACTER[Tag::MENTION] . '[url=' . $thread_parent_contact['url'] . ']' . $thread_parent_contact['nick'] . '[/url]'; + if (!stripos(implode($tags), '[url=' . $thread_parent_contact['url'] . ']')) { + $tags[] = $contact; } } diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 2f4628972..5a62aae91 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -325,6 +325,27 @@ class Tag } } + /** + * Create implicit mentions for a given post + * + * @param integer $uri_id + * @param integer $parent_uri_id + */ + public static function createImplicitMentions(int $uri_id, int $parent_uri_id) + { + if (DI::config()->get('system', 'disable_implicit_mentions')) { + return; + } + + $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id]); + while ($tag = DBA::fetch($tags)) { + self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']); + } + + $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]); + self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']); + } + /** * Retrieves the terms from the provided type(s) associated with the provided item ID. * diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 0dfef3ebd..014afe096 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1294,7 +1294,7 @@ class Transmitter $body = $item['body']; if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { - $body = self::prependMentions($body, $permission_block); + $body = self::prependMentions($body, $item['uri-id']); } if ($type == 'Note') { @@ -1843,7 +1843,7 @@ class Transmitter HTTPSignature::transmit($signed, $profile['inbox'], $uid); } - private static function prependMentions($body, array $permission_block) + private static function prependMentions($body, int $uriid) { if (DI::config()->get('system', 'disable_implicit_mentions')) { return $body; @@ -1851,14 +1851,14 @@ class Transmitter $mentions = []; - foreach ($permission_block['to'] as $profile_url) { - $profile = Contact::getDetailsByURL($profile_url); + foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) { + $profile = Contact::getDetailsByURL($tag['url']); if (!empty($profile['addr']) && $profile['contact-type'] != Contact::TYPE_COMMUNITY && !strstr($body, $profile['addr']) - && !strstr($body, $profile_url) + && !strstr($body, $tag['url']) ) { - $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]'; + $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]'; } } From bebc6615fc796e8faab2ebaaa39e0d0a0590f92b Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 06:51:13 +0000 Subject: [PATCH 2/9] Removed unneeded functionality --- src/Protocol/ActivityPub/Processor.php | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 197fb2baa..92678e59c 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -359,9 +359,7 @@ class Processor return false; } - $potential_implicit_mentions = self::getImplicitMentionList($parent); - $content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions); - $activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions); + $content = self::removeImplicitMentionsFromBody($content, self::getImplicitMentionList($parent)); } $item['content-warning'] = HTML::toBBCode($activity['summary']); $item['body'] = $content; @@ -1033,24 +1031,4 @@ class Processor return implode('', $kept_mentions); } - - private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions) - { - if (DI::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(Tag::TAG_CHARACTER[Tag::MENTION], '/') . '/', - Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION], - $activity_tags[$index]['name'], - 1 - ); - } - } - - return $activity_tags; - } } From 556cc3fb13bf4ba8f597178e7bf7b55947323f94 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 08:08:33 +0000 Subject: [PATCH 3/9] Always mention the parent author --- mod/item.php | 26 ++++---------------------- src/Model/Tag.php | 7 ++++--- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/mod/item.php b/mod/item.php index acce4ecee..f07411144 100644 --- a/mod/item.php +++ b/mod/item.php @@ -379,10 +379,6 @@ function item_post(App $a) { $tags = BBCode::getTags($body); - if ($thread_parent_uriid && !\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) { - $tags = item_add_implicit_mentions($tags, $thread_parent_contact, $thread_parent_uriid); - } - $tagged = []; $private_forum = false; @@ -747,7 +743,10 @@ function item_post(App $a) { } Tag::storeFromBody($datarray['uri-id'], $datarray['body']); - Tag::createImplicitMentions($datarray['uri-id'], $datarray['thr-parent-id']); + + if (!\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) { + Tag::createImplicitMentions($datarray['uri-id'], $datarray['thr-parent-id']); + } // update filetags in pconfig FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category'); @@ -1000,20 +999,3 @@ function handle_tag(&$body, &$inform, $profile_uid, $tag, $network = "") return ['replaced' => $replaced, 'contact' => $contact]; } - -function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $thread_parent_uriid) -{ - if (!DI::config()->get('system', 'disable_implicit_mentions')) { - return $tags; - } - - // Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them) - if (in_array($thread_parent_contact['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) { - $contact = Tag::TAG_CHARACTER[Tag::MENTION] . '[url=' . $thread_parent_contact['url'] . ']' . $thread_parent_contact['nick'] . '[/url]'; - if (!stripos(implode($tags), '[url=' . $thread_parent_contact['url'] . ']')) { - $tags[] = $contact; - } - } - - return $tags; -} diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 5a62aae91..bbe516e49 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -333,6 +333,10 @@ class Tag */ public static function createImplicitMentions(int $uri_id, int $parent_uri_id) { + // Always mention the direct parent author + $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]); + self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']); + if (DI::config()->get('system', 'disable_implicit_mentions')) { return; } @@ -341,9 +345,6 @@ class Tag while ($tag = DBA::fetch($tags)) { self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']); } - - $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]); - self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']); } /** From eb4c14695c42a5d126eaa814cee84f1a7b163751 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 08:35:58 +0000 Subject: [PATCH 4/9] The implicit mentions are added in any case --- src/Protocol/ActivityPub/Transmitter.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 014afe096..2e212f0d6 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1845,10 +1845,6 @@ class Transmitter private static function prependMentions($body, int $uriid) { - if (DI::config()->get('system', 'disable_implicit_mentions')) { - return $body; - } - $mentions = []; foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) { From a1fda8f74ae45a838a5b0306fca3f1585b715899 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 08:39:21 +0000 Subject: [PATCH 5/9] Dant try to add mentions on starting posts --- src/Model/Tag.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Model/Tag.php b/src/Model/Tag.php index bbe516e49..41c6de111 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -333,6 +333,10 @@ class Tag */ public static function createImplicitMentions(int $uri_id, int $parent_uri_id) { + if ($uri_id == $parent_uri_id) { + return; + } + // Always mention the direct parent author $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]); self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']); From 9679fad5e23de6a0c17a8a31b87329070606c777 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 08:55:10 +0000 Subject: [PATCH 6/9] Concentrating functionality --- src/Protocol/ActivityPub/Processor.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 92678e59c..60f29f415 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -359,7 +359,7 @@ class Processor return false; } - $content = self::removeImplicitMentionsFromBody($content, self::getImplicitMentionList($parent)); + $content = self::removeImplicitMentionsFromBody($content, $parent); } $item['content-warning'] = HTML::toBBCode($activity['summary']); $item['body'] = $content; @@ -969,10 +969,6 @@ class Processor */ private static function getImplicitMentionList(array $parent) { - if (DI::config()->get('system', 'disable_implicit_mentions')) { - return []; - } - $parent_terms = Tag::getByURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]); $parent_author = Contact::getDetailsByURL($parent['author-link'], 0); @@ -1006,15 +1002,17 @@ class Processor * Strips from the body prepended implicit mentions * * @param string $body - * @param array $potential_mentions + * @param array $parent * @return string */ - private static function removeImplicitMentionsFromBody($body, array $potential_mentions) + private static function removeImplicitMentionsFromBody(string $body, array $parent) { if (DI::config()->get('system', 'disable_implicit_mentions')) { return $body; } + $potential_mentions = self::getImplicitMentionList($parent); + $kept_mentions = []; // Extract one prepended mention at a time from the body From 221a659abeaf3bda3cefd88c80d1b7814f168f3e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 09:38:49 +0000 Subject: [PATCH 7/9] Unused variables removed --- mod/item.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/mod/item.php b/mod/item.php index f07411144..441997faa 100644 --- a/mod/item.php +++ b/mod/item.php @@ -100,14 +100,9 @@ function item_post(App $a) { $toplevel_item_id = intval($_REQUEST['parent'] ?? 0); $thr_parent_uri = trim($_REQUEST['parent_uri'] ?? ''); - $thread_parent_uriid = 0; - $thread_parent_contact = null; - $toplevel_item = null; $parent_user = null; - $parent_contact = null; - $objecttype = null; $profile_uid = ($_REQUEST['profile_uid'] ?? 0) ?: local_user(); $posttype = ($_REQUEST['post_type'] ?? '') ?: Item::PT_ARTICLE; @@ -122,9 +117,7 @@ function item_post(App $a) { // if this isn't the top-level parent of the conversation, find it if (DBA::isResult($toplevel_item)) { // The URI and the contact is taken from the direct parent which needn't to be the top parent - $thread_parent_uriid = $toplevel_item['uri-id']; $thr_parent_uri = $toplevel_item['uri']; - $thread_parent_contact = Contact::getDetailsByURL($toplevel_item["author-link"]); if ($toplevel_item['id'] != $toplevel_item['parent']) { $toplevel_item = Item::selectFirst([], ['id' => $toplevel_item['parent']]); From 19b5b83ac6d61080743df129528f59a762634fd2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 15:43:41 +0000 Subject: [PATCH 8/9] Forgotten "close" --- src/Model/Tag.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 41c6de111..b28e723b7 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -349,6 +349,7 @@ class Tag while ($tag = DBA::fetch($tags)) { self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']); } + DBA::close($tags); } /** From 258e9df0648b8670b05eee03e690f19aa66cc8b9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 May 2020 15:50:49 +0000 Subject: [PATCH 9/9] Moved the check for a comment to a different place --- mod/item.php | 2 +- src/Model/Tag.php | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/mod/item.php b/mod/item.php index 441997faa..30d9f03e6 100644 --- a/mod/item.php +++ b/mod/item.php @@ -737,7 +737,7 @@ function item_post(App $a) { Tag::storeFromBody($datarray['uri-id'], $datarray['body']); - if (!\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) { + if (!\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions') && ($datarray['gravity'] == GRAVITY_COMMENT)) { Tag::createImplicitMentions($datarray['uri-id'], $datarray['thr-parent-id']); } diff --git a/src/Model/Tag.php b/src/Model/Tag.php index b28e723b7..2f38608cc 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -333,10 +333,6 @@ class Tag */ public static function createImplicitMentions(int $uri_id, int $parent_uri_id) { - if ($uri_id == $parent_uri_id) { - return; - } - // Always mention the direct parent author $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]); self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);