Skip to content

Commit cdec135

Browse files
committed
BugFix: Comparing users based on user_id than user_name.
Fixes #1151 Added a function named user_name_count in model.py and a variable user_name_dict_count that would keep count of all the usernames in zulip-terminal in a dict with key as the username and value as the count of people having the same username. Adding a check in the boxes.py to compare based on user_id instead of username so that users with same names are shown with different headers. Also added a check if someone else with same username already exists so common usernames could be displayed with their user_id appended to the name. Changing label name from "name" to "msg_sender" in every theme test_ui_tools: changing test cases and parameterized cases to add sender_id model.py: Changing the function to return just if a user with same name exists or not instead of returning the number of users with same name so that it passed the written test cases boxes.py: Adding a style class so the id appended to the name doesn't look like it's a part of the name. Changing the type of "textType" so that it supports the test type of Tuple[Optional[str], str].
1 parent d3ab069 commit cdec135

File tree

9 files changed

+45
-26
lines changed

9 files changed

+45
-26
lines changed

tests/ui/test_ui_tools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,7 @@ def test_main_view(self, mocker, message, last_message):
19831983
[
19841984
{
19851985
"id": 4,
1986+
"sender_id": 4,
19861987
"type": "stream",
19871988
"display_recipient": "Verona",
19881989
"stream_id": 5,
@@ -2021,6 +2022,7 @@ def test_main_view_renders_slash_me(self, mocker, message, content, is_me_messag
20212022
[
20222023
{
20232024
"id": 4,
2025+
"sender_id": 4,
20242026
"type": "stream",
20252027
"display_recipient": "Verona",
20262028
"stream_id": 5,
@@ -2196,6 +2198,7 @@ def test_msg_generates_search_and_header_bar(
21962198
[
21972199
{
21982200
"id": 4,
2201+
"sender_id": 4,
21992202
"type": "stream",
22002203
"display_recipient": "Verona",
22012204
"stream_id": 5,
@@ -2221,11 +2224,11 @@ def test_msg_generates_search_and_header_bar(
22212224
"expected_header, to_vary_in_last_message",
22222225
[
22232226
(
2224-
[STATUS_INACTIVE, "alice", " ", "DAYDATETIME"],
2227+
[STATUS_INACTIVE, "alice(4)", " ", "DAYDATETIME"],
22252228
{"sender_full_name": "bob"},
22262229
),
22272230
([" ", " ", " ", "DAYDATETIME"], {"timestamp": 1532103779}),
2228-
([STATUS_INACTIVE, "alice", " ", "DAYDATETIME"], {"timestamp": 0}),
2231+
([STATUS_INACTIVE, "alice(4)", " ", "DAYDATETIME"], {"timestamp": 0}),
22292232
],
22302233
ids=[
22312234
"show_author_as_authors_different",

zulipterminal/config/themes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'header' : 'bold',
3030
'general_narrow' : 'standout',
3131
'general_bar' : '',
32-
'name' : '',
32+
'msg_sender' : '',
3333
'unread' : 'strikethrough',
3434
'user_active' : 'bold',
3535
'user_idle' : '',

zulipterminal/model.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import json
66
import time
7-
from collections import OrderedDict, defaultdict
7+
from collections import Counter, OrderedDict, defaultdict
88
from concurrent.futures import Future, ThreadPoolExecutor, wait
99
from copy import deepcopy
1010
from datetime import datetime
@@ -1018,6 +1018,7 @@ def get_all_users(self) -> List[Dict[str, Any]]:
10181018
# and a user-id to email mapping
10191019
self.user_dict: Dict[str, Dict[str, Any]] = dict()
10201020
self.user_id_email_dict: Dict[int, str] = dict()
1021+
self.user_name_dict_count: Dict[str, int] = dict()
10211022
for user in self.initial_data["realm_users"]:
10221023
if self.user_id == user["user_id"]:
10231024
self._all_users_by_id[self.user_id] = user
@@ -1130,7 +1131,9 @@ def get_all_users(self) -> List[Dict[str, Any]]:
11301131
user_list.insert(0, current_user)
11311132
self.user_dict[current_user["email"]] = current_user
11321133
self.user_id_email_dict[self.user_id] = current_user["email"]
1133-
1134+
self.user_name_dict_count = Counter(
1135+
user["full_name"] for user in user_list
1136+
) # Counting number of users having same name
11341137
return user_list
11351138

11361139
def user_name_from_id(self, user_id: int) -> str:
@@ -1144,6 +1147,12 @@ def user_name_from_id(self, user_id: int) -> str:
11441147

11451148
return self.user_dict[user_email]["full_name"]
11461149

1150+
def user_name_count_compare(self, user_name: str) -> int:
1151+
"""
1152+
Returns if the count of the users with the same name is more than 1.
1153+
"""
1154+
return True if self.user_name_dict_count.get(user_name, -1) > 1 else False
1155+
11471156
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
11481157
def make_reduced_stream_data(stream: Subscription) -> StreamData:
11491158
# stream_id has been changed to id.

zulipterminal/themes/gruvbox_dark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'header' : (Color.NEUTRAL_BLUE, Color.BRIGHT_BLUE),
2525
'general_narrow' : (Color.DARK0_HARD, Color.BRIGHT_BLUE),
2626
'general_bar' : (Color.LIGHT2, Color.DARK0_HARD),
27-
'name' : (Color.NEUTRAL_YELLOW__BOLD, Color.DARK0_HARD),
27+
'msg_sender' : (Color.NEUTRAL_YELLOW__BOLD, Color.DARK0_HARD),
2828
'unread' : (Color.NEUTRAL_PURPLE, Color.DARK0_HARD),
2929
'user_active' : (Color.BRIGHT_GREEN, Color.DARK0_HARD),
3030
'user_idle' : (Color.NEUTRAL_YELLOW, Color.DARK0_HARD),

zulipterminal/themes/gruvbox_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
'header' : (Color.NEUTRAL_BLUE, Color.FADED_BLUE),
2424
'general_narrow' : (Color.LIGHT0_HARD, Color.FADED_BLUE),
2525
'general_bar' : (Color.DARK2, Color.LIGHT0_HARD),
26-
'name' : (Color.NEUTRAL_YELLOW, Color.LIGHT0_HARD),
26+
'msg_sender' : (Color.NEUTRAL_YELLOW, Color.LIGHT0_HARD),
2727
'unread' : (Color.NEUTRAL_PURPLE, Color.LIGHT0_HARD),
2828
'user_active' : (Color.FADED_GREEN, Color.LIGHT0_HARD),
2929
'user_idle' : (Color.NEUTRAL_YELLOW, Color.LIGHT0_HARD),

zulipterminal/themes/zt_blue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'header' : (Color.BLACK, Color.DARK_BLUE),
1919
'general_narrow' : (Color.WHITE, Color.DARK_BLUE),
2020
'general_bar' : (Color.DARK_BLUE, Color.LIGHT_BLUE),
21-
'name' : (Color.DARK_RED, Color.LIGHT_BLUE),
21+
'msg_sender' : (Color.DARK_RED, Color.LIGHT_BLUE),
2222
'unread' : (Color.LIGHT_GRAY, Color.LIGHT_BLUE),
2323
'user_active' : (Color.LIGHT_GREEN__BOLD, Color.LIGHT_BLUE),
2424
'user_idle' : (Color.DARK_GRAY, Color.LIGHT_BLUE),

zulipterminal/themes/zt_dark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'header' : (Color.DARK_CYAN, Color.DARK_BLUE),
1919
'general_narrow' : (Color.WHITE, Color.DARK_BLUE),
2020
'general_bar' : (Color.WHITE, Color.BLACK),
21-
'name' : (Color.YELLOW__BOLD, Color.BLACK),
21+
'msg_sender' : (Color.YELLOW__BOLD, Color.BLACK),
2222
'unread' : (Color.DARK_BLUE, Color.BLACK),
2323
'user_active' : (Color.LIGHT_GREEN, Color.BLACK),
2424
'user_idle' : (Color.YELLOW, Color.BLACK),

zulipterminal/themes/zt_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'header' : (Color.WHITE, Color.DARK_BLUE),
1919
'general_narrow' : (Color.WHITE, Color.DARK_BLUE),
2020
'general_bar' : (Color.DARK_BLUE, Color.WHITE),
21-
'name' : (Color.DARK_GREEN, Color.WHITE),
21+
'msg_sender' : (Color.DARK_GREEN, Color.WHITE),
2222
'unread' : (Color.DARK_GRAY, Color.LIGHT_GRAY),
2323
'user_active' : (Color.DARK_GREEN, Color.WHITE),
2424
'user_idle' : (Color.DARK_BLUE, Color.WHITE),

zulipterminal/ui_tools/boxes.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ def __init__(self, view: Any) -> None:
7676
super().__init__(self.main_view(True))
7777
self.model = view.model
7878
self.view = view
79-
8079
# Used to indicate user's compose status, "closed" by default
8180
self.compose_box_status: Literal[
8281
"open_with_private", "open_with_stream", "closed"
@@ -575,7 +574,9 @@ def autocomplete_mentions(
575574
return combined_typeahead, combined_names
576575

577576
def autocomplete_users(
578-
self, text: str, prefix_string: str
577+
self,
578+
text: str,
579+
prefix_string: str,
579580
) -> Tuple[List[str], List[str]]:
580581
users_list = self.view.users
581582
matching_users = [
@@ -1530,9 +1531,7 @@ def main_view(self) -> List[Any]:
15301531
"author": (
15311532
msg["sender_full_name"] if "sender_full_name" in msg else None
15321533
),
1533-
"author_id":(
1534-
msg["sender_id"] if "sender_id" in msg else None
1535-
),
1534+
"author_id": (msg["sender_id"] if "sender_id" in msg else None),
15361535
"time": (
15371536
self.model.formatted_local_time(
15381537
msg["timestamp"], show_seconds=False
@@ -1550,7 +1549,8 @@ def main_view(self) -> List[Any]:
15501549
}
15511550
different = { # How this message differs from the previous one
15521551
"recipients": recipient_header is not None,
1553-
"author": message["this"]["author_id"] != message["last"]["author_id"],
1552+
"author": message["this"]["author"] != message["last"]["author"],
1553+
"author_id": message["this"]["author_id"] != message["last"]["author_id"],
15541554
"24h": (
15551555
message["last"]["datetime"] is not None
15561556
and ((message["this"]["datetime"] - message["last"]["datetime"]).days)
@@ -1566,12 +1566,19 @@ def main_view(self) -> List[Any]:
15661566
any_differences = any(different.values())
15671567

15681568
if any_differences: # Construct content_header, if needed
1569-
TextType = Dict[str, urwid_MarkupTuple]
1570-
text_keys = ("author", "star", "time", "status")
1571-
text: TextType = {key: (None, " ") for key in text_keys}
1572-
1573-
if any(different[key] for key in ("recipients", "author", "24h")):
1574-
text["author"] = ("name", message["this"]["author"])
1569+
TextType = Dict[str, List[urwid_MarkupTuple]]
1570+
text_keys = ("author", "star", "time", "status", "author_id")
1571+
text: TextType = {key: [(None, " ")] for key in text_keys}
1572+
if any(
1573+
different[key] for key in ("recipients", "author", "24h", "author_id")
1574+
):
1575+
if self.model.user_name_count_compare(message["this"]["author"]):
1576+
text["author"] = [
1577+
("msg_sender", message["this"]["author"]),
1578+
("msg_mention", "(" + str(message["this"]["author_id"]) + ")"),
1579+
]
1580+
else:
1581+
text["author"] = [("msg_sender", message["this"]["author"])]
15751582

15761583
# TODO: Refactor to use user ids for look up instead of emails.
15771584
email = self.message.get("sender_email", "")
@@ -1582,17 +1589,17 @@ def main_view(self) -> List[Any]:
15821589

15831590
# The default text['status'] value is (None, ' ')
15841591
if status in STATE_ICON:
1585-
text["status"] = (f"user_{status}", STATE_ICON[status])
1592+
text["status"] = [(f"user_{status}", STATE_ICON[status])]
15861593

15871594
if message["this"]["is_starred"]:
1588-
text["star"] = ("starred", "*")
1595+
text["star"] = [("starred", "*")]
15891596
if any(different[key] for key in ("recipients", "author", "timestamp")):
15901597
this_year = date.today().year
15911598
msg_year = message["this"]["datetime"].year
15921599
if this_year != msg_year:
1593-
text["time"] = ("time", f"{msg_year} - {message['this']['time']}")
1600+
text["time"] = [("time", f"{msg_year} - {message['this']['time']}")]
15941601
else:
1595-
text["time"] = ("time", message["this"]["time"])
1602+
text["time"] = [("time", message["this"]["time"])]
15961603

15971604
content_header = urwid.Columns(
15981605
[

0 commit comments

Comments
 (0)