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

Comment mentions with spaces #11836

Merged
merged 2 commits into from
Nov 7, 2018
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
20 changes: 16 additions & 4 deletions apps/comments/js/commentstabview.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,22 @@
return;
}
var mention = '@' + mentions[i].mentionId;
if (mentions[i].mentionId.indexOf(' ') !== -1) {
mention = _.escape('@"' + mentions[i].mentionId + '"');
}

// escape possible regex characters in the name
mention = mention.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
var regex = new RegExp("(^|\\s)(" + mention + ")\\b", 'g');
if (mentions[i].mentionId.indexOf(' ') !== -1) {
regex = new RegExp("(^|\\s)(" + mention + ")", 'g');
}

var displayName = this._composeHTMLMention(mentions[i].mentionId, mentions[i].mentionDisplayName);

// replace every mention either at the start of the input or after a whitespace
// followed by a non-word character.
message = message.replace(new RegExp("(^|\\s)(" + mention + ")\\b", 'g'),
message = message.replace(regex,
function(match, p1) {
// to get number of whitespaces (0 vs 1) right
return p1+displayName;
Expand Down Expand Up @@ -602,9 +609,14 @@
var $comment = $el.clone();

$comment.find('.avatar-name-wrapper').each(function () {
var $this = $(this);
var $inserted = $this.parent();
$inserted.html('@' + $this.find('.avatar').data('username'));
var $this = $(this),
$inserted = $this.parent(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho this style is rather confusing than improving readability. Might be just me tho.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userId = $this.find('.avatar').data('username');
if (userId.indexOf(' ') !== -1) {
$inserted.html('@"' + userId + '"');
} else {
$inserted.html('@' + userId);
}
});

$comment.html(OCP.Comments.richToPlain($comment.html()));
Expand Down
7 changes: 6 additions & 1 deletion apps/comments/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,13 @@ protected function parseMessage(IEvent $event) {
continue;
}

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

$message = preg_replace(
'/(^|\s)(' . '@' . $mention['id'] . ')(\b)/',
$pattern,
//'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}',
'${1}' . '{mention' . $mentionCount . '}' . '${3}',
$message
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ public function setMessage($message) {
*
*/
public function getMentions() {
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@[a-z0-9_\-@\.\']+/i", $this->getMessage(), $mentions);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
return [];
}
$uids = array_unique($mentions[0]);
$result = [];
foreach ($uids as $uid) {
$result[] = ['type' => 'user', 'id' => substr($uid, 1)];
$result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')];
}
return $result;
}
Expand Down
7 changes: 5 additions & 2 deletions tests/lib/Comments/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ public function mentionsProvider() {
['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
],
[
'@@chef is also a valid mention, no matter how strange it looks', ['@chef']
]
'@@chef is also a valid mention, no matter how strange it looks', ['@chef']
],
[
'Also @"user with spaces" are now supported', ['user with spaces']
],
];
}

Expand Down