def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This is experimental code that only works with the webapp for now. ''' if not settings.ALLOW_SUB_MESSAGES: return content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None if content in ['/poll', '/tictactoe']: widget_type = content[1:] if widget_type: content = dict( widget_type=widget_type, extra_data=extra_data ) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This is experimental code that only works with the webapp for now. ''' if not settings.ALLOW_SUB_MESSAGES: return content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None widget_type, extra_data = get_widget_data(content) widget_content = message.get('widget_content') if widget_content is not None: # Note that we validate this data in check_message, # so we can trust it here. widget_type = widget_content['widget_type'] extra_data = widget_content['extra_data'] if widget_type: content = dict( widget_type=widget_type, extra_data=extra_data ) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This code works with the webapp; mobile and other clients should also start supporting this soon. ''' content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None widget_type, extra_data = get_widget_data(content) widget_content = message.get('widget_content') if widget_content is not None: # Note that we validate this data in check_message, # so we can trust it here. widget_type = widget_content['widget_type'] extra_data = widget_content['extra_data'] if widget_type: content = dict(widget_type=widget_type, extra_data=extra_data) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This is experimental code that only works with the webapp for now. ''' if not settings.ALLOW_SUB_MESSAGES: return content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None if content in ['/poll', '/tictactoe']: widget_type = content[1:] if widget_type: content = dict(widget_type=widget_type, extra_data=extra_data) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This is experimental code that only works with the webapp for now. ''' if not settings.ALLOW_SUB_MESSAGES: return content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None widget_type, extra_data = get_widget_data(content) widget_content = message.get('widget_content') if widget_content is not None: # Note that we validate this data in check_message, # so we can trust it here. widget_type = widget_content['widget_type'] extra_data = widget_content['extra_data'] if widget_type: content = dict( widget_type=widget_type, extra_data=extra_data ) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(send_request: SendMessageRequest) -> None: """ This code works with the web app; mobile and other clients should also start supporting this soon. """ message_content = send_request.message.content sender_id = send_request.message.sender_id message_id = send_request.message.id widget_type = None extra_data = None widget_type, extra_data = get_widget_data(message_content) widget_content = send_request.widget_content if widget_content is not None: # Note that we validate this data in check_message, # so we can trust it here. widget_type = widget_content["widget_type"] extra_data = widget_content["extra_data"] if widget_type: content = dict( widget_type=widget_type, extra_data=extra_data, ) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type="widget", content=json.dumps(content), ) submessage.save() send_request.submessages = SubMessage.get_raw_db_rows([message_id])
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None: ''' This code works with the webapp; mobile and other clients should also start supporting this soon. ''' content = message['message'].content sender_id = message['message'].sender_id message_id = message['message'].id widget_type = None extra_data = None widget_type, extra_data = get_widget_data(content) widget_content = message.get('widget_content') if widget_content is not None: # Note that we validate this data in check_message, # so we can trust it here. widget_type = widget_content['widget_type'] extra_data = widget_content['extra_data'] if widget_type: content = dict( widget_type=widget_type, extra_data=extra_data ) submessage = SubMessage( sender_id=sender_id, message_id=message_id, msg_type='widget', content=json.dumps(content), ) submessage.save() message['submessages'] = SubMessage.get_raw_db_rows([message_id])
def get_raw_db_rows(needed_ids: List[int]) -> List[Dict[str, Any]]: # This is a special purpose function optimized for # callers like get_messages_backend(). fields = [ 'id', DB_TOPIC_NAME, 'pub_date', 'last_edit_time', 'edit_history', 'content', 'rendered_content', 'rendered_content_version', 'recipient_id', 'recipient__type', 'recipient__type_id', 'sender_id', 'sending_client__name', 'sender__realm_id', ] messages = Message.objects.filter(id__in=needed_ids).values(*fields) submessages = SubMessage.get_raw_db_rows(needed_ids) sew_messages_and_submessages(messages, submessages) reactions = Reaction.get_raw_db_rows(needed_ids) return sew_messages_and_reactions(messages, reactions)
def get_raw_db_rows(needed_ids: List[int]) -> List[Dict[str, Any]]: # This is a special purpose function optimized for # callers like get_messages_backend(). fields = [ 'id', DB_TOPIC_NAME, 'date_sent', 'last_edit_time', 'edit_history', 'content', 'rendered_content', 'rendered_content_version', 'recipient_id', 'recipient__type', 'recipient__type_id', 'sender_id', 'sending_client__name', 'sender__realm_id', ] messages = Message.objects.filter(id__in=needed_ids).values(*fields) submessages = SubMessage.get_raw_db_rows(needed_ids) sew_messages_and_submessages(messages, submessages) reactions = Reaction.get_raw_db_rows(needed_ids) return sew_messages_and_reactions(messages, reactions)
def sew_submessages_and_reactions_to_msgs(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: msg_ids = [msg['id'] for msg in messages] submessages = SubMessage.get_raw_db_rows(msg_ids) sew_messages_and_submessages(messages, submessages) reactions = Reaction.get_raw_db_rows(msg_ids) return sew_messages_and_reactions(messages, reactions)
def test_endpoint_success(self) -> None: cordelia = self.example_user("cordelia") hamlet = self.example_user("hamlet") stream_name = "Verona" message_id = self.send_stream_message( sender=cordelia, stream_name=stream_name, ) self.login_user(cordelia) payload = dict( message_id=message_id, msg_type="whatever", content='{"name": "alice", "salary": 20}', ) events: List[Mapping[str, Any]] = [] with self.tornado_redirected_to_list(events, expected_num_events=1): result = self.client_post("/json/submessage", payload) self.assert_json_success(result) submessage = SubMessage.objects.get(message_id=message_id) expected_data = dict( message_id=message_id, submessage_id=submessage.id, content=payload["content"], msg_type="whatever", sender_id=cordelia.id, type="submessage", ) data = events[0]["event"] self.assertEqual(data, expected_data) users = events[0]["users"] self.assertIn(cordelia.id, users) self.assertIn(hamlet.id, users) rows = SubMessage.get_raw_db_rows([message_id]) self.assert_length(rows, 1) row = rows[0] expected_data = dict( id=row["id"], message_id=message_id, content='{"name": "alice", "salary": 20}', msg_type="whatever", sender_id=cordelia.id, ) self.assertEqual(row, expected_data)
def test_endpoint_success(self) -> None: cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') stream_name = 'Verona' message_id = self.send_stream_message( sender_email=cordelia.email, stream_name=stream_name, ) self.login(cordelia.email) payload = dict( message_id=message_id, msg_type='whatever', content='{"name": "alice", "salary": 20}' ) with mock.patch('zerver.lib.actions.send_event') as m: result = self.client_post('/json/submessage', payload) self.assert_json_success(result) submessage = SubMessage.objects.get(message_id=message_id) expected_data = dict( message_id=message_id, submessage_id=submessage.id, content=payload['content'], msg_type='whatever', sender_id=cordelia.id, type='submessage', ) self.assertEqual(m.call_count, 1) data = m.call_args[0][0] self.assertEqual(data, expected_data) users = m.call_args[0][1] self.assertIn(cordelia.id, users) self.assertIn(hamlet.id, users) rows = SubMessage.get_raw_db_rows([message_id]) self.assertEqual(len(rows), 1) row = rows[0] expected_data = dict( id=row['id'], message_id=message_id, content='{"name": "alice", "salary": 20}', msg_type='whatever', sender_id=cordelia.id, ) self.assertEqual(row, expected_data)
def test_endpoint_success(self) -> None: cordelia = self.example_user("cordelia") hamlet = self.example_user("hamlet") stream_name = "Verona" message_id = self.send_stream_message( sender=cordelia, stream_name=stream_name, ) self.login_user(cordelia) payload = dict( message_id=message_id, msg_type="whatever", content='{"name": "alice", "salary": 20}', ) with mock.patch("zerver.lib.actions.send_event") as m: result = self.client_post("/json/submessage", payload) self.assert_json_success(result) submessage = SubMessage.objects.get(message_id=message_id) expected_data = dict( message_id=message_id, submessage_id=submessage.id, content=payload["content"], msg_type="whatever", sender_id=cordelia.id, type="submessage", ) self.assertEqual(m.call_count, 1) data = m.call_args[0][1] self.assertEqual(data, expected_data) users = m.call_args[0][2] self.assertIn(cordelia.id, users) self.assertIn(hamlet.id, users) rows = SubMessage.get_raw_db_rows([message_id]) self.assertEqual(len(rows), 1) row = rows[0] expected_data = dict( id=row["id"], message_id=message_id, content='{"name": "alice", "salary": 20}', msg_type="whatever", sender_id=cordelia.id, ) self.assertEqual(row, expected_data)
def test_endpoint_success(self) -> None: cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') stream_name = 'Verona' message_id = self.send_stream_message( sender_email=cordelia.email, stream_name=stream_name, ) self.login(cordelia.email) payload = dict(message_id=message_id, msg_type='whatever', content='{"name": "alice", "salary": 20}') with mock.patch('zerver.lib.actions.send_event') as m: result = self.client_post('/json/submessage', payload) self.assert_json_success(result) submessage = SubMessage.objects.get(message_id=message_id) expected_data = dict( message_id=message_id, submessage_id=submessage.id, content=payload['content'], msg_type='whatever', sender_id=cordelia.id, type='submessage', ) self.assertEqual(m.call_count, 1) data = m.call_args[0][0] self.assertEqual(data, expected_data) users = m.call_args[0][1] self.assertIn(cordelia.id, users) self.assertIn(hamlet.id, users) rows = SubMessage.get_raw_db_rows([message_id]) self.assertEqual(len(rows), 1) row = rows[0] expected_data = dict( id=row['id'], message_id=message_id, content='{"name": "alice", "salary": 20}', msg_type='whatever', sender_id=cordelia.id, ) self.assertEqual(row, expected_data)
def to_dict_uncached_helper(message: Message) -> Dict[str, Any]: return MessageDict.build_message_dict( message=message, message_id=message.id, last_edit_time=message.last_edit_time, edit_history=message.edit_history, content=message.content, topic_name=message.topic_name(), date_sent=message.date_sent, rendered_content=message.rendered_content, rendered_content_version=message.rendered_content_version, sender_id=message.sender.id, sender_realm_id=message.sender.realm_id, sending_client_name=message.sending_client.name, recipient_id=message.recipient.id, recipient_type=message.recipient.type, recipient_type_id=message.recipient.type_id, reactions=Reaction.get_raw_db_rows([message.id]), submessages=SubMessage.get_raw_db_rows([message.id]), )
def to_dict_uncached_helper(message: Message) -> Dict[str, Any]: return MessageDict.build_message_dict( message = message, message_id = message.id, last_edit_time = message.last_edit_time, edit_history = message.edit_history, content = message.content, topic_name = message.topic_name(), pub_date = message.pub_date, rendered_content = message.rendered_content, rendered_content_version = message.rendered_content_version, sender_id = message.sender.id, sender_realm_id = message.sender.realm_id, sending_client_name = message.sending_client.name, recipient_id = message.recipient.id, recipient_type = message.recipient.type, recipient_type_id = message.recipient.type_id, reactions = Reaction.get_raw_db_rows([message.id]), submessages = SubMessage.get_raw_db_rows([message.id]), )
def get_raw_rows() -> List[Dict[str, Any]]: query = SubMessage.get_raw_db_rows([message_id]) rows = list(query) return rows
def get_raw_rows() -> List[Dict[str, Any]]: query = SubMessage.get_raw_db_rows([message_id]) rows = list(query) return rows