def _handle_update_message_flags_event(self, event: Event) -> None: """ Handle change to message flags (eg. starred, read) """ if event['all']: # FIXME Should handle eventually return flag_to_change = event['flag'] if flag_to_change not in {'starred', 'read'}: return if flag_to_change == 'read' and event['operation'] == 'remove': return indexed_message_ids = set(self.index['messages']) message_ids_to_mark = set(event['messages']) for message_id in message_ids_to_mark & indexed_message_ids: msg = self.index['messages'][message_id] if event['operation'] == 'add': if flag_to_change not in msg['flags']: msg['flags'].append(flag_to_change) elif event['operation'] == 'remove': if flag_to_change in msg['flags']: msg['flags'].remove(flag_to_change) else: raise RuntimeError(event, msg['flags']) self.index['messages'][message_id] = msg self._update_rendered_view(message_id) if event['operation'] == 'add' and flag_to_change == 'read': set_count(list(message_ids_to_mark & indexed_message_ids), self.controller, -1)
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 mark_message_ids_as_read(self, id_list: List[int]) -> None: if not id_list: return self.client.update_message_flags({ 'messages': id_list, 'flag': 'read', 'op': 'add', }) set_count(id_list, self.controller, -1) # FIXME Update?
def update_flag(self, id_list: List[int]) -> None: if not id_list: return request = { 'messages': id_list, 'flag': 'read', 'op': 'add', } self.client.do_api_query(request, '/json/messages/flags', method="POST") set_count(id_list, self.controller, -1) # FIXME Update?
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 _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()