Пример #1
0
def create_conversation():
    user_id = get_user_id_from_jwt()

    try:
        participant_id = int(request.get_json()['participant_id'])
    except (ValueError, KeyError):
        return response(
            'You must provide a valid participant id (only digits).', 400)

    if user_id == participant_id:
        return response('You cannot create a conversation with yourself.', 400)

    token = get_token_from_authorization_header(request)
    auth_header = get_authorization_header(token)
    res = requests.get(f'http://localhost:5000/api/user/{participant_id}',
                       headers=auth_header)

    if res.status_code == 404:
        return response('This user does not exist.', 400)
    elif res.status_code == 401:
        return response('Invalid authorization token.', 401)

    conversation = Conversation(creator_id=user_id,
                                participant_id=participant_id)
    db.session.add(conversation)
    db.session.commit()

    return response('Successfully created conversation.')
Пример #2
0
def create_message():
    request_body = request.get_json()

    token = get_token_from_authorization_header(request)
    auth_header = get_authorization_header(token)

    res = requests.get(f'http://localhost:5000/api/user/{get_user_id_from_jwt()}', headers=auth_header)
    user = res.json()

    if 'conversation_id' not in request_body or 'text' not in request_body:
        return response('You must provide both conversation id and message text.', 400)

    conversation_id = int(request_body['conversation_id'])
    text = request_body['text']

    conversation = Conversation.query.filter_by(id=conversation_id).first()
    if conversation is None or (user['id'] != conversation.creator_id and user['id'] != conversation.participant_id):
        return response('You are not part of this conversation.', 400)

    receiver_id = conversation.creator_id if user['id'] == conversation.participant_id else conversation.participant_id

    message = Message(conversation_id=conversation.id, sender_id=user['id'], text=text)
    db.session.add(message)
    db.session.commit()

    requests.put(
        'http://localhost:8080/api/notifications',
        json={
            'receiverID': receiver_id,
            'message': f'You have a new message from {user["username"]}.'
        },
        headers=auth_header
    )

    return response('Successfully sent message.')
Пример #3
0
def get_messages_with_limit(conversation_id, message_limit):
    user_id = get_user_id_from_jwt()
    conversation = Conversation.query.filter_by(id=conversation_id).first()

    if conversation is None or (user_id != conversation.creator_id
                                and user_id != conversation.participant_id):
        return response('You are not part of this conversation.', 400)

    messages = list(Message.query.filter_by(conversation_id=conversation.id))
    messages.reverse()
    return jsonify([m.json() for m in messages[:message_limit]])
Пример #4
0
 def page_not_found(e):
     return response('Page not found.', 404)