Skip to content

Commit 6f23e83

Browse files
committed
model: Add handling for Submessage events.
Added _handle_submessage_event method to Model to handle new changes to widgets (polls, todo etc.). Tests added.
1 parent 53bf449 commit 6f23e83

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

tests/model/test_model.py

+209
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
237237
"message",
238238
"update_message",
239239
"reaction",
240+
"submessage",
240241
"subscription",
241242
"typing",
242243
"update_message_flags",
@@ -2913,6 +2914,214 @@ def test__handle_reaction_event_for_msg_in_index(
29132914

29142915
model._update_rendered_view.assert_called_once_with(event_message_id)
29152916

2917+
@pytest.mark.parametrize(
2918+
"submessages, event, expected_updated_submessage",
2919+
[
2920+
case(
2921+
[
2922+
{
2923+
"id": 1,
2924+
"message_id": 1958326,
2925+
"sender_id": 27294,
2926+
"msg_type": "widget",
2927+
"content": '{"widget_type": "todo", "extra_data": '
2928+
'{"task_list_title": "Today\'s Work", "tasks": [{"task"'
2929+
': "Handle submessages events on ZT", "desc": ""}, {"task": '
2930+
'"Play ping pong", "desc": ""}]}}',
2931+
}
2932+
],
2933+
{
2934+
"type": "submessage",
2935+
"msg_type": "widget",
2936+
"message_id": 1958326,
2937+
"submessage_id": 1,
2938+
"sender_id": 27294,
2939+
"content": '{"type":"strike","key":"0,canned"}',
2940+
"id": 1,
2941+
},
2942+
[
2943+
{
2944+
"id": 1,
2945+
"message_id": 1958326,
2946+
"sender_id": 27294,
2947+
"msg_type": "widget",
2948+
"content": '{"widget_type": "todo", "extra_data": '
2949+
'{"task_list_title": "Today\'s Work", "tasks": '
2950+
'[{"task": "Handle submessages events on ZT", "desc": ""}, '
2951+
'{"task": "Play ping pong", "desc": ""}]}}',
2952+
},
2953+
{
2954+
"type": "submessage",
2955+
"msg_type": "widget",
2956+
"message_id": 1958326,
2957+
"submessage_id": 1,
2958+
"sender_id": 27294,
2959+
"content": '{"type":"strike","key":"0,canned"}',
2960+
"id": 1,
2961+
},
2962+
],
2963+
id="submessage_strike_event_todo_widget",
2964+
),
2965+
case(
2966+
[
2967+
{
2968+
"id": 1,
2969+
"message_id": 1958326,
2970+
"sender_id": 27294,
2971+
"msg_type": "widget",
2972+
"content": '{"widget_type": "todo", "extra_data": '
2973+
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
2974+
'"Handle submessages events on ZT", "desc": ""}, {"task": '
2975+
'"Play ping pong", "desc": ""}]}}',
2976+
},
2977+
{
2978+
"id": 12154,
2979+
"message_id": 1958326,
2980+
"sender_id": 27294,
2981+
"msg_type": "widget",
2982+
"content": '{"type":"strike","key":"0,canned"}',
2983+
},
2984+
],
2985+
{
2986+
"type": "submessage",
2987+
"msg_type": "widget",
2988+
"message_id": 1958326,
2989+
"submessage_id": 12185,
2990+
"sender_id": 27294,
2991+
"content": '{"type":"new_task","key":2,"task":"Make a coffee",'
2992+
'"desc":"","completed":false}',
2993+
"id": 0,
2994+
},
2995+
[
2996+
{
2997+
"id": 1,
2998+
"message_id": 1958326,
2999+
"sender_id": 27294,
3000+
"msg_type": "widget",
3001+
"content": '{"widget_type": "todo", "extra_data": '
3002+
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
3003+
'"Handle submessages events on ZT", "desc": ""}, {"task": '
3004+
'"Play ping pong", "desc": ""}]}}',
3005+
},
3006+
{
3007+
"id": 12154,
3008+
"message_id": 1958326,
3009+
"sender_id": 27294,
3010+
"msg_type": "widget",
3011+
"content": '{"type":"strike","key":"0,canned"}',
3012+
},
3013+
{
3014+
"type": "submessage",
3015+
"msg_type": "widget",
3016+
"message_id": 1958326,
3017+
"submessage_id": 12185,
3018+
"sender_id": 27294,
3019+
"content": '{"type":"new_task","key":2,"task":"Make a coffee",'
3020+
'"desc":"","completed":false}',
3021+
"id": 0,
3022+
},
3023+
],
3024+
id="submessage_new_task_event_todo_widget",
3025+
),
3026+
case(
3027+
[
3028+
{
3029+
"id": 12153,
3030+
"message_id": 1958326,
3031+
"sender_id": 27294,
3032+
"msg_type": "widget",
3033+
"content": '{"widget_type": "todo", "extra_data": '
3034+
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
3035+
'"Handle submessages events on ZT", "desc": ""}, {"task": '
3036+
'"Play ping pong", "desc": ""}]}}',
3037+
},
3038+
{
3039+
"id": 12154,
3040+
"message_id": 1958326,
3041+
"sender_id": 27294,
3042+
"msg_type": "widget",
3043+
"content": '{"type":"strike","key":"0,canned"}',
3044+
},
3045+
{
3046+
"type": "submessage",
3047+
"msg_type": "widget",
3048+
"message_id": 1958326,
3049+
"submessage_id": 12185,
3050+
"sender_id": 27294,
3051+
"content": '{"type":"new_task","key":2,"task":"Make a coffee"'
3052+
',"desc":"","completed":false}',
3053+
"id": 0,
3054+
},
3055+
],
3056+
{
3057+
"type": "submessage",
3058+
"msg_type": "widget",
3059+
"message_id": 1958326,
3060+
"submessage_id": 12186,
3061+
"sender_id": 27294,
3062+
"content": '{"type":"new_task_list_title","title":"Today\'s Work '
3063+
'[Updated]"}',
3064+
"id": 11,
3065+
},
3066+
[
3067+
{
3068+
"id": 12153,
3069+
"message_id": 1958326,
3070+
"sender_id": 27294,
3071+
"msg_type": "widget",
3072+
"content": '{"widget_type": "todo", "extra_data": '
3073+
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
3074+
'"Handle submessages events on ZT", "desc": ""}, {"task": '
3075+
'"Play ping pong", "desc": ""}]}}',
3076+
},
3077+
{
3078+
"id": 12154,
3079+
"message_id": 1958326,
3080+
"sender_id": 27294,
3081+
"msg_type": "widget",
3082+
"content": '{"type":"strike","key":"0,canned"}',
3083+
},
3084+
{
3085+
"type": "submessage",
3086+
"msg_type": "widget",
3087+
"message_id": 1958326,
3088+
"submessage_id": 12185,
3089+
"sender_id": 27294,
3090+
"content": '{"type":"new_task","key":2,"task":"Make a coffee"'
3091+
',"desc":"","completed":false}',
3092+
"id": 0,
3093+
},
3094+
{
3095+
"type": "submessage",
3096+
"msg_type": "widget",
3097+
"message_id": 1958326,
3098+
"submessage_id": 12186,
3099+
"sender_id": 27294,
3100+
"content": '{"type":"new_task_list_title",'
3101+
'"title":"Today\'s Work [Updated]"}',
3102+
"id": 11,
3103+
},
3104+
],
3105+
id="submessage_new_task_list_title_event_todo_widget",
3106+
),
3107+
],
3108+
)
3109+
def test__handle_submessage_event(
3110+
self,
3111+
mocker,
3112+
model,
3113+
submessages,
3114+
event,
3115+
expected_updated_submessage,
3116+
id=1958326,
3117+
):
3118+
model.index["messages"][id]["submessages"] = submessages
3119+
model._update_rendered_view = mocker.Mock()
3120+
3121+
model._handle_submessage_event(event)
3122+
3123+
assert model.index["messages"][id]["submessages"] == expected_updated_submessage
3124+
29163125
@pytest.fixture(
29173126
params=[
29183127
("op", 32), # At server feature level 32, event uses standard field

zulipterminal/model.py

+14
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def __init__(self, controller: Any) -> None:
152152
"message": self._handle_message_event,
153153
"update_message": self._handle_update_message_event,
154154
"reaction": self._handle_reaction_event,
155+
"submessage": self._handle_submessage_event,
155156
"subscription": self._handle_subscription_event,
156157
"typing": self._handle_typing_event,
157158
"update_message_flags": self._handle_update_message_flags_event,
@@ -1860,6 +1861,19 @@ def _handle_reaction_event(self, event: Event) -> None:
18601861
self.index["messages"][message_id] = message
18611862
self._update_rendered_view(message_id)
18621863

1864+
def _handle_submessage_event(self, event: Event) -> None:
1865+
"""
1866+
Handle change to submessages on a message (todo, poll etc.)
1867+
"""
1868+
assert event["type"] == "submessage"
1869+
message_id = event["message_id"]
1870+
if message_id in self.index["messages"]:
1871+
message = self.index["messages"][message_id]
1872+
message["submessages"].append(cast(Dict[str, Any], event))
1873+
1874+
self.index["messages"][message_id] = message
1875+
self._update_rendered_view(message_id)
1876+
18631877
def _handle_update_message_flags_event(self, event: Event) -> None:
18641878
"""
18651879
Handle change to message flags (eg. starred, read)

0 commit comments

Comments
 (0)