[UTIL][Formatting] Fix group mentions

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-27 21:16:39 +00:00
parent c40e38c5ba
commit f5b06e2c7e
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 46 additions and 46 deletions

View File

@ -475,21 +475,6 @@ class Actor extends Entity
return $url;
}
public static function getPlaceholderUri(string $nickname, int $type = self::PERSON, int $uri_type = Router::ABSOLUTE_URL): string
{
switch ($type) {
case self::PERSON:
case self::ORGANIZATION:
case self::BUSINESS:
case self::BOT:
return Router::url('actor_view_nickname', ['nickname' => $nickname], $uri_type);
case self::GROUP:
return Router::url('group_actor_view_nickname', ['nickname' => $nickname], $uri_type);
default:
throw new BugFoundException('Actor type added but `Actor::getPlaceholderUri` was not updated');
}
}
public function getAliases(): array
{
return array_keys($this->getAliasesWithIDs());

View File

@ -234,7 +234,7 @@ abstract class Formatting
{
$text = self::quoteAndRemoveControlCodes($text);
// Split \n\n into paragraphs, process each paragrah and merge
// Split \n\n into paragraphs, process each paragraph and merge
return implode("\n", F\map(explode("\n\n", $text), function (string $paragraph) use ($language) {
$paragraph = nl2br($paragraph, use_xhtml: false);
Event::handle('RenderPlainTextNoteContent', [&$paragraph, $language]);
@ -294,11 +294,12 @@ abstract class Formatting
// XXX: We remove <span> because when content is in html the tag comes as #<span>hashtag</span>
$text = str_replace('<span>', '', $text);
if (Event::handle('StartFindMentions', [$actor, $text, &$mentions])) {
$matches = self::findMentionsRaw($text, '@');
foreach ($matches as $match) {
// Person mentions
$person_matches = self::findMentionsRaw($text, '@');
foreach ($person_matches as $match) {
try {
$nickname = Nickname::normalize($match[0], check_already_used: false);
$nickname = Nickname::normalize($match[0], check_already_used: false, check_is_allowed: false);
} catch (NicknameException) {
// Bogus match? Drop it.
continue;
@ -354,21 +355,35 @@ abstract class Formatting
// 'url' => $url, ];
// }
// Group mentions
$group_matches = self::findMentionsRaw($text, '!');
foreach ($group_matches as $group_match) {
$nickname = Nickname::normalize($group_match[0], check_already_used: false, check_is_allowed: false);
$group = LocalGroup::getActorByNickname($nickname);
foreach ($group_matches as $match) {
try {
$nickname = Nickname::normalize($match[0], check_already_used: false, check_is_allowed: false);
} catch (NicknameException) {
// Bogus match? Drop it.
continue;
}
$mentioned = LocalGroup::getActorByNickname($nickname);
if ($mentioned instanceof Actor) {
$url = $mentioned->getUri(); // prefer the URI as URL, if it is one.
if (!Common::isValidHttpUrl($url)) {
$url = $mentioned->getUrl();
}
$mentions[] = [
'mentioned' => [$group],
'mentioned' => [$mentioned],
'type' => 'group',
'text' => $group_match[0],
'position' => $group_match[1],
'length' => mb_strlen($group_match[0]),
'url' => !\is_null($group) ? $group->getUri() : Actor::getPlaceholderUri($nickname, Actor::GROUP),
'title' => $group?->getFullname() ?? $group?->getNickname(),
'text' => $match[0],
'position' => $match[1],
'length' => mb_strlen($match[0]),
'title' => $mentioned?->getFullname() ?? $mentioned?->getNickname(),
'url' => $url,
];
}
}
Event::handle('EndFindMentions', [$actor, $text, &$mentions]);
}