mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 20:44:04 +00:00
refactor: Convert FullNameInfo to a dataclass.
As part of this we no longer query for email, which is a vestige of when we used emails to identify users on the frontend.
This commit is contained in:
@@ -54,7 +54,7 @@ from zerver.lib.emoji import EMOTICON_RE, codepoint_to_name, name_to_codepoint,
|
||||
from zerver.lib.exceptions import MarkdownRenderingException
|
||||
from zerver.lib.markdown import fenced_code
|
||||
from zerver.lib.markdown.fenced_code import FENCE_RE
|
||||
from zerver.lib.mention import MentionData, get_stream_name_map
|
||||
from zerver.lib.mention import FullNameInfo, MentionData, get_stream_name_map
|
||||
from zerver.lib.outgoing_http import OutgoingSession
|
||||
from zerver.lib.subdomains import is_static_or_current_realm_url
|
||||
from zerver.lib.tex import render_tex
|
||||
@@ -1865,7 +1865,7 @@ class UserMentionPattern(CompiledInlineProcessor):
|
||||
# This enforces our decision that
|
||||
# @**user_1_name|id_for_user_2** should be invalid syntax.
|
||||
if full_name:
|
||||
if user and user["full_name"] != full_name:
|
||||
if user and user.full_name != full_name:
|
||||
return None, None, None
|
||||
else:
|
||||
# For @**name** syntax.
|
||||
@@ -1875,11 +1875,13 @@ class UserMentionPattern(CompiledInlineProcessor):
|
||||
if not silent:
|
||||
self.md.zulip_rendering_result.mentions_wildcard = True
|
||||
user_id = "*"
|
||||
elif user:
|
||||
elif user is not None:
|
||||
assert isinstance(user, FullNameInfo)
|
||||
|
||||
if not silent:
|
||||
self.md.zulip_rendering_result.mentions_user_ids.add(user["id"])
|
||||
name = user["full_name"]
|
||||
user_id = str(user["id"])
|
||||
self.md.zulip_rendering_result.mentions_user_ids.add(user.id)
|
||||
name = user.full_name
|
||||
user_id = str(user.id)
|
||||
else:
|
||||
# Don't highlight @mentions that don't refer to a valid user
|
||||
return None, None, None
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import functools
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, List, Match, Optional, Set, Tuple
|
||||
|
||||
from django.db.models import Q
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from zerver.models import Realm, UserGroup, UserProfile, get_active_streams
|
||||
|
||||
@@ -15,9 +15,9 @@ USER_GROUP_MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*(?P<m
|
||||
wildcards = ["all", "everyone", "stream"]
|
||||
|
||||
|
||||
class FullNameInfo(TypedDict):
|
||||
@dataclass
|
||||
class FullNameInfo:
|
||||
id: int
|
||||
email: str
|
||||
full_name: str
|
||||
|
||||
|
||||
@@ -80,21 +80,20 @@ def get_possible_mentions_info(realm_id: int, mention_texts: Set[str]) -> List[F
|
||||
.filter(
|
||||
functools.reduce(lambda a, b: a | b, q_list),
|
||||
)
|
||||
.values(
|
||||
.only(
|
||||
"id",
|
||||
"full_name",
|
||||
"email",
|
||||
)
|
||||
)
|
||||
return list(rows)
|
||||
return [FullNameInfo(id=row.id, full_name=row.full_name) for row in rows]
|
||||
|
||||
|
||||
class MentionData:
|
||||
def __init__(self, realm_id: int, content: str) -> None:
|
||||
mention_texts, has_wildcards = possible_mentions(content)
|
||||
possible_mentions_info = get_possible_mentions_info(realm_id, mention_texts)
|
||||
self.full_name_info = {row["full_name"].lower(): row for row in possible_mentions_info}
|
||||
self.user_id_info = {row["id"]: row for row in possible_mentions_info}
|
||||
self.full_name_info = {row.full_name.lower(): row for row in possible_mentions_info}
|
||||
self.user_id_info = {row.id: row for row in possible_mentions_info}
|
||||
self.init_user_group_data(realm_id=realm_id, content=content)
|
||||
self.has_wildcards = has_wildcards
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ from zerver.lib.markdown import (
|
||||
from zerver.lib.markdown.fenced_code import FencedBlockPreprocessor
|
||||
from zerver.lib.mdiff import diff_strings
|
||||
from zerver.lib.mention import (
|
||||
FullNameInfo,
|
||||
MentionData,
|
||||
get_possible_mentions_info,
|
||||
possible_mentions,
|
||||
@@ -234,22 +235,20 @@ class MarkdownMiscTest(ZulipTestCase):
|
||||
lst = get_possible_mentions_info(
|
||||
realm.id, {"Fred Flintstone", "Cordelia, LEAR's daughter", "Not A User"}
|
||||
)
|
||||
set_of_names = set(map(lambda x: x["full_name"].lower(), lst))
|
||||
set_of_names = set(map(lambda x: x.full_name.lower(), lst))
|
||||
self.assertEqual(set_of_names, {"fred flintstone", "cordelia, lear's daughter"})
|
||||
|
||||
by_id = {row["id"]: row for row in lst}
|
||||
by_id = {row.id: row for row in lst}
|
||||
self.assertEqual(
|
||||
by_id.get(fred2.id),
|
||||
dict(
|
||||
email=fred2.email,
|
||||
FullNameInfo(
|
||||
full_name="Fred Flintstone",
|
||||
id=fred2.id,
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
by_id.get(fred4.id),
|
||||
dict(
|
||||
email=fred4.email,
|
||||
FullNameInfo(
|
||||
full_name="Fred Flintstone",
|
||||
id=fred4.id,
|
||||
),
|
||||
@@ -264,8 +263,7 @@ class MarkdownMiscTest(ZulipTestCase):
|
||||
self.assertEqual(mention_data.get_user_ids(), {hamlet.id, cordelia.id})
|
||||
self.assertEqual(
|
||||
mention_data.get_user_by_id(hamlet.id),
|
||||
dict(
|
||||
email=hamlet.email,
|
||||
FullNameInfo(
|
||||
full_name=hamlet.full_name,
|
||||
id=hamlet.id,
|
||||
),
|
||||
@@ -273,7 +271,7 @@ class MarkdownMiscTest(ZulipTestCase):
|
||||
|
||||
user = mention_data.get_user_by_name("king hamLET")
|
||||
assert user is not None
|
||||
self.assertEqual(user["email"], hamlet.email)
|
||||
self.assertEqual(user.full_name, hamlet.full_name)
|
||||
|
||||
self.assertFalse(mention_data.message_has_wildcards())
|
||||
content = "@**King Hamlet** @**Cordelia, lear's daughter** @**all**"
|
||||
|
||||
Reference in New Issue
Block a user