Exemplo n.º 1
0
def read_message():
    user = g.user

    if not user:
        return jsonify({'err': 1, 'message': 'Must be logged in'})

    username = request.form.get('username')

    from_user = User.query.outerjoin(Player).filter(
        or_(User.username == username, Player.username == username)).first()

    if not from_user:
        return jsonify({'err': 1, 'message': 'Username not found'})

    messages = Message.query.filter_by(from_user_id=from_user.id,
                                       to_user=user,
                                       seen_at=None)

    now = datetime.utcnow()

    for message in messages:
        message.seen_at = now
        message.save(commit=False)

        notify_message_read(message)

    db.session.commit()

    realtime.unread_message_count(user)

    return jsonify({'err': 0})
Exemplo n.º 2
0
def messages_json(username):
    user = g.user

    to_user = User.query.outerjoin(Player).options(joinedload(
        User.player)).filter(
            or_(Player.username == username,
                User.username == username)).first()

    if to_user == user:
        # don't allow user to send messages to themselves
        return jsonify({'err': 1})

    if to_user:
        to_player = to_user.player
    else:
        # for cases of messages sent to players with no users created yet
        to_player = Player.query.filter_by(username=username).first()

        if not to_player:
            rollbar.report_message('to_player None', request=request)
            return jsonify({'err': 1})

    if to_user:
        # If the username matches an existing user, use it for the message query
        recipient_filter = or_(
            and_(Message.from_user == user, Message.to_user == to_user),
            and_(Message.from_user == to_user, Message.to_user == user))
    else:
        # Otherwise, use the player matched by the username for the message query
        recipient_filter = or_(
            and_(Message.from_user == user, Message.to_player == to_player),
            and_(Message.from_user == to_user, Message.to_user == user))

    messages = Message.query.filter(
        recipient_filter, Message.deleted == False).options(
            joinedload(Message.from_user).joinedload(User.player)).options(
                joinedload(Message.to_user).joinedload(User.player)).options(
                    joinedload(Message.to_player)).order_by(
                        Message.sent_at.desc()).limit(200).all()

    Message.query.filter(recipient_filter, Message.to_user == user,
                         Message.seen_at.is_(None)).update(
                             {'seen_at': datetime.utcnow()})

    db.session.commit()

    for message in messages:
        if message.to_user == user and not message.seen_at:
            notify_message_read(message)

    realtime.unread_message_count(user)

    messages = map(lambda x: x.to_dict(), messages)

    return jsonify({'err': 0, 'username': username, 'messages': messages})
Exemplo n.º 3
0
def read_message():
    user = g.user

    if not user:
        return jsonify({
            'err': 1,
            'message': 'Must be logged in'
        })

    username = request.form.get('username')

    from_user = User.query.outerjoin(Player).filter(
        or_(
            User.username == username,
            Player.username == username
        )
    ).first()

    if not from_user:
        return jsonify({
            'err': 1,
            'message': 'Username not found'
        })

    messages = Message.query.filter_by(
        from_user_id=from_user.id,
        to_user=user,
        seen_at=None
    )

    now = datetime.utcnow()

    for message in messages:
        message.seen_at = now
        message.save(commit=False)

        notify_message_read(message)

    db.session.commit()

    realtime.unread_message_count(user)

    return jsonify({
        'err': 0
    })
Exemplo n.º 4
0
def messages_json(username):
    user = g.user

    to_user = User.query.outerjoin(Player).options(
        joinedload(User.player)
    ).filter(
        or_(Player.username == username, User.username == username)
    ).first()

    if to_user == user:
        # don't allow user to send messages to themselves
        return jsonify({
            'err': 1
        })

    if to_user:
        to_player = to_user.player
    else:
        # for cases of messages sent to players with no users created yet
        to_player = Player.query.filter_by(username=username).first()

        if not to_player:
            rollbar.report_message('to_player None', request=request)
            return jsonify({
                'err': 1
            })

    if to_user:
        # If the username matches an existing user, use it for the message query
        recipient_filter = or_(
            and_(Message.from_user == user, Message.to_user == to_user),
            and_(Message.from_user == to_user, Message.to_user == user)
        )
    else:
        # Otherwise, use the player matched by the username for the message query
        recipient_filter = or_(
            and_(Message.from_user == user, Message.to_player == to_player),
            and_(Message.from_user == to_user, Message.to_user == user)
        )

    messages = Message.query.filter(
        recipient_filter,
        Message.deleted == False
    ).options(
        joinedload(Message.from_user)
        .joinedload(User.player)
    ).options(
        joinedload(Message.to_user)
        .joinedload(User.player)
    ).options(
        joinedload(Message.to_player)
    ).order_by(
        Message.sent_at.desc()
    ).limit(200).all()

    Message.query.filter(
        recipient_filter,
        Message.to_user == user,
        Message.seen_at.is_(None)
    ).update({
        'seen_at': datetime.utcnow()
    })

    db.session.commit()

    for message in messages:
        if message.to_user == user and not message.seen_at:
            notify_message_read(message)

    realtime.unread_message_count(user)

    messages = map(lambda x: x.to_dict(), messages)

    return jsonify({
        'err': 0,
        'username': username,
        'messages': messages
    })