]> littlesong.place Git - littlesongplace.git/commitdiff
Work on comment restructuring TODOs
authorChris Fulljames <christianfulljames@gmail.com>
Fri, 21 Mar 2025 11:27:15 +0000 (07:27 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Fri, 21 Mar 2025 11:27:15 +0000 (07:27 -0400)
main.py
schema_update.sql
templates/activity.html
templates/comment.html

diff --git a/main.py b/main.py
index 1b31d88da29c42edd009358ba05ea3d8a21ec17f..68564e26c49788dc71e66816dab9cac4d14cf9e1 100644 (file)
--- a/main.py
+++ b/main.py
@@ -577,14 +577,12 @@ def comment():
     if not "userid" in session:
         return redirect("/login")
 
-    if not "songid" in request.args:
-        abort(400) # Must have songid
+    if not "threadid" in request.args:
+        abort(400) # Must have threadid
 
-    try:
-        # TODO: Handle other comment types
-        song = Song.by_id(request.args["songid"])
-    except ValueError:
-        abort(404) # Invald songid
+    thread = query_db("select * from comment_threads where threadid = ?", [threadid], one=True)
+    if not thread:
+        abort(400) # Invalid threadid
 
     # Check for comment being replied to
     replyto = None
@@ -607,8 +605,24 @@ def comment():
     if request.method == "GET":
         # Show the comment editor
         session["previous_page"] = request.referrer
-        # TODO: update page to handle other comment types
-        return render_template("comment.html", song=song, replyto=replyto, comment=comment)
+        threadtype = thread["threadtype"]
+        song = None
+        profile = None
+        playlist = None
+        if threadtype == ThreadType.SONG:
+            song = Song.by_threadid(request.args["threadid"])
+        elif threadtype == ThreadType.PROFILE:
+            profile = query_db("select * from users where threadid = ?", [request.args["threadid"]], one=True)
+        elif threadtype == ThreadType.PLAYLIST:
+            profile = query_db("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,
+            profile=profile,
+            playlist=playlist,
+            replyto=replyto,
+            comment=comment,
+        )
 
     elif request.method == "POST":
         # Add/update comment (user clicked the Post Comment button)
@@ -628,9 +642,8 @@ def comment():
                     args=[threadid, userid, replytoid, timestamp, content], one=True)
             commentid = comment["commentid"]
 
-            # TODO: Notify content owner
-            # Notify song owner
-            notification_targets = {song.userid}
+            # Notify content owner
+            notification_targets = {thread["userid"]}
             if replyto:
                 # Notify parent commenter
                 notification_targets.add(replyto["userid"])
@@ -664,8 +677,7 @@ def comment_delete(commentid):
     if "userid" not in session:
         return redirect("/login")
 
