Skip to content
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

[stable21] Improve mention matches #25573

Merged
merged 4 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 3 additions & 9 deletions apps/comments/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,11 @@ protected function parseMessage(IEvent $event) {
continue;
}

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

$message = preg_replace(
$pattern,
//'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}',
'${1}' . '{mention' . $mentionCount . '}' . '${3}',
$message
);
$mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
$mentionCount++;
}
Expand Down
6 changes: 5 additions & 1 deletion apps/comments/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ public function commentToRichMessage(IComment $comment): array {
// could contain characters like '@' for user IDs) but a one-based
// index of the mentions of that type.
$mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']];
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
$message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message);
if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) {
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
}

try {
$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
} catch (\OutOfBoundsException $e) {
Expand Down
3 changes: 3 additions & 0 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ public function getMentions() {
return [];
}
$uids = array_unique($mentions[0]);
usort($uids, static function ($uid1, $uid2) {
return mb_strlen($uid2) <=> mb_strlen($uid1);
});
$result = [];
foreach ($uids as $uid) {
$cleanUid = trim(substr($uid, 1), '"');
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/Comments/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testSettersValidInput() {
$this->assertSame($object['id'], $comment->getObjectId());
}


public function testSetIdIllegalInput() {
$this->expectException(\OCP\Comments\IllegalIDChangeException::class);

Expand Down Expand Up @@ -120,7 +120,7 @@ public function testSetRoleInvalidInput($role, $type, $id) {
$comment->$setter($type, $id);
}


public function testSetUberlongMessage() {
$this->expectException(\OCP\Comments\MessageTooLongException::class);

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