|
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,61 @@ 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 | + self.height = 10 |
| 944 | + |
| 945 | + self.widgets = [] |
| 946 | + # NOTE: 'edit_history' is likely to be changed to a boolean value in |
| 947 | + # the future. |
| 948 | + if 'edit_history' in self.msg: |
| 949 | + self.controller.model.get_message_history(self.msg) |
| 950 | + for index, snapshot in enumerate(self.msg['message_history']): |
| 951 | + if index == len(self.msg['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 | + else: |
| 957 | + self.widgets.append(self.build_pile(snapshot)) |
| 958 | + self.widgets.append(urwid.Text('')) # Add newline. |
| 959 | + else: |
| 960 | + self.height = 1 |
| 961 | + text = 'The content has not been edited.' |
| 962 | + self.widgets = [urwid.Text(text, align='center')] |
| 963 | + |
| 964 | + super().__init__(controller, self.widgets, 'MSG_INFO') |
| 965 | + |
| 966 | + def build_pile(self, snapshot: Dict[str, Any], tag: str='') -> Any: |
| 967 | + timestamp = time.ctime(snapshot['timestamp'])[:-5] |
| 968 | + full_name = self.get_full_name(snapshot['user_id']) |
| 969 | + topic = snapshot['topic'] |
| 970 | + if 'prev_topic' in snapshot and topic != snapshot['prev_topic']: |
| 971 | + topic += ' (updated from {})'.format(snapshot['prev_topic']) |
| 972 | + |
| 973 | + header = urwid.Columns([ |
| 974 | + urwid.Text(('edit_topic', topic)), |
| 975 | + (19, urwid.Text(('edit_time', timestamp))), |
| 976 | + ], dividechars=2, |
| 977 | + ) |
| 978 | + subheader = urwid.Columns([ |
| 979 | + urwid.Text(('edit_author', full_name)), |
| 980 | + (18, urwid.Text(('edit_tag', '({} Version)'.format(tag) if tag |
| 981 | + else ''), align='right')), |
| 982 | + ], dividechars=2, |
| 983 | + ) |
| 984 | + message = urwid.Text(snapshot['content']) |
| 985 | + return urwid.Pile([urwid.AttrWrap(header, 'popup_contrast'), subheader, |
| 986 | + message]) |
| 987 | + |
| 988 | + def get_full_name(self, user_id: int) -> str: |
| 989 | + if self.msg['sender_id'] == user_id: |
| 990 | + return self.msg['sender_full_name'] |
| 991 | + else: |
| 992 | + for user in self.controller.view.users: |
| 993 | + if user['user_id'] == user_id: |
| 994 | + return user['full_name'] |
| 995 | + # NOTE: This return statement is redundant and only put here to pass |
| 996 | + # mypy as a user with `user_id` will always exist. |
| 997 | + return '' |
945 | 998 |
|
946 | 999 | def keypress(self, size: urwid_Size, key: str) -> str:
|
947 | 1000 | if (is_command_key('GO_BACK', key)
|
|
0 commit comments