|
1 | 1 | import threading
|
2 | 2 | import time
|
3 | 3 | from collections import OrderedDict
|
4 |
| -from typing import Any, Callable, List, Optional, Tuple |
| 4 | +from typing import Any, Callable, Dict, List, Optional, Tuple |
5 | 5 |
|
6 | 6 | import urwid
|
7 | 7 |
|
@@ -940,8 +940,72 @@ def __init__(self, controller: Any, msg: Any) -> None:
|
940 | 940 | self.controller = controller
|
941 | 941 | self.msg = msg
|
942 | 942 | self.width = 64
|
943 |
| - self.height = 0 |
944 |
| - super().__init__(controller, [], 'MSG_INFO') |
| 943 | + |
| 944 | + self.widgets = [] |
| 945 | + # FIXME: Limit calls by checking if the message has been edited using |
| 946 | + # 'edit_history' boolean (requires server change). |
| 947 | + assert self.controller.model.get_message_history(self.msg) == '' |
| 948 | + message_history = self.msg['message_history'] |
| 949 | + if len(message_history) > 1: |
| 950 | + for index, snapshot in enumerate(message_history): |
| 951 | + if index == len(message_history) - 1: |
| 952 | + self.widgets.append(self.build_pile(snapshot, 'Current')) |
| 953 | + continue |
| 954 | + elif index == 0: |
| 955 | + self.widgets.append(self.build_pile(snapshot, 'Original', |
| 956 | + 'Posted')) |
| 957 | + else: |
| 958 | + self.widgets.append(self.build_pile(snapshot)) |
| 959 | + # Add a blank line after an edit block/pile. |
| 960 | + self.widgets.append(urwid.Text('')) |
| 961 | + else: |
| 962 | + text = 'The content has not been edited.' |
| 963 | + self.widgets = [urwid.Text(text, align='center')] |
| 964 | + |
| 965 | + self.height = sum(widget.rows((self.width,)) |
| 966 | + for widget in self.widgets) |
| 967 | + |
| 968 | + super().__init__(controller, self.widgets, 'MSG_INFO') |
| 969 | + |
| 970 | + def build_pile(self, snapshot: Dict[str, Any], tag: str='', |
| 971 | + author_prefix: str='Edited') -> Any: |
| 972 | + timestamp = time.ctime(snapshot['timestamp'])[:-5] |
| 973 | + author = self.get_author(snapshot['user_id']) |
| 974 | + topic = snapshot['topic'] |
| 975 | + |
| 976 | + if 'prev_topic' in snapshot and topic != snapshot['prev_topic']: |
| 977 | + topic += ' (updated from {})'.format(snapshot['prev_topic']) |
| 978 | + if 'prev_content' in snapshot: |
| 979 | + author_prefix += ' and Topic updated' |
| 980 | + else: |
| 981 | + author_prefix = 'Topic updated' |
| 982 | + |
| 983 | + header = urwid.Columns([ |
| 984 | + urwid.Text(('edit_topic', topic)), |
| 985 | + (19, urwid.Text(('edit_time', timestamp))), |
| 986 | + ], dividechars=2, |
| 987 | + ) |
| 988 | + subheader = urwid.Columns([ |
| 989 | + urwid.Text(('edit_author', |
| 990 | + '{} by {}'.format(author_prefix, author))), |
| 991 | + (18, urwid.Text(('edit_tag', '({} Version)'.format(tag) if tag |
| 992 | + else ''), align='right')), |
| 993 | + ], dividechars=2, |
| 994 | + ) |
| 995 | + message = urwid.Text(snapshot['content']) |
| 996 | + return urwid.Pile([urwid.AttrWrap(header, 'popup_contrast'), subheader, |
| 997 | + message]) |
| 998 | + |
| 999 | + def get_author(self, user_id: int) -> str: |
| 1000 | + if self.msg['sender_id'] == user_id: |
| 1001 | + return self.msg['sender_full_name'] |
| 1002 | + else: |
| 1003 | + for user in self.controller.view.users: |
| 1004 | + if user['user_id'] == user_id: |
| 1005 | + return user['full_name'] |
| 1006 | + # NOTE: This return statement is redundant and only put here to pass |
| 1007 | + # mypy as a user with `user_id` will always exist. |
| 1008 | + return '' |
945 | 1009 |
|
946 | 1010 | def keypress(self, size: urwid_Size, key: str) -> str:
|
947 | 1011 | if (is_command_key('GO_BACK', key)
|
|
0 commit comments