def test_archiving_message_with_submessages(self) -> None: msg_id = self.send_stream_message(self.sender, "Verona") cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') do_add_submessage(realm=get_realm('zulip'), sender_id=cordelia.id, message_id=msg_id, msg_type='whatever', content='{"name": "alice", "salary": 20}') do_add_submessage(realm=get_realm('zulip'), sender_id=hamlet.id, message_id=msg_id, msg_type='whatever', content='{"name": "john", "salary": 30}') submessage_ids = list( SubMessage.objects.filter(message_id=msg_id).values_list( 'id', flat=True)) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 2) move_messages_to_archive(message_ids=[msg_id]) self.assertEqual( set( ArchivedSubMessage.objects.filter( message_id=msg_id).values_list("id", flat=True)), set(submessage_ids)) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 0)
def process_submessage(request: HttpRequest, user_profile: UserProfile, message_id: int=REQ(validator=check_int), msg_type: str=REQ(), content: str=REQ(), ) -> HttpResponse: message, user_message = access_message(user_profile, message_id) if not settings.ALLOW_SUB_MESSAGES: # nocoverage msg = 'Feature not enabled' return json_error(msg) try: data = ujson.loads(content) except Exception: return json_error(_("Invalid json for submessage")) do_add_submessage( sender_id=user_profile.id, message_id=message.id, msg_type=msg_type, content=content, data=data, ) return json_success()
def test_submessage_event_sent_after_transaction_commits(self) -> None: """ Tests that `send_event` is hooked to `transaction.on_commit`. This is important, because we don't want to end up holding locks on message rows for too long if the event queue runs into a problem. """ hamlet = self.example_user("hamlet") message_id = self.send_stream_message(hamlet, "Scotland") with self.tornado_redirected_to_list([], expected_num_events=1): with mock.patch("zerver.lib.actions.send_event") as m: m.side_effect = AssertionError( "Events should be sent only after the transaction commits." ) do_add_submessage(hamlet.realm, hamlet.id, message_id, "whatever", "whatever")
def process_submessage( request: HttpRequest, user_profile: UserProfile, message_id: int = REQ(json_validator=check_int), msg_type: str = REQ(), content: str = REQ(), ) -> HttpResponse: message, user_message = access_message(user_profile, message_id, lock_message=True) verify_submessage_sender( message_id=message.id, message_sender_id=message.sender_id, submessage_sender_id=user_profile.id, ) try: widget_data = orjson.loads(content) except orjson.JSONDecodeError: raise JsonableError(_("Invalid json for submessage")) widget_type = get_widget_type(message_id=message.id) is_widget_author = message.sender_id == user_profile.id if widget_type == "poll": try: validate_poll_data(poll_data=widget_data, is_widget_author=is_widget_author) except ValidationError as error: raise JsonableError(error.message) if widget_type == "todo": try: validate_todo_data(todo_data=widget_data) except ValidationError as error: raise JsonableError(error.message) do_add_submessage( realm=user_profile.realm, sender_id=user_profile.id, message_id=message.id, msg_type=msg_type, content=content, ) return json_success(request)
def test_archiving_message_with_submessages(self) -> None: msg_id = self.send_stream_message(self.sender, "Verona") cordelia = self.example_user("cordelia") hamlet = self.example_user("hamlet") do_add_submessage( realm=get_realm("zulip"), sender_id=cordelia.id, message_id=msg_id, msg_type="whatever", content='{"name": "alice", "salary": 20}', ) do_add_submessage( realm=get_realm("zulip"), sender_id=hamlet.id, message_id=msg_id, msg_type="whatever", content='{"name": "john", "salary": 30}', ) submessage_ids = list( SubMessage.objects.filter(message_id=msg_id).values_list( "id", flat=True), ) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 2) move_messages_to_archive(message_ids=[msg_id]) self.assertEqual( set( ArchivedSubMessage.objects.filter( message_id=msg_id).values_list("id", flat=True)), set(submessage_ids), ) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 0) restore_all_data_from_archive() self.assertEqual( set( SubMessage.objects.filter(id__in=submessage_ids).values_list( "id", flat=True)), set(submessage_ids), )
def process_submessage(request: HttpRequest, user_profile: UserProfile, message_id: int=REQ(validator=check_int), msg_type: str=REQ(), content: str=REQ(), ) -> HttpResponse: message, user_message = access_message(user_profile, message_id) try: ujson.loads(content) except Exception: return json_error(_("Invalid json for submessage")) do_add_submessage( realm=user_profile.realm, sender_id=user_profile.id, message_id=message.id, msg_type=msg_type, content=content, ) return json_success()
def process_submessage( request: HttpRequest, user_profile: UserProfile, message_id: int = REQ(validator=check_int), msg_type: str = REQ(), content: str = REQ(), ) -> HttpResponse: message, user_message = access_message(user_profile, message_id) try: orjson.loads(content) except Exception: return json_error(_("Invalid json for submessage")) do_add_submessage( realm=user_profile.realm, sender_id=user_profile.id, message_id=message.id, msg_type=msg_type, content=content, ) return json_success()
def test_original_sender_enforced(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(hamlet) payload = dict( message_id=message_id, msg_type="whatever", content="{}", ) # Hamlet can't just go attaching submessages to Cordelia's # message, even though he does have read access here to the # message itself. result = self.client_post("/json/submessage", payload) self.assert_json_error( result, "You cannot attach a submessage to this message.") # Since Hamlet is actually subscribed to the stream, he is welcome # to send submessages to Cordelia once she initiates the "subconversation". do_add_submessage( realm=cordelia.realm, sender_id=cordelia.id, message_id=message_id, msg_type="whatever", content="whatever", ) result = self.client_post("/json/submessage", payload) self.assert_json_success(result)
def test_archiving_submessages(self) -> None: expired_msg_ids = self._make_expired_zulip_messages(2) cordelia = self.example_user("cordelia") hamlet = self.example_user("hamlet") do_add_submessage( realm=self.zulip_realm, sender_id=cordelia.id, message_id=expired_msg_ids[0], msg_type="whatever", content='{"name": "alice", "salary": 20}', ) do_add_submessage( realm=self.zulip_realm, sender_id=hamlet.id, message_id=expired_msg_ids[0], msg_type="whatever", content='{"name": "john", "salary": 30}', ) do_add_submessage( realm=self.zulip_realm, sender_id=cordelia.id, message_id=expired_msg_ids[1], msg_type="whatever", content='{"name": "jack", "salary": 10}', ) submessage_ids = list( SubMessage.objects.filter( message_id__in=expired_msg_ids).values_list("id", flat=True), ) self.assert_length(submessage_ids, 3) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 3) archive_messages() self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 0) self.assertEqual( set( ArchivedSubMessage.objects.filter( id__in=submessage_ids).values_list("id", flat=True)), set(submessage_ids), ) restore_all_data_from_archive() self.assertEqual( set( SubMessage.objects.filter(id__in=submessage_ids).values_list( "id", flat=True)), set(submessage_ids), )
def test_archiving_submessages(self) -> None: expired_msg_ids = self._make_expired_zulip_messages(2) cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') do_add_submessage(realm=self.zulip_realm, sender_id=cordelia.id, message_id=expired_msg_ids[0], msg_type='whatever', content='{"name": "alice", "salary": 20}') do_add_submessage(realm=self.zulip_realm, sender_id=hamlet.id, message_id=expired_msg_ids[0], msg_type='whatever', content='{"name": "john", "salary": 30}') do_add_submessage(realm=self.zulip_realm, sender_id=cordelia.id, message_id=expired_msg_ids[1], msg_type='whatever', content='{"name": "jack", "salary": 10}') submessage_ids = list( SubMessage.objects.filter( message_id__in=expired_msg_ids).values_list('id', flat=True)) self.assertEqual(len(submessage_ids), 3) self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 3) archive_messages() self.assertEqual( SubMessage.objects.filter(id__in=submessage_ids).count(), 0) self.assertEqual( set( ArchivedSubMessage.objects.filter( id__in=submessage_ids).values_list('id', flat=True)), set(submessage_ids))