Prepend implicit mentions to outgoing ActivityPub messages

- Add Transmitter::prependMentions method
- Fix Transmitter::mentionCallback return value when contact isn't found
This commit is contained in:
Hypolite Petovan 2019-02-08 23:09:02 -05:00
parent 9887f2c3d0
commit e514ecb6fa

View file

@ -5,6 +5,7 @@
namespace Friendica\Protocol\ActivityPub; namespace Friendica\Protocol\ActivityPub;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Content\Feature;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Logger; use Friendica\Core\Logger;
@ -891,12 +892,12 @@ class Transmitter
private static function mentionCallback($match) private static function mentionCallback($match)
{ {
if (empty($match[1])) { if (empty($match[1])) {
return; return '';
} }
$data = Contact::getDetailsByURL($match[1]); $data = Contact::getDetailsByURL($match[1]);
if (empty($data) || empty($data['nick'])) { if (empty($data['nick'])) {
return; return $match[0];
} }
return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
@ -1037,8 +1038,14 @@ class Transmitter
$data['name'] = BBCode::toPlaintext($item['title'], false); $data['name'] = BBCode::toPlaintext($item['title'], false);
} }
$permission_block = self::createPermissionBlockForItem($item, false);
$body = $item['body']; $body = $item['body'];
if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
$body = self::prependMentions($body, $permission_block);
}
if ($type == 'Note') { if ($type == 'Note') {
$body = self::removePictures($body); $body = self::removePictures($body);
} }
@ -1069,7 +1076,7 @@ class Transmitter
$data['generator'] = ['type' => 'Application', 'name' => $item['app']]; $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
} }
$data = array_merge($data, self::createPermissionBlockForItem($item, false)); $data = array_merge($data, $permission_block);
return $data; return $data;
} }
@ -1422,4 +1429,24 @@ class Transmitter
$signed = LDSignature::sign($data, $owner); $signed = LDSignature::sign($data, $owner);
HTTPSignature::transmit($signed, $profile['inbox'], $uid); 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);
}
} }