예제 #1
0
def chat(contact_id):
    # TODO xss check
    user_id = session.get("user_id")
    if user_id is None:
        return redirect(url_for(".home"))

    sender = User.get(user_id)
    recipient = User.get(contact_id)
    if recipient is None:
        return "Recipient not found"

    UnreadMessageCount.reset_unread(recipient.id, sender.id)
    messages = Message.get_many_by_user_pair(sender.id, recipient.id)

    name_dict = {
        sender.id: sender.name,
        recipient.id: recipient.name,
    }
    return render_template(
        "chat.jinja2",
        sender=sender,
        recipient=recipient,
        messages=messages,
        name_dict=name_dict,
    )
예제 #2
0
    def test_retrieval(self):
        uid1 = User.create("jack", "password")
        user = User.get(uid1)
        user2 = User.get_by_name("jack")
        assert user.id == user2.id

        assert None is User.get(999)
        assert None is User.get_by_name("not-existed")
예제 #3
0
def disconnect():
    user_id = request.values.get("user_id")

    if "user_id" not in session:
        return jsonify({"error": 1003, "error_msg": "login first"})

    target_user = User.get(user_id)
    if target_user is None:
        return jsonify({"error": 1004, "error_msg": "user not found"})

    ContactRelation.disconnect(session["user_id"], target_user.id)
    return jsonify({"error": 0})
예제 #4
0
def home():
    user_id = session.get("user_id")
    if user_id is None:
        return render_template("login.jinja2")

    user = User.get(user_id)
    if user is None:
        session.pop("user_id")
        return render_template("login.jinja2")

    contacts = user.get_contacts()
    unread_counts = {
        contact_id: UnreadMessageCount.get_unread(contact_id, user_id)
        for contact_id, _ in contacts
    }
    return render_template(
        "index.jinja2",
        user=user,
        contacts=contacts,
        unread_counts=unread_counts,
    )
예제 #5
0
 def test_contacts(self):
     jack_id = User.create("jack", "password")
     rose_id = User.create("rose", "password")
     ContactRelation.connect(jack_id, rose_id)
     jack = User.get(jack_id)
     jack.get_contacts()