Skip to content

Commit e91468a

Browse files
committed
messages: WIP poll widget
1 parent b71aec2 commit e91468a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

zulipterminal/ui_tools/messages.py

+47
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
UI to render a Zulip message for display, and respond contextually to actions
33
"""
44

5+
import json
56
import typing
67
from collections import defaultdict
78
from datetime import date, datetime
@@ -729,6 +730,52 @@ def main_view(self) -> List[Any]:
729730
"/me", f"<strong>{self.message['sender_full_name']}</strong>", 1
730731
)
731732

733+
# WIP: Implementation for POLL messages
734+
if self.message["submessages"]:
735+
question = ""
736+
options = {}
737+
738+
for submessage in self.message["submessages"]:
739+
content = submessage.get("content", {})
740+
if isinstance(content, str):
741+
try:
742+
content = json.loads(content)
743+
except json.JSONDecodeError:
744+
continue
745+
746+
if "widget_type" in content and content["widget_type"] == "poll":
747+
question = content["extra_data"]["question"]
748+
options = {option: 0 for option in content["extra_data"]["options"]}
749+
break
750+
751+
if question:
752+
for submessage in self.message["submessages"]:
753+
content = submessage.get("content", {})
754+
if isinstance(content, str):
755+
try:
756+
content = json.loads(content)
757+
except json.JSONDecodeError:
758+
continue
759+
760+
if "type" in content and content["type"] == "vote":
761+
key = content["key"]
762+
if key.startswith("canned,"):
763+
index_str = key.split(",")[1]
764+
if index_str.isdigit():
765+
index = int(index_str)
766+
if index < len(options):
767+
option = list(options.keys())[index]
768+
if option in options:
769+
options[option] += content["vote"]
770+
771+
self.message[
772+
"content"
773+
] = f"<strong>Poll Question: {question}</strong>\n"
774+
for option, count in options.items():
775+
self.message["content"] += f"Option {option}: {count}\n"
776+
else:
777+
pass
778+
732779
# Transform raw message content into markup (As needed by urwid.Text)
733780
content, self.message_links, self.time_mentions = self.transform_content(
734781
self.message["content"], self.model.server_url

0 commit comments

Comments
 (0)