2 # Copyright (C) 2018 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 bp = Blueprint("threads", __name__)
22 from flask_user import *
23 from app.models import *
24 from app.utils import addNotification, clearNotifications, isYes, addAuditLog
28 from flask_wtf import FlaskForm
30 from wtforms.validators import *
31 from app.utils import get_int_or_abort
33 @bp.route("/threads/")
36 if not Permission.SEE_THREAD.check(current_user):
37 query = query.filter_by(private=False)
39 pid = request.args.get("pid")
41 pid = get_int_or_abort(pid)
42 query = query.filter_by(package_id=pid)
44 query = query.order_by(db.desc(Thread.created_at))
46 return render_template("threads/list.html", threads=query.all())
49 @bp.route("/threads/<int:id>/subscribe/", methods=["POST"])
52 thread = Thread.query.get(id)
53 if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
56 if current_user in thread.watchers:
57 flash("Already subscribed!", "success")
59 flash("Subscribed to thread", "success")
60 thread.watchers.append(current_user)
63 return redirect(thread.getViewURL())
66 @bp.route("/threads/<int:id>/unsubscribe/", methods=["POST"])
69 thread = Thread.query.get(id)
70 if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
73 if current_user in thread.watchers:
74 flash("Unsubscribed!", "success")
75 thread.watchers.remove(current_user)
78 flash("Already not subscribed!", "success")
80 return redirect(thread.getViewURL())
83 @bp.route("/threads/<int:id>/set-lock/", methods=["POST"])
86 thread = Thread.query.get(id)
87 if thread is None or not thread.checkPerm(current_user, Permission.LOCK_THREAD):
90 thread.locked = isYes(request.args.get("lock"))
91 if thread.locked is None:
96 msg = "Locked thread '{}'".format(thread.title)
97 flash("Locked thread", "success")
99 msg = "Unlocked thread '{}'".format(thread.title)
100 flash("Unlocked thread", "success")
102 addNotification(thread.watchers, current_user, msg, thread.getViewURL(), thread.package)
103 addAuditLog(AuditSeverity.MODERATION, current_user, msg, thread.getViewURL(), thread.package)
107 return redirect(thread.getViewURL())
110 @bp.route("/threads/<int:id>/delete/", methods=["GET", "POST"])
112 def delete_reply(id):
113 thread = Thread.query.get(id)
117 reply_id = request.args.get("reply")
121 reply = ThreadReply.query.get(reply_id)
122 if reply is None or reply.thread != thread:
125 if thread.replies[0] == reply:
126 flash("Cannot delete thread opening post!", "danger")
127 return redirect(thread.getViewURL())
129 if not reply.checkPerm(current_user, Permission.DELETE_REPLY):
132 if request.method == "GET":
133 return render_template("threads/delete_reply.html", thread=thread, reply=reply)
135 msg = "Deleted reply by {}".format(reply.author.display_name)
136 addAuditLog(AuditSeverity.MODERATION, current_user, msg, thread.getViewURL(), thread.package, reply.comment)
138 db.session.delete(reply)
141 return redirect(thread.getViewURL())
146 class CommentForm(FlaskForm):
147 comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
148 submit = SubmitField("Comment")
152 @bp.route("/threads/<int:id>/edit/", methods=["GET", "POST"])
155 thread = Thread.query.get(id)
159 reply_id = request.args.get("reply")
163 reply = ThreadReply.query.get(reply_id)
164 if reply is None or reply.thread != thread:
167 if not reply.checkPerm(current_user, Permission.EDIT_REPLY):
170 form = CommentForm(formdata=request.form, obj=reply)
171 if request.method == "POST" and form.validate():
172 comment = form.comment.data
174 msg = "Edited reply by {}".format(reply.author.display_name)
175 severity = AuditSeverity.NORMAL if current_user == reply.author else AuditSeverity.MODERATION
176 addNotification(reply.author, current_user, msg, thread.getViewURL(), thread.package)
177 addAuditLog(severity, current_user, msg, thread.getViewURL(), thread.package, reply.comment)
179 reply.comment = comment
183 return redirect(thread.getViewURL())
185 return render_template("threads/edit_reply.html", thread=thread, reply=reply, form=form)
188 @bp.route("/threads/<int:id>/", methods=["GET", "POST"])
190 thread = Thread.query.get(id)
191 if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
194 if current_user.is_authenticated and request.method == "POST":
195 comment = request.form["comment"]
197 if not thread.checkPerm(current_user, Permission.COMMENT_THREAD):
198 flash("You cannot comment on this thread", "danger")
199 return redirect(thread.getViewURL())
201 if not current_user.canCommentRL():
202 flash("Please wait before commenting again", "danger")
203 return redirect(thread.getViewURL())
205 if len(comment) <= 500 and len(comment) > 3:
206 reply = ThreadReply()
207 reply.author = current_user
208 reply.comment = comment
209 db.session.add(reply)
211 thread.replies.append(reply)
212 if not current_user in thread.watchers:
213 thread.watchers.append(current_user)
215 msg = "New comment on '{}'".format(thread.title)
216 addNotification(thread.watchers, current_user, msg, thread.getViewURL(), thread.package)
219 return redirect(thread.getViewURL())
222 flash("Comment needs to be between 3 and 500 characters.")
224 return render_template("threads/view.html", thread=thread)
227 class ThreadForm(FlaskForm):
228 title = StringField("Title", [InputRequired(), Length(3,100)])
229 comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
230 private = BooleanField("Private")
231 submit = SubmitField("Open Thread")
234 @bp.route("/threads/new/", methods=["GET", "POST"])
237 form = ThreadForm(formdata=request.form)
240 if "pid" in request.args:
241 package = Package.query.get(int(request.args.get("pid")))
243 flash("Unable to find that package!", "danger")
245 # Don't allow making orphan threads on approved packages for now
249 def_is_private = request.args.get("private") or False
251 def_is_private = True
252 allow_change = package and package.approved
253 is_review_thread = package and not package.approved
255 # Check that user can make the thread
256 if not package.checkPerm(current_user, Permission.CREATE_THREAD):
257 flash("Unable to create thread!", "danger")
258 return redirect(url_for("homepage.home"))
260 # Only allow creating one thread when not approved
261 elif is_review_thread and package.review_thread is not None:
262 flash("A review thread already exists!", "danger")
263 return redirect(package.review_thread.getViewURL())
265 elif not current_user.canOpenThreadRL():
266 flash("Please wait before opening another thread", "danger")
269 return redirect(package.getDetailsURL())
271 return redirect(url_for("homepage.home"))
274 elif request.method == "GET":
275 form.private.data = def_is_private
276 form.title.data = request.args.get("title") or ""
278 # Validate and submit
279 elif request.method == "POST" and form.validate():
281 thread.author = current_user
282 thread.title = form.title.data
283 thread.private = form.private.data if allow_change else def_is_private
284 thread.package = package
285 db.session.add(thread)
287 thread.watchers.append(current_user)
288 if package is not None and package.author != current_user:
289 thread.watchers.append(package.author)
291 reply = ThreadReply()
292 reply.thread = thread
293 reply.author = current_user
294 reply.comment = form.comment.data
295 db.session.add(reply)
297 thread.replies.append(reply)
302 package.review_thread = thread
304 notif_msg = "New thread '{}'".format(thread.title)
305 if package is not None:
306 addNotification(package.maintainers, current_user, notif_msg, thread.getViewURL(), package)
308 editors = User.query.filter(User.rank >= UserRank.EDITOR).all()
309 addNotification(editors, current_user, notif_msg, thread.getViewURL(), package)
313 return redirect(thread.getViewURL())
316 return render_template("threads/new.html", form=form, allow_private_change=allow_change, package=package)