Skip to content

Commit b800eae

Browse files
authored
Fix period in remote usernames (#1533)
1 parent 68d2e03 commit b800eae

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/Service/MentionManager.php

+28-6
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,56 @@ function ($val) {
7878
);
7979
}
8080

81-
public function extract(?string $val, $type = self::ALL): ?array
81+
/**
82+
* Try to extract mentions from the body (eg. @[email protected]).
83+
*
84+
* @param val Body input string
85+
* @param type Type of mentions to extract (ALL, LOCAL only or REMOTE only)
86+
*
87+
* @return string[]
88+
*/
89+
public function extract(?string $body, $type = self::ALL): ?array
8290
{
83-
if (!$val) {
91+
if (!$body) {
8492
return null;
8593
}
8694

8795
$result = match ($type) {
88-
self::ALL => array_merge($this->byApPrefix($val), $this->byPrefix($val)),
89-
self::LOCAL => $this->byPrefix($val),
90-
self::REMOTE => $this->byApPrefix($val),
96+
self::ALL => array_merge($this->byApPrefix($body), $this->byPrefix($body)),
97+
self::LOCAL => $this->byPrefix($body),
98+
self::REMOTE => $this->byApPrefix($body),
9199
};
92100

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

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

106+
/**
107+
* Remote activitypub prefix, like @[email protected].
108+
*
109+
* @param value Input string
110+
*
111+
* @return string[]
112+
*/
98113
private function byApPrefix(string $value): array
99114
{
100115
preg_match_all(
101-
'/(?<!\/)\B@(\w{1,30})(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u',
116+
'/(?<!\/)\B@([a-zA-Z0-9._-]+@?)(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u',
102117
$value,
103118
$matches
104119
);
105120

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

124+
/**
125+
* Local username prefix, like @username.
126+
*
127+
* @param value Input string
128+
*
129+
* @return string[]
130+
*/
109131
private function byPrefix(string $value): array
110132
{
111133
preg_match_all('/(?<!\/)\B@([a-zA-Z0-9_-]{1,30}@?)/u', $value, $matches);

0 commit comments

Comments
 (0)