v4: every live update reaches every page that shows what it changed — the client kept each kind of event in one "latest" slot, so two arriving before a render lost the first (two notifications of one dump going public showed one); they now go through a subscription, useWSEvent, and each reaches every subscriber. A playlist made private sent its new title and description to everyone, and kept them on a visitor's page; playlists now follow the dumps' rule, broadcastChange: others get playlist_deleted, and playlist_created when it goes public again. A dump deleted or turned private sends its playlists' dumps again, so their counts and pages follow. Comments on a private dump reach its poster's other tabs. Reading the notifications clears the bell in every tab (notifications_read), and a category change makes every client fetch the categories again (categories_changed). The dump page gives way to a notice when its dump is deleted or made private. A profile gains the user's new dumps, the followed feed those of the users followed and the playlists followed, the search results follow edits and deletions, the index's category feed drops a dump edited out of it, and a renamed user's name follows on cards and comments. After a reconnection, every live list fetches the pages it holds again (usePagedList's resync), and the playlist, the profile, the dump, the categories and the follows are fetched again. The position memory of usePositionAwareSync is now inside the list sync hooks, so a returning item is put back without depending on the order the hooks are called.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -114,12 +114,44 @@ export function createComment(
|
||||
return comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* The dump a comment is on, as {@link updateComment} and {@link deleteComment}
|
||||
* return it: what the route needs to choose the broadcast's audience.
|
||||
*/
|
||||
export interface CommentDump {
|
||||
id: string;
|
||||
isPrivate: boolean;
|
||||
/** The dump's poster, the only one who sees a private dump's comments. */
|
||||
userId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the dump comment `commentId` is on.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such comment.
|
||||
*/
|
||||
function commentDump(commentId: string): CommentDump {
|
||||
const row = db.prepare(
|
||||
`SELECT d.id, d.is_private, d.user_id FROM comments c JOIN dumps d ON c.dump_id = d.id WHERE c.id = ?;`,
|
||||
).get(commentId) as
|
||||
| { id: string; is_private: number; user_id: string }
|
||||
| undefined;
|
||||
if (!row) {
|
||||
throw new APIException(404, "Comment not found");
|
||||
}
|
||||
return {
|
||||
id: row.id,
|
||||
isPrivate: Boolean(row.is_private),
|
||||
userId: row.user_id,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a comment's body, for `PATCH /api/comments/:commentId`.
|
||||
* `canModerate` lets someone other than the author do it. It notifies, in the
|
||||
* author's name, the users it mentions for the first time, unless the dump is
|
||||
* private, and relinks the attachments and backlinks. The route broadcasts the
|
||||
* comment unless `isPrivate`.
|
||||
* comment to those who may see its `dump`.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such comment, 400 when it is
|
||||
* deleted, 403 when the user is neither its author nor a moderator.
|
||||
@@ -129,13 +161,8 @@ export function updateComment(
|
||||
body: string,
|
||||
requestingUserId: string,
|
||||
canModerate: boolean,
|
||||
): { comment: Comment; dumpId: string; isPrivate: boolean } {
|
||||
const row = db.prepare(
|
||||
`SELECT c.dump_id, d.is_private FROM comments c JOIN dumps d ON c.dump_id = d.id WHERE c.id = ?;`,
|
||||
).get(commentId) as { dump_id: string; is_private: number } | undefined;
|
||||
if (!row) {
|
||||
throw new APIException(404, "Comment not found");
|
||||
}
|
||||
): { comment: Comment; dump: CommentDump } {
|
||||
const dump = commentDump(commentId);
|
||||
const existing = fetchComment(commentId);
|
||||
if (existing.deleted) {
|
||||
throw new APIException(400, "Cannot edit a deleted comment");
|
||||
@@ -149,9 +176,9 @@ export function updateComment(
|
||||
now,
|
||||
commentId,
|
||||
);
|
||||
if (!row.is_private) {
|
||||
if (!dump.isPrivate) {
|
||||
const dumpRow = db.prepare(`SELECT title FROM dumps WHERE id = ?;`).get(
|
||||
row.dump_id,
|
||||
dump.id,
|
||||
) as { title: string } | undefined;
|
||||
// A moderator's edit keeps the author's words, so the author mentions.
|
||||
notifyMentions(
|
||||
@@ -160,23 +187,19 @@ export function updateComment(
|
||||
"comment",
|
||||
commentId,
|
||||
dumpRow?.title ?? "",
|
||||
row.dump_id,
|
||||
dump.id,
|
||||
);
|
||||
}
|
||||
linkAttachments(body, "comment", commentId);
|
||||
relinkBacklinks("comment", commentId, row.dump_id, body);
|
||||
return {
|
||||
comment: fetchComment(commentId),
|
||||
dumpId: row.dump_id,
|
||||
isPrivate: Boolean(row.is_private),
|
||||
};
|
||||
relinkBacklinks("comment", commentId, dump.id, body);
|
||||
return { comment: fetchComment(commentId), dump };
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a comment, for `DELETE /api/comments/:commentId`. `canModerate` lets
|
||||
* someone other than the author do it. The body is emptied and the backlinks
|
||||
* dropped, but the row stays so its replies keep their place. The route
|
||||
* broadcasts the deletion unless `isPrivate`.
|
||||
* broadcasts the deletion to those who may see the returned dump.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such comment, 403 when the user
|
||||
* is neither its author nor a moderator.
|
||||
@@ -185,13 +208,8 @@ export function deleteComment(
|
||||
commentId: string,
|
||||
requestingUserId: string,
|
||||
canModerate: boolean,
|
||||
): { dumpId: string; isPrivate: boolean } {
|
||||
const row = db.prepare(
|
||||
`SELECT c.dump_id, d.is_private FROM comments c JOIN dumps d ON c.dump_id = d.id WHERE c.id = ?;`,
|
||||
).get(commentId) as { dump_id: string; is_private: number } | undefined;
|
||||
if (!row) {
|
||||
throw new APIException(404, "Comment not found");
|
||||
}
|
||||
): CommentDump {
|
||||
const dump = commentDump(commentId);
|
||||
const comment = fetchComment(commentId);
|
||||
if (comment.userId !== requestingUserId && !canModerate) {
|
||||
throw new APIException(403, "Not authorized to delete this comment");
|
||||
@@ -199,6 +217,6 @@ export function deleteComment(
|
||||
db.prepare(`UPDATE comments SET deleted = 1, body = '' WHERE id = ?;`).run(
|
||||
commentId,
|
||||
);
|
||||
relinkBacklinks("comment", commentId, row.dump_id, "");
|
||||
return { dumpId: row.dump_id, isPrivate: Boolean(row.is_private) };
|
||||
relinkBacklinks("comment", commentId, dump.id, "");
|
||||
return dump;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user