Esempio n. 1
0
    def post(self, id):
        try:
            user_id = get_jwt_identity()
            chat = Chat.objects.get(id=id)
            user = User.objects.get(id=user_id)

            message = Message(text=request.form['text'], author=user)
            chat.messages.append(message)

            message.save()
            chat.save()

            if 'file[]' in request.files:
                filenames = []
                files = request.files.getlist('file[]')
                for file in files:
                    filename = str(message.id) + file.filename
                    if save_file(filename, file):
                        filenames.append(filename)

                message.images = filenames
                message.save()

            socketio.emit('update', {}, room='chat' + str(chat.id))

            return { 'id': str(message.id) }, 200
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Esempio n. 2
0
    def post(self, id):
        try:
            body = request.get_json()
            user_id = get_jwt_identity()

            user = User.objects.get(id=user_id)
            to_remove = User.objects.get(id=body['id'])
            chat = Chat.objects.get(id=id)

            if to_remove not in chat.users:
                return '', 401

            to_remove_name = to_remove.name + ' ' + to_remove.surname
            if user in chat.admins or user == chat.host:
                chat.users.remove(to_remove)
                message = Message(type='userRemoved', text=to_remove_name, author=user)
                chat.messages.append(message)

                message.save()
                chat.save()

                return '', 200

            return '', 401
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Esempio n. 3
0
    def post(self, id):
        try:
            user_id = get_jwt_identity()
            user = User.objects.get(id=user_id)
            chat = Chat.objects.get(id=id)

            if user in chat.users:
                chat.users.remove(user)
                message = Message(type='userLeft', text='', author=user)
                chat.messages.append(message)

                message.save()
                chat.save()

                return '', 200
            elif user in chat.admins:
                chat.admins.remove(user)
                message = Message(type='userLeft', text='', author=user)
                chat.messages.append(message)

                message.save()
                chat.save()

                return '', 200

            return '', 404
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Esempio n. 4
0
 def post(self, conversation_id):
     body = request.get_json()
     user_id = get_jwt_identity()
     conversation = Conversation.objects.get(id=conversation_id)
     user = User.objects.get(id=user_id)
     message =  Message(**body)
     message.conversation = conversation
     message.save()
     id = message.id
     return {'id': str(id)}, 200
Esempio n. 5
0
    def post(self, conversation_id):
        conversation = Conversation.objects.get(id=conversation_id)
        query = Message.objects()
        character = Character.objects.get(id=conversation.character.id)

        query = Knowledge.objects()

        knowledges = Knowledge.objects(character=conversation.character.id)

        ramdom_message = []

        for i in range(0,random.randint(1, 10)):
            ramdom_message.append(knowledges[random.randrange(len(knowledges))].content+' ')

        message =  Message()
        message.content = ''.join(ramdom_message).strip()
        message.conversation = conversation
        message.sender_character = True
        message.save()
        return Response(message.to_json(), mimetype="application/json", status=200)
Esempio n. 6
0
    def post(self, host_id):
        try:
            user_id = get_jwt_identity()
            user = User.objects.get(id=user_id)
            host = User.objects.get(id=host_id)

            host_chat_info = None
            chat = Chat.objects(host=host, type='public').first()
            if chat:
                if user not in chat.users and user not in chat.admins and user != chat.host:
                    chat.users.append(user)
            else:
                chat = Chat(host=host)
                chat.users.append(user)
                host_chat_info = ChatInfo(chat=chat, user=host)


            message = Message(type='userJoined', text='', author=user)
            chat.messages.append(message)

            chat_info = ChatInfo.objects(chat=chat, user=user).first()
            if not chat_info:
                chat_info = ChatInfo(chat=chat, user=user)

            chat_info.message = get_last_message(chat)

            message.save()
            chat.save()
            chat_info.save()

            if host_chat_info:
                host_chat_info.save()

            return { 'id': str(chat.id) }, 200
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Esempio n. 7
0
 def post(self):
     jti = get_jwt()['jti']
     body = request.get_json()
     user = get_user_by_jti(jti)
     if user:
         if token_valid(user):
             token_update(user)
             message = Message(user.name, body["message"])
             id = message.save()
             return {
                 'id': id,
                 'sender': message.sender,
                 'message': message.message
             }
         else:
             return response_token_expired_logout(user)
     else:
         return response_token_invalid()