Esempio n. 1
0
 def __init__(self, *args, **kwargs):
     sender_id = kwargs.get('sender_id')
     receiver_id = kwargs.get('receiver_id')
     if not User.is_chat_between(sender_id, receiver_id):
         logger.info('Message __init__ is making a chat between users')
         User.create_chat(sender_id, receiver_id)
     if 'chat_id' not in kwargs:
         self.chat_id = User.get_chat_id_by_users_ids(
             sender_id, receiver_id)
     else:
         if not kwargs.get('chat_id') == User.get_chat_id_by_users_ids(
                 sender_id, receiver_id):
             logger.critical(
                 "Chat id in message __init__ turned out to be wrong somehow"
             )
             raise AssertionError
     super().__init__(*args, **kwargs)
Esempio n. 2
0
 def delete_messages(two_users_ids: list = None, chat_id: int = None):
     """
     Deletes all the messages from the chat between two given users in the list. Instead of list, chat id can be
     used directly to delete all the messages from it. Changes must be committed after executing this method in order
     to save them.
     :param two_users_ids: users ids in a list. The chat between them will be deleted
     :type two_users_ids: list
     :param chat_id: the chat, which will be deleted
     :type chat_id: int
     """
     chat_id = chat_id or User.get_chat_id_by_users_ids(*two_users_ids)
     db.session.execute(delete(Message).where(Message.chat_id == chat_id))
     logger.warning(
         f"All the messages between {two_users_ids} were deleted")
Esempio n. 3
0
    def post(self):
        """Creates a new chat between the current user and the users with the given in json id"""
        parser = reqparse.RequestParser()
        parser.add_argument('companion_id',
                            required=True,
                            type=int,
                            help='User\'s id to create a chat with')
        args = parser.parse_args()

        current_user_id = g.user.user_id
        user_id = args.get('companion_id')
        return_user_or_abort(user_id)
        try:
            User.create_chat(current_user_id, user_id)
        except ChatAlreadyExistsError:
            abort(400,
                  message=f'Your chat with the user {user_id} already exists')
        db.session.commit()
        return {
            'user_id': current_user_id,
            'companion_id': user_id,
            'chat_id': User.get_chat_id_by_users_ids(current_user_id, user_id),
            'message': 'Chat was successfully created'
        }, 201