v3: various improvements and fixes to real-time updates
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 40s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 40s
This commit is contained in:
@@ -658,6 +658,11 @@ export interface NotificationCreatedMessage {
|
||||
export interface ErrorMessage {
|
||||
type: "error";
|
||||
message?: string;
|
||||
// Context for optimistic-action failures so the client can revert the exact
|
||||
// pending vote/like immediately instead of waiting for its ACK timeout.
|
||||
dumpId?: string;
|
||||
commentId?: string;
|
||||
action?: "cast" | "remove";
|
||||
}
|
||||
|
||||
export interface ForceLogoutMessage {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { Router } from "@oak/oak";
|
||||
import { authMiddleware } from "../middleware/auth.ts";
|
||||
import { getUserById, updateUserAvatar } from "../services/user-service.ts";
|
||||
import { updateClientAvatar } from "../services/ws-service.ts";
|
||||
import {
|
||||
broadcastUserUpdated,
|
||||
updateClientAvatar,
|
||||
} from "../services/ws-service.ts";
|
||||
import { APIErrorCode, APIException } from "../model/interfaces.ts";
|
||||
import {
|
||||
AVATARS_DIR,
|
||||
@@ -45,6 +48,11 @@ router.post("/api/avatars/me", authMiddleware, async (ctx) => {
|
||||
updateClientAvatar(authPayload.userId, mime);
|
||||
|
||||
const { passwordHash: _, ...publicUser } = getUserById(authPayload.userId);
|
||||
// Let every client refresh this user's avatar wherever it's shown (feeds,
|
||||
// comments, OP, profiles) — not just the presence row. updated_at, bumped on
|
||||
// the avatar write, doubles as the cache-busting version.
|
||||
const { email: _email, ...broadcastUser } = publicUser;
|
||||
broadcastUserUpdated(broadcastUser);
|
||||
ctx.response.status = 200;
|
||||
ctx.response.body = { success: true, data: publicUser };
|
||||
});
|
||||
|
||||
@@ -153,7 +153,7 @@ function handleVote(
|
||||
broadcastVoteUpdate(dumpId, newCount, client.userId, action);
|
||||
} catch (err) {
|
||||
const message = err instanceof APIException ? err.message : "Vote failed";
|
||||
socket.send(JSON.stringify({ type: "error", message }));
|
||||
socket.send(JSON.stringify({ type: "error", message, dumpId, action }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ function handleCommentLike(
|
||||
broadcastCommentLikeUpdate(commentId, newCount, client.userId, action);
|
||||
} catch (err) {
|
||||
const message = err instanceof APIException ? err.message : "Like failed";
|
||||
socket.send(JSON.stringify({ type: "error", message }));
|
||||
socket.send(JSON.stringify({ type: "error", message, commentId, action }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -169,13 +169,18 @@ export function notifyUserFollowersNewDump(
|
||||
const createdAt = new Date().toISOString();
|
||||
const sourceKey = `dump:${dumpId}`;
|
||||
|
||||
// Batch INSERT all follower notifications in a single statement
|
||||
const params: (string | number | null)[] = [];
|
||||
const placeholders: string[] = [];
|
||||
// Insert per follower so we can broadcast only the rows that were actually
|
||||
// created (INSERT OR IGNORE skips followers who already have this dump's
|
||||
// notification) and reuse each persisted id in the live event — otherwise a
|
||||
// later refetch would dedupe against a mismatched id and show a duplicate.
|
||||
const insert = db.prepare(
|
||||
`INSERT OR IGNORE INTO notifications (id, user_id, type, data, read, created_at, source_key)
|
||||
VALUES (?, ?, ?, ?, 0, ?, ?);`,
|
||||
);
|
||||
|
||||
for (const row of followerRows) {
|
||||
const id = crypto.randomUUID();
|
||||
placeholders.push("(?, ?, ?, ?, 0, ?, ?)");
|
||||
params.push(
|
||||
const result = insert.run(
|
||||
id,
|
||||
row.follower_id,
|
||||
"user_dump_posted",
|
||||
@@ -183,19 +188,11 @@ export function notifyUserFollowersNewDump(
|
||||
createdAt,
|
||||
sourceKey,
|
||||
);
|
||||
}
|
||||
|
||||
const result = db.prepare(
|
||||
`INSERT OR IGNORE INTO notifications (id, user_id, type, data, read, created_at, source_key)
|
||||
VALUES ${placeholders.join(", ")};`,
|
||||
).run(...params);
|
||||
|
||||
if ((result.changes as number) > 0) {
|
||||
for (const row of followerRows) {
|
||||
if ((result.changes as number) > 0) {
|
||||
sendToUser(row.follower_id, {
|
||||
type: "notification_created",
|
||||
notification: {
|
||||
id: crypto.randomUUID(),
|
||||
id,
|
||||
userId: row.follower_id,
|
||||
type: "user_dump_posted",
|
||||
data,
|
||||
|
||||
Reference in New Issue
Block a user