]> littlesong.place Git - littlesongplace.git/commitdiff
Add activity page
authorChris Fulljames <christianfulljames@gmail.com>
Tue, 28 Jan 2025 01:51:06 +0000 (20:51 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Tue, 28 Jan 2025 01:51:06 +0000 (20:51 -0500)
main.py
schema.sql
static/styles.css
templates/activity.html [new file with mode: 0644]

diff --git a/main.py b/main.py
index a56db5feac9e07a92c9650d5b0d8d694da78ef31..23cb2a72d8ad2dd9985608c998876b503368561e 100644 (file)
--- a/main.py
+++ b/main.py
@@ -476,7 +476,7 @@ def songs():
             song_list=render_template("song-list.html", songs=songs))
 
 @app.route("/comment", methods=["GET", "POST"])
-def comment_get():
+def comment():
     if not "songid" in request.args:
         abort(400) # Must have songid
 
@@ -523,9 +523,31 @@ def comment_get():
             userid = session["userid"]
             songid = request.args["songid"]
             replytoid = request.args.get("replytoid", None)
-            query_db(
-                    "insert into song_comments (songid, userid, replytoid, created, content) values (?, ?, ?, ?, ?)",
-                    args=[songid, userid, replytoid, timestamp, content])
+
+            comment = query_db(
+                    "insert into song_comments (songid, userid, replytoid, created, content) values (?, ?, ?, ?, ?) returning (commentid)",
+                    args=[songid, userid, replytoid, timestamp, content], one=True)
+            commentid = comment["commentid"]
+
+            # Notify song owner
+            notification_targets = {song.userid}
+            if replyto:
+                # Notify parent commenter
+                notification_targets.add(replyto["userid"])
+
+                # Notify previous repliers in thread
+                previous_replies = query_db("select * from song_comments where replytoid = ?", [replytoid])
+                for reply in previous_replies:
+                    notification_targets.add(reply["userid"])
+
+            # Don't notify the person who wrote the comment
+            if userid in notification_targets:
+                notification_targets.remove(userid)
+
+            # Create notifications
+            for target in notification_targets:
+                query_db("insert into song_comment_notifications (commentid, targetuserid) values (?, ?)", [commentid, target])
+
         get_db().commit()
 
         if "previous_page" in session:
@@ -557,63 +579,22 @@ def activity():
     if not "userid" in session:
         return redirect("/login")
 
