def append_message(self, response: Dict[str, Any]) -> None: """ Adds message to the end of the view. """ response['flags'] = [] if hasattr(self.controller, 'view') and self.update: self.index = index_messages([response], self, self.index) msg_w_list = create_msg_box_list(self, [response['id']]) if not msg_w_list: return else: msg_w = msg_w_list[0] if not self.narrow: self.msg_list.log.append(msg_w) elif self.narrow[0][1] == response['type'] and\ len(self.narrow) == 1: self.msg_list.log.append(msg_w) elif response['type'] == 'stream' and len(self.narrow) == 2 and\ self.narrow[1][1] == response['subject']: self.msg_list.log.append(msg_w) elif response['type'] == 'private' and len(self.narrow) == 1 and\ self.narrow[0][0] == "pm_with": recipients = self.recipients msg_recipients = frozenset([ self.user_id, self.user_dict[self.narrow[0][1]]['user_id'] ]) if recipients == msg_recipients: self.msg_list.log.append(msg_w) set_count([response['id']], self.controller, 1) self.controller.update_screen()
def test_index_mentioned_messages( mocker: MockerFixture, messages_successful_response: Dict[str, Any], empty_index: Index, mentioned_messages_combination: Tuple[Set[int], Set[int]], initial_index: Index, ) -> None: messages = messages_successful_response["messages"] mentioned_messages, wildcard_mentioned_messages = mentioned_messages_combination for msg in messages: if msg["id"] in mentioned_messages and "mentioned" not in msg["flags"]: msg["flags"].append("mentioned") if (msg["id"] in wildcard_mentioned_messages and "wildcard_mentioned" not in msg["flags"]): msg["flags"].append("wildcard_mentioned") model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [["is", "mentioned"]] model.is_search_narrow.return_value = False expected_index: Dict[str, Any] = dict( empty_index, private_msg_ids={537287, 537288}, mentioned_msg_ids=(mentioned_messages | wildcard_mentioned_messages), ) for msg_id, msg in expected_index["messages"].items(): if msg_id in mentioned_messages and "mentioned" not in msg["flags"]: msg["flags"].append("mentioned") if (msg["id"] in wildcard_mentioned_messages and "wildcard_mentioned" not in msg["flags"]): msg["flags"].append("wildcard_mentioned") assert index_messages(messages, model, model.index) == expected_index
def get_messages(self, *, num_after: int, num_before: int, anchor: Optional[int]) -> Any: # anchor value may be specific message (int) or next unread (None) first_anchor = anchor is None anchor_value = anchor if anchor is not None else 0 request = { 'anchor': anchor_value, 'num_before': num_before, 'num_after': num_after, 'apply_markdown': True, 'use_first_unread_anchor': first_anchor, 'client_gravatar': False, 'narrow': json.dumps(self.narrow), } response = self.client.do_api_query(request, '/json/messages', method="GET") for msg in response['messages']: with open('../res.txt', 'a') as f: f.write(str(msg['content']) + "\n\n") if response['result'] == 'success': self.index = index_messages(response['messages'], self, self.index) if first_anchor: self.index[str(self.narrow)] = response['anchor'] query_range = num_after + num_before + 1 if len(response['messages']) < (query_range): self.update = True return self.index
def get_messages(self, *, num_after: int, num_before: int, anchor: Optional[int]) -> str: # anchor value may be specific message (int) or next unread (None) first_anchor = anchor is None anchor_value = anchor if anchor is not None else 0 request = { 'anchor': anchor_value, 'num_before': num_before, 'num_after': num_after, 'apply_markdown': True, 'use_first_unread_anchor': first_anchor, 'client_gravatar': True, 'narrow': json.dumps(self.narrow), } response = self.client.get_messages(message_filters=request) if response['result'] == 'success': self.index = index_messages(response['messages'], self, self.index) narrow_str = repr(self.narrow) if first_anchor and response['anchor'] != 10000000000000000: self.index['pointer'][narrow_str] = response['anchor'] if 'found_newest' in response: self._have_last_message[narrow_str] = response['found_newest'] else: # Older versions of the server does not contain the # 'found_newest' flag. Instead, we use this logic: query_range = num_after + num_before + 1 self._have_last_message[narrow_str] = (len( response['messages']) < query_range) return "" return response['msg']
def get_messages(self, *, num_after: int, num_before: int, anchor: Optional[int]) -> bool: # anchor value may be specific message (int) or next unread (None) first_anchor = anchor is None anchor_value = anchor if anchor is not None else 0 request = { 'anchor': anchor_value, 'num_before': num_before, 'num_after': num_after, 'apply_markdown': True, 'use_first_unread_anchor': first_anchor, 'client_gravatar': True, 'narrow': json.dumps(self.narrow), } response = self.client.get_messages(message_filters=request) if response['result'] == 'success': self.index = index_messages(response['messages'], self, self.index) if first_anchor and response['anchor'] != 10000000000000000: self.index['pointer'][str(self.narrow)] = response['anchor'] query_range = num_after + num_before + 1 if len(response['messages']) < (query_range): self.update = True return True return False
def test_index_messages_narrow_all_messages(mocker, messages_successful_response, index_all_messages) -> None: messages = messages_successful_response['messages'] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.narrow = [] assert index_messages(messages, model) == index_all_messages
def test_index_messages_narrow_topic(mocker, messages_successful_response, index_topic) -> None: messages = messages_successful_response['messages'] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.narrow = [['stream', '7'], ['topic', 'Test']] model.stream_id = 205 assert index_messages(messages, model) == index_topic
def test_index_messages_narrow_stream(mocker, messages_successful_response, index_stream, initial_index) -> None: messages = messages_successful_response['messages'] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.index = initial_index model.narrow = [['stream', 'PTEST']] model.stream_id = 205 assert index_messages(messages, model, model.index) == index_stream
def test_index_messages_narrow_all_messages(mocker, messages_successful_response, index_all_messages, initial_index) -> None: messages = messages_successful_response["messages"] model = mocker.patch("zulipterminal.model.Model.__init__", return_value=None) model.index = initial_index model.narrow = [] assert index_messages(messages, model, model.index) == index_all_messages
def test_index_messages_narrow_topic(mocker, messages_successful_response, index_topic, initial_index) -> None: messages = messages_successful_response["messages"] model = mocker.patch("zulipterminal.model.Model.__init__", return_value=None) model.index = initial_index model.narrow = [["stream", "7"], ["topic", "Test"]] model.is_search_narrow.return_value = False model.stream_id = 205 assert index_messages(messages, model, model.index) == index_topic
def test_index_messages_narrow_all_messages( mocker: MockerFixture, messages_successful_response: Dict[str, Any], index_all_messages: Index, initial_index: Index, ) -> None: messages = messages_successful_response["messages"] model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [] assert index_messages(messages, model, model.index) == index_all_messages
def test_index_messages_narrow_topic( mocker: MockerFixture, messages_successful_response: Dict[str, Any], index_topic: Index, initial_index: Index, ) -> None: messages = messages_successful_response["messages"] model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [["stream", "7"], ["topic", "Test"]] model.is_search_narrow.return_value = False model.stream_id = 205 assert index_messages(messages, model, model.index) == index_topic
def test_index_messages_narrow_user(mocker, messages_successful_response, index_user): messages = messages_successful_response['messages'] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.narrow = [['pm_with', '*****@*****.**']] model.user_id = 5140 model.user_dict = { '*****@*****.**': { 'user_id': 5179, } } assert index_messages(messages, model) == index_user
def append_message(self, event: Event) -> None: """ Adds message to the end of the view. """ response = event['message'] # sometimes `flags` are missing in `event` so initialize # an empty list of flags in that case. response['flags'] = event.get('flags', []) if hasattr(self.controller, 'view') and self.found_newest: self.index = index_messages([response], self, self.index) if self.msg_list.log: last_message = self.msg_list.log[-1].original_widget.message else: last_message = None msg_w_list = create_msg_box_list(self, [response['id']], last_message=last_message) if not msg_w_list: return else: msg_w = msg_w_list[0] if not self.narrow: self.msg_list.log.append(msg_w) elif self.narrow[0][1] == response['type'] and\ len(self.narrow) == 1: self.msg_list.log.append(msg_w) elif response['type'] == 'stream' and \ self.narrow[0][0] == "stream": recipient_stream = response['display_recipient'] narrow_stream = self.narrow[0][1] append_to_stream = recipient_stream == narrow_stream if append_to_stream and ( len(self.narrow) == 1 or (len(self.narrow) == 2 and self.narrow[1][1] == response['subject'])): self.msg_list.log.append(msg_w) elif response['type'] == 'private' and len(self.narrow) == 1 and\ self.narrow[0][0] == "pm_with": narrow_recipients = self.recipients message_recipients = frozenset( [user['id'] for user in response['display_recipient']]) if narrow_recipients == message_recipients: self.msg_list.log.append(msg_w) if 'read' not in response['flags']: set_count([response['id']], self.controller, 1) self.controller.update_screen()
def test_index_messages_narrow_user(mocker, messages_successful_response, index_user, initial_index) -> None: messages = messages_successful_response["messages"] model = mocker.patch("zulipterminal.model.Model.__init__", return_value=None) model.index = initial_index model.narrow = [["pm_with", "*****@*****.**"]] model.is_search_narrow.return_value = False model.user_id = 5140 model.user_dict = { "*****@*****.**": { "user_id": 5179, } } assert index_messages(messages, model, model.index) == index_user
def test_index_messages_narrow_user( mocker: MockerFixture, messages_successful_response: Dict[str, Any], index_user: Index, initial_index: Index, ) -> None: messages = messages_successful_response["messages"] model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [["pm_with", "*****@*****.**"]] model.is_search_narrow.return_value = False model.user_id = 5140 model.user_dict = { "*****@*****.**": { "user_id": 5179, } } assert index_messages(messages, model, model.index) == index_user
def test_index_edited_message(mocker, messages_successful_response, empty_index, edited_msgs, initial_index): messages = messages_successful_response['messages'] for msg in messages: if msg['id'] in edited_msgs: msg['edit_history'] = [] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.index = initial_index model.narrow = [] expected_index = dict(empty_index, edited_messages=edited_msgs, all_msg_ids={537286, 537287, 537288}) for msg_id, msg in expected_index['messages'].items(): if msg_id in edited_msgs: msg['edit_history'] = [] assert index_messages(messages, model, model.index) == expected_index
def test_index_starred(mocker, messages_successful_response, empty_index, msgs_with_stars, initial_index): messages = messages_successful_response["messages"] for msg in messages: if msg["id"] in msgs_with_stars and "starred" not in msg["flags"]: msg["flags"].append("starred") model = mocker.patch("zulipterminal.model.Model.__init__", return_value=None) model.index = initial_index model.narrow = [["is", "starred"]] model.is_search_narrow.return_value = False expected_index = dict(empty_index, private_msg_ids={537287, 537288}, starred_msg_ids=msgs_with_stars) for msg_id, msg in expected_index["messages"].items(): if msg_id in msgs_with_stars and "starred" not in msg["flags"]: msg["flags"].append("starred") assert index_messages(messages, model, model.index) == expected_index
def test_index_messages_narrow_user_multiple(mocker, messages_successful_response, index_user_multiple, initial_index) -> None: messages = messages_successful_response['messages'] model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.index = initial_index model.narrow = [['pm_with', '[email protected], [email protected]']] model.is_search_narrow.return_value = False model.user_id = 5140 model.user_dict = { '*****@*****.**': { 'user_id': 5179, }, '*****@*****.**': { 'user_id': 5180 } } assert index_messages(messages, model, model.index) == index_user_multiple
def test_index_starred(mocker, messages_successful_response, empty_index, msgs_with_stars, initial_index): messages = messages_successful_response['messages'] for msg in messages: if msg['id'] in msgs_with_stars and 'starred' not in msg['flags']: msg['flags'].append('starred') model = mocker.patch('zulipterminal.model.Model.__init__', return_value=None) model.index = initial_index model.narrow = [['is', 'starred']] model.is_search_narrow.return_value = False expected_index = dict(empty_index, private_msg_ids={537287, 537288}, starred_msg_ids=msgs_with_stars) for msg_id, msg in expected_index['messages'].items(): if msg_id in msgs_with_stars and 'starred' not in msg['flags']: msg['flags'].append('starred') assert index_messages(messages, model, model.index) == expected_index
def get_messages(self, first_anchor: bool) -> Any: request = { 'anchor': self.anchor, 'num_before': self.num_before, 'num_after': self.num_after, 'apply_markdown': True, 'use_first_unread_anchor': first_anchor, 'client_gravatar': False, 'narrow': json.dumps(self.narrow), } response = self.client.do_api_query(request, '/json/messages', method="GET") for msg in response['messages']: with open('../res.txt', 'a') as f: f.write(str(msg['content']) + "\n\n") if response['result'] == 'success': self.index = index_messages(response['messages'], self, self.index) if first_anchor: self.index[str(self.narrow)] = response['anchor'] query_range = self.num_after + self.num_before + 1 if len(response['messages']) < (query_range): self.update = True return self.index
def test_index_edited_message( mocker: MockerFixture, messages_successful_response: Dict[str, Any], empty_index: Index, edited_msgs: Set[int], initial_index: Index, ) -> None: messages = messages_successful_response["messages"] for msg in messages: if msg["id"] in edited_msgs: msg["edit_history"] = [] model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [] expected_index: Dict[str, Any] = dict( empty_index, edited_messages=edited_msgs, all_msg_ids={537286, 537287, 537288} ) for msg_id, msg in expected_index["messages"].items(): if msg_id in edited_msgs: msg["edit_history"] = [] assert index_messages(messages, model, model.index) == expected_index
def test_index_starred( mocker: MockerFixture, messages_successful_response: Dict[str, Any], empty_index: Index, msgs_with_stars: Set[int], initial_index: Index, ) -> None: messages = messages_successful_response["messages"] for msg in messages: if msg["id"] in msgs_with_stars and "starred" not in msg["flags"]: msg["flags"].append("starred") model = mocker.patch(MODEL + ".__init__", return_value=None) model.index = initial_index model.narrow = [["is", "starred"]] model.is_search_narrow.return_value = False expected_index: Dict[str, Any] = dict( empty_index, private_msg_ids={537287, 537288}, starred_msg_ids=msgs_with_stars ) for msg_id, msg in expected_index["messages"].items(): if msg_id in msgs_with_stars and "starred" not in msg["flags"]: msg["flags"].append("starred") assert index_messages(messages, model, model.index) == expected_index
def _handle_message_event(self, event: Event) -> None: """ Handle new messages (eg. add message to the end of the view) """ message = event['message'] # sometimes `flags` are missing in `event` so initialize # an empty list of flags in that case. message['flags'] = event.get('flags', []) # We need to update the topic order in index, unconditionally. if message['type'] == 'stream': self._update_topic_index(message['stream_id'], message['subject']) # If the topic view is toggled for incoming message's # recipient stream, then we re-arrange topic buttons # with most recent at the top. if (hasattr(self.controller, 'view') and self.controller.view.left_panel.is_in_topic_view and message['stream_id'] == self.controller.view.topic_w.stream_button.stream_id): self.controller.view.topic_w.update_topics_list( message['stream_id'], message['subject'], message['sender_id']) self.controller.update_screen() # We can notify user regardless of whether UI is rendered or not, # but depend upon the UI to indicate failures. failed_command = self.notify_user(message) if (failed_command and hasattr(self.controller, 'view') and not self._notified_user_of_notification_failure): notice_template = ( "You have enabled notifications, but your notification " "command '{}' could not be found." "\n\n" "The application will continue attempting to run this command " "in this session, but will not notify you again." "\n\n" "Press '{}' to close this window.") notice = notice_template.format(failed_command, keys_for_command("GO_BACK").pop()) self.controller.popup_with_message(notice, width=50) self.controller.update_screen() self._notified_user_of_notification_failure = True # Index messages before calling set_count. self.index = index_messages([message], self, self.index) if 'read' not in message['flags']: set_count([message['id']], self.controller, 1) if (hasattr(self.controller, 'view') and self._have_last_message[repr(self.narrow)]): if self.msg_list.log: last_message = self.msg_list.log[-1].original_widget.message else: last_message = None msg_w_list = create_msg_box_list(self, [message['id']], last_message=last_message) if not msg_w_list: return else: msg_w = msg_w_list[0] if not self.narrow: self.msg_list.log.append(msg_w) elif (self.narrow[0][1] == 'mentioned' and 'mentioned' in message['flags']): self.msg_list.log.append(msg_w) elif (self.narrow[0][1] == message['type'] and len(self.narrow) == 1): self.msg_list.log.append(msg_w) elif (message['type'] == 'stream' and self.narrow[0][0] == "stream"): recipient_stream = message['display_recipient'] narrow_stream = self.narrow[0][1] append_to_stream = recipient_stream == narrow_stream if (append_to_stream and (len(self.narrow) == 1 or (len(self.narrow) == 2 and self.narrow[1][1] == message['subject']))): self.msg_list.log.append(msg_w) elif (message['type'] == 'private' and len(self.narrow) == 1 and self.narrow[0][0] == "pm_with"): narrow_recipients = self.recipients message_recipients = frozenset( [user['id'] for user in message['display_recipient']]) if narrow_recipients == message_recipients: self.msg_list.log.append(msg_w) self.controller.update_screen()
def append_message(self, event: Event) -> None: """ Adds message to the end of the view. """ response = event['message'] # sometimes `flags` are missing in `event` so initialize # an empty list of flags in that case. response['flags'] = event.get('flags', []) # We need to update the topic order in index, unconditionally. if response['type'] == 'stream': self.update_topic_index(response['stream_id'], response['subject']) # If the topic view is toggled for incoming message's # recipient stream, then we re-arrange topic buttons # with most recent at the top. if (hasattr(self.controller, 'view') and self.controller.view.left_panel.is_in_topic_view and response['stream_id'] == self.controller.view.topic_w.stream_button.stream_id): self.controller.view.topic_w.update_topics_list( response['stream_id'], response['subject'], response['sender_id']) self.controller.update_screen() # We can notify user regardless of whether UI is rendered or not. self.notify_user(response) if hasattr(self.controller, 'view') and self.found_newest: self.index = index_messages([response], self, self.index) if self.msg_list.log: last_message = self.msg_list.log[-1].original_widget.message else: last_message = None msg_w_list = create_msg_box_list(self, [response['id']], last_message=last_message) if not msg_w_list: return else: msg_w = msg_w_list[0] if not self.narrow: self.msg_list.log.append(msg_w) elif self.narrow[0][1] == response['type'] and\ len(self.narrow) == 1: self.msg_list.log.append(msg_w) elif response['type'] == 'stream' and \ self.narrow[0][0] == "stream": recipient_stream = response['display_recipient'] narrow_stream = self.narrow[0][1] append_to_stream = recipient_stream == narrow_stream if append_to_stream and ( len(self.narrow) == 1 or (len(self.narrow) == 2 and self.narrow[1][1] == response['subject'])): self.msg_list.log.append(msg_w) elif response['type'] == 'private' and len(self.narrow) == 1 and\ self.narrow[0][0] == "pm_with": narrow_recipients = self.recipients message_recipients = frozenset( [user['id'] for user in response['display_recipient']]) if narrow_recipients == message_recipients: self.msg_list.log.append(msg_w) if 'read' not in response['flags']: set_count([response['id']], self.controller, 1) self.controller.update_screen()