]> git.lizzy.rs Git - cheatdb.git/blobdiff - app/views/threads.py
Fix thumbnails
[cheatdb.git] / app / views / threads.py
index c975e5fa344e9784c65a5abb7c81f0462aef75e0..37ac3d1b7a59b1c57d0c19e2cfcf0b0a27065978 100644 (file)
@@ -27,8 +27,45 @@ from wtforms.validators import *
 
 @app.route("/threads/")
 def threads_page():
-       threads = Thread.query.filter_by(private=False).all()
-       return render_template("threads/list.html", threads=threads)
+       query = Thread.query
+       if not Permission.SEE_THREAD.check(current_user):
+               query = query.filter_by(private=False)
+       return render_template("threads/list.html", threads=query.all())
+
+
+@app.route("/threads/<int:id>/subscribe/", methods=["POST"])
+@login_required
+def thread_subscribe_page(id):
+       thread = Thread.query.get(id)
+       if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
+               abort(404)
+
+       if current_user in thread.watchers:
+               flash("Already subscribed!", "success")
+       else:
+               flash("Subscribed to thread", "success")
+               thread.watchers.append(current_user)
+               db.session.commit()
+
+       return redirect(url_for("thread_page", id=id))
+
+
+@app.route("/threads/<int:id>/unsubscribe/", methods=["POST"])
+@login_required
+def thread_unsubscribe_page(id):
+       thread = Thread.query.get(id)
+       if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
+               abort(404)
+
+       if current_user in thread.watchers:
+               flash("Unsubscribed!", "success")
+               thread.watchers.remove(current_user)
+               db.session.commit()
+       else:
+               flash("Not subscribed to thread", "success")
+
+       return redirect(url_for("thread_page", id=id))
+
 
 @app.route("/threads/<int:id>/", methods=["GET", "POST"])
 def thread_page(id):
@@ -48,6 +85,20 @@ def thread_page(id):
                        db.session.add(reply)
 
                        thread.replies.append(reply)
+                       if not current_user in thread.watchers:
+                               thread.watchers.append(current_user)
+
+                       msg = None
+                       if thread.package is None:
+                               msg = "New comment on '{}'".format(thread.title)
+                       else:
+                               msg = "New comment on '{}' on package {}".format(thread.title, thread.package.title)
+
+
+                       for user in thread.watchers:
+                               if user != current_user:
+                                       triggerNotif(user, current_user, msg, url_for("thread_page", id=thread.id))
+
                        db.session.commit()
 
                        return redirect(url_for("thread_page", id=id))
@@ -76,7 +127,7 @@ def new_thread_page():
                        flash("Unable to find that package!", "error")
 
        # Don't allow making threads on approved packages for now
-       if package is None or package.approved:
+       if package is None:
                abort(403)
 
        def_is_private   = request.args.get("private") or False
@@ -86,8 +137,7 @@ def new_thread_page():
        is_review_thread = package is not None and not package.approved
 
        # Check that user can make the thread
-       if is_review_thread and not (package.author == current_user or \
-                       package.checkPerm(current_user, Permission.APPROVE_NEW)):
+       if not package.checkPerm(current_user, Permission.CREATE_THREAD):
                flash("Unable to create thread!", "error")
                return redirect(url_for("home_page"))
 
@@ -111,6 +161,10 @@ def new_thread_page():
                thread.package = package
                db.session.add(thread)
 
+               thread.watchers.append(current_user)
+               if package is not None and package.author != current_user:
+                       thread.watchers.append(package.author)
+
                reply = ThreadReply()
                reply.thread  = thread
                reply.author  = current_user
@@ -127,7 +181,6 @@ def new_thread_page():
                if package is not None:
                        triggerNotif(package.author, current_user,
                                        "New thread '{}' on package {}".format(thread.title, package.title), url_for("thread_page", id=thread.id))
-                       db.session.commit()
 
                db.session.commit()