-    # For all:
-    # - Comment content
-    # - Comment created time
-    # - Commenter username
-    # - Song name
-    # - Song username
-
-    # For replies:
-    # - Parent username
-    # - Parent comment
-
-    # Get comments on user's songs
-    comments_on_songs = query_db(
+    # Get comment notifications
+    comments = query_db(
         """\
-        select c.content, c.created, cu.username as comment_username, s.title, su.username as song_username
-        from song_comments as c
+        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
+        from song_comment_notifications as scn
+        inner join song_comments as c on scn.commentid == c.commentid
+        left join song_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 s.userid = ?""",
+        where scn.targetuserid = ?
+        order by c.created desc
+        """,
         [session["userid"]])
 
-    # Get replies to user's comments
-    replies_to_user = query_db(
-        """\
-        select c.content, c.created, cu.username as comment_username, s.title, su.username as song_username, pu.username as parent_username, p.content as parent_content
-        from song_comments as c
-        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
-        inner join song_comments as p on c.replytoid == p.commentid
-        inner join users as pu on pu.userid == p.userid
-        where p.userid == ?""",
-        [session["userid"]])
-
-    # Get comments user has replied to
-    comments_user_replied_to = query_db(
-        "select * from song_comments where (replytoid is not null) and (userid == ?)",
-        [session["userid"]])
-
-
-    # Get replies to comments user has also replied to
-    # TODO:
-    # replies_to_user = query_db(
-    #     """\
-    #     select c.content, c.created, cu.username as comment_username, s.title, su.username as song_username, pu.username as parent_username, p.content as parent_content
-    #     from song_comments as c
-    #     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
-    #     inner join song_comments as p on c.replytoid == p.commentid
-    #     inner join users as pu on pu.userid == p.userid
-    #     where p.userid == ?""",
-    #     [session["userid"]])
-
-    # Filter duplicates
-    return [dict(c) for c in comments_on_songs] + [dict(c) for c in replies_to_user]
+    return render_template("activity.html", comments=comments)
 
 @app.get("/site-news")
 def site_news():
index d637cd8368fa7386be7ecfd32185b6fb9bef7b41..d1e17fba1612316ff6421f05cc069245790bbbb8 100644 (file)
@@ -49,4 +49,15 @@ CREATE TABLE song_comments (
 );
 CREATE INDEX idx_comments_by_song ON song_comments(songid);
 CREATE INDEX idx_comments_by_user ON song_comments(userid);
+CREATE INDEX idx_comments_by_replyto ON song_comments(replytoid);
+
+DROP TABLE IF EXISTS song_comment_notifications;
+CREATE TABLE song_comment_notifications (
+    notificationid INTEGER PRIMARY KEY,
+    commentid INTEGER NOT NULL,
+    targetuserid INTEGER NOT NULL,
+    FOREIGN KEY(commentid) REFERENCES song_comments(commentid),
+    FOREIGN KEY(targetuserid) REFERENCES users(userid)
+);
+CREATE INDEX idx_song_comment_notifications_by_target ON song_comment_notifications(targetuserid);
 
index ba1a70ebf45421c13143fe1b48e71e4967615887..82e1fb52b8397ef6b6c7db874c0dcb699bd962f3 100644 (file)
@@ -325,3 +325,11 @@ a.player-button img {
     height: 20px;
     border-radius: 50%;
 }
+
+/* Activity */
+div.comment-notification {
+    margin: 10px;
+    padding: 10px;
+    box-shadow: 0px 0px 5px 0px;
+    border-radius: 10px;
+}
diff --git a/templates/activity.html b/templates/activity.html
new file mode 100644 (file)
index 0000000..d802b5b
--- /dev/null
@@ -0,0 +1,45 @@
+{% extends "base.html" %}
+
+{% block title %}Activity{% endblock %}
+
+{% block body %}
+
+{% if comments %}
+<h1>Activity</h1>
+
+    {% for comment in comments %}
+        <div class="comment-notification">
+            <a href="/users/{{ comment['comment_username'] }}" class="profile-link">{{ comment['comment_username'] }}</a>
+            {% if comment['replyto_content'] %}
+            replied to &quot;{{ comment['replyto_content'] }}&quot;
+            {% else %}
+            commented
+            {% endif %}
+            on
+            <a href="/song/{{ comment['song_userid'] }}/{{ comment['songid'] }}?action=view">{{ comment['title'] }}</a>
+            -
+            <a href="/users/{{ comment['song_username'] }}" class="profile-link">{{ comment['song_username'] }}</a>
+            <div class="top-level-comment">
+                <a href="/users/{{ comment['comment_username'] }}" class="profile-link">{{ comment['comment_username'] }}</a>:
+                {{ comment['content'] }}
+
+                <div class="comment-button-container">
+                    {% if comment['replytoid'] %}
+                        <!-- Comment is already part of a thread; reply to the same thread -->
+                        <a href="/comment?songid={{ comment['songid'] }}&replytoid={{ comment['replytoid'] }}">Reply</a>
+                    {% else %}
+                        <!-- Comment is a top-level, reply to the comment -->
+                        <a href="/comment?songid={{ comment['songid'] }}&replytoid={{ comment['commentid'] }}">Reply</a>
+                    {% endif %}
+                </div>
+            </div>
+        </div>
+    {% endfor %}
+
+{% else %}
+
+    Nothing to show here yet!
+
+{% endif %}
+
+{% endblock %}