Skip to content

Commit ad54cb0

Browse files
committed
Update boxes.py
Updating boxes.py to pass lint check Fixes #1151 Update boxes.py made users_list global by adding global keyword Update boxes.py user names now show id even when not adjacent Update boxes.py globally defined user_names_counter so it needs to be calculated only once Update boxes.py cleaning up the code a bit Update boxes.py black reformatting made function in model.py to get username count created a class variable in model class so that username count dict is calculated only once and using it to get number of users with same name and display id beside their name black reformatting black lint isort isort import order squashing all commits in one Updating boxes.py to pass lint check Fixes #1151 Update boxes.py made users_list global by adding global keyword Update boxes.py user names now show id even when not adjacent Update boxes.py globally defined user_names_counter so it needs to be calculated only once Update boxes.py cleaning up the code a bit made function in model.py to get username count created a class variable in model class so that username count dict is calculated only once and using it to get number of users with same name and display id beside their name isort isort import order Update boxes.py Updating boxes.py to pass lint check Fixes #1151 Update boxes.py made users_list global by adding global keyword Update boxes.py user names now show id even when not adjacent Update boxes.py globally defined user_names_counter so it needs to be calculated only once Update boxes.py cleaning up the code a bit Update boxes.py black reformatting made function in model.py to get username count created a class variable in model class so that username count dict is calculated only once and using it to get number of users with same name and display id beside their name black reformatting black lint isort isort import order reformatting using .get() instead of the key and removing commented out code squashing commits reformatting using .get() instead of the key and removing commented out code
1 parent e2b0a06 commit ad54cb0

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

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(self, user_name: str) -> int:
1151+
"""
1152+
Returns the count of the users with the same name as the given name.
1153+
"""
1154+
return self.user_name_dict_count.get(user_name, -1)
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/ui_tools/boxes.py

Lines changed: 38 additions & 11 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,9 +574,11 @@ 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]]:
580-
users_list = self.view.users
581+
# users_list = self.view.users
581582
matching_users = [
582583
user for user in users_list if match_user(user, text[len(prefix_string) :])
583584
]
@@ -1512,6 +1513,23 @@ def soup2markup(
15121513
markup.extend(cls.soup2markup(element, metadata)[0])
15131514
return markup, metadata["message_links"], metadata["time_mentions"]
15141515

1516+
# Function to count same number of users
1517+
def _count_same_users(author: str) -> Counter:
1518+
# users_list = self.view.users
1519+
matching_users = [user for user in users_list if match_user(user, author)]
1520+
matching_ids = set([user["user_id"] for user in matching_users])
1521+
matching_recipient_ids = set(self.recipient_user_ids) & set(matching_ids)
1522+
# Display subscribed users/recipients first.
1523+
sorted_matching_users = sorted(
1524+
matching_users,
1525+
key=lambda user: user["user_id"] in matching_recipient_ids,
1526+
reverse=True,
1527+
)
1528+
1529+
user_names = [user["full_name"] for user in sorted_matching_users]
1530+
user_names_counter = Counter(user_names)
1531+
return user_names_counter
1532+
15151533
def main_view(self) -> List[Any]:
15161534

15171535
# Recipient Header
@@ -1530,9 +1548,7 @@ def main_view(self) -> List[Any]:
15301548
"author": (
15311549
msg["sender_full_name"] if "sender_full_name" in msg else None
15321550
),
1533-
"author_id":(
1534-
msg["sender_id"] if "sender_id" in msg else None
1535-
),
1551+
"author_id": (msg["sender_id"] if "sender_id" in msg else None),
15361552
"time": (
15371553
self.model.formatted_local_time(
15381554
msg["timestamp"], show_seconds=False
@@ -1550,7 +1566,8 @@ def main_view(self) -> List[Any]:
15501566
}
15511567
different = { # How this message differs from the previous one
15521568
"recipients": recipient_header is not None,
1553-
"author": message["this"]["author_id"] != message["last"]["author_id"],
1569+
"author": message["this"]["author"] != message["last"]["author"],
1570+
"author_id": message["this"]["author_id"] != message["last"]["author_id"],
15541571
"24h": (
15551572
message["last"]["datetime"] is not None
15561573
and ((message["this"]["datetime"] - message["last"]["datetime"]).days)
@@ -1567,11 +1584,21 @@ def main_view(self) -> List[Any]:
15671584

15681585
if any_differences: # Construct content_header, if needed
15691586
TextType = Dict[str, urwid_MarkupTuple]
1570-
text_keys = ("author", "star", "time", "status")
1587+
text_keys = ("author", "star", "time", "status", "author_id")
15711588
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"])
1589+
if any(
1590+
different[key] for key in ("recipients", "author", "24h", "author_id")
1591+
):
1592+
if self.model.user_name_count(message["this"]["author"]) > 1:
1593+
text["author"] = (
1594+
"name",
1595+
message["this"]["author"]
1596+
+ " ("
1597+
+ str(message["this"]["author_id"])
1598+
+ ")",
1599+
)
1600+
else:
1601+
text["author"] = ("name", message["this"]["author"])
15751602

15761603
# TODO: Refactor to use user ids for look up instead of emails.
15771604
email = self.message.get("sender_email", "")

0 commit comments

Comments
 (0)