Skip to content

Commit 88a7c07

Browse files
rsashankneiljp
authored andcommitted
widget: Add find_widget_type function.
Added find_widget_type to identify the type of widget present in the message (e.g., polls, todo, etc.). Added test.
1 parent dc4d9bc commit 88a7c07

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

docs/developer-file-overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Zulip Terminal uses [Zulip's API](https://zulip.com/api/) to store and retrieve
1717
| | unicode_emojis.py | Unicode emoji data, synchronized semi-regularly with the server source |
1818
| | urwid_types.py | Types from the urwid API, to improve type checking |
1919
| | version.py | Keeps track of the version of the current code |
20+
| | widget.py | Process widgets (submessages) like polls, todo lists, etc. |
2021
| | | |
2122
| zulipterminal/cli | run.py | Marks the entry point into the application |
2223
| | | |

tests/widget/test_widget.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from typing import List
2+
3+
import pytest
4+
from pytest import param as case
5+
6+
from zulipterminal.widget import Submessage, find_widget_type
7+
8+
9+
@pytest.mark.parametrize(
10+
"submessages, expected_widget_type",
11+
[
12+
case(
13+
[
14+
{
15+
"id": 11897,
16+
"message_id": 1954461,
17+
"sender_id": 27294,
18+
"msg_type": "widget",
19+
"content": (
20+
'{"widget_type": "poll", "extra_data": '
21+
'{"question": "Sample Question?", "options": ["Yes", "No"]}}'
22+
),
23+
},
24+
{
25+
"id": 11898,
26+
"message_id": 1954461,
27+
"sender_id": 27294,
28+
"msg_type": "widget",
29+
"content": '{"type":"new_option","idx":1,"option":"Maybe"}',
30+
},
31+
],
32+
"poll",
33+
),
34+
case(
35+
[
36+
{
37+
"id": 11899,
38+
"message_id": 1954463,
39+
"sender_id": 27294,
40+
"msg_type": "widget",
41+
"content": (
42+
'{"widget_type": "todo", "extra_data": '
43+
'{"task_list_title": "Today\'s Tasks", "tasks": [{"task": '
44+
'"Write code", "desc": ""}, {"task": "Sleep", "desc": ""}]}}'
45+
),
46+
},
47+
{
48+
"id": 11900,
49+
"message_id": 1954463,
50+
"sender_id": 27294,
51+
"msg_type": "widget",
52+
"content": (
53+
'{"type":"new_task","key":2,"task":"Eat","desc":"",'
54+
'"completed":false}'
55+
),
56+
},
57+
],
58+
"todo",
59+
),
60+
case([{}], "unknown"),
61+
],
62+
)
63+
def test_find_widget_type(
64+
submessages: List[Submessage], expected_widget_type: str
65+
) -> None:
66+
widget_type = find_widget_type(submessages)
67+
68+
assert widget_type == expected_widget_type

zulipterminal/widget.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Process widgets (submessages) like polls, todo lists, etc.
3+
"""
4+
5+
import json
6+
from typing import Dict, List, Union
7+
8+
9+
Submessage = Dict[str, Union[int, str]]
10+
11+
12+
def find_widget_type(submessages: List[Submessage]) -> str:
13+
if submessages and "content" in submessages[0]:
14+
content = submessages[0]["content"]
15+
16+
if isinstance(content, str):
17+
try:
18+
loaded_content = json.loads(content)
19+
return loaded_content.get("widget_type", "unknown")
20+
except json.JSONDecodeError:
21+
return "unknown"
22+
else:
23+
return "unknown"
24+
else:
25+
return "unknown"

0 commit comments

Comments
 (0)