Skip to content

Commit 708f2c6

Browse files
committed
model/views: Show edit history using EditHistoryView.
This GETs 'message_history', using message id, from the server and displays it in the EditHistoryView. The EditHistoryView (popup) can be triggered with 'EDIT_HISTORY' key from the MsgInfoView. The following information is displayed in the view: * topic (with 'updated from' prefix if it's been updated) * timestamp * author name * tag (for current and original message) * content
1 parent 3d6c9bb commit 708f2c6

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

zulipterminal/model.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ def get_messages(self, *,
356356
return ""
357357
return response['msg']
358358

359+
def get_message_history(self, message: Message) -> str:
360+
"""
361+
GETs 'message_history' using messsage id.
362+
"""
363+
response = self.client.get_message_history(message_id=message['id'])
364+
if response['result'] == 'success':
365+
message['message_history'] = response['message_history']
366+
return ''
367+
return response['msg']
368+
359369
def get_topics_in_stream(self, stream_list: Iterable[int]) -> str:
360370
"""
361371
Fetch all topics with specified stream_id's and

zulipterminal/ui_tools/views.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import threading
22
import time
33
from collections import OrderedDict
4-
from typing import Any, Callable, List, Optional, Tuple
4+
from typing import Any, Callable, Dict, List, Optional, Tuple
55

66
import urwid
77

@@ -940,8 +940,72 @@ def __init__(self, controller: Any, msg: Any) -> None:
940940
self.controller = controller
941941
self.msg = msg
942942
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 ''
9451009

9461010
def keypress(self, size: urwid_Size, key: str) -> str:
9471011
if (is_command_key('GO_BACK', key)

0 commit comments

Comments
 (0)