detached 128e01c

fix(mentions): address PR review feedback from copilot

- Batch Set updates to avoid repeated allocations in loop
- Reset pendingMentionTimestamp on user logout to prevent cross-user state leaks
- Precompute message timestamp Map for O(1) lookup in IntersectionObserver
- Remove unused authMutation (redundant with HTTP endpoint flow)
- Add future timestamp validation to prevent notification suppression attacks
- Include lastReadMentionTimestamp in session validation for cross-device persistence
commit
128e01c78179176c2e15beec17fc66e849efe137
author
Kit · · 2026-01-24 05:25 UTC
parent
51dcfa2
signature
SSH UnverifiedSHA256:H91DFuEG4jpk5edzLA4aUKkpv4zv9Q339ipRmkph6Tk The key is unknown
4 files · +29 -25
M src/convex/mutations.ts +0 -19
@@ -113,22 +113,3 @@ export const getOrCreateDefaultRoom = mutation({
113113 };
114114 }
115115 });
116-
117-// Authenticated mutation: Update the timestamp of the last read mention
118-export const updateLastReadMentionTimestamp = authMutation({
119- args: {
120- timestamp: v.number()
121- },
122- handler: async (ctx, args) => {
123- const currentTimestamp = ctx.user.lastReadMentionTimestamp ?? 0;
124-
125- // Only update if the new timestamp is greater (more recent)
126- if (args.timestamp > currentTimestamp) {
127- await ctx.db.patch(ctx.user._id, {
128- lastReadMentionTimestamp: args.timestamp
129- });
130- }
131-
132- return { success: true };
133- }
134-});
@@ -113,22 +113,3 @@ export const getOrCreateDefaultRoom = mutation({
113 };113 };
114 }114 }
115 });115 });
116-
117-// Authenticated mutation: Update the timestamp of the last read mention
118-export const updateLastReadMentionTimestamp = authMutation({
119- args: {
120- timestamp: v.number()
121- },
122- handler: async (ctx, args) => {
123- const currentTimestamp = ctx.user.lastReadMentionTimestamp ?? 0;
124-
125- // Only update if the new timestamp is greater (more recent)
126- if (args.timestamp > currentTimestamp) {
127- await ctx.db.patch(ctx.user._id, {
128- lastReadMentionTimestamp: args.timestamp
129- });
130- }
131-
132- return { success: true };
133- }
134-});
M src/lib/api/session.server.ts +2 -1
@@ -92,7 +92,8 @@ export async function validateSessionToken(token: string): Promise<SessionValida
9292 nickname: result.user.nickname,
9393 status: result.user.status as SafeUser['status'],
9494 avatarUrl: result.user.avatarUrl,
95- lastSeen: result.user.lastSeen
95+ lastSeen: result.user.lastSeen,
96+ lastReadMentionTimestamp: result.user.lastReadMentionTimestamp
9697 };
9798
9899 log.debug('Session validated', { tokenHash, userId: user.id });
@@ -92,7 +92,8 @@ export async function validateSessionToken(token: string): Promise<SessionValida
92 nickname: result.user.nickname,92 nickname: result.user.nickname,
93 status: result.user.status as SafeUser['status'],93 status: result.user.status as SafeUser['status'],
94 avatarUrl: result.user.avatarUrl,94 avatarUrl: result.user.avatarUrl,
95- lastSeen: result.user.lastSeen95+ lastSeen: result.user.lastSeen,
96+ lastReadMentionTimestamp: result.user.lastReadMentionTimestamp
96 };97 };
97 98
98 log.debug('Session validated', { tokenHash, userId: user.id });99 log.debug('Session validated', { tokenHash, userId: user.id });
M src/lib/components/chat-room.svelte +17 -5
@@ -348,6 +348,8 @@
348348 if (mentionUnreadCount !== 0) {
349349 mentionUnreadCount = 0;
350350 }
351+ // Reset pending timestamp when user logs out
352+ pendingMentionTimestamp = null;
351353 // mention observer reset
352354 if (mentionObserver) {
353355 mentionObserver.disconnect();
@@ -357,16 +359,21 @@
357359 }
358360 const mentionIds = new SvelteSet<string>();
359361 const lastReadTimestamp = currentUser.lastReadMentionTimestamp ?? 0;
362+ const mentionIdsToPreObserve: string[] = [];
360363
361364 for (const message of visibleMessages) {
362365 if (shouldHighlightMention(message)) {
363366 mentionIds.add(message.id);
364367 // Pre-mark as observed if message is older than lastReadMentionTimestamp
365368 if (message.timestamp <= lastReadTimestamp && !mentionObservedIds.has(message.id)) {
366- mentionObservedIds = new SvelteSet([...mentionObservedIds, message.id]);
369+ mentionIdsToPreObserve.push(message.id);
367370 }
368371 }
369372 }
373+ // Batch update observed IDs to avoid repeated Set allocations
374+ if (mentionIdsToPreObserve.length > 0) {
375+ mentionObservedIds = new SvelteSet([...mentionObservedIds, ...mentionIdsToPreObserve]);
376+ }
370377 const nextMentionIds = mentionIds;
371378 if (!setsEqual(nextMentionIds, mentionMessageIds)) {
372379 mentionMessageIds = nextMentionIds;
@@ -386,6 +393,11 @@
386393 if (mentionObserver) {
387394 mentionObserver.disconnect();
388395 }
396+ // Precompute message timestamp map to avoid O(n) lookup in callback
397+ const messageTimestampMap = new Map<string, number>();
398+ for (const msg of visibleMessages) {
399+ messageTimestampMap.set(msg.id, msg.timestamp);
400+ }
389401 mentionObserver = new IntersectionObserver(
390402 (entries) => {
391403 const updatedObserved = new SvelteSet(mentionObservedIds);
@@ -402,10 +414,10 @@
402414 updatedObserved.add(messageId);
403415 changed = true;
404416
405- // Find the message to get its timestamp
406- const message = visibleMessages.find((m) => m.id === messageId);
407- if (message && message.timestamp > maxTimestamp) {
408- maxTimestamp = message.timestamp;
417+ // Get timestamp from precomputed map (O(1) instead of O(n))
418+ const timestamp = messageTimestampMap.get(messageId);
419+ if (timestamp !== undefined && timestamp > maxTimestamp) {
420+ maxTimestamp = timestamp;
409421 }
410422 }
411423 }
@@ -348,6 +348,8 @@
348 if (mentionUnreadCount !== 0) {348 if (mentionUnreadCount !== 0) {
349 mentionUnreadCount = 0;349 mentionUnreadCount = 0;
350 }350 }
351+ // Reset pending timestamp when user logs out
352+ pendingMentionTimestamp = null;
351 // mention observer reset353 // mention observer reset
352 if (mentionObserver) {354 if (mentionObserver) {
353 mentionObserver.disconnect();355 mentionObserver.disconnect();
@@ -357,16 +359,21 @@
357 }359 }
358 const mentionIds = new SvelteSet<string>();360 const mentionIds = new SvelteSet<string>();
359 const lastReadTimestamp = currentUser.lastReadMentionTimestamp ?? 0;361 const lastReadTimestamp = currentUser.lastReadMentionTimestamp ?? 0;
362+ const mentionIdsToPreObserve: string[] = [];
360 363
361 for (const message of visibleMessages) {364 for (const message of visibleMessages) {
362 if (shouldHighlightMention(message)) {365 if (shouldHighlightMention(message)) {
363 mentionIds.add(message.id);366 mentionIds.add(message.id);
364 // Pre-mark as observed if message is older than lastReadMentionTimestamp367 // Pre-mark as observed if message is older than lastReadMentionTimestamp
365 if (message.timestamp <= lastReadTimestamp && !mentionObservedIds.has(message.id)) {368 if (message.timestamp <= lastReadTimestamp && !mentionObservedIds.has(message.id)) {
366- mentionObservedIds = new SvelteSet([...mentionObservedIds, message.id]);369+ mentionIdsToPreObserve.push(message.id);
367 }370 }
368 }371 }
369 }372 }
373+ // Batch update observed IDs to avoid repeated Set allocations
374+ if (mentionIdsToPreObserve.length > 0) {
375+ mentionObservedIds = new SvelteSet([...mentionObservedIds, ...mentionIdsToPreObserve]);
376+ }
370 const nextMentionIds = mentionIds;377 const nextMentionIds = mentionIds;
371 if (!setsEqual(nextMentionIds, mentionMessageIds)) {378 if (!setsEqual(nextMentionIds, mentionMessageIds)) {
372 mentionMessageIds = nextMentionIds;379 mentionMessageIds = nextMentionIds;
@@ -386,6 +393,11 @@
386 if (mentionObserver) {393 if (mentionObserver) {
387 mentionObserver.disconnect();394 mentionObserver.disconnect();
388 }395 }
396+ // Precompute message timestamp map to avoid O(n) lookup in callback
397+ const messageTimestampMap = new Map<string, number>();
398+ for (const msg of visibleMessages) {
399+ messageTimestampMap.set(msg.id, msg.timestamp);
400+ }
389 mentionObserver = new IntersectionObserver(401 mentionObserver = new IntersectionObserver(
390 (entries) => {402 (entries) => {
391 const updatedObserved = new SvelteSet(mentionObservedIds);403 const updatedObserved = new SvelteSet(mentionObservedIds);
@@ -402,10 +414,10 @@
402 updatedObserved.add(messageId);414 updatedObserved.add(messageId);
403 changed = true;415 changed = true;
404 416
405- // Find the message to get its timestamp417+ // Get timestamp from precomputed map (O(1) instead of O(n))
406- const message = visibleMessages.find((m) => m.id === messageId);418+ const timestamp = messageTimestampMap.get(messageId);
407- if (message && message.timestamp > maxTimestamp) {419+ if (timestamp !== undefined && timestamp > maxTimestamp) {
408- maxTimestamp = message.timestamp;420+ maxTimestamp = timestamp;
409 }421 }
410 }422 }
411 }423 }
M src/routes/api/chat/mention-read/+server.ts +10 -0
@@ -32,6 +32,16 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
3232 });
3333 }
3434
35+ // Prevent far-future timestamps that could suppress notifications permanently
36+ const now = Date.now();
37+ const maxSkewMs = 5 * 60 * 1000; // 5 minutes clock skew tolerance
38+ if (timestamp > now + maxSkewMs) {
39+ return new Response(JSON.stringify({ error: 'Timestamp too far in the future' }), {
40+ status: 400,
41+ headers: { 'Content-Type': 'application/json' }
42+ });
43+ }
44+
3545 await users.updateLastReadMentionTimestamp(user.id, timestamp);
3646
3747 return new Response(JSON.stringify({ success: true }), {
@@ -32,6 +32,16 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
32 });32 });
33 }33 }
34 34
35+ // Prevent far-future timestamps that could suppress notifications permanently
36+ const now = Date.now();
37+ const maxSkewMs = 5 * 60 * 1000; // 5 minutes clock skew tolerance
38+ if (timestamp > now + maxSkewMs) {
39+ return new Response(JSON.stringify({ error: 'Timestamp too far in the future' }), {
40+ status: 400,
41+ headers: { 'Content-Type': 'application/json' }
42+ });
43+ }
44+
35 await users.updateLastReadMentionTimestamp(user.id, timestamp);45 await users.updateLastReadMentionTimestamp(user.id, timestamp);
36 46
37 return new Response(JSON.stringify({ success: true }), {47 return new Response(JSON.stringify({ success: true }), {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 8

↑↓ moveenter openesc close