Example #1
0
def send_message():
    sender_id = request.json.get('sender_id')  # type:int
    receiver_id = request.json.get('receiver_id')  # type:int
    message_ciphertext = request.json.get('message')  # type:str
    timestamp = request.json.get('timestamp')  # type:str

    if any(x is None
           for x in [sender_id, receiver_id, message_ciphertext, timestamp]):
        return bad_request(Error.MISSING_ARGS)

    login_model = g.login  # type: LoginModel
    if sender_id != login_model.user_id:
        return bad_request(Error.ILLEGAL_ARGS,
                           'No permission to send as user ' + str(sender_id))

    try:
        timestamp = date_time_from_str(timestamp)  # type:datetime
    except (IndexError, AttributeError):
        return bad_request(Error.ILLEGAL_ARGS, 'Malformed timestamp')

    message = MessageModel(sender_id=sender_id,
                           receiver_id=receiver_id,
                           message_ciphertext=message_ciphertext,
                           timestamp=timestamp)
    db.session.add(message)
    db.session.commit()

    return jsonify({'message': message.to_dict(), 'error': None}), 201
Example #2
0
def receive_message(receiver_id: int, sender_id: int):
    receiver = UserModel.query.filter(
        UserModel.id == receiver_id).first()  # type:UserModel
    if receiver is None:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'Receiver with id ' + str(receiver_id) + ' not found')

    sender = UserModel.query.filter(
        UserModel.id == sender_id).first()  # type:UserModel
    if sender is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'Sender with id ' + str(sender_id) + ' not found')

    login_model = g.login  # type: LoginModel
    receiver = login_model.user  # type: UserModel

    if receiver_id != receiver.id:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'No permission to get user messages ' + str(receiver_id))

    messageRecords = MessageModel.query.filter(
        MessageModel.receiver_id == receiver_id and MessageModel.sender_id
        == sender_id).all()  # type:List[MessageModel]
    messages = [m.to_dict() for m in messageRecords]

    for m in messageRecords:
        db.session.delete(m)
    db.session.commit()

    return jsonify({'messages': messages, 'error': None}), 201
Example #3
0
def update_user_keys(user_id: int):
    user = UserModel.query.filter(
        UserModel.id == user_id).first()  # type:UserModel
    if user is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'User with id ' + str(user_id) + ' not found')

    login_model = g.login  # type: LoginModel
    user = login_model.user  # type: UserModel

    if user_id != user.id:
        return bad_request(Error.ILLEGAL_ARGS,
                           'No permission to update user ' + str(user_id))

    registration_id = request.json.get('registration_id')  # type:int
    device_id = request.json.get('device_id')  # type:int
    identity_public_key = request.json.get('identity_public_key')  # type:str
    signed_pre_key = request.json.get('signed_pre_key')  # type:str
    one_time_pre_keys = request.json.get('one_time_pre_keys')  # type:List[int]

    if any(x is None for x in [
            registration_id, device_id, identity_public_key, one_time_pre_keys,
            signed_pre_key
    ]):
        return bad_request(Error.MISSING_ARGS)

    user.registration_id = registration_id
    user.device_id = device_id
    user.identity_public_key = identity_public_key
    user.signed_pre_key = signed_pre_key
    user.one_time_pre_keys = one_time_pre_keys

    db.session.commit()
    return jsonify({'user': user.to_dict(), 'error': None}), 201
Example #4
0
def get_other_user_keys(user_id: int):
    user = UserModel.query.filter(
        UserModel.id == user_id).first()  # type:UserModel
    if user is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'User with id ' + str(user_id) + ' not found')
    user_dict = user.to_public_dict()
    user.one_time_pre_keys = user.one_time_pre_keys[1:]
    db.session.commit()
    return jsonify({'user': user_dict, 'error': None})
Example #5
0
def remove_friend(user_id: int, friend_user_id: int):
    user = UserModel.query.filter(
        UserModel.id == user_id).first()  # type:UserModel
    if user is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'User with id ' + str(user_id) + ' not found')

    login_model = g.login  # type: LoginModel
    user = login_model.user  # type: UserModel
    if user_id != user.id:
        return bad_request(Error.ILLEGAL_ARGS,
                           'No permission to update user ' + str(user_id))

    if user.friends is None or friend_user_id not in user.friends:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'User \'' + str(friend_user_id) + '\' is not a friend')

    user.friends = user.friends.remove(friend_user_id)
    db.session.commit()
    return jsonify({'friends': user.friends}), 201
