From e6a75b2609b99a8e147288a40bbba8462f727f62 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 22:32:50 -0500 Subject: [PATCH 1/9] Add alias field to ActivityPub contacts --- src/Model/APContact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 3d196c46de..bf36306b41 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -192,7 +192,7 @@ class APContact extends BaseObject DBA::update('apcontact', $apcontact, ['url' => $url], true); // Update some data in the contact table with various ways to catch them all - $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']]; + $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about'], 'alias' => $apcontact['alias']]; // Fetch the type and match it with the contact type $contact_types = array_keys(ActivityPub::ACCOUNT_TYPES, $apcontact['type']); From 6577b1abb0fc0220500d48fc0a7efd6a3c16ff6e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 22:38:23 -0500 Subject: [PATCH 2/9] Add alias field to mod/crepair - Replace deprecated q() call by DBA::update --- mod/crepair.php | 55 +++++++++++++-------------- view/templates/crepair.tpl | 2 + view/theme/frio/templates/crepair.tpl | 2 + 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/mod/crepair.php b/mod/crepair.php index 91b22dbc92..7e10b2ff2b 100644 --- a/mod/crepair.php +++ b/mod/crepair.php @@ -19,20 +19,6 @@ function crepair_init(App $a) if (!local_user()) { return; } - - $contact = null; - if (($a->argc == 2) && intval($a->argv[1])) { - $contact = DBA::selectFirst('contact', [], ['uid' => local_user(), 'id' => $a->argv[1]]); - } - - if (empty($a->page['aside'])) { - $a->page['aside'] = ''; - } - - if (DBA::isResult($contact)) { - $a->data['contact'] = $contact; - Model\Profile::load($a, "", 0, Model\Contact::getDetailsByURL($contact["url"])); - } } function crepair_post(App $a) @@ -55,6 +41,7 @@ function crepair_post(App $a) $name = defaults($_POST, 'name' , $contact['name']); $nick = defaults($_POST, 'nick' , ''); $url = defaults($_POST, 'url' , ''); + $alias = defaults($_POST, 'alias' , ''); $request = defaults($_POST, 'request' , ''); $confirm = defaults($_POST, 'confirm' , ''); $notify = defaults($_POST, 'notify' , ''); @@ -64,20 +51,22 @@ function crepair_post(App $a) $remote_self = defaults($_POST, 'remote_self', false); $nurl = Strings::normaliseLink($url); - $r = q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `nurl` = '%s', `request` = '%s', `confirm` = '%s', `notify` = '%s', `poll` = '%s', `attag` = '%s' , `remote_self` = %d - WHERE `id` = %d AND `uid` = %d", - DBA::escape($name), - DBA::escape($nick), - DBA::escape($url), - DBA::escape($nurl), - DBA::escape($request), - DBA::escape($confirm), - DBA::escape($notify), - DBA::escape($poll), - DBA::escape($attag), - intval($remote_self), - intval($contact['id']), - local_user() + $r = DBA::update( + 'contact', + [ + 'name' => $name, + 'nick' => $nick, + 'url' => $url, + 'nurl' => $nurl, + 'alias' => $alias, + 'request' => $request, + 'confirm' => $confirm, + 'notify' => $notify, + 'poll' => $poll, + 'attag' => $attag, + 'remote_self' => $remote_self, + ], + ['id' => $contact['id'], 'uid' => local_user()] ); if ($photo) { @@ -114,6 +103,15 @@ function crepair_content(App $a) return; } + if (empty($a->page['aside'])) { + $a->page['aside'] = ''; + } + + if (DBA::isResult($contact)) { + $a->data['contact'] = $contact; + Model\Profile::load($a, "", 0, Model\Contact::getDetailsByURL($contact["url"])); + } + $warning = L10n::t('WARNING: This is highly advanced and if you enter incorrect information your communications with this contact may stop working.'); $info = L10n::t('Please use your browser \'Back\' button now if you are uncertain what to do on this page.'); @@ -162,6 +160,7 @@ function crepair_content(App $a) '$nick' => ['nick', L10n::t('Account Nickname'), $contact['nick']], '$attag' => ['attag', L10n::t('@Tagname - overrides Name/Nickname'), $contact['attag']], '$url' => ['url', L10n::t('Account URL'), $contact['url']], + '$alias' => ['alias', L10n::t('Account URL Alias'), $contact['alias']], '$request' => ['request', L10n::t('Friend Request URL'), $contact['request']], 'confirm' => ['confirm', L10n::t('Friend Confirm URL'), $contact['confirm']], 'notify' => ['notify', L10n::t('Notification Endpoint URL'), $contact['notify']], diff --git a/view/templates/crepair.tpl b/view/templates/crepair.tpl index bb24e096ea..77fa993e19 100644 --- a/view/templates/crepair.tpl +++ b/view/templates/crepair.tpl @@ -27,6 +27,8 @@ {{include file="field_input.tpl" field=$url}} + {{include file="field_input.tpl" field=$alias}} + {{include file="field_input.tpl" field=$request}} {{include file="field_input.tpl" field=$confirm}} diff --git a/view/theme/frio/templates/crepair.tpl b/view/theme/frio/templates/crepair.tpl index 6695682e0b..df49762001 100644 --- a/view/theme/frio/templates/crepair.tpl +++ b/view/theme/frio/templates/crepair.tpl @@ -29,6 +29,8 @@ {{include file="field_input.tpl" field=$url}} + {{include file="field_input.tpl" field=$alias}} + {{include file="field_input.tpl" field=$request}} {{include file="field_input.tpl" field=$confirm}} From e3524088768f897d3424c886b80631ec0112b0a5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 22:57:35 -0500 Subject: [PATCH 3/9] Remove implicit mentions from incoming ActivityPub messages - Add Processor::getImplicitMentionList and Processor::removeImplicitMentionsFromBody methods - Rename Processor::constructTagList to Processor::constructTagString - Add implicit mention skip in Processor::constructTagString - Invert parameter order in Processor::replaceEmojis --- src/Protocol/ActivityPub/Processor.php | 113 ++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 13 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index fd6d70a42a..ef351f30de 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -13,6 +13,7 @@ use Friendica\Model\Contact; use Friendica\Model\APContact; use Friendica\Model\Item; use Friendica\Model\Event; +use Friendica\Model\Term; use Friendica\Model\User; use Friendica\Protocol\ActivityPub; use Friendica\Util\DateTimeFormat; @@ -47,7 +48,7 @@ class Processor * * @return string with replaced emojis */ - public static function replaceEmojis($emojis, $body) + public static function replaceEmojis($body, array $emojis) { foreach ($emojis as $emoji) { $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]'; @@ -59,12 +60,12 @@ class Processor /** * Constructs a string with tags for a given tag array * - * @param array $tags + * @param array $tags * @param boolean $sensitive - * + * @param array $implicit_mentions List of profile URLs to skip * @return string with tags */ - private static function constructTagList($tags, $sensitive) + private static function constructTagString($tags, $sensitive, array $implicit_mentions) { if (empty($tags)) { return ''; @@ -72,7 +73,7 @@ class Processor $tag_text = ''; foreach ($tags as $tag) { - if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) { + if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag']) && !in_array($tag['href'], $implicit_mentions)) { if (!empty($tag_text)) { $tag_text .= ','; } @@ -128,14 +129,35 @@ class Processor */ public static function updateItem($activity) { - $item = []; + $item = Item::selectFirst(['uri', 'parent-uri', 'gravity'], ['uri' => $activity['id']]); + if (!DBA::isResult($item)) { + Logger::warning('Unknown item with uri: ' . $activity['id']); + return; + } + $item['changed'] = DateTimeFormat::utcNow(); $item['edited'] = $activity['updated']; $item['title'] = HTML::toBBCode($activity['name']); $item['content-warning'] = HTML::toBBCode($activity['summary']); - $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content'])); - $item['body'] = self::convertMentions($content); - $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']); + + $content = HTML::toBBCode($activity['content']); + $content = self::replaceEmojis($content, $activity['emojis']); + $content = self::convertMentions($content); + + $implicit_mentions = []; + if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { + $parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['parent-uri']]); + if (!DBA::isResult($parent)) { + Logger::warning('Unknown item parent with uri: ' . $item['parent-uri']); + return; + } + + $implicit_mentions = self::getImplicitMentionList($parent); + $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions); + } + + $item['body'] = $content; + $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions); Item::update($item, ['uri' => $activity['id']]); } @@ -273,10 +295,15 @@ class Processor } $item['uri'] = $activity['id']; + $content = HTML::toBBCode($activity['content']); + $content = self::replaceEmojis($content, $activity['emojis']); + $content = self::convertMentions($content); + + $implicit_mentions = []; if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { $item_private = !in_array(0, $activity['item_receiver']); - $parent = Item::selectFirst(['private'], ['uri' => $item['parent-uri']]); + $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['parent-uri']]); if (!DBA::isResult($parent)) { return; } @@ -284,6 +311,9 @@ class Processor Logger::log('Item ' . $item['uri'] . ' is private but the parent ' . $item['parent-uri'] . ' is not. So we drop it.'); return; } + + $implicit_mentions = self::getImplicitMentionList($parent); + $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions); } $item['created'] = $activity['published']; @@ -291,8 +321,7 @@ class Processor $item['guid'] = $activity['diaspora:guid']; $item['title'] = HTML::toBBCode($activity['name']); $item['content-warning'] = HTML::toBBCode($activity['summary']); - $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content'])); - $item['body'] = self::convertMentions($content); + $item['body'] = $content; if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) { $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]'; @@ -304,7 +333,7 @@ class Processor $item['coord'] = $item['latitude'] . ' ' . $item['longitude']; } - $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']); + $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions); $item['app'] = $activity['generator']; $item['plink'] = defaults($activity, 'alternate-url', $item['uri']); @@ -610,4 +639,62 @@ class Processor Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.'); Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB); } + + /** + * 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) + { + $parent_terms = Term::tagArrayFromItemId($parent['id'], [TERM_MENTION]); + + $implicit_mentions = [ + $parent['author-link'] + ]; + + if ($parent['alias']) { + $implicit_mentions[] = $parent['alias']; + } + + foreach ($parent_terms as $term) { + $contact = Contact::getDetailsByURL($term['url']); + + $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 $implicit_mentions List of profile URLs + * @return string + */ + private static function removeImplicitMentionsFromBody($body, array $implicit_mentions) + { + $kept_mentions = []; + + // Extract one prepended mention at a time from the body + while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#mi', $body, $matches)) { + if (!in_array($matches[2], $implicit_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); + } } From e8ca8182dbfae055bcdba1593df7e5c1ee6e6605 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 22:58:48 -0500 Subject: [PATCH 4/9] Add explicit_mentions feature setting --- src/Content/Feature.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Content/Feature.php b/src/Content/Feature.php index db0a70e2fb..58ef856f3d 100644 --- a/src/Content/Feature.php +++ b/src/Content/Feature.php @@ -90,6 +90,7 @@ class Feature 'composition' => [ L10n::t('Post Composition Features'), ['aclautomention', L10n::t('Auto-mention Forums'), L10n::t('Add/remove mention when a forum page is selected/deselected in ACL window.'), false, Config::get('feature_lock', 'aclautomention', false)], + ['explicit_mentions', L10n::t('Explicit Mentions'), L10n::t('Add explicit mentions to comment box for manual control over who gets mentioned in replies.'), false, Config::get('feature_lock', 'explicit_mentions', false)], ], // Network sidebar widgets From 9887f2c3d097208680d759388660d1fbfbeebb96 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 23:07:36 -0500 Subject: [PATCH 5/9] Toggle explicit mentions in comment boxes with explicit_mentions config --- src/Object/Post.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Object/Post.php b/src/Object/Post.php index 6e2483c6a0..36451c854f 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -6,6 +6,7 @@ namespace Friendica\Object; use Friendica\BaseObject; use Friendica\Content\ContactSelector; +use Friendica\Content\Feature; use Friendica\Core\Addon; use Friendica\Core\Config; use Friendica\Core\Hook; @@ -772,13 +773,18 @@ class Post extends BaseObject * Get default text for the comment box * * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private function getDefaultText() { $a = self::getApp(); if (!local_user() || empty($a->profile['addr'])) { - return; + return ''; + } + + if (!Feature::isEnabled(local_user(), 'explicit_mentions')) { + return ''; } $item = Item::selectFirst(['author-addr'], ['id' => $this->getId()]); From e514ecb6fabfbcf18b0fccff91e7a93a498fa814 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 23:09:02 -0500 Subject: [PATCH 6/9] Prepend implicit mentions to outgoing ActivityPub messages - Add Transmitter::prependMentions method - Fix Transmitter::mentionCallback return value when contact isn't found --- src/Protocol/ActivityPub/Transmitter.php | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index f5953e7c5f..1032627b32 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -5,6 +5,7 @@ namespace Friendica\Protocol\ActivityPub; use Friendica\BaseObject; +use Friendica\Content\Feature; use Friendica\Database\DBA; use Friendica\Core\Config; use Friendica\Core\Logger; @@ -891,12 +892,12 @@ class Transmitter private static function mentionCallback($match) { if (empty($match[1])) { - return; + return ''; } $data = Contact::getDetailsByURL($match[1]); - if (empty($data) || empty($data['nick'])) { - return; + if (empty($data['nick'])) { + return $match[0]; } return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; @@ -1037,8 +1038,14 @@ class Transmitter $data['name'] = BBCode::toPlaintext($item['title'], false); } + $permission_block = self::createPermissionBlockForItem($item, false); + $body = $item['body']; + if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { + $body = self::prependMentions($body, $permission_block); + } + if ($type == 'Note') { $body = self::removePictures($body); } @@ -1069,7 +1076,7 @@ class Transmitter $data['generator'] = ['type' => 'Application', 'name' => $item['app']]; } - $data = array_merge($data, self::createPermissionBlockForItem($item, false)); + $data = array_merge($data, $permission_block); return $data; } @@ -1422,4 +1429,24 @@ class Transmitter $signed = LDSignature::sign($data, $owner); HTTPSignature::transmit($signed, $profile['inbox'], $uid); } + + private static function prependMentions($body, array $permission_block) + { + $mentions = []; + + foreach ($permission_block['to'] as $profile_url) { + $profile = Contact::getDetailsByURL($profile_url); + if (!empty($profile['addr']) + && $profile['contact-type'] != Contact::TYPE_COMMUNITY + && !strstr($body, $profile['addr']) + && !strstr($body, $profile_url) + ) { + $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]'; + } + } + + $mentions[] = $body; + + return implode(' ', $mentions); + } } From 199fac33975022499c1a33d17d4c7b135a9ba021 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 23:10:36 -0500 Subject: [PATCH 7/9] Prepend implicit author mentions in outgoing Diaspora comments - Add Diaspora::prependParentAuthorMention method --- src/Protocol/Diaspora.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 34bdf2e876..77ad017044 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -10,6 +10,7 @@ namespace Friendica\Protocol; +use Friendica\Content\Feature; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\Markdown; use Friendica\Core\Cache; @@ -3666,6 +3667,20 @@ class Diaspora return $msg; } + private static function prependParentAuthorMention($body, $profile_url) + { + $profile = Contact::getDetailsByURL($profile_url); + if (!empty($profile['addr']) + && $profile['contact-type'] != Contact::TYPE_COMMUNITY + && !strstr($body, $profile['addr']) + && !strstr($body, $profile_url) + ) { + $body = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url] ' . $body; + } + + return $body; + } + /** * @brief Sends a post * @@ -3773,12 +3788,18 @@ class Diaspora return $result; } - $parent = Item::selectFirst(['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]); + $parent = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]); if (!DBA::isResult($parent)) { return false; } - $text = html_entity_decode(BBCode::toMarkdown($item["body"])); + $body = $item["body"]; + + if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { + $body = self::prependParentAuthorMention($body, $parent['author-link']); + } + + $text = html_entity_decode(BBCode::toMarkdown($body)); $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); $comment = ["author" => self::myHandle($owner), From ca65450537aa7c1903480e7b7daecde22c2463cf Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 23:12:05 -0500 Subject: [PATCH 8/9] Add query path capabilities to /itemsource --- src/Module/Itemsource.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Module/Itemsource.php b/src/Module/Itemsource.php index 4d85ef15ce..9e62a568c8 100644 --- a/src/Module/Itemsource.php +++ b/src/Module/Itemsource.php @@ -17,10 +17,18 @@ class Itemsource extends \Friendica\BaseModule return; } + $a = self::getApp(); + + if (!empty($a->argv[1])) { + $guid = $a->argv[1]; + } + + $guid = defaults($_REQUEST['guid'], $guid); + $source = ''; $item_uri = ''; - if (!empty($_REQUEST['guid'])) { - $item = Model\Item::selectFirst([], ['guid' => $_REQUEST['guid']]); + if (!empty($guid)) { + $item = Model\Item::selectFirst([], ['guid' => $guid]); $conversation = Model\Conversation::getByItemUri($item['uri']); From ed89a22995a2b3914c4afa26fa420719e7061f56 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 9 Feb 2019 08:34:54 -0500 Subject: [PATCH 9/9] Add context to some logger calls in ActivityPub\Processor --- src/Protocol/ActivityPub/Processor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index ef351f30de..6d94b1b49e 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -131,7 +131,7 @@ class Processor { $item = Item::selectFirst(['uri', 'parent-uri', 'gravity'], ['uri' => $activity['id']]); if (!DBA::isResult($item)) { - Logger::warning('Unknown item with uri: ' . $activity['id']); + Logger::warning('Unknown item', ['uri' => $activity['id']]); return; } @@ -148,7 +148,7 @@ class Processor if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { $parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['parent-uri']]); if (!DBA::isResult($parent)) { - Logger::warning('Unknown item parent with uri: ' . $item['parent-uri']); + Logger::warning('Unknown parent item.', ['uri' => $item['parent-uri']]); return; }