Skip to content

Commit 3aba094

Browse files
authored
Merge pull request #25573 from nextcloud/backport/25378/stable21
[stable21] Improve mention matches
2 parents b4b5561 + 5164f43 commit 3aba094

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

apps/comments/lib/Activity/Provider.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,11 @@ protected function parseMessage(IEvent $event) {
213213
continue;
214214
}
215215

216-
$pattern = '/(^|\s)(' . '@' . $mention['id'] . ')(\b)/';
217-
if (strpos($mention['id'], ' ') !== false) {
218-
$pattern = '/(^|\s)(' . '@"' . $mention['id'] . '"' . ')(\b)?/';
216+
$message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
217+
if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) {
218+
$message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
219219
}
220220

221-
$message = preg_replace(
222-
$pattern,
223-
//'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}',
224-
'${1}' . '{mention' . $mentionCount . '}' . '${3}',
225-
$message
226-
);
227221
$mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
228222
$mentionCount++;
229223
}

apps/comments/lib/Notification/Notifier.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ public function commentToRichMessage(IComment $comment): array {
195195
// could contain characters like '@' for user IDs) but a one-based
196196
// index of the mentions of that type.
197197
$mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']];
198-
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
198+
$message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message);
199+
if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) {
200+
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
201+
}
202+
199203
try {
200204
$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
201205
} catch (\OutOfBoundsException $e) {

lib/private/Comments/Comment.php

+3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public function getMentions() {
233233
return [];
234234
}
235235
$uids = array_unique($mentions[0]);
236+
usort($uids, static function ($uid1, $uid2) {
237+
return mb_strlen($uid2) <=> mb_strlen($uid1);
238+
});
236239
$result = [];
237240
foreach ($uids as $uid) {
238241
$cleanUid = trim(substr($uid, 1), '"');

tests/lib/Comments/CommentTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testSettersValidInput() {
5151
$this->assertSame($object['id'], $comment->getObjectId());
5252
}
5353

54-
54+
5555
public function testSetIdIllegalInput() {
5656
$this->expectException(\OCP\Comments\IllegalIDChangeException::class);
5757

@@ -120,7 +120,7 @@ public function testSetRoleInvalidInput($role, $type, $id) {
120120
$comment->$setter($type, $id);
121121
}
122122

123-
123+
124124
public function testSetUberlongMessage() {
125125
$this->expectException(\OCP\Comments\MessageTooLongException::class);
126126

@@ -149,7 +149,7 @@ public function mentionsProvider() {
149149
' cc @23452-4333-54353-2342 @yolo!' .
150150
' however the most important thing to know is that www.croissant.com/@oil is not valid' .
151151
' and won\'t match anything at all',
152-
['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
152+
['bar@foo.org@foobar.io', '23452-4333-54353-2342', 'foo@bar.com', 'foobar', 'barfoo', 'yolo']
153153
],
154154
[
155155
'@@chef is also a valid mention, no matter how strange it looks', ['@chef']

0 commit comments

Comments
 (0)