Example #6
0
def get_all_friends(user_id: int):
    user = UserModel.query.filter(
        UserModel.id == user_id).first()  # type:UserModel
    if user is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'User with id ' + str(user_id) + ' not found')

    login_model = g.login  # type: LoginModel
    user = login_model.user  # type: UserModel
    if user_id != user.id:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'No permission to view friends of user ' + str(user_id))

    friend_list = user.friends
    friends = []

    if friend_list is None:
        return jsonify({
            'friends': [],
            'error': None
        })

    for friend_user_id in friend_list:
        friend_user = UserModel.query.filter(
            UserModel.id == friend_user_id).first()  # type:UserModel
        friend_registration_id = friend_user.registration_id
        friend_device_id = friend_user.device_id

        friend_user_login = LoginModel.query.filter(
            LoginModel.user_id == friend_user_id).first()
        friend_username = friend_user_login.username
        friends.append({
            'user_id': friend_user_id,
            'username': friend_username,
            'registration_id': friend_registration_id,
            'device_id': friend_device_id
        })

    return jsonify({'friends': friends, 'error': None})
Example #7
0
    def image_data():
        '''
        Extract OCR and Exif metadata from uploaded images and return as JSON.

        GET - display a simple upload form.
        POST - extract data from the uploaded form and return it as JSON.
        '''
        if request.method == 'POST':
            # check if the post request has the file part
            if 'file' not in request.files:
                return bad_request('Post data has no file part')

            file = request.files['file']

            filename = secure_filename(file.filename)
            save_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)

            # Validate if is a valid image file
            if not is_image_file(save_path):
                return bad_request('File is not a valid image file')

            file.save(save_path)

            # limit file upload size
            if not is_allowed_file_size(save_path):
                limit = app.config['FILE_UPLOAD_LIMIT_IN_MB']
                error = 'File size too large. Limit is {} MB'.format(limit)
                os.remove(save_path)
                return bad_request(error)

            image_text = extract_image_text(save_path)
            image_text = image_text if image_text else 'No text detected in image'

            image_data = {'image_text': image_text}
            image_data['metadata'] = extract_exif_data(save_path)

            return jsonify(image_data)

        return render_template('index.html')
Example #8
0
def new_user():
    username = request.json.get('username')  # type:str
    password = request.json.get('password')  # type:str

    if any(x is None for x in [username, password]):
        return bad_request(Error.MISSING_ARGS)

    if LoginModel.query.filter_by(username=username).first() is not None:
        return bad_request(Error.ILLEGAL_ARGS, 'User already exists')

    user = UserModel(friends=[])
    db.session.add(user)
    db.session.flush()  # assigns id to user

    login = LoginModel(user_id=user.id, username=username)
    login.hash_password(password)
    db.session.add(login)

    db.session.commit()
    return jsonify({
        'token': login.generate_auth_token().decode('ascii'),
        'error': None
    }), 201
Example #9
0
    def post(self):
        # 新增数据
        if not request.is_json:
            return bad_request('post json data fail')

        username = request.json.get('username', None)
        password = request.json.get('password', None)
        if not username:
            return bad_request('Missing username parameter')
        if not password:
            return bad_request('Missing password parameter')

        user = UserModel.query.filter_by(username=username,
                                         password=password).first()
        if not user:
            return bad_request('username or password is incorrect')

        # Identity can be any data that is json serializable
        access_token = create_access_token(identity=username)
        return query_request(
            {
                'token': access_token,
                'user': user_schema.dump(user)
            }, "create token success")
Example #10
0
def add_friend(user_id: int):
    user = UserModel.query.filter(
        UserModel.id == user_id).first()  # type:UserModel
    if user is None:
        return bad_request(Error.ILLEGAL_ARGS,
                           'User with id ' + str(user_id) + ' not found')

    login_model = g.login  # type: LoginModel
    user = login_model.user  # type: UserModel
    if user_id != user.id:
        return bad_request(Error.ILLEGAL_ARGS,
                           'No permission to update user ' + str(user_id))

    friend_username = request.json.get('username')
    if friend_username is None:
        return bad_request(Error.MISSING_ARGS)

    if login_model.username == friend_username:
        return bad_request(Error.ILLEGAL_ARGS, 'Cannot add self as friend')

    friend_login = LoginModel.query.filter_by(username=friend_username).first()
    if friend_login is None:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'User with username \'' + friend_username + '\' not found')

    if user.friends is not None and friend_login.user_id in user.friends:
        return bad_request(
            Error.ILLEGAL_ARGS,
            'User \'' + friend_username + '\' is already a friend')

    if user.friends is None:
        user.friends = [friend_login.user_id]
    else:
        user.friends = user.friends + [friend_login.user_id]
    db.session.commit()
    return get_other_user_keys(friend_login.user_id), 201
Example #11
0
def token_auth_error():
    return bad_request('auth fail')
Example #12
0
def basic_auth_error():
    return bad_request('auth fail')