bugdown: Refactor get_possible_mentions_info and related functions.

This commit changes the return type of get_possible_mentions_info  to a
list instead of a dict, thus disposing off the hacky logic of storing
users with duplicate full names with name|id keys that made the code
obfuscated.

The other functions continue to use the dicts as before, however, there
are minor variable changes where needed in accordance with the updated
definition of get_possible_mentions_info.
This commit is contained in:
Rohitt Vashishtha
2018-11-02 08:15:46 +00:00
committed by Tim Abbott
parent e4946dd182
commit 681368b937
2 changed files with 28 additions and 23 deletions

View File

@@ -1877,21 +1877,24 @@ def get_email_info(realm_id: int, emails: Set[str]) -> Dict[str, FullNameInfo]:
}
return dct
def get_possible_mentions_info(realm_id: int, mention_texts: Set[str]) -> Dict[str, FullNameInfo]:
def get_possible_mentions_info(realm_id: int, mention_texts: Set[str]) -> List[FullNameInfo]:
if not mention_texts:
return dict()
return list()
# Remove the trailing part of the `user|id` mention syntax.
# Remove the trailing part of the `name|id` mention syntax,
# thus storing only full names in full_names.
full_names = set()
name_re = r'(?P<full_name>.+)\|\d+$'
for mention_text in mention_texts.copy():
for mention_text in mention_texts:
name_syntax_match = re.match(name_re, mention_text)
if name_syntax_match:
mention_texts.remove(mention_text)
mention_texts.add(name_syntax_match.group("full_name"))
full_names.add(name_syntax_match.group("full_name"))
else:
full_names.add(mention_text)
q_list = {
Q(full_name__iexact=mention_text)
for mention_text in mention_texts
Q(full_name__iexact=full_name)
for full_name in full_names
}
rows = UserProfile.objects.filter(
@@ -1904,22 +1907,19 @@ def get_possible_mentions_info(realm_id: int, mention_texts: Set[str]) -> Dict[s
'full_name',
'email',
)
dct = {} # type: Dict[str, FullNameInfo]
for row in rows:
key = row['full_name'].lower()
# To insert users with duplicate full names in the dict
if key in dct:
key = '{}|{}'.format(key, row['id'])
dct[key] = row
return dct
return list(rows)
class MentionData:
def __init__(self, realm_id: int, content: str) -> None:
mention_texts = possible_mentions(content)
self.full_name_info = get_possible_mentions_info(realm_id, mention_texts)
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 self.full_name_info.values()
for row in possible_mentions_info
}
self.init_user_group_data(realm_id=realm_id, content=content)

View File

@@ -199,16 +199,21 @@ class BugdownMiscTest(ZulipTestCase):
fred3.save()
fred4 = make_user('fred4@example.com', 'Fred Flintstone')
fred4_key = 'fred flintstone|{}'.format(fred4.id)
dct = bugdown.get_possible_mentions_info(realm.id, {'Fred Flintstone', 'cordelia LEAR', 'Not A User'})
self.assertEqual(set(dct.keys()), {'fred flintstone', fred4_key, 'cordelia lear'})
self.assertEqual(dct['fred flintstone'], dict(
lst = bugdown.get_possible_mentions_info(realm.id, {'Fred Flintstone', 'cordelia LEAR', 'Not A User'})
set_of_names = set(map(lambda x: x['full_name'].lower(), lst))
self.assertEqual(set_of_names, {'fred flintstone', 'cordelia lear'})
by_id = {
row['id']: row
for row in lst
}
self.assertEqual(by_id.get(fred2.id), dict(
email='fred2@example.com',
full_name='Fred Flintstone',
id=fred2.id
))
self.assertEqual(dct[fred4_key], dict(
self.assertEqual(by_id.get(fred4.id), dict(
email='fred4@example.com',
full_name='Fred Flintstone',
id=fred4.id