def delete_message_backend(request: HttpRequest, user_profile: UserProfile, message_id: int=REQ(converter=to_non_negative_int, path_only=True)) -> HttpResponse: message, ignored_user_message = access_message(user_profile, message_id) validate_can_delete_message(user_profile, message) try: do_delete_messages(user_profile.realm, [message]) except (Message.DoesNotExist, IntegrityError): raise JsonableError(_("Message already deleted")) return json_success()
def test_do_delete_messages_multiple(self) -> None: realm = get_realm("zulip") cordelia = self.example_user('cordelia') message_ids = [self.send_stream_message(cordelia, "Denmark", str(i)) for i in range(0, 10)] messages = Message.objects.filter(id__in=message_ids) with queries_captured() as queries: do_delete_messages(realm, messages) self.assertFalse(Message.objects.filter(id__in=message_ids).exists()) self.assert_length(queries, 37) archived_messages = ArchivedMessage.objects.filter(id__in=message_ids) self.assertEqual(archived_messages.count(), len(message_ids)) self.assertEqual(len({message.archive_transaction_id for message in archived_messages}), 1)
def delete_in_topic(request: HttpRequest, user_profile: UserProfile, stream_id: int=REQ(converter=to_non_negative_int), topic_name: str=REQ("topic_name")) -> HttpResponse: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) messages = messages_for_topic(stream.id, topic_name) if not stream.is_history_public_to_subscribers(): # Don't allow the user to delete messages that they don't have access to. deletable_message_ids = UserMessage.objects.filter( user_profile=user_profile, message_id__in=messages).values_list("message_id", flat=True) messages = [message for message in messages if message.id in deletable_message_ids] do_delete_messages(user_profile, messages) return json_success()
def delete_message_backend( request: HttpRequest, user_profile: UserProfile, message_id: int = REQ(converter=to_non_negative_int, path_only=True), ) -> HttpResponse: # We lock the `Message` object to ensure that any transactions modifying the `Message` object # concurrently are serialized properly with deleting the message; this prevents a deadlock # that would otherwise happen because of the other transaction holding a lock on the `Message` # row. message, ignored_user_message = access_message(user_profile, message_id, lock_message=True) validate_can_delete_message(user_profile, message) try: do_delete_messages(user_profile.realm, [message]) except (Message.DoesNotExist, IntegrityError): raise JsonableError(_("Message already deleted")) return json_success()
def delete_in_topic( request: HttpRequest, user_profile: UserProfile, stream_id: int = REQ(converter=to_non_negative_int, path_only=True), topic_name: str = REQ("topic_name"), ) -> HttpResponse: (stream, sub) = access_stream_by_id(user_profile, stream_id) messages = messages_for_topic(assert_is_not_none(stream.recipient_id), topic_name) if not stream.is_history_public_to_subscribers(): # Don't allow the user to delete messages that they don't have access to. deletable_message_ids = UserMessage.objects.filter( user_profile=user_profile, message_id__in=messages ).values_list("message_id", flat=True) messages = messages.filter(id__in=deletable_message_ids) messages = messages.select_for_update(of=("self",)) do_delete_messages(user_profile.realm, messages) return json_success()