From 1344c61145c2a9eb36c1c211daa9018f00693c7c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 29 Jan 2021 09:46:14 +0100 Subject: [PATCH] Improve mention matches The previous regex didn't correctly match users with . ' or - Signed-off-by: Joas Schilling --- apps/comments/lib/Activity/Provider.php | 12 +++--------- apps/comments/lib/Notification/Notifier.php | 6 +++++- lib/private/Comments/Comment.php | 1 + 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/apps/comments/lib/Activity/Provider.php b/apps/comments/lib/Activity/Provider.php index 527c9c4ca6..fca3b9e75d 100644 --- a/apps/comments/lib/Activity/Provider.php +++ b/apps/comments/lib/Activity/Provider.php @@ -213,17 +213,11 @@ class Provider implements IProvider { 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++; } diff --git a/apps/comments/lib/Notification/Notifier.php b/apps/comments/lib/Notification/Notifier.php index 9f69e5599f..20e0667d14 100644 --- a/apps/comments/lib/Notification/Notifier.php +++ b/apps/comments/lib/Notification/Notifier.php @@ -195,7 +195,11 @@ class Notifier implements INotifier { // 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) { diff --git a/lib/private/Comments/Comment.php b/lib/private/Comments/Comment.php index 7368425174..c838e9571c 100644 --- a/lib/private/Comments/Comment.php +++ b/lib/private/Comments/Comment.php @@ -233,6 +233,7 @@ class Comment implements IComment { return []; } $uids = array_unique($mentions[0]); + usort($uids, 'mb_strlen'); $result = []; foreach ($uids as $uid) { $cleanUid = trim(substr($uid, 1), '"');