diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 61c9032fce..bb6adb3379 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -1376,7 +1376,13 @@ class UserMentionPattern(markdown.inlinepatterns.Pattern): return None wildcard = mention.user_mention_matches_wildcard(name) - user = arguments.db_data['mention_data'].get_user(name) + + id_syntax_match = re.match(r'.+\|(?P\d+)$', name) + if id_syntax_match: + id = id_syntax_match.group("user_id") + user = arguments.db_data['mention_data'].get_user_by_id(id) + else: + user = arguments.db_data['mention_data'].get_user(name) if wildcard: arguments.current_message.mentions_wildcard = True diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index 8bbe8a5539..94ad6f1d82 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -968,6 +968,37 @@ class BugdownTest(ZulipTestCase): 'check this out

' % (hamlet.id, cordelia.id)) self.assertEqual(msg.mentions_user_ids, set([hamlet.id, cordelia.id])) + def test_mention_duplicate_full_name(self) -> None: + realm = get_realm('zulip') + + def make_user(email: str, full_name: str) -> UserProfile: + return create_user( + email=email, + password='whatever', + realm=realm, + full_name=full_name, + short_name='whatever', + ) + + sender_user_profile = self.example_user('othello') + twin1 = make_user('twin1@example.com', 'Mark Twin') + twin2 = make_user('twin2@example.com', 'Mark Twin') + cordelia = self.example_user('cordelia') + msg = Message(sender=sender_user_profile, sending_client=get_client("test")) + + content = "@**Mark Twin|{}**, @**Mark Twin|{}** and @**Cordelia Lear**, hi.".format(twin1.id, twin2.id) + + self.assertEqual(render_markdown(msg, content), + '

' + '@Mark Twin, ' + '@Mark Twin and ' + '@Cordelia Lear, ' + 'hi.

' % (twin1.id, twin2.id, cordelia.id)) + self.assertEqual(msg.mentions_user_ids, set([twin1.id, twin2.id, cordelia.id])) + def test_mention_invalid(self) -> None: sender_user_profile = self.example_user('othello') msg = Message(sender=sender_user_profile, sending_client=get_client("test"))