forked from GNUsocial/gnu-social
[UTIL][Formatting] Fix group mentions
This commit is contained in:
parent
c40e38c5ba
commit
f5b06e2c7e
@ -475,21 +475,6 @@ class Actor extends Entity
|
|||||||
return $url;
|
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
|
public function getAliases(): array
|
||||||
{
|
{
|
||||||
return array_keys($this->getAliasesWithIDs());
|
return array_keys($this->getAliasesWithIDs());
|
||||||
|
@ -234,7 +234,7 @@ abstract class Formatting
|
|||||||
{
|
{
|
||||||
$text = self::quoteAndRemoveControlCodes($text);
|
$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) {
|
return implode("\n", F\map(explode("\n\n", $text), function (string $paragraph) use ($language) {
|
||||||
$paragraph = nl2br($paragraph, use_xhtml: false);
|
$paragraph = nl2br($paragraph, use_xhtml: false);
|
||||||
Event::handle('RenderPlainTextNoteContent', [&$paragraph, $language]);
|
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>
|
// XXX: We remove <span> because when content is in html the tag comes as #<span>hashtag</span>
|
||||||
$text = str_replace('<span>', '', $text);
|
$text = str_replace('<span>', '', $text);
|
||||||
if (Event::handle('StartFindMentions', [$actor, $text, &$mentions])) {
|
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 {
|
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) {
|
} catch (NicknameException) {
|
||||||
// Bogus match? Drop it.
|
// Bogus match? Drop it.
|
||||||
continue;
|
continue;
|
||||||
@ -354,21 +355,35 @@ abstract class Formatting
|
|||||||
// 'url' => $url, ];
|
// 'url' => $url, ];
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// Group mentions
|
||||||
$group_matches = self::findMentionsRaw($text, '!');
|
$group_matches = self::findMentionsRaw($text, '!');
|
||||||
foreach ($group_matches as $group_match) {
|
foreach ($group_matches as $match) {
|
||||||
$nickname = Nickname::normalize($group_match[0], check_already_used: false, check_is_allowed: false);
|
try {
|
||||||
$group = LocalGroup::getActorByNickname($nickname);
|
$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[] = [
|
$mentions[] = [
|
||||||
'mentioned' => [$group],
|
'mentioned' => [$mentioned],
|
||||||
'type' => 'group',
|
'type' => 'group',
|
||||||
'text' => $group_match[0],
|
'text' => $match[0],
|
||||||
'position' => $group_match[1],
|
'position' => $match[1],
|
||||||
'length' => mb_strlen($group_match[0]),
|
'length' => mb_strlen($match[0]),
|
||||||
'url' => !\is_null($group) ? $group->getUri() : Actor::getPlaceholderUri($nickname, Actor::GROUP),
|
'title' => $mentioned?->getFullname() ?? $mentioned?->getNickname(),
|
||||||
'title' => $group?->getFullname() ?? $group?->getNickname(),
|
'url' => $url,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Event::handle('EndFindMentions', [$actor, $text, &$mentions]);
|
Event::handle('EndFindMentions', [$actor, $text, &$mentions]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user