]> littlesong.place Git - littlesongplace.git/commitdiff
Implement comment deletion
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 23:56:39 +0000 (18:56 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 23:56:39 +0000 (18:56 -0500)
main.py
templates/comment.html
todo.txt

diff --git a/main.py b/main.py
index 244d889fa547859c9c4fe598c60cc5563f81194a..ee256a703a53178ffd7a63810e4fd510edb9f3cf 100644 (file)
--- a/main.py
+++ b/main.py
@@ -492,6 +492,8 @@ def comment_get():
     if "replytoid" in request.args:
         replytoid = request.args["replytoid"]
         comment = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where commentid = ?", [replytoid], one=True)
+        if not comment:
+            abort(404) # Invalid comment
 
     session["previous_page"] = request.referrer
     return render_template("comment.html", song=song, comment=comment)
@@ -506,6 +508,9 @@ def comment_post():
     except ValueError:
         abort(404) # Invald songid
 
+    if not "userid" in session:
+        abort(401) # Must be logged in
+
     comment = None
     if "replytoid" in request.args:
         replytoid = request.args["replytoid"]
@@ -513,9 +518,6 @@ def comment_post():
         if not comment:
             abort(404) # Invalid comment
 
-    if not "userid" in session:
-        abort(401) # Must be logged in
-
     # Add new comment
     timestamp = datetime.now(timezone.utc).isoformat()
     content = request.form["content"]
@@ -532,6 +534,24 @@ def comment_post():
     else:
         return redirect("/")
 
+@app.get("/delete-comment/<int:commentid>")
+def comment_delete(commentid):
+    comment = query_db("select c.userid as comment_user, s.userid as song_user from song_comments as c inner join songs as s on c.songid == s.songid where commentid = ?", [commentid], one=True)
+    print(dict(comment))
+    if not comment:
+        abort(404) # Invalid comment
+
+    if not (
+        ("userid" in session)
+        and ((comment["comment_user"] == session["userid"])
+            or (comment["song_user"] == session["userid"]))):
+        abort(401)
+
+    query_db("delete from song_comments where (commentid = ?) or (replytoid = ?)", [commentid, commentid])
+    get_db().commit()
+
+    return redirect(request.referrer)
+
 @app.get("/site-news")
 def site_news():
     return render_template("news.html")
index 2ed0100894638f265bba10baf019f88a0bbdc9cc..c385ebee38b97353e9e75f32c4f0a18e15190112 100644 (file)
@@ -7,7 +7,7 @@
 <h1>Write a Comment</h1>
 
 <p>
-Commenting on {{ song.title }}
+Commenting on {{ song.title }} by <a href="/users/{{ song.username }}" class="profile-link">{{ song.username }}</a>
 </p>
 
 {% if comment %}
index 48f88e592ae4e3b615608b44c00b1cfed7cf4b88..bbb073d87dcb56b905ebc3acac72f21102acea55 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -1,6 +1,5 @@
 - Comments:
-    - Leave comment
-    - Delete comment
+    - Edit comment
     - Notifications
 - YouTube importer
 - Autoplay toggle