From e514ecb6fabfbcf18b0fccff91e7a93a498fa814 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Feb 2019 23:09:02 -0500 Subject: [PATCH] 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); + } } -- 2.39.5