def create_msg_box_list(model: Any, messages: Union[None, Iterable[Any]] = None, focus_msg_id: Any = None) -> List[Any]: if model.narrow == [] and messages is None: messages = list(model.index['all_messages']) if messages is not None: message_list = [model.index['messages'][id] for id in messages] message_list.sort(key=lambda msg: msg['timestamp']) w_list = [] focus_msg = None last_message = None for msg in message_list: msg_flag = 'unread' # type: Union[str, None] flags = msg.get('flags') # update_messages sends messages with no flags # but flags are set to [] when fetching old messages. if flags and ('read' in flags): msg_flag = None elif focus_msg is None: focus_msg = message_list.index(msg) if msg['id'] == focus_msg_id: focus_msg = message_list.index(msg) w_list.append( urwid.AttrMap(MessageBox(msg, model, last_message), msg_flag, 'msg_selected')) last_message = msg if model.index['pointer'][str(model.narrow)] == set() and\ focus_msg is not None: model.index['pointer'][str(model.narrow)] = focus_msg return w_list
def test_main_view(self, mocker, message, last_message): self.model.stream_dict = { 5: { 'color': '#bfd56f', }, } msg_box = MessageBox(message, self.model, last_message)
def msg_box(mocker, messages_successful_response): """ Mocked MessageBox with stream message """ return MessageBox( messages_successful_response['messages'][0], mocker.patch('zulipterminal.model.Model') )
def msg_box(mocker, messages_successful_response): """ Mocked MessageBox with stream message """ return MessageBox( messages_successful_response["messages"][0], mocker.patch("zulipterminal.model.Model"), None, )
def msg_box(mocker: MockerFixture, messages_successful_response: Dict[str, Any]) -> MessageBox: """ Mocked MessageBox with stream message """ return MessageBox( messages_successful_response["messages"][0], mocker.patch("zulipterminal.model.Model"), None, )
def test_footlinks(self, message_links, expected_text, expected_attrib, expected_footlinks_width): footlinks, footlinks_width = MessageBox.footlinks_view( message_links, maximum_footlinks=10, padded=False, wrap='space', ) assert footlinks.text == expected_text assert footlinks.attrib == expected_attrib assert footlinks_width == expected_footlinks_width
def update_messages(self, response: Dict[str, str]) -> None: if hasattr(self.controller, 'view'): cmsg = classify_message(self.client.email, [response]) key = list(cmsg.keys())[0] if ((self.narrow == []) or (self.narrow[0][1] == key)) and\ self.update: self.msg_list.log.append( urwid.AttrMap(MessageBox(cmsg[key][0], self), cmsg[key][0]['color'], 'msg_selected')) self.controller.loop.draw_screen() else: if hasattr(self, 'messages'): self.messages = classify_message(self.client.email, [response], self.messages) else: self.initial_update.append(response)
def create_msg_box_list( model: Any, messages: Optional[Iterable[Any]] = None, *, focus_msg_id: Optional[int] = None, last_message: Optional[Message] = None, ) -> List[Any]: """ MessageBox for every message displayed is created here. """ if not model.narrow and messages is None: messages = list(model.index["all_msg_ids"]) if messages is not None: message_list = [model.index["messages"][id] for id in messages] message_list.sort(key=lambda msg: msg["timestamp"]) w_list = [] focus_msg = None last_msg = last_message muted_msgs = 0 # No of messages that are muted. for msg in message_list: if is_unsubscribed_message(msg, model): continue # Remove messages of muted topics / streams. if is_muted(msg, model): muted_msgs += 1 if model.narrow == []: # Don't show in 'All messages'. continue msg_flag: Optional[str] = "unread" flags = msg.get("flags") # update_messages sends messages with no flags # but flags are set to [] when fetching old messages. if flags and ("read" in flags): msg_flag = None elif focus_msg is None: focus_msg = message_list.index(msg) - muted_msgs if msg["id"] == focus_msg_id: focus_msg = message_list.index(msg) - muted_msgs w_list.append( urwid.AttrMap(MessageBox(msg, model, last_msg), msg_flag, "msg_selected")) last_msg = msg if focus_msg is not None: model.set_focus_in_current_narrow(focus_msg) return w_list
def create_msg_box_list(model: Any, messages: Union[None, Iterable[Any]]=None, focus_msg_id: Union[None, int]=None, last_message: Union[None, Any]=None) -> List[Any]: """ MessageBox for every message displayed is created here. """ if not model.narrow and messages is None: messages = list(model.index['all_msg_ids']) if messages is not None: message_list = [model.index['messages'][id] for id in messages] message_list.sort(key=lambda msg: msg['timestamp']) w_list = [] focus_msg = None last_msg = last_message muted_msgs = 0 # No of messages that are muted. for msg in message_list: # Remove messages of muted topics / streams. if is_muted(msg, model): muted_msgs += 1 if model.narrow == []: # Don't show in 'All messages'. continue if is_unsubscribed_message(msg, model): continue msg_flag = 'unread' # type: Union[str, None] flags = msg.get('flags') # update_messages sends messages with no flags # but flags are set to [] when fetching old messages. if flags and ('read' in flags): msg_flag = None elif focus_msg is None: focus_msg = message_list.index(msg) - muted_msgs if msg['id'] == focus_msg_id: focus_msg = message_list.index(msg) - muted_msgs w_list.append(urwid.AttrMap( MessageBox(msg, model, last_msg), msg_flag, 'msg_selected' )) last_msg = msg if focus_msg is not None: model.set_focus_in_current_narrow(focus_msg) return w_list
def load_old_messages(self, anchor: int = 10000000000) -> None: self.old_loading = True # Use the currently focused image as anchor self.model.anchor = anchor # We don't want message after the current message self.model.num_after = 0 self.model.num_before = 30 new_messages = self.model.load_old_messages(False) new_messages = itertools.chain.from_iterable(new_messages.values()) new_messages = sorted(new_messages, key=lambda msg: msg['time'], reverse=True) # Skip the first message as we don't want to display # the focused message again for msg in new_messages[1:]: self.log.insert( 0, urwid.AttrMap(MessageBox(msg, self.model), msg['color'], 'msg_selected')) self.old_loading = False
def load_new_messages(self, anchor: int, focus_position: int) -> None: self.new_loading = True self.model.anchor = anchor self.model.num_before = 0 self.model.num_after = 30 new_messages = self.model.load_old_messages(False) msg_list = list(itertools.chain.from_iterable(new_messages.values())) if len(msg_list) < 31: self.model.update = True new_messages = msg_list new_messages = sorted(new_messages, key=lambda msg: msg['time'], reverse=True) # Skip the first message as we don't want to display the # focused message again for msg in new_messages[:-1]: self.log.insert( self.focus_position + 1, urwid.AttrMap(MessageBox(msg, self.model), msg['color'], 'msg_selected')) self.new_loading = False
def create_msg_box_list(messages: List[Dict[str, Any]], model: Any, narrow: bool = False, id: int = None) -> List[Any]: messages = sorted(messages, key=lambda msg: msg['time']) focus_msg_id = model.focus_all_msg if narrow: focus_msg_id = model.focus_narrow if id is not None: focus_msg_id = id focus_msg = len(messages) - 1 for msg in messages: if msg['id'] == focus_msg_id: focus_msg = messages.index(msg) w_list = [ urwid.AttrMap(MessageBox(item, model), item['color'], 'msg_selected') for item in messages ] if focus_msg > 0: focus_msg -= 1 return w_list, focus_msg
def test_init(self, mocker): mocker.patch.object(MessageBox, 'main_view') msg_box = MessageBox({}, self.model, None) assert msg_box.last_message == defaultdict(dict)