COMMENT = 0
def create_thread(threadtype, userid):
- thread = db.query("insert into comment_threads (threadtype, userid) values (?, ?) returning threadid", [threadtype, userid], one=True)
+ thread = db.query(
+ """
+ insert into comment_threads (threadtype, userid)
+ values (?, ?)
+ returning threadid
+ """,
+ [threadtype, userid],
+ one=True)
db.commit()
return thread["threadid"]
def for_thread(threadid):
- thread_comments = db.query("select * from comments inner join users on comments.userid == users.userid where comments.threadid = ?", [threadid])
+ thread_comments = db.query(
+ """
+ select * from comments
+ inner join users on comments.userid == users.userid
+ where comments.threadid = ?
+ """,
+ [threadid])
thread_comments = [dict(c) for c in thread_comments]
for c in thread_comments:
c["content"] = sanitize_user_text(c["content"])
# Top-level comments
- song_comments = sorted([dict(c) for c in thread_comments if c["replytoid"] is None], key=lambda c: c["created"])
+ song_comments = sorted(
+ [dict(c) for c in thread_comments if c["replytoid"] is None],
+ key=lambda c: c["created"])
song_comments = list(reversed(song_comments))
# Replies (can only reply to top-level)
for comment in song_comments:
- comment["replies"] = sorted([c for c in thread_comments if c["replytoid"] == comment["commentid"]], key=lambda c: c["created"])
+ comment["replies"] = sorted(
+ [c for c in thread_comments if c["replytoid"] == comment["commentid"]],
+ key=lambda c: c["created"])
return song_comments
if not "threadid" in request.args:
abort(400) # Must have threadid
- thread = db.query("select * from comment_threads where threadid = ?", [request.args["threadid"]], one=True)
+ thread = db.query(
+ """
+ select * from comment_threads
+ where threadid = ?
+ """,
+ [request.args["threadid"]],
+ one=True)
if not thread:
abort(404) # Invalid threadid
replyto = None
if "replytoid" in request.args:
replytoid = request.args["replytoid"]
- replyto = db.query("select * from comments inner join users on comments.userid == users.userid where commentid = ?", [replytoid], one=True)
+ replyto = db.query(
+ """
+ select * from comments
+ inner join users on comments.userid == users.userid
+ where commentid = ?
+ """,
+ [replytoid],
+ one=True)
if not replyto:
abort(404) # Invalid comment
comment = None
if "commentid" in request.args:
commentid = request.args["commentid"]
- comment = db.query("select * from comments inner join users on comments.userid == users.userid where commentid = ?", [commentid], one=True)
+ comment = db.query(
+ """
+ select * from comments
+ inner join users on comments.userid == users.userid
+ where commentid = ?
+ """,
+ [commentid],
+ one=True)
if not comment:
abort(404) # Invalid comment
if comment["userid"] != session["userid"]:
if threadtype == ThreadType.SONG:
song = songs.by_threadid(request.args["threadid"])
elif threadtype == ThreadType.PROFILE:
- profile = db.query("select * from users where threadid = ?", [request.args["threadid"]], one=True)
+ profile = db.query(
+ "select * from users where threadid = ?",
+ [request.args["threadid"]],
+ one=True)
elif threadtype == ThreadType.PLAYLIST:
- profile = db.query("select * from playlists inner join users on playlists.userid = users.userid where playlists.threadid = ?", [request.args["threadid"]], one=True)
+ profile = db.query(
+ """
+ select * from playlists
+ inner join users on playlists.userid = users.userid
+ where playlists.threadid = ?
+ """,
+ [request.args["threadid"]],
+ one=True)
return render_template(
"comment.html",
song=song,
content = request.form["content"]
if comment:
# Update existing comment
- db.query("update comments set content = ? where commentid = ?", args=[content, comment["commentid"]])
+ db.query(
+ "update comments set content = ? where commentid = ?",
+ args=[content, comment["commentid"]])
else:
# Add new comment
timestamp = datetime.now(timezone.utc).isoformat()
threadid = request.args["threadid"]
comment = db.query(
- "insert into comments (threadid, userid, replytoid, created, content) values (?, ?, ?, ?, ?) returning (commentid)",
- args=[threadid, userid, replytoid, timestamp, content], one=True)
+ """
+ insert into comments
+ (threadid, userid, replytoid, created, content)
+ values (?, ?, ?, ?, ?)
+ returning (commentid)
+ """,
+ args=[threadid, userid, replytoid, timestamp, content],
+ one=True)
commentid = comment["commentid"]
# Notify content owner
notification_targets.add(replyto["userid"])
# Notify previous repliers in thread
- previous_replies = db.query("select * from comments where replytoid = ?", [replytoid])
+ previous_replies = db.query(
+ "select * from comments where replytoid = ?", [replytoid])
for reply in previous_replies:
notification_targets.add(reply["userid"])
# Create notifications
for target in notification_targets:
- db.query("insert into notifications (objectid, objecttype, targetuserid, created) values (?, ?, ?, ?)", [commentid, ObjectType.COMMENT, target, timestamp])
+ db.query(
+ """
+ insert into notifications
+ (objectid, objecttype, targetuserid, created)
+ values (?, ?, ?, ?)
+ """,
+ [commentid, ObjectType.COMMENT, target, timestamp])
db.commit()
if "userid" not in session:
return redirect("/login")
- comment = db.query("select c.userid as comment_user, t.userid as thread_user from comments as c inner join comment_threads as t on c.threadid == t.threadid where commentid = ?", [commentid], one=True)
+ comment = db.query(
+ """
+ select c.userid as comment_user, t.userid as thread_user
+ from comments as c
+ inner join comment_threads as t
+ on c.threadid == t.threadid
+ where commentid = ?
+ """,
+ [commentid],
+ one=True)
if not comment:
abort(404) # Invalid comment
or (comment["thread_user"] == session["userid"])):
abort(403)
- db.query("delete from comments where (commentid = ?) or (replytoid = ?)", [commentid, commentid])
+ db.query(
+ "delete from comments where (commentid = ?) or (replytoid = ?)",
+ [commentid, commentid])
db.commit()
return redirect(request.referrer)
+