Beispiel #1
0
def view_conversation(conversation_id):
    conversation = Conversation.query.filter_by(
        id=conversation_id,
        user_id=current_user.id
    ).first_or_404()

    if conversation.unread:
        conversation.unread = False
        current_user.invalidate_cache(permissions=False)
        conversation.save()

    form = MessageForm()
    if form.validate_on_submit():

        message_count = Conversation.query.\
            filter(Conversation.user_id == current_user.id).\
            count()

        if message_count >= flaskbb_config["MESSAGE_QUOTA"]:
            flash(_("You cannot send any messages anymore because you have"
                    "reached your message limit."), "danger")
            return redirect(url_for("message.view_conversation",
                                    conversation_id=conversation.id))

        to_user_id = None
        # If the current_user is the user who recieved the message
        # then we have to change the id's a bit.
        if current_user.id == conversation.to_user_id:
            to_user_id = conversation.from_user_id
        else:
            to_user_id = conversation.to_user_id

        form.save(conversation=conversation, user_id=current_user.id)

        # save the message in the recievers conversation
        old_conv = conversation
        conversation = Conversation.query.\
            filter(
                Conversation.user_id == to_user_id,
                Conversation.shared_id == conversation.shared_id
            ).first()

        # user deleted the conversation, start a new conversation with just
        # the recieving message
        if conversation is None:
            conversation = Conversation(
                subject=old_conv.subject,
                from_user_id=current_user.id,
                to_user=to_user_id,
                user_id=to_user_id,
                shared_id=old_conv.shared_id
            )
            conversation.save()

        form.save(conversation=conversation, user_id=current_user.id,
                  unread=True)

        return redirect(url_for("message.view_conversation",
                                conversation_id=old_conv.id))

    return render_template("message/conversation.html",
                           conversation=conversation, form=form)
Beispiel #2
0
def view_conversation(conversation_id):
    conversation = Conversation.query.filter_by(
        id=conversation_id,
        user_id=current_user.id
    ).first_or_404()

    if conversation.unread:
        conversation.unread = False
        conversation.save()

    form = MessageForm()
    if form.validate_on_submit():

        message_count = Conversation.query.\
            filter(Conversation.user_id == current_user.id).\
            count()

        if message_count >= flaskbb_config["MESSAGE_QUOTA"]:
            flash(_("You cannot send any messages anymore because you have"
                    "reached your message limit."), "danger")
            return redirect(url_for("message.view_conversation",
                                    conversation_id=conversation.id))

        to_user_id = None
        # If the current_user is the user who recieved the message
        # then we have to change the id's a bit.
        if current_user.id == conversation.to_user_id:
            to_user_id = conversation.from_user_id
        else:
            to_user_id = conversation.to_user_id

        form.save(conversation=conversation, user_id=current_user.id)

        # save the message in the recievers conversation
        old_conv = conversation
        conversation = Conversation.query.\
            filter(
                Conversation.user_id == to_user_id,
                Conversation.shared_id == conversation.shared_id
            ).first()

        # user deleted the conversation, start a new conversation with just
        # the recieving message
        if conversation is None:
            conversation = Conversation(
                subject=old_conv.subject,
                from_user_id=current_user.id,
                to_user=to_user_id,
                user_id=to_user_id,
                shared_id=old_conv.shared_id
            )
            conversation.save()

        form.save(conversation=conversation, user_id=current_user.id,
                  unread=True)

        return redirect(url_for("message.view_conversation",
                                conversation_id=old_conv.id))

    return render_template("message/conversation.html",
                           conversation=conversation, form=form)