コード例 #1
0
ファイル: message.py プロジェクト: NailNaMeste/messenger
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, message_id, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        eid = token.get('eid')

        try:
            recipient_id = session.get_recipient_id_by_message(message_id)
        except AttributeError:
            raise DBMessageNotExistException("Message not found")

        if eid != recipient_id:
            raise SanicMessageForbidden("Not allowed")
        try:
            db_message = message_queries.get_message(session, message_id)
        except SanicMessageReadNotAllowedException as e:
            raise e
        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(body=response_model.dump())
コード例 #2
0
ファイル: message.py プロジェクト: NailNaMeste/messenger
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:
        eid = token.get('eid')
        try:
            recipient_id = session.get_recipient_id_by_message(message_id)
            sender_id = session.get_message_by_id(message_id).sender_id
            # проверка доступа к удалению отправленного или полученного сообщения через sender_id или recipient_id
            if eid != sender_id and eid != recipient_id:
                raise SanicMessageForbidden("Forbidden")
        except AttributeError:
            raise DBMessageNotExistException("Message not found")
        try:
            message = message_queries.delete_massage(session, message_id)
        except AttributeError:
            raise DBMessageNotExistException('message not found')

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

        return await self.make_response_json(status=204)