From: Chris Fulljames Date: Sat, 22 Mar 2025 22:52:23 +0000 (-0400) Subject: Generalize notification table X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=f1429c593de3b2e41c25c4a371d2870fb1bafeb3;p=littlesongplace.git Generalize notification table --- diff --git a/main.py b/main.py index 28cd6db..37b79a3 100644 --- a/main.py +++ b/main.py @@ -670,7 +670,7 @@ def comment(): # Create notifications for target in notification_targets: - query_db("insert into comment_notifications (commentid, targetuserid) values (?, ?)", [commentid, target]) + query_db("insert into notifications (objectid, objecttype, targetuserid, created) values (?, ?, ?, ?)", [commentid, ObjectType.COMMENT, target, timestamp]) get_db().commit() @@ -707,20 +707,19 @@ def activity(): if not "userid" in session: return redirect("/login") - # 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, rc.content as replyto_content, c.threadid, t.threadtype - from comment_notifications as cn - inner join comments as c on cn.commentid == c.commentid + from notifications as n + inner join comments as c on n.objectid == 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 users as cu on cu.userid == c.userid - where cn.targetuserid = ? + where (n.targetuserid = ?) and (n.objecttype = ?) order by c.created desc """, - [session["userid"]]) + [session["userid"], ObjectType.COMMENT]) comments = [dict(c) for c in comments] for comment in comments: @@ -750,7 +749,6 @@ def activity(): 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() @@ -764,10 +762,9 @@ def new_activity(): user_data = query_db("select activitytime from users where userid = ?", [session["userid"]], one=True) comment_data = query_db( """\ - select c.created from comment_notifications as cn - inner join comments as c on cn.commentid = c.commentid - where cn.targetuserid = ? - order by c.created desc + select created from notifications + where targetuserid = ? + order by created desc limit 1""", [session["userid"]], one=True) @@ -1104,12 +1101,19 @@ def get_db(): comment_cur.close() cur.close() - # Copy song comment notifications to new comment notifications table - cur = db.execute( - """\ - insert into comment_notifications - select * from song_comment_notifications + # Copy song comment notifications to new notifications table + cur = db.execute("""\ + select * from song_comment_notifications as scn + inner join song_comments as sc on + scn.commentid = sc.commentid """) + for row in cur: + db.execute( + """\ + insert into notifications (notificationid, objectid, objecttype, targetuserid, created) + values (?, ?, ?, ?, ?) + """, + [row["notificationid"], row["commentid"], ObjectType.COMMENT, row["targetuserid"], row["created"]]) db.execute("PRAGMA user_version = 4").close() @@ -1162,6 +1166,9 @@ def gen_key(): import secrets print(secrets.token_hex()) +class ObjectType(enum.IntEnum): + COMMENT = 0 + class ThreadType(enum.IntEnum): SONG = 0 PROFILE = 1 diff --git a/schema_revert.sql b/schema_revert.sql new file mode 100644 index 0000000..3670241 --- /dev/null +++ b/schema_revert.sql @@ -0,0 +1,10 @@ +DROP TRIGGER trg_delete_song_comments; +DROP TRIGGER trg_delete_profile_comments; +DROP TRIGGER trg_delete_playlist_comments; +ALTER TABLE songs DROP COLUMN threadid; +ALTER TABLE users DROP COLUMN threadid; +ALTER TABLE playlists DROP COLUMN threadid; +DROP TABLE notifications; +DROP TABLE comments; +DROP TABLE comment_threads; +PRAGMA user_version = 3; diff --git a/schema_update.sql b/schema_update.sql index b9cc515..cc9a48a 100644 --- a/schema_update.sql +++ b/schema_update.sql @@ -1,4 +1,12 @@ --- Create new comment tables +-- Add comment thread to songs table +ALTER TABLE songs ADD COLUMN threadid INTEGER; + +-- Add profile comment thread to users table +ALTER TABLE users ADD COLUMN threadid INTEGER; + +-- Add playlist comment thread to playlists table +ALTER TABLE playlists ADD COLUMN threadid INTEGER; + CREATE TABLE comment_threads ( threadid INTEGER PRIMARY KEY, threadtype INTEGER NOT NULL, @@ -6,6 +14,27 @@ CREATE TABLE comment_threads ( FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE ); +-- Delete comment thread when song deleted +CREATE TRIGGER trg_delete_song_comments +BEFORE DELETE ON songs FOR EACH ROW +BEGIN + DELETE FROM comment_threads WHERE threadid = OLD.threadid; +END; + +-- Delete comment thread when profile deleted +CREATE TRIGGER trg_delete_profile_comments +BEFORE DELETE ON users FOR EACH ROW +BEGIN + DELETE FROM comment_threads WHERE threadid = OLD.threadid; +END; + +-- Delete comment thread when playlist deleted +CREATE TRIGGER trg_delete_playlist_comments +BEFORE DELETE ON playlists FOR EACH ROW +BEGIN + DELETE FROM comment_threads WHERE threadid = OLD.threadid; +END; + CREATE TABLE comments ( commentid INTEGER PRIMARY KEY, threadid INTEGER NOT NULL, @@ -20,21 +49,21 @@ CREATE INDEX idx_comments_user ON comments(userid); CREATE INDEX idx_comments_replyto ON comments(replytoid); CREATE INDEX idx_comments_time ON comments(created); -CREATE TABLE comment_notifications ( +CREATE TABLE notifications ( notificationid INTEGER PRIMARY KEY, - commentid INTEGER NOT NULL, + objectid INTEGER NOT NULL, + objecttype INTEGER NOT NULL, targetuserid INTEGER NOT NULL, - FOREIGN KEY(commentid) REFERENCES comments(commentid) ON DELETE CASCADE, + created TEXT NOT NULL, FOREIGN KEY(targetuserid) REFERENCES users(userid) ON DELETE CASCADE ); -CREATE INDEX idx_comment_notifications_by_target ON comment_notifications(targetuserid); - --- Add comment thread to songs table -ALTER TABLE songs ADD COLUMN threadid INTEGER; +CREATE INDEX idx_notifications_by_target ON notifications(targetuserid); +CREATE INDEX idx_notifications_by_object ON notifications(objectid); --- Add profile comment thread to users table -ALTER TABLE users ADD COLUMN threadid INTEGER; - --- Add playlist comment thread to playlists table -ALTER TABLE playlists ADD COLUMN threadid INTEGER; +-- Delete comment notifications when comment deleted +CREATE TRIGGER trg_delete_notifications +BEFORE DELETE ON comments FOR EACH ROW +BEGIN + DELETE FROM notifications WHERE objectid = OLD.commentid AND objecttype = 0; +END;