Skip to content

Commit 23a773c

Browse files
Niloth-ptimabbott
authored andcommitted
messages/views: Prepare for the deprecation of user metadata.
The "user" field is to be deprecated from messages' reactions and reaction events in the API. Add support for `user_id` in MsgInfoView, by using the recently introduced `get_user_id_from_reaction()`. Remove dependence on reaction["user"]["full_name"] by instead using `_all_users_by_id` with the "user_id". Tests updated.
1 parent ccea7b5 commit 23a773c

File tree

4 files changed

+68
-114
lines changed

4 files changed

+68
-114
lines changed

tests/ui_tools/test_messages.py

+26-87
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict, defaultdict
22
from datetime import date
3+
from unittest.mock import patch
34

45
import pytest
56
import pytz
@@ -1597,60 +1598,6 @@ def test_transform_content(self, mocker, raw_html, expected_content):
15971598
@pytest.mark.parametrize(
15981599
"to_vary_in_each_message, expected_text, expected_attributes",
15991600
[
1600-
case(
1601-
{
1602-
"reactions": [
1603-
{
1604-
"emoji_name": "thumbs_up",
1605-
"emoji_code": "1f44d",
1606-
"user": {
1607-
"email": "[email protected]",
1608-
"full_name": "Iago",
1609-
"id": 5,
1610-
},
1611-
"reaction_type": "unicode_emoji",
1612-
},
1613-
{
1614-
"emoji_name": "zulip",
1615-
"emoji_code": "zulip",
1616-
"user": {
1617-
"email": "[email protected]",
1618-
"full_name": "Iago",
1619-
"id": 5,
1620-
},
1621-
"reaction_type": "zulip_extra_emoji",
1622-
},
1623-
{
1624-
"emoji_name": "zulip",
1625-
"emoji_code": "zulip",
1626-
"user": {
1627-
"email": "[email protected]",
1628-
"full_name": "aaron",
1629-
"id": 1,
1630-
},
1631-
"reaction_type": "zulip_extra_emoji",
1632-
},
1633-
{
1634-
"emoji_name": "heart",
1635-
"emoji_code": "2764",
1636-
"user": {
1637-
"email": "[email protected]",
1638-
"full_name": "Iago",
1639-
"id": 5,
1640-
},
1641-
"reaction_type": "unicode_emoji",
1642-
},
1643-
],
1644-
},
1645-
" :thumbs_up: 1 :zulip: 2 :heart: 1 ",
1646-
[
1647-
("reaction", 15),
1648-
(None, 1),
1649-
("reaction_mine", 11),
1650-
(None, 1),
1651-
("reaction", 11),
1652-
],
1653-
),
16541601
case(
16551602
{
16561603
"reactions": [
@@ -1701,31 +1648,19 @@ def test_transform_content(self, mocker, raw_html, expected_content):
17011648
{
17021649
"emoji_name": "zulip",
17031650
"emoji_code": "zulip",
1704-
"user": {
1705-
"email": "[email protected]",
1706-
"full_name": "Iago",
1707-
"id": 5,
1708-
},
1651+
"user_id": 5,
17091652
"reaction_type": "unicode_emoji",
17101653
},
17111654
{
17121655
"emoji_name": "zulip",
17131656
"emoji_code": "zulip",
1714-
"user": {
1715-
"email": "[email protected]",
1716-
"full_name": "aaron",
1717-
"id": 1,
1718-
},
1657+
"user_id": 1,
17191658
"reaction_type": "zulip_extra_emoji",
17201659
},
17211660
{
17221661
"emoji_name": "zulip",
17231662
"emoji_code": "zulip",
1724-
"user": {
1725-
"email": "[email protected]",
1726-
"full_name": "Shivam",
1727-
"id": 6,
1728-
},
1663+
"user_id": 6,
17291664
"reaction_type": "unicode_emoji",
17301665
},
17311666
],
@@ -1741,31 +1676,19 @@ def test_transform_content(self, mocker, raw_html, expected_content):
17411676
{
17421677
"emoji_name": "heart",
17431678
"emoji_code": "2764",
1744-
"user": {
1745-
"email": "[email protected]",
1746-
"full_name": "Iago",
1747-
"id": 5,
1748-
},
1679+
"user_id": 5,
17491680
"reaction_type": "unicode_emoji",
17501681
},
17511682
{
17521683
"emoji_name": "zulip",
17531684
"emoji_code": "zulip",
1754-
"user": {
1755-
"email": "[email protected]",
1756-
"full_name": "aaron",
1757-
"id": 1,
1758-
},
1685+
"user_id": 1,
17591686
"reaction_type": "zulip_extra_emoji",
17601687
},
17611688
{
17621689
"emoji_name": "zulip",
17631690
"emoji_code": "zulip",
1764-
"user": {
1765-
"email": "[email protected]",
1766-
"full_name": "Shivam",
1767-
"id": 6,
1768-
},
1691+
"user_id": 6,
17691692
"reaction_type": "unicode_emoji",
17701693
},
17711694
],
@@ -1821,10 +1744,26 @@ def test_reactions_view(
18211744
msg_box = MessageBox(varied_message, self.model, None)
18221745
reactions = to_vary_in_each_message["reactions"]
18231746

1824-
reactions_view = msg_box.reactions_view(reactions)
1747+
mock_all_users_by_id = {
1748+
1: {"full_name": "aaron"},
1749+
5: {"full_name": "Iago"},
1750+
6: {"full_name": "Shivam"},
1751+
}
1752+
1753+
def mock_get_user_id(reaction):
1754+
if "user_id" in reaction:
1755+
return reaction["user_id"]
1756+
return reaction["user"]["id"]
1757+
1758+
with patch.object(
1759+
self.model,
1760+
"get_user_id_from_reaction",
1761+
side_effect=mock_get_user_id,
1762+
), patch.object(self.model, "_all_users_by_id", mock_all_users_by_id):
1763+
reactions_view = msg_box.reactions_view(reactions)
18251764

1826-
assert reactions_view.original_widget.text == expected_text
1827-
assert reactions_view.original_widget.attrib == expected_attributes
1765+
assert reactions_view.original_widget.text == expected_text
1766+
assert reactions_view.original_widget.attrib == expected_attributes
18281767

18291768
@pytest.mark.parametrize(
18301769
"message_links, expected_text, expected_attrib, expected_footlinks_width",

tests/ui_tools/test_popups.py

+34-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22
from typing import Any, Callable, Dict, List, Optional, Tuple
3+
from unittest.mock import patch
34

45
import pytest
56
from pytest import param as case
@@ -1169,21 +1170,13 @@ def test_height_noreactions(self) -> None:
11691170
{
11701171
"emoji_name": "zulip",
11711172
"emoji_code": "zulip",
1172-
"user": {
1173-
"email": "[email protected]",
1174-
"full_name": "Iago",
1175-
"id": 5,
1176-
},
1173+
"user_id": 5,
11771174
"reaction_type": "zulip_extra_emoji",
11781175
},
11791176
{
11801177
"emoji_name": "zulip",
11811178
"emoji_code": "zulip",
1182-
"user": {
1183-
"email": "[email protected]",
1184-
"full_name": "aaron",
1185-
"id": 1,
1186-
},
1179+
"user_id": 1,
11871180
"reaction_type": "zulip_extra_emoji",
11881181
},
11891182
{
@@ -1207,18 +1200,37 @@ def test_height_reactions(
12071200
) -> None:
12081201
varied_message = message_fixture
12091202
varied_message.update(to_vary_in_each_message)
1210-
self.msg_info_view = MsgInfoView(
1211-
self.controller,
1212-
varied_message,
1213-
"Message Information",
1214-
OrderedDict(),
1215-
OrderedDict(),
1216-
list(),
1217-
)
1218-
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category)
1219-
# + 4 reactions (excluding 'Message Links').
1220-
expected_height = 14
1221-
assert self.msg_info_view.height == expected_height
1203+
1204+
mock_all_users_by_id = {
1205+
1: {"full_name": "aaron"},
1206+
5: {"full_name": "Iago"},
1207+
}
1208+
1209+
def mock_get_user_id(reaction: Dict[str, Any]) -> int:
1210+
if "user_id" in reaction:
1211+
return reaction["user_id"]
1212+
return reaction["user"]["id"]
1213+
1214+
with patch.object(
1215+
self.controller.model,
1216+
"get_user_id_from_reaction",
1217+
side_effect=mock_get_user_id,
1218+
), patch.object(
1219+
self.controller.model, "_all_users_by_id", mock_all_users_by_id
1220+
):
1221+
self.msg_info_view = MsgInfoView(
1222+
self.controller,
1223+
varied_message,
1224+
"Message Information",
1225+
OrderedDict(),
1226+
OrderedDict(),
1227+
list(),
1228+
)
1229+
1230+
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category)
1231+
# + 4 reactions (excluding 'Message Links').
1232+
expected_height = 14
1233+
assert self.msg_info_view.height == expected_height
12221234

12231235
@pytest.mark.parametrize(
12241236
[

zulipterminal/ui_tools/messages.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,8 @@ def reactions_view(
272272
my_user_id = self.model.user_id
273273
reaction_stats = defaultdict(list)
274274
for reaction in reactions:
275-
user_id = int(reaction["user"].get("id", -1))
276-
if user_id == -1:
277-
user_id = int(reaction["user"]["user_id"])
278-
user_name = reaction["user"]["full_name"]
275+
user_id = self.model.get_user_id_from_reaction(reaction)
276+
user_name = self.model._all_users_by_id[user_id]["full_name"]
279277
if user_id == my_user_id:
280278
user_name = "You"
281279
reaction_stats[reaction["emoji_name"]].append((user_id, user_name))

zulipterminal/ui_tools/views.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,12 @@ def __init__(
16571657
msg_info.append(("Time mentions", time_mentions))
16581658
if msg["reactions"]:
16591659
reactions = sorted(
1660-
(reaction["emoji_name"], reaction["user"]["full_name"])
1660+
(
1661+
reaction["emoji_name"],
1662+
controller.model._all_users_by_id[
1663+
controller.model.get_user_id_from_reaction(reaction)
1664+
]["full_name"],
1665+
)
16611666
for reaction in msg["reactions"]
16621667
)
16631668
grouped_reactions: Dict[str, str] = dict()

0 commit comments

Comments
 (0)