Skip to content

Fix period in remote usernames #1533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/Service/MentionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,56 @@ function ($val) {
);
}

public function extract(?string $val, $type = self::ALL): ?array
/**
* Try to extract mentions from the body (eg. @[email protected]).
*
* @param val Body input string
* @param type Type of mentions to extract (ALL, LOCAL only or REMOTE only)
*
* @return string[]
*/
public function extract(?string $body, $type = self::ALL): ?array
{
if (!$val) {
if (!$body) {
return null;
}

$result = match ($type) {
self::ALL => array_merge($this->byApPrefix($val), $this->byPrefix($val)),
self::LOCAL => $this->byPrefix($val),
self::REMOTE => $this->byApPrefix($val),
self::ALL => array_merge($this->byApPrefix($body), $this->byPrefix($body)),
self::LOCAL => $this->byPrefix($body),
self::REMOTE => $this->byApPrefix($body),
};

$result = array_map(fn ($val) => trim($val), $result);

return \count($result) ? array_unique($result) : null;
}

/**
* Remote activitypub prefix, like @[email protected].
*
* @param value Input string
*
* @return string[]
*/
private function byApPrefix(string $value): array
{
preg_match_all(
'/(?<!\/)\B@(\w{1,30})(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u',
'/(?<!\/)\B@([a-zA-Z0-9._-]+@?)(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u',
$value,
$matches
);

return \count($matches[0]) ? array_unique(array_values($matches[0])) : [];
}

/**
* Local username prefix, like @username.
*
* @param value Input string
*
* @return string[]
*/
private function byPrefix(string $value): array
{
preg_match_all('/(?<!\/)\B@([a-zA-Z0-9_-]{1,30}@?)/u', $value, $matches);
Expand Down
Loading