Esempio n. 1
0
def validate_friend_request(sender_id, reciever_id):
    '''Return false if three request made, unresponded request exists, any request was already accepted or user is blocked.'''
    if sender_id == reciever_id:
        return False

    if Blacklist(app.config['blacklists_location'],
                 reciever_id).find_user(sender_id):
        return False

    all_requests = FriendRequest(get_db()).get_requests_by_sender_and_reciever(
        sender_id, reciever_id)

    if len(all_requests) >= 3:
        return False

    #add requests send by reciever to sender
    all_requests.extend(
        FriendRequest(get_db()).get_requests_by_sender_and_reciever(
            reciever_id, sender_id))

    for request in all_requests:
        if request['status'] == 0:
            return False

    for request in all_requests:
        if request['status'] == 1:
            return False

    return True
Esempio n. 2
0
def send_friend_request(id):
    db = get_db()
    user = User(db).get_user_by_username(session['username'])
    friend = User(db).get_user_by_username(request.json['username'])
    friend_request = FriendRequest(db)

    if not validate_friend_request(user['user_id'], friend['user_id']):
        response = {
            'message':
            'Requests limit excedded or person is your friend already!'
        }

        return jsonify(**response)

    friend_request.create_friend_request(user['user_id'], friend['user_id'])
    all_friend_requests = friend_request.get_requests_by_sender_and_reciever(
        user['user_id'], friend['user_id'])
    current_friend_request = all_friend_requests[-1]

    emit_friend_request(user, friend['room'],
                        current_friend_request['request_id'])

    response = {'message': 'Friend request send'}

    return jsonify(**response)
Esempio n. 3
0
def new_request():
    current_user = User.get_by_id(get_jwt_identity())
    recipient = User.get_by_id(request.form.get("id"))

    requests = FriendRequest(sender_id=current_user.id,
                             recipient_id=recipient.id)

    if requests.save():
        return jsonify({
            "successful": True,
            "message": "Friend request has been sent!"
        })
    else:
        return jsonify({"error": "Something went wrong!"})
Esempio n. 4
0
def connect():
    db = get_db()
    user = User(db).get_user_by_username(session['username'])
    user_rooms = Room(db).get_user_rooms(user['user_id'])

    join_room(user['room'])

    for room in user_rooms:
        join_room(room['room_name'])
        response = {'user_id': user['user_id'], 'emit': True}

        if room['first_user_id'] == user['user_id']:
            friend = User(db).get_user(room['second_user_id'])
        else:
            friend = User(db).get_user(room['first_user_id'])

        emit('friend connect', response, room=friend['room'])

    awaiting_friend_request = FriendRequest(db).get_waiting_request(
        user['user_id'])

    if awaiting_friend_request is not None:
        sender = User(db).get_user(awaiting_friend_request['sender_id'])
        emit_friend_request(sender, user['room'],
                            awaiting_friend_request['request_id'])
Esempio n. 5
0
def check_request(id):

    current_user = User.get_by_id(get_jwt_identity())
    recipient = User.get_by_id(id)

    check = FriendRequest.select().where(
        FriendRequest.sender_id == current_user.id,
        FriendRequest.recipient_id == recipient.id)
    check2 = FriendRequest.select().where(
        FriendRequest.sender_id == recipient.id,
        FriendRequest.recipient_id == current_user.id)

    if check or check2:
        return jsonify({"exist": True})
    else:
        return jsonify({"exist": False})
Esempio n. 6
0
def delete_request():
    current_user = User.get_by_id(get_jwt_identity())

    sender = User.get_by_id(request.form.get("id"))

    delete = FriendRequest.delete().where(
        FriendRequest.sender_id == sender.id,
        FriendRequest.recipient_id == current_user.id)

    if delete.execute():
        return jsonify({"message": "Friend request has been declined!"})
    else:
        return jsonify({"error": "Something went wrong!"})
Esempio n. 7
0
def show_request():
    current_user = User.get_by_id(get_jwt_identity())

    friend_request = FriendRequest.select().where(
        FriendRequest.recipient_id == current_user.id)

    results = []
    for f in friend_request:
        sender = User.get_by_id(f.sender_id)
        data = {"id": sender.id, "name": sender.username}
        results.append(data)

    return jsonify({"data": results})
Esempio n. 8
0
def new_friend():
    current_user = User.get_by_id(get_jwt_identity())

    approve_user = User.get_by_id(request.form.get("id"))

    friend = Friend(user1_id=current_user.id, user2_id=approve_user.id)

    if friend.save():
        delete = FriendRequest.delete().where(
            FriendRequest.sender_id == approve_user.id,
            FriendRequest.recipient_id == current_user.id)
        delete.execute()
        return jsonify({
            "successful": True,
            "message": "Friend request has been accepted!"
        })
    else:
        return jsonify({"error": "Something went wrong!"})
Esempio n. 9
0
def send_friend_request_response(id):
    db = get_db()
    answer = request.json['answer']
    request_id = request.json['request_id']
    friend_request = FriendRequest(db).get_request_by_id(request_id)
    user = User(db)
    current_user = user.get_user_by_username(session['username'])
    friend_id = request.json['user_id']
    friend = user.get_user(friend_id)

    if friend_request is not None:
        if answer == 1:
            FriendRequest(db).update_status(request_id, 1)
            Room(db).create_room(current_user['user_id'], friend_id)
            new_room = Room(db).get_users_room(current_user['user_id'],
                                               friend_id)

            response = {
                'message': 'Response accepted.',
                'answer': True,
                'request_id': request_id,
                'room': new_room['room_name']
            }

            user_button = render_template('friend_button.html',
                                          friend=current_user)
            friend_button = render_template('friend_button.html',
                                            friend=friend)
            socketio.emit('friend request answer',
                          (response, user_button, current_user['user_id']),
                          room=friend['room'])
            socketio.emit('friend request answer',
                          (response, friend_button, friend_id),
                          room=current_user['room'])
        elif answer == 2:
            FriendRequest(db).update_status(request_id, 2)

            response = {
                'message': 'Response rejected.',
                'answer': False,
                'request_id': request_id
            }

            socketio.emit('friend request answer',
                          response,
                          room=friend['room'])
        elif answer == 3:
            FriendRequest(db).update_status(request_id, 2)
            Blacklist(app.config['blacklists_location'],
                      current_user['user_id']).add_user(friend_id)

            response = {
                'message': 'User blocked.',
                'answer': False,
                'request_id': request_id
            }

            socketio.emit('friend request answer',
                          response,
                          room=friend['room'])
    else:
        response = {'error': 'something goes wrong'}

    #check if user have more unresponded requests
    awaiting_friend_request = FriendRequest(db).get_waiting_request(
        current_user['user_id'])
    if awaiting_friend_request is not None:
        sender = User(db).get_user(awaiting_friend_request['sender_id'])
        emit_friend_request(sender, current_user['room'],
                            awaiting_friend_request['request_id'])

    return jsonify(**response)