Ejemplo n.º 1
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, *args,
                            **kwargs) -> BaseHTTPResponse:
        file_ids = file_queries.get_file_ids_by_msd_id(session, message_id)

        try:
            for file_id in file_ids:
                file_queries.delete_msg_file_relation(session, body['uid'],
                                                      message_id, file_id)
        except DBMsgFileNotExistsException:
            raise SanicFileNotFound('File not found')
        except DBResourceOwnerException:
            raise SanicUserConflictException("This is not your message")

        try:
            message_queries.delete_message(session, message_id, body['uid'])
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')
        except DBResourceOwnerException:
            raise SanicUserConflictException("This is not your message")

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 2
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, mid: int, token: dict, *args,
                            **kwargs) -> BaseHTTPResponse:
        rights_holders = [
            messages_queries.get_sender(session, mid),
            messages_queries.get_recipient(session, mid)
        ]
        if rights_holders is None or token.get('uid') not in rights_holders:
            return await self.make_response_json(status=403)
        elif token.get('uid') == rights_holders[0]:
            try:
                messages_queries.get_message(session, message_id=mid)
                message = messages_queries.delete_message(session, mid)
            except DBMessageNotExistsException:
                raise SanicMessageNotFound('Message not found')
        else:
            try:
                messages_queries.get_message(session, message_id=mid)
                message = messages_queries.delete_message(
                    session, mid, attribute='is_delete_recipient')
            except DBMessageNotExistsException:
                raise SanicMessageNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 3
0
    async def method_delete(
            self, request: Request, body: dict, session, mid: int, token: dict, *args, **kwargs
    ) -> BaseHTTPResponse:

        try:
            message_queries.delete_message(session, mid, uid=token.get("uid"))
        except (DBMessagesNoAccess, DBMessageNotExistsException):
            raise SanicResponseForbiddenException('message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 4
0
    async def method_delete(
        self,
        request: Request,
        body: dict,
        session: DBSession,
        message_id: int,
        token: dict,
        *args,
        **kwargs,
    ) -> BaseHTTPResponse:
        """
        Delete message by id
        :param request:
        :param body:
        :param session:
        :param message_id:
        :param token:
        :param args:
        :param kwargs:
        :return:
        """
        try:
            db_message = delete_message(session, message_id=message_id)
        except DBMessageNotExists as e:
            raise NotFound(f"Message {message_id} not found") from e

        if token["sub"] not in (db_message.sender_id, db_message.recipient_id):
            raise Forbidden("user is not recipient or sender")

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 5
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:
        """
        Удаление сообщения по его id
        """

        try:
            message_queries.delete_message(session, message_id=message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(body={}, status=201)
Ejemplo n.º 6
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, token: dict, mid: int, *args,
                            **kwargs) -> BaseHTTPResponse:
        try:
            db_message = message_queries.get_message(session=session, mid=mid)
        except DBMessageDoesntExistException:
            raise SanicMessageNotFound('message not found')

        if db_message.sender_id != token.get('id_auth'):
            return await self.make_response_json(status=403)

        message_queries.delete_message(db_message)

        try:
            session.commit_session()
        except (DBDataError, DBIntegrityError) as error:
            raise SanicDBException(str(error))

        return await self.make_response_json(status=204, body=None)
Ejemplo n.º 7
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:

        try:
            sender_id = message_queries.get_message(session,
                                                    message_id).sender_id
        except DBMessageNotExistsException:
            raise SanicMessagesNotFound('')

        if token.get('eid') != sender_id:
            return await self.make_response_json(status=403)

        try:
            message_queries.delete_message(session, message_id=message_id)
        except DBMessageNotExistsException:
            raise SanicMessagesNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 8
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, msg_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:

        # проверяем, что сообщение есть в БД и не удалено
        try:
            db_message = message_queries.get_message(session, msg_id)
        except DBMessageNotFoundException:
            raise SanicMessageNotFoundException('Message not found')
        except DBMessageDeletedException:
            raise SanicMessageDeletedException('Message deleted')

        # проверка на то, что пользователь пытается удалить сообщение, которое ему отправили
        if token['id'] != db_message.recipient_id:
            return await self.make_response_json(status=403)

        message_queries.delete_message(session, msg_id)

        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as error:
            raise SanicDBException(str(error))

        return await self.make_response_json(status=204)
Ejemplo n.º 9
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, *args,
                            **kwargs) -> BaseHTTPResponse:

        try:
            message = message_queries.delete_message(session, message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        if get_id_from_token(request) != message_queries.get_message_author(
                session, message_id):
            raise SanicForbidden('You have no rights to delete this message')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Ejemplo n.º 10
0
    async def method_delete(self, request: Request, body: dict, session: DBSession, msg_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:

        try:
            db_message = message_queries.get_db_message(session, msg_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        await self.check_sender_id_by_token(token, db_message,
                                            response_error_message='You can only delete your own data')

        try:
            db_message = message_queries.delete_message(db_message)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)