|
2 | 2 | UI to render a Zulip message for display, and respond contextually to actions
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import json |
5 | 6 | import typing
|
6 | 7 | from collections import defaultdict
|
7 | 8 | from datetime import date, datetime
|
@@ -729,6 +730,52 @@ def main_view(self) -> List[Any]:
|
729 | 730 | "/me", f"<strong>{self.message['sender_full_name']}</strong>", 1
|
730 | 731 | )
|
731 | 732 |
|
| 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 | + |
732 | 779 | # Transform raw message content into markup (As needed by urwid.Text)
|
733 | 780 | content, self.message_links, self.time_mentions = self.transform_content(
|
734 | 781 | self.message["content"], self.model.server_url
|
|
0 commit comments