feat(chat): unread read-receipts with lobby/game dot and history-open ack
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 49s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m16s
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 49s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m16s
Persist per-message read state as a chat_messages.unread_seats bitmask
(migration 00008): a text message seeds every recipient seat's bit, a nudge
only the awaited seat's. A seat's bit clears when the player opens the move
history or chat (POST /games/:id/chat/read, sent only when something is
unread), and a nudge additionally clears when its recipient answers by moving
(a wired game NudgeClearer, dependency-inverted so game keeps off social).
UI shows a per-viewer unread dot in the lobby (next to the opponent) and the
game score bar — the unread_chat game-view flag seeds it from authoritative
REST views, live chat/nudge events raise it. Opening the move history counts
as reading (even without entering chat): the 💬 fade-blinks twice and the
client acks. Admin Messages gains an unread-only filter, a read/unread column,
and a per-message card with the per-seat read breakdown. Observability:
chat_read_duration histogram + chat_unread_messages gauge + social tracing.
This commit is contained in:
+36
-11
@@ -61,8 +61,10 @@ export const app = $state<{
|
||||
localeLocked: boolean;
|
||||
/** Pending incoming friend requests, for the lobby ⚙️ badge and the Settings Friends tab. */
|
||||
notifications: number;
|
||||
/** Unread chat-message count per game id, for the in-game score-bar and 💬 badges. */
|
||||
chatUnread: Record<string, number>;
|
||||
/** Per-game flag: the player has at least one unread chat entry (message or nudge) in that
|
||||
* game, for the lobby and in-game unread dot. Seeded from the authoritative REST views and
|
||||
* raised by live chat/nudge events; cleared (with a backend ack) on opening the history/chat. */
|
||||
chatUnread: Record<string, boolean>;
|
||||
/** Whether an operator feedback reply awaits the player, for the lobby ⚙️ badge (combined
|
||||
* with friend requests) and the Settings → Info badge. */
|
||||
feedbackReplyUnread: boolean;
|
||||
@@ -149,9 +151,29 @@ export function dismissStaleInvite(): void {
|
||||
app.staleInvite = false;
|
||||
}
|
||||
|
||||
/** clearChatUnread resets a game's unread chat-message count (called when its chat is opened). */
|
||||
export function clearChatUnread(gameId: string): void {
|
||||
if (app.chatUnread[gameId]) app.chatUnread = { ...app.chatUnread, [gameId]: 0 };
|
||||
/**
|
||||
* seedChatUnread sets a game's unread flag from an authoritative per-viewer REST view (the lobby
|
||||
* list, a game's state, or a move result). The live-event GameView omits the flag, so the live
|
||||
* stream raises unread (bumpChatUnread) rather than seeding it from a GameView.
|
||||
*/
|
||||
export function seedChatUnread(gameId: string, unread: boolean): void {
|
||||
if ((app.chatUnread[gameId] ?? false) !== unread) {
|
||||
app.chatUnread = { ...app.chatUnread, [gameId]: unread };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* markChatRead clears a game's unread badge and tells the backend the player has read the chat —
|
||||
* called when the move history or the chat opens. It hits the backend only when something is
|
||||
* actually unread, so opening the history does not call the backend on every tap. The clear is
|
||||
* optimistic; a failed ack restores the badge so a later open retries.
|
||||
*/
|
||||
export function markChatRead(gameId: string): void {
|
||||
if (!app.chatUnread[gameId]) return;
|
||||
app.chatUnread = { ...app.chatUnread, [gameId]: false };
|
||||
void gateway.markChatRead(gameId).catch(() => {
|
||||
app.chatUnread = { ...app.chatUnread, [gameId]: true };
|
||||
});
|
||||
}
|
||||
|
||||
/** handleError maps a GatewayError to a toast; an invalid session logs out. A connectivity
|
||||
@@ -220,15 +242,18 @@ function openStream(): void {
|
||||
(router.route.name === 'gameChat' || router.route.name === 'gameCheck') &&
|
||||
router.route.params.id === e.message.gameId;
|
||||
if (!inComms) {
|
||||
if (e.message.kind !== 'nudge') {
|
||||
const gid = e.message.gameId;
|
||||
app.chatUnread = { ...app.chatUnread, [gid]: (app.chatUnread[gid] ?? 0) + 1 };
|
||||
}
|
||||
app.chatUnread = { ...app.chatUnread, [e.message.gameId]: true };
|
||||
showToast(e.message.kind === 'nudge' ? t('chat.nudge') : e.message.body, 'info');
|
||||
}
|
||||
} else if (e.kind === 'nudge') {
|
||||
// Name the nudger (their per-game seat name, carried on the event), so the toast reads
|
||||
// "<opponent>: …" like the your-turn toast; fall back to the plain phrase when absent.
|
||||
// A nudge only reaches the awaited player; raise their unread badge for the game (unless
|
||||
// they are already in its chat), then toast — naming the nudger (their per-game seat name,
|
||||
// carried on the event), so it reads "<opponent>: …" like the your-turn toast; fall back to
|
||||
// the plain phrase when absent.
|
||||
const inComms =
|
||||
(router.route.name === 'gameChat' || router.route.name === 'gameCheck') &&
|
||||
router.route.params.id === e.gameId;
|
||||
if (!inComms) app.chatUnread = { ...app.chatUnread, [e.gameId]: true };
|
||||
showToast(e.senderName ? t('chat.nudgeBy', { name: e.senderName }) : t('chat.nudge'), 'info');
|
||||
} else if (e.kind === 'your_turn') {
|
||||
// Name the player who moved just before this one (the previous seat in turn order), so the
|
||||
|
||||
Reference in New Issue
Block a user