From 827ef2c1c5f96c7b2f5bd3ea903d4725ad4c2c31 Mon Sep 17 00:00:00 2001 From: rsashank Date: Mon, 4 Nov 2024 14:40:10 +0530 Subject: [PATCH 1/7] widget: Add process_poll_widget function. Added process_poll_widget function to process submessages containing a poll widget. Returns the question of the poll along with options and their voters in a dict. --- tests/widget/test_widget.py | 322 +++++++++++++++++++++++++++++++++++- zulipterminal/widget.py | 42 +++++ 2 files changed, 363 insertions(+), 1 deletion(-) diff --git a/tests/widget/test_widget.py b/tests/widget/test_widget.py index ee2636e820..7d8098a576 100644 --- a/tests/widget/test_widget.py +++ b/tests/widget/test_widget.py @@ -3,7 +3,12 @@ import pytest from pytest import param as case -from zulipterminal.widget import Submessage, find_widget_type, process_todo_widget +from zulipterminal.widget import ( + Submessage, + find_widget_type, + process_poll_widget, + process_todo_widget, +) @pytest.mark.parametrize( @@ -343,3 +348,318 @@ def test_process_todo_widget( assert title == expected_title assert tasks == expected_tasks + + +@pytest.mark.parametrize( + "submessage, expected_poll_question, expected_options", + [ + case( + [ + { + "id": 12082, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {' + '"question": "Do polls work on ZT?", "options": ["Yes", "No"]}}' + ), + }, + { + "id": 12083, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + { + "id": 12084, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":-1}', + }, + { + "id": 12085, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":1}', + }, + { + "id": 12086, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + { + "id": 12087, + "message_id": 1957499, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":-1}', + }, + ], + "Do polls work on ZT?", + { + "canned,0": {"option": "Yes", "votes": [27294]}, + "canned,1": {"option": "No", "votes": []}, + }, + id="poll_widget_with_votes", + ), + case( + [ + { + "id": 12089, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {"question": "Is ' + 'this a poll with options added later?", ' + '"options": ["Yes", "No"]}}' + ), + }, + { + "id": 12090, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"new_option","idx":1,"option":"Maybe"}', + }, + { + "id": 12091, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":1}', + }, + { + "id": 12092, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":-1}', + }, + { + "id": 12093, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"27294,1","vote":1}', + }, + { + "id": 12094, + "message_id": 1957662, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + ], + "Is this a poll with options added later?", + { + "canned,0": {"option": "Yes", "votes": [27294]}, + "canned,1": {"option": "No", "votes": []}, + "27294,1": {"option": "Maybe", "votes": [27294]}, + }, + id="poll_widget_with_new_option_and_votes", + ), + case( + [ + { + "id": 12095, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {"question": ' + '"Let\'s change this question later?", "options": ["Yes"]}}' + ), + }, + { + "id": 12096, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + { + "id": 12097, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"new_option","idx":1,"option":"No"}', + }, + { + "id": 12098, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":-1}', + }, + { + "id": 12099, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"question",' + '"question":"Has this question stayed the same?"}', + }, + { + "id": 12100, + "message_id": 1957682, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"27294,1","vote":1}', + }, + ], + "Has this question stayed the same?", + { + "canned,0": {"option": "Yes", "votes": []}, + "27294,1": {"option": "No", "votes": [27294]}, + }, + id="poll_widget_with_new_question_and_votes", + ), + case( + [ + { + "id": 12101, + "message_id": 1957693, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {"question": "",' + ' "options": ["Yes", "No"]}}' + ), + } + ], + "", + { + "canned,0": {"option": "Yes", "votes": []}, + "canned,1": {"option": "No", "votes": []}, + }, + id="poll_widget_with_empty_question", + ), + case( + [ + { + "id": 12102, + "message_id": 1957700, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {' + '"question": "Does this poll have options?", "options": []}}' + ), + } + ], + "Does this poll have options?", + {}, + id="poll_widget_with_empty_options", + ), + case( + [ + { + "id": 12112, + "message_id": 1957722, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {"question": "",' + ' "options": []}}' + ), + } + ], + "", + {}, + id="poll_widget_with_empty_question_and_options", + ), + case( + [ + { + "id": 12103, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": ( + '{"widget_type": "poll", "extra_data": {"question": "Does' + ' this poll have multiple voters?", "options": ["Yes", "No"]}}' + ), + }, + { + "id": 12104, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + { + "id": 12105, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":1}', + }, + { + "id": 12106, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":-1}', + }, + { + "id": 12107, + "message_id": 1957719, + "sender_id": 32159, + "msg_type": "widget", + "content": '{"type":"new_option","idx":1,"option":"Maybe"}', + }, + { + "id": 12108, + "message_id": 1957719, + "sender_id": 32159, + "msg_type": "widget", + "content": '{"type":"vote","key":"32159,1","vote":1}', + }, + { + "id": 12109, + "message_id": 1957719, + "sender_id": 32159, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + { + "id": 12110, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,1","vote":-1}', + }, + { + "id": 12111, + "message_id": 1957719, + "sender_id": 27294, + "msg_type": "widget", + "content": '{"type":"vote","key":"canned,0","vote":1}', + }, + ], + "Does this poll have multiple voters?", + { + "canned,0": {"option": "Yes", "votes": [32159, 27294]}, + "canned,1": {"option": "No", "votes": []}, + "32159,1": {"option": "Maybe", "votes": [32159]}, + }, + id="poll_widget_with_multiple_voters", + ), + ], +) +def test_process_poll_widget( + submessage: List[Submessage], + expected_poll_question: str, + expected_options: Dict[str, Dict[str, Union[str, List[str]]]], +) -> None: + poll_question, options = process_poll_widget(submessage) + + assert poll_question == expected_poll_question + assert options == expected_options diff --git a/zulipterminal/widget.py b/zulipterminal/widget.py index 05e80c7c97..c1eac2aff8 100644 --- a/zulipterminal/widget.py +++ b/zulipterminal/widget.py @@ -74,3 +74,45 @@ def process_todo_widget( title = widget["title"] return title, tasks + + +def process_poll_widget( + poll_content: List[Submessage], +) -> Tuple[str, Dict[str, Dict[str, Union[str, List[str]]]]]: + poll_question = "" + options = {} + + for entry in poll_content: + content = entry["content"] + sender_id = entry["sender_id"] + msg_type = entry["msg_type"] + + if msg_type == "widget" and isinstance(content, str): + widget = json.loads(content) + + if widget.get("widget_type") == "poll": + poll_question = widget["extra_data"]["question"] + for i, option in enumerate(widget["extra_data"]["options"]): + option_id = f"canned,{i}" + options[option_id] = {"option": option, "votes": []} + + elif widget.get("type") == "question": + poll_question = widget["question"] + + elif widget.get("type") == "vote": + option_id = widget["key"] + vote_type = widget["vote"] + + if option_id in options: + if vote_type == 1 and sender_id not in options[option_id]["votes"]: + options[option_id]["votes"].append(sender_id) + elif vote_type == -1 and sender_id in options[option_id]["votes"]: + options[option_id]["votes"].remove(sender_id) + + elif widget.get("type") == "new_option": + idx = widget["idx"] + new_option = widget["option"] + option_id = f"{sender_id},{idx}" + options[option_id] = {"option": new_option, "votes": []} + + return poll_question, options From af39cee0c9d8955b93ffb856c1c38d7ce5d45fe8 Mon Sep 17 00:00:00 2001 From: rsashank Date: Mon, 4 Nov 2024 14:44:33 +0530 Subject: [PATCH 2/7] messages: Implement logic to display polls on ZT. --- zulipterminal/ui_tools/messages.py | 34 +++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/zulipterminal/ui_tools/messages.py b/zulipterminal/ui_tools/messages.py index 2b67ae6e71..42bc893ee2 100644 --- a/zulipterminal/ui_tools/messages.py +++ b/zulipterminal/ui_tools/messages.py @@ -33,7 +33,11 @@ from zulipterminal.server_url import near_message_url from zulipterminal.ui_tools.tables import render_table from zulipterminal.urwid_types import urwid_MarkupTuple, urwid_Size -from zulipterminal.widget import find_widget_type, process_todo_widget +from zulipterminal.widget import ( + find_widget_type, + process_poll_widget, + process_todo_widget, +) if typing.TYPE_CHECKING: @@ -755,6 +759,34 @@ def main_view(self) -> List[Any]: # though it's not very useful. self.message["content"] = todo_widget + elif widget_type == "poll": + poll_question, poll_options = process_poll_widget( + self.message.get("submessages", []) + ) + + poll_widget = ( + f"Poll\n{poll_question}" + if poll_question + else "No poll question provided. Please add one via the web app." + ) + + if poll_options: + max_votes_len = max( + len(str(len(option["votes"]))) + for option in poll_options.values() + ) + + for option_info in poll_options.values(): + padded_votes = f"{len(option_info['votes']):>{max_votes_len}}" + poll_widget += f"\n[ {padded_votes} ] {option_info['option']}" + else: + poll_widget += "\nNo options provided." + "Please add them via the web app." + + # Update the message content with the latest poll_widget, + # similar to the todo_widget above. + self.message["content"] = poll_widget + # Transform raw message content into markup (As needed by urwid.Text) content, self.message_links, self.time_mentions = self.transform_content( self.message["content"], self.model.server_url From 17d3ade254d57068cd0f9bc4f1219ed789ec9960 Mon Sep 17 00:00:00 2001 From: rsashank Date: Sat, 12 Oct 2024 11:20:22 +0530 Subject: [PATCH 3/7] keys: Add SHOW_POLL_VOTES hotkey. SHOW_POLL_VOTES : 'v' to check votes for a given poll. --- docs/hotkeys.md | 1 + zulipterminal/config/keys.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/docs/hotkeys.md b/docs/hotkeys.md index 43bbf6125a..02f8824350 100644 --- a/docs/hotkeys.md +++ b/docs/hotkeys.md @@ -62,6 +62,7 @@ |Toggle star status of the current message|Ctrl + s / *| |Show/hide message information|i| |Show/hide message sender information|u| +|Show/hide poll voter list|v| ## Stream list actions |Command|Key Combination| diff --git a/zulipterminal/config/keys.py b/zulipterminal/config/keys.py index a86420a364..b34d0904a0 100644 --- a/zulipterminal/config/keys.py +++ b/zulipterminal/config/keys.py @@ -300,6 +300,11 @@ class KeyBinding(TypedDict): 'help_text': 'Show/hide message sender information', 'key_category': 'msg_actions', }, + 'SHOW_POLL_VOTES': { + 'keys': ['v'], + 'help_text': 'Show/hide poll voter list', + 'key_category': 'msg_actions', + }, 'EDIT_HISTORY': { 'keys': ['e'], 'help_text': 'Show/hide edit history', From 3b296427a8775687832005767f22ed8d98a58dbe Mon Sep 17 00:00:00 2001 From: rsashank Date: Sat, 12 Oct 2024 11:27:50 +0530 Subject: [PATCH 4/7] views: Add PollResultsView class. Takes poll_question and options as an input and displays voters for each option. --- zulipterminal/ui_tools/views.py | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/zulipterminal/ui_tools/views.py b/zulipterminal/ui_tools/views.py index 00158f88e2..e31d6e7eca 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -23,6 +23,7 @@ from zulipterminal.config.symbols import ( CHECK_MARK, COLUMN_TITLE_BAR_LINE, + INVALID_MARKER, PINNED_STREAMS_DIVIDER, SECTION_DIVIDER_LINE, ) @@ -2172,3 +2173,37 @@ def keypress(self, size: urwid_Size, key: str) -> str: self.controller.exit_popup() return key return super().keypress(size, key) + + +class PollResultsView(PopUpView): + def __init__( + self, + controller: Any, + poll_question: str, + poll_options: Dict[str, Dict[str, Any]], + ) -> None: + poll_results_content: List[Tuple[str, List[Tuple[str, str]]]] = [("", [])] + + for option_key, option_data in poll_options.items(): + option_text = option_data["option"] + if len(option_text) >= 13: + option_text = option_text[:10] + "…" + voter_names = option_data["votes"] + + voters_display = ( + "\n".join(map(str, voter_names)) + if voter_names + else f"{INVALID_MARKER} No votes yet" + ) + + poll_results_content[0][1].append((option_text, voters_display)) + + popup_width, column_widths = self.calculate_table_widths( + poll_results_content, len(poll_question) + ) + + widgets = self.make_table_with_categories(poll_results_content, column_widths) + + super().__init__( + controller, widgets, "SHOW_POLL_VOTES", popup_width, poll_question + ) From 990118a8adc6b625334ae75a5e2ab25e4bf7d511 Mon Sep 17 00:00:00 2001 From: rsashank Date: Sat, 12 Oct 2024 11:29:14 +0530 Subject: [PATCH 5/7] core: Add show_poll_vote method to Controller. Creates an instance of PollResultsView class and shows the popup. --- zulipterminal/core.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/zulipterminal/core.py b/zulipterminal/core.py index 61c5f79922..3540f527cb 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -43,6 +43,7 @@ MarkdownHelpView, MsgInfoView, NoticeView, + PollResultsView, PopUpConfirmationView, StreamInfoView, StreamMembersView, @@ -281,6 +282,34 @@ def show_msg_info( ) self.show_pop_up(msg_info_view, "area:msg") + def show_poll_vote( + self, + poll_question: str, + options: Dict[str, Dict[str, Any]], + ) -> None: + options_with_names = {} + for option_key, option_data in options.items(): + option_text = option_data["option"] + voter_ids = option_data["votes"] + + voter_names = [] + for voter_id in voter_ids: + voter_names.append(self.model.user_name_from_id(voter_id)) + + options_with_names[option_key] = { + "option": option_text, + "votes": voter_names if voter_names else [], + } + + self.show_pop_up( + PollResultsView( + self, + poll_question, + options_with_names, + ), + "area:msg", + ) + def show_emoji_picker(self, message: Message) -> None: all_emoji_units = [ (emoji_name, emoji["code"], emoji["aliases"]) From 78fd5dfa95791b35fde88261269d83a2040294d3 Mon Sep 17 00:00:00 2001 From: rsashank Date: Sat, 12 Oct 2024 11:30:27 +0530 Subject: [PATCH 6/7] messages: Add logic to handle the SHOW_POLL_VOTES hotkey. --- zulipterminal/ui_tools/messages.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/zulipterminal/ui_tools/messages.py b/zulipterminal/ui_tools/messages.py index 42bc893ee2..1c5a112f65 100644 --- a/zulipterminal/ui_tools/messages.py +++ b/zulipterminal/ui_tools/messages.py @@ -69,6 +69,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None: self.topic_links: Dict[str, Tuple[str, int, bool]] = dict() self.time_mentions: List[Tuple[str, str]] = list() self.last_message = last_message + self.widget_type: str = "" # if this is the first message if self.last_message is None: self.last_message = defaultdict(dict) @@ -735,9 +736,9 @@ def main_view(self) -> List[Any]: ) if self.message.get("submessages"): - widget_type = find_widget_type(self.message.get("submessages", [])) + self.widget_type = find_widget_type(self.message.get("submessages", [])) - if widget_type == "todo": + if self.widget_type == "todo": title, tasks = process_todo_widget(self.message.get("submessages", [])) todo_widget = "To-do\n" + f"{title}" @@ -759,24 +760,24 @@ def main_view(self) -> List[Any]: # though it's not very useful. self.message["content"] = todo_widget - elif widget_type == "poll": - poll_question, poll_options = process_poll_widget( + elif self.widget_type == "poll": + self.poll_question, self.poll_options = process_poll_widget( self.message.get("submessages", []) ) poll_widget = ( - f"Poll\n{poll_question}" - if poll_question + f"Poll\n{self.poll_question}" + if self.poll_question else "No poll question provided. Please add one via the web app." ) - if poll_options: + if self.poll_options: max_votes_len = max( len(str(len(option["votes"]))) - for option in poll_options.values() + for option in self.poll_options.values() ) - for option_info in poll_options.values(): + for option_info in self.poll_options.values(): padded_votes = f"{len(option_info['votes']):>{max_votes_len}}" poll_widget += f"\n[ {padded_votes} ] {option_info['option']}" else: @@ -1185,4 +1186,6 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]: self.model.controller.show_emoji_picker(self.message) elif is_command_key("MSG_SENDER_INFO", key): self.model.controller.show_msg_sender_info(self.message["sender_id"]) + elif is_command_key("SHOW_POLL_VOTES", key) and self.widget_type == "poll": + self.model.controller.show_poll_vote(self.poll_question, self.poll_options) return key From 9d75f5056fe174e62efe2bc28ca0198262059a4a Mon Sep 17 00:00:00 2001 From: rsashank Date: Mon, 4 Nov 2024 15:19:35 +0530 Subject: [PATCH 7/7] messages: Update poll_question with a default text if empty. This prevents an empty-title popup in the Poll Results view. --- zulipterminal/ui_tools/messages.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/zulipterminal/ui_tools/messages.py b/zulipterminal/ui_tools/messages.py index 1c5a112f65..1ea76bd561 100644 --- a/zulipterminal/ui_tools/messages.py +++ b/zulipterminal/ui_tools/messages.py @@ -771,6 +771,11 @@ def main_view(self) -> List[Any]: else "No poll question provided. Please add one via the web app." ) + if not self.poll_question: + # If no poll question is provided, set a message to display + # in Poll Results popup. + self.poll_question = "No poll question provided." + if self.poll_options: max_votes_len = max( len(str(len(option["votes"])))