Beispiel #1
0
def send_message(recipient=False):
    try:
        g.user.username
    except AttributeError:
        abort(401)

    t = Thread(redis=g.r, user=g.user)
    m = Message(redis=g.r, key=False, user=g.user)
    if recipient:
        try:
            t.parse_recipients(recipient)
        except InvalidRecipients:
            pass

    if request.method == "POST":
        try:
            t.subject = request.form["subject"]
            m.update(request.form)
            t.parse_recipients(request.form["recipients"])
            t.save()
            t.add_message(m)
            m.send()
            flash(
                "Your message has been successfully wired, \
                    and should arrive shortly.",
                "success",
            )
            return redirect(url_for("view_thread", thread_id=t.key))
        except MessageValidationError:
            for error in m.validation_errors:
                flash(error, "error")
        except InvalidRecipients:
            for recipient in t.invalid_recipients:
                flash("%s is not a valid recipient" % recipient, "error")
    return render_template("forms/message.html", new=True, message=m, thread=t, recipients=t.get_form_recipients())
Beispiel #2
0
 def unread_count(self, thread=False):
     r = self.redis
     count = 0
     for thread in r.lrange("user:%s:threads" % self.user.key, 0, -1):
         t = Thread(redis=self.redis, user=self.user)
         count += t.get_unread_count(key=thread)
     return count
Beispiel #3
0
def add_recipient(thread_id):
    try:
        g.user.username
        if str(thread_id) not in g.user.get_threads():
            abort(401)
    except AttributeError:
        abort(401)
    username = request.form["username"]
    if request.form["confirm"] == "1":
        try:
            t = Thread(redis=g.r, user=g.user)
            t.load(thread_id)
            t.parse_recipients(username)
            t.save()
            flash("Added recipient.", "success")
        except InvalidRecipients:
            flash(u"Invalid recipient.", "error")
        return redirect(url_for("view_thread", thread_id=thread_id))
    else:
        return render_template(
            "confirm.html",
            _message="Are you sure you wish to add recipient %s to\
 this thread?"
            % username,
            _ok=url_for("add_recipient", thread_id=thread_id),
            _cancel=url_for("view_thread", thread_id=thread_id),
            _hiddens=[("username", username)],
        )
Beispiel #4
0
def unsubscribe_thread(thread_id):
    try:
        g.user.username
    except AttributeError:
        abort(401)
    if request.method == "POST":
        t = Thread(redis=g.r, user=g.user)
        t.load(thread_id)
        t.unsubscribe()
        flash(u"Unsubscribed from thread.", "success")
        return redirect(url_for("inbox"))
    else:
        return render_template(
            "confirm.html",
            _message="Are you sure you wish to unsubscribe from this thread?",
            _ok=url_for("unsubscribe_thread", thread_id=thread_id),
            _cancel=url_for("inbox"),
        )
Beispiel #5
0
def delete_message(message_id, thread_id):
    if request.method == "POST":
        t = Thread(redis=g.r, user=g.user)
        t.load(thread_id)
        m = Message(redis=g.r, user=g.user, key=message_id)
        m.load()
        if g.r.get("username:%s" % m.sender.username) != g.user.key:
            abort(401)
        t.delete_message(m)
        flash(u"Message deleted", "success")
        return redirect(url_for("view_thread", thread_id=thread_id))
    else:
        return render_template(
            "confirm.html",
            _message="Are you sure you want to delete this message?",
            _ok=url_for("delete_message", thread_id=thread_id, message_id=message_id),
            _cancel=url_for("view_thread", thread_id=thread_id),
        )
Beispiel #6
0
def del_thread(thread_id):
    try:
        g.user.username
        if str(thread_id) not in g.user.get_threads():
            abort(401)
    except AttributeError:
        abort(401)
    if request.method == "POST":
        t = Thread(redis=g.r, user=g.user)
        t.load(thread_id)
        t.delete()
        flash(u"Deleted thread.", "success")
        return redirect(url_for("inbox"))
    else:
        return render_template(
            "confirm.html",
            _message="Are you sure you wish to DELETE this thread?",
            _ok=url_for("del_thread", thread_id=thread_id),
            _cancel=url_for("inbox"),
        )
Beispiel #7
0
 def load_messages(self):
     thread_keys = self.user.get_threads()
     for thread_key in thread_keys:
         t = Thread(redis=self.redis, user=self.user)
         try:
             t.load(key=thread_key)
             t.get_unread_count()
             self.threads.append(t)
         except ThreadError:
             t.delete(recipient=self.user)
Beispiel #8
0
def view_thread(thread_id):
    encryption_key = False
    if str(thread_id) not in g.user.get_threads():
        abort(401)

    t = Thread(redis=g.r, user=g.user)
    try:
        t.load(thread_id)
        if request.method == "POST":
            if request.form["action"] == "reply":
                m = Message(redis=g.r, key=False, user=g.user)
                m.update(request.form)
                t.save()
                t.add_message(m)
                m.send()
                t.load(thread_id)
                flash("Reply has been sent.", "success")
                return redirect(url_for("view_thread", thread_id=t.key))
            try:
                encryption_key = request.form["encryption_key"]
                t.decrypt(encryption_key)
                flash("Thread successfully decrypted.", "success")
            except DecryptFailed:
                flash("Decryption was unsuccessful.", "error")
                return redirect(url_for("view_thread", thread_id=thread_id))
            except DestroyedThreadError:
                flash("System error. Message lost.", "error")
                return redirect(url_for("inbox"))
            except KeyError:
                pass
        if t.decrypted:
            t.reset_unread_count()
        return render_template(
            "thread.html",
            messages=t.messages,
            thread=t,
            decrypted=t.decrypted,
            encryption_key=encryption_key,
            subject=t.subject,
        )
    except ThreadError:
        abort(404)