-    # TODO: Handle non-song comments
-    comment = query_db("select c.userid as comment_user, s.userid as song_user from comments as c inner join songs as s on c.songid == s.songid where commentid = ?", [commentid], one=True)
+    comment = query_db("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
 
@@ -684,22 +696,50 @@ def activity():
     if not "userid" in session:
         return redirect("/login")
 
-    # TODO: Handle other comment types
+    # TODO: Update activity page to handle other comment types
     # Get comment notifications
     comments = query_db(
         """\
-        select c.content, c.commentid, c.replytoid, cu.username as comment_username, s.songid, s.title, s.userid as song_userid, su.username as song_username, rc.content as replyto_content
+        select c.content, c.commentid, c.replytoid, cu.username as comment_username, rc.content as replyto_content, c.threadid, t.threadtype
         from comment_notifications as cn
         inner join comments as c on cn.commentid == c.commentid
+        inner join comment_threads as t on c.threadid = t.threadid
         left join comments as rc on c.replytoid == rc.commentid
-        inner join songs as s on c.songid == s.songid
-        inner join users as su on su.userid == s.userid
         inner join users as cu on cu.userid == c.userid
         where cn.targetuserid = ?
         order by c.created desc
         """,
         [session["userid"]])
 
+    comments = [dict(c) for c in comments]
+    for comment in comments:
+        threadtype = comment["threadtype"]
+        if threadtype == ThreadType.SONG:
+            song = Song.by_threadid(comment["threadid"])
+            comment["songid"] = song.songid
+            comment["title"] = song.title
+            comment["content_userid"] = song.userid
+            comment["content_username"] = song.username
+        elif threadtype == ThreadType.PROFILE:
+            profile = query_db("select * from users where threadid = ?", [comment["threadid"]], one=True)
+            comment["content_userid"] = profile["userid"]
+            comment["content_username"] = profile["username"]
+        elif threadtype == ThreadType.PLAYLIST:
+            playlist = query_db(
+                """\
+                select * from playlists
+                inner join users on playlists.userid == users.userid
+                where playlists.threadid = ?
+                """,
+                [comment["threadid"]],
+                one=True,
+            )
+            comment["playlistid"] = playlist["playlistid"]
+            comment["title"] = playlist["title"]
+            comment["content_userid"] = playlist["userid"]
+            comment["content_username"] = playlist["username"]
+
+
     timestamp = datetime.now(timezone.utc).isoformat()
     query_db("update users set activitytime = ? where userid = ?", [timestamp, session["userid"]])
     get_db().commit()
@@ -1127,6 +1167,14 @@ class Song:
 
         return songs[0]
 
+    @classmethod
+    def by_threadid(cls, threadid):
+        songs = cls._from_db("select * from songs inner join users on songs.userid = users.userid where songs.threadid = ?", [threadid])
+        if not songs:
+            raise ValueError(f"No song for Thread ID {songid:d}")
+
+        return songs[0]
+
     @classmethod
     def get_all_for_userid(cls, userid):
         return cls._from_db("select * from songs inner join users on songs.userid = users.userid where songs.userid = ? order by songs.created desc", [userid])
index 64c84fc63078157179f36a8baf4569174f1f10de..12d6d7c6e7f0111f20775c289483ea9a03b8094a 100644 (file)
@@ -2,6 +2,8 @@
 CREATE TABLE comment_threads (
     threadid INTEGER PRIMARY KEY,
     threadtype INTEGER NOT NULL
+    userid INTEGER NOT NULL,
+    FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE
 );
 
 CREATE TABLE comments (
index d802b5bd1f5a2033fc452a6abb6d7259a0ca4b09..334a13716fdd70f05e3e26dd84f75ea6573372dd 100644 (file)
@@ -16,9 +16,9 @@
             commented
             {% endif %}
             on
-            <a href="/song/{{ comment['song_userid'] }}/{{ comment['songid'] }}?action=view">{{ comment['title'] }}</a>
+            <a href="/song/{{ comment['content_userid'] }}/{{ comment['songid'] }}?action=view">{{ comment['title'] }}</a>
             -
-            <a href="/users/{{ comment['song_username'] }}" class="profile-link">{{ comment['song_username'] }}</a>
+            <a href="/users/{{ comment['content_username'] }}" class="profile-link">{{ comment['content_username'] }}</a>
             <div class="top-level-comment">
                 <a href="/users/{{ comment['comment_username'] }}" class="profile-link">{{ comment['comment_username'] }}</a>:
                 {{ comment['content'] }}
index dc6cbb086cb140843099b00782e4c7cc6ed6bfad..737904fc022900a8bd2c996dd41edaea441338ae 100644 (file)
@@ -7,7 +7,13 @@
 <h1>Write a Comment</h1>
 
 <p>
+{% if song -%}
 Commenting on {{ song.title }} by <a href="/users/{{ song.username }}" class="profile-link">{{ song.username }}</a>
+{% elif profile -%}
+Commenting on <a href="/users/{{ profile['username'] }}" class="profile-link">{{ profile['username'] }}</a>'s profile
+{% elif playlist -%}
+Commenting on {{ playlist['title'] }} by <a href="/users/{{ playlist['username'] }}" class="profile-link">{{ playlist['username'] }}</a>
+{%- endif %}
 </p>
 
 {% if replyto %}