Skip to content

Commit

Permalink
model: Add handling for Submessage events.
Browse files Browse the repository at this point in the history
Added _handle_submessage_event method to Model to handle
new changes to widgets (polls, todo etc.).

Tests added.
  • Loading branch information
rsashank authored and neiljp committed Nov 4, 2024
1 parent 4e71ea5 commit d88adae
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 0 deletions.
230 changes: 230 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
"message",
"update_message",
"reaction",
"submessage",
"subscription",
"typing",
"update_message_flags",
Expand Down Expand Up @@ -2913,6 +2914,235 @@ def test__handle_reaction_event_for_msg_in_index(

model._update_rendered_view.assert_called_once_with(event_message_id)

@pytest.mark.parametrize(
"submessages, event, expected_updated_submessage",
[
case(
[
{
"id": 1,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": [{"task"'
': "Handle submessages events on ZT", "desc": ""}, {"task":'
' "Play ping pong", "desc": ""}]}}'
),
}
],
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 1,
"sender_id": 27294,
"content": '{"type":"strike","key":"0,canned"}',
"id": 1,
},
[
{
"id": 1,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": '
'[{"task": "Handle submessages events on ZT", "desc": ""}, '
'{"task": "Play ping pong", "desc": ""}]}}'
),
},
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 1,
"sender_id": 27294,
"content": '{"type":"strike","key":"0,canned"}',
},
],
id="submessage_strike_event_todo_widget",
),
case(
[
{
"id": 1,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
'"Handle submessages events on ZT", "desc": ""}, {"task": '
'"Play ping pong", "desc": ""}]}}'
),
},
{
"id": 12154,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": '{"type":"strike","key":"0,canned"}',
},
],
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12185,
"sender_id": 27294,
"content": (
'{"type":"new_task","key":2,"task":"Make a coffee",'
'"desc":"","completed":false}'
),
"id": 0,
},
[
{
"id": 1,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
'"Handle submessages events on ZT", "desc": ""}, {"task": '
'"Play ping pong", "desc": ""}]}}'
),
},
{
"id": 12154,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": '{"type":"strike","key":"0,canned"}',
},
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12185,
"sender_id": 27294,
"content": (
'{"type":"new_task","key":2,"task":"Make a coffee",'
'"desc":"","completed":false}'
),
},
],
id="submessage_new_task_event_todo_widget",
),
case(
[
{
"id": 12153,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
'"Handle submessages events on ZT", "desc": ""}, {"task": '
'"Play ping pong", "desc": ""}]}}'
),
},
{
"id": 12154,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": '{"type":"strike","key":"0,canned"}',
},
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12185,
"sender_id": 27294,
"content": (
'{"type":"new_task","key":2,"task":"Make a coffee"'
',"desc":"","completed":false}'
),
"id": 0,
},
],
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12186,
"sender_id": 27294,
"content": (
'{"type":"new_task_list_title","title":"Today\'s Work '
'[Updated]"}'
),
"id": 11,
},
[
{
"id": 12153,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": (
'{"widget_type": "todo", "extra_data": '
'{"task_list_title": "Today\'s Work", "tasks": [{"task": '
'"Handle submessages events on ZT", "desc": ""}, {"task": '
'"Play ping pong", "desc": ""}]}}'
),
},
{
"id": 12154,
"message_id": 1958326,
"sender_id": 27294,
"msg_type": "widget",
"content": '{"type":"strike","key":"0,canned"}',
},
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12185,
"sender_id": 27294,
"content": (
'{"type":"new_task","key":2,"task":"Make a coffee"'
',"desc":"","completed":false}'
),
"id": 0,
},
{
"type": "submessage",
"msg_type": "widget",
"message_id": 1958326,
"submessage_id": 12186,
"sender_id": 27294,
"content": (
'{"type":"new_task_list_title",'
'"title":"Today\'s Work [Updated]"}'
),
},
],
id="submessage_new_task_list_title_event_todo_widget",
),
],
)
def test__handle_submessage_event(
self,
mocker,
model,
submessages,
event,
expected_updated_submessage,
id=1958326,
):
model.index["messages"][id]["submessages"] = submessages
model._update_rendered_view = mocker.Mock()

model._handle_submessage_event(event)

assert model.index["messages"][id]["submessages"] == expected_updated_submessage

@pytest.fixture(
params=[
("op", 32), # At server feature level 32, event uses standard field
Expand Down
22 changes: 22 additions & 0 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def __init__(self, controller: Any) -> None:
"message": self._handle_message_event,
"update_message": self._handle_update_message_event,
"reaction": self._handle_reaction_event,
"submessage": self._handle_submessage_event,
"subscription": self._handle_subscription_event,
"typing": self._handle_typing_event,
"update_message_flags": self._handle_update_message_flags_event,
Expand Down Expand Up @@ -1860,6 +1861,27 @@ def _handle_reaction_event(self, event: Event) -> None:
self.index["messages"][message_id] = message
self._update_rendered_view(message_id)

def _handle_submessage_event(self, event: Event) -> None:
"""
Handle change to submessages on a message (todo, poll etc.)
"""
assert event["type"] == "submessage"
message_id = event["message_id"]
if message_id in self.index["messages"]:
message = self.index["messages"][message_id]
message["submessages"].append(
{
"type": event["type"],
"msg_type": event["msg_type"],
"message_id": event["message_id"],
"submessage_id": event["submessage_id"],
"sender_id": event["sender_id"],
"content": event["content"],
}
)
self.index["messages"][message_id] = message
self._update_rendered_view(message_id)

def _handle_update_message_flags_event(self, event: Event) -> None:
"""
Handle change to message flags (eg. starred, read)
Expand Down

0 comments on commit d88adae

Please sign in to comment.