Ejemplo n.º 1
0
def message(id):
    if not UserConnect.is_friend(id):
        return redirect(url_for('app.home'))
    form = MessageForm(request.form)
    # 自分と相手のやり取りのメッセージを取得
    messages = Message.get_friend_messages(current_user.get_id(), id)
    user = User.select_user_by_id(id)
    # まだ読まれていないが、新たに読まれるメッセージ
    read_message_ids = [message.id for message in messages if (not message.is_read) and (message.from_user_id == int(id))]
    # すでに読まれていて、かつまだチェックしていない自分のメッセージをチェック
    not_checked_message_ids = [message.id for message in messages if message.is_read and (not message.is_checked) and (message.from_user_id == int(current_user.get_id()))]
    if not_checked_message_ids:
        with db.session.begin(subtransactions=True):
            Message.update_is_checked_by_ids(not_checked_message_ids)
        db.session.commit()
    # read_message_idsのis_readをTrueに変更
    if read_message_ids:
        with db.session.begin(subtransactions=True):
            Message.update_is_read_by_ids(read_message_ids)
        db.session.commit()
    if request.method == 'POST' and form.validate():
        new_message = Message(current_user.get_id(), id, form.message.data)
        with db.session.begin(subtransactions=True):
            new_message.create_message()
        db.session.commit()
        return redirect(url_for('app.message', id=id))
    return render_template(
        'message.html', form=form,
        messages=messages, to_user_id=id,
        user=user
    )
Ejemplo n.º 2
0
def load_old_messages():
    user_id = request.args.get('user_id', -1, type=int)
    offset_value = request.args.get('offset_value', -1, type=int)
    if user_id == -1 or offset_value == -1:
        return
    messages = Message.get_friend_messages(current_user.get_id(), user_id, offset_value * 100)
    user = User.select_user_by_id(user_id)
    return jsonify(data=make_old_message_format(user, messages))
Ejemplo n.º 3
0
def message(id):
    if not UserConnect.is_friend(id):
        return redirect(url_for('app.home'))
    form = MessageForm(request.form)
    messages = Message.get_friend_messages(current_user.get_id(), id)
    if request.method == 'POST' and form.validate():
        new_message = Message(current_user.get_id(), id, form.message.data)
        with db.session.begin(subtransactions=True):
            new_message.create_message()
        db.session.commit()
        return redirect(url_for('app.message', id=id))
    return render_template('message.html',
                           form=form,
                           messages=messages,
                           to_user_id=id)