Skip to content

Commit c330ae1

Browse files
committed
widget: Add find_widget_type method.
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 c330ae1

File tree

3 files changed

+78
-0
lines changed

3 files changed

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

zulipterminal/widget.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Process widgets (submessages) like polls, todo lists, etc.
3+
"""
4+
5+
import json
6+
from typing import Any
7+
8+
9+
def find_widget_type(submessage: Any) -> str:
10+
if submessage and isinstance(submessage, list) and "content" in submessage[0]:
11+
try:
12+
content = json.loads(submessage[0]["content"])
13+
return content.get("widget_type", "unknown")
14+
except (json.JSONDecodeError, KeyError):
15+
return "unknown"
16+
else:
17+
return "unknown"

0 commit comments

Comments
 (0)