]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/threads/__init__.py
113cdfac11ad9567f543486afeb0af4aa1b83322
[cheatdb.git] / app / blueprints / threads / __init__.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from flask import *
19
20 bp = Blueprint("threads", __name__)
21
22 from flask_user import *
23 from app.models import *
24 from app.utils import addNotification, clearNotifications, isYes, addAuditLog
25
26 import datetime
27
28 from flask_wtf import FlaskForm
29 from wtforms import *
30 from wtforms.validators import *
31 from app.utils import get_int_or_abort
32
33 @bp.route("/threads/")
34 def list_all():
35         query = Thread.query
36         if not Permission.SEE_THREAD.check(current_user):
37                 query = query.filter_by(private=False)
38
39         pid = request.args.get("pid")
40         if pid:
41                 pid = get_int_or_abort(pid)
42                 query = query.filter_by(package_id=pid)
43
44         query = query.order_by(db.desc(Thread.created_at))
45
46         return render_template("threads/list.html", threads=query.all())
47
48
49 @bp.route("/threads/<int:id>/subscribe/", methods=["POST"])
50 @login_required
51 def subscribe(id):
52         thread = Thread.query.get(id)
53         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
54                 abort(404)
55
56         if current_user in thread.watchers:
57                 flash("Already subscribed!", "success")
58         else:
59                 flash("Subscribed to thread", "success")
60                 thread.watchers.append(current_user)
61                 db.session.commit()
62
63         return redirect(thread.getViewURL())
64
65
66 @bp.route("/threads/<int:id>/unsubscribe/", methods=["POST"])
67 @login_required
68 def unsubscribe(id):
69         thread = Thread.query.get(id)
70         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
71                 abort(404)
72
73         if current_user in thread.watchers:
74                 flash("Unsubscribed!", "success")
75                 thread.watchers.remove(current_user)
76                 db.session.commit()
77         else:
78                 flash("Already not subscribed!", "success")
79
80         return redirect(thread.getViewURL())
81
82
83 @bp.route("/threads/<int:id>/set-lock/", methods=["POST"])
84 @login_required
85 def set_lock(id):
86         thread = Thread.query.get(id)
87         if thread is None or not thread.checkPerm(current_user, Permission.LOCK_THREAD):
88                 abort(404)
89
90         thread.locked = isYes(request.args.get("lock"))
91         if thread.locked is None:
92                 abort(400)
93
94         msg = None
95         if thread.locked:
96                 msg = "Locked thread '{}'".format(thread.title)
97                 flash("Locked thread", "success")
98         else:
99                 msg = "Unlocked thread '{}'".format(thread.title)
100                 flash("Unlocked thread", "success")
101
102         addNotification(thread.watchers, current_user, msg, thread.getViewURL(), thread.package)
103         addAuditLog(AuditSeverity.MODERATION, current_user, msg, thread.getViewURL(), thread.package)
104
105         db.session.commit()
106
107         return redirect(thread.getViewURL())
108
109
110 @bp.route("/threads/<int:id>/delete/", methods=["GET", "POST"])
111 @login_required
112 def delete_reply(id):
113         thread = Thread.query.get(id)
114         if thread is None:
115                 abort(404)
116
117         reply_id = request.args.get("reply")
118         if reply_id is None:
119                 abort(404)
120
121         reply = ThreadReply.query.get(reply_id)
122         if reply is None or reply.thread != thread:
123                 abort(404)
124
125         if thread.replies[0] == reply:
126                 flash("Cannot delete thread opening post!", "danger")
127                 return redirect(thread.getViewURL())
128
129         if not reply.checkPerm(current_user, Permission.DELETE_REPLY):
130                 abort(403)
131
132         if request.method == "GET":
133                 return render_template("threads/delete_reply.html", thread=thread, reply=reply)
134
135         msg = "Deleted reply by {}".format(reply.author.display_name)
136         addAuditLog(AuditSeverity.MODERATION, current_user, msg, thread.getViewURL(), thread.package, reply.comment)
137
138         db.session.delete(reply)
139         db.session.commit()
140
141         return redirect(thread.getViewURL())
142
143
144 @bp.route("/threads/<int:id>/", methods=["GET", "POST"])
145 def view(id):
146         thread = Thread.query.get(id)
147         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
148                 abort(404)
149
150         if current_user.is_authenticated and request.method == "POST":
151                 comment = request.form["comment"]
152
153                 if not thread.checkPerm(current_user, Permission.COMMENT_THREAD):
154                         flash("You cannot comment on this thread", "danger")
155                         return redirect(thread.getViewURL())
156
157                 if not current_user.canCommentRL():
158                         flash("Please wait before commenting again", "danger")
159                         return redirect(thread.getViewURL())
160
161                 if len(comment) <= 500 and len(comment) > 3:
162                         reply = ThreadReply()
163                         reply.author = current_user
164                         reply.comment = comment
165                         db.session.add(reply)
166
167                         thread.replies.append(reply)
168                         if not current_user in thread.watchers:
169                                 thread.watchers.append(current_user)
170
171                         msg = "New comment on '{}'".format(thread.title)
172                         addNotification(thread.watchers, current_user, msg, thread.getViewURL(), thread.package)
173                         db.session.commit()
174
175                         return redirect(thread.getViewURL())
176
177                 else:
178                         flash("Comment needs to be between 3 and 500 characters.")
179
180         return render_template("threads/view.html", thread=thread)
181
182
183 class ThreadForm(FlaskForm):
184         title   = StringField("Title", [InputRequired(), Length(3,100)])
185         comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
186         private = BooleanField("Private")
187         submit  = SubmitField("Open Thread")
188
189
190 @bp.route("/threads/new/", methods=["GET", "POST"])
191 @login_required
192 def new():
193         form = ThreadForm(formdata=request.form)
194
195         package = None
196         if "pid" in request.args:
197                 package = Package.query.get(int(request.args.get("pid")))
198                 if package is None:
199                         flash("Unable to find that package!", "danger")
200
201         # Don't allow making orphan threads on approved packages for now
202         if package is None:
203                 abort(403)
204
205         def_is_private   = request.args.get("private") or False
206         if package is None:
207                 def_is_private = True
208         allow_change     = package and package.approved
209         is_review_thread = package and not package.approved
210
211         # Check that user can make the thread
212         if not package.checkPerm(current_user, Permission.CREATE_THREAD):
213                 flash("Unable to create thread!", "danger")
214                 return redirect(url_for("homepage.home"))
215
216         # Only allow creating one thread when not approved
217         elif is_review_thread and package.review_thread is not None:
218                 flash("A review thread already exists!", "danger")
219                 return redirect(package.review_thread.getViewURL())
220
221         elif not current_user.canOpenThreadRL():
222                 flash("Please wait before opening another thread", "danger")
223
224                 if package:
225                         return redirect(package.getDetailsURL())
226                 else:
227                         return redirect(url_for("homepage.home"))
228
229         # Set default values
230         elif request.method == "GET":
231                 form.private.data = def_is_private
232                 form.title.data   = request.args.get("title") or ""
233
234         # Validate and submit
235         elif request.method == "POST" and form.validate():
236                 thread = Thread()
237                 thread.author  = current_user
238                 thread.title   = form.title.data
239                 thread.private = form.private.data if allow_change else def_is_private
240                 thread.package = package
241                 db.session.add(thread)
242
243                 thread.watchers.append(current_user)
244                 if package is not None and package.author != current_user:
245                         thread.watchers.append(package.author)
246
247                 reply = ThreadReply()
248                 reply.thread  = thread
249                 reply.author  = current_user
250                 reply.comment = form.comment.data
251                 db.session.add(reply)
252
253                 thread.replies.append(reply)
254
255                 db.session.commit()
256
257                 if is_review_thread:
258                         package.review_thread = thread
259
260                 notif_msg = "New thread '{}'".format(thread.title)
261                 if package is not None:
262                         addNotification(package.maintainers, current_user, notif_msg, thread.getViewURL(), package)
263
264                 editors = User.query.filter(User.rank >= UserRank.EDITOR).all()
265                 addNotification(editors, current_user, notif_msg, thread.getViewURL(), package)
266
267                 db.session.commit()
268
269                 return redirect(thread.getViewURL())
270
271
272         return render_template("threads/new.html", form=form, allow_private_change=allow_change, package=package)