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})
Exemple #2
0
def rts_user_connection():
    user = g.user

    if user:
        realtime.unread_message_count(user)
        realtime.unread_notification_count(user)

    return jsonify({})
def rts_user_connection():
    user = g.user

    if user:
        realtime.unread_message_count(user)
        realtime.unread_notification_count(user)

    return jsonify({})
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})
def notify_new_message(message):
    from_user = message.from_user
    to_user = message.to_user
    to_player = message.to_player

    if to_user:
        realtime.new_message(message)
        realtime.unread_message_count(to_user)
        email.schedule_new_message_email(message)

    if to_player:
        api.new_message(to_player, from_user)
def notify_new_message(message):
    from_user = message.from_user
    to_user = message.to_user
    to_player = message.to_player

    if to_user:
        realtime.new_message(message)
        realtime.unread_message_count(to_user)
        email.schedule_new_message_email(message)

    if to_player:
        api.new_message(to_player, from_user)
def notify_new_message(message, send_email=True):
    from_user = message.from_user
    to_user = message.to_user
    to_player = message.to_player

    if to_user:
        realtime.new_message(to_user, message)
        realtime.unread_message_count(to_user)

        if send_email:
            email.send_new_message_email(to_user, message)

    if to_player:
        api.new_message(to_player, from_user)
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
    })
def mark_messages_read():
    user = g.user
    if not user:
        return jsonify({
            'err': 1,
            'message': 'Must be logged in'
        })

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

    Message.query.filter_by(
        from_user_id=other_user_id,
        to_user=user
    ).update({
        'seen_at': datetime.utcnow()
    })

    db.session.commit()

    realtime.unread_message_count(user)

    return jsonify({
        'err': 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
    })