fix: harden data integrity, connection stability, and memory management
Wrap all multi-write DB operations in atomic transactions (server/channel creation, message+attachment linking, DM creation, friend acceptance, cascading deletes) to prevent partial-write corruption. Batch N+1 queries in WS ready payload into O(1) bulk fetches with chunked inArray() to respect SQLite's variable limit. Fix chat history regression where background WS messages bypassed channel load by switching the guard from messages.has() to hasMore.has(). Add LRU channel eviction (20 cached, evict to 15) and per-channel message cap (200) to bound client memory growth. Shorten WS heartbeat from 30s to 15s for aggressive proxy/NAT environments. Clear all user-scoped stores on logout to prevent cross-session data leaks. Extract LiveKit internal accessors into shared livekitInternals utility.
This commit is contained in:
@@ -207,15 +207,22 @@ export async function socialRoutes(app: FastifyInstance): Promise<void> {
|
||||
}
|
||||
|
||||
if (status === 'accepted') {
|
||||
// Add to friends table
|
||||
// Insert friend and update request status atomically
|
||||
const now = Date.now();
|
||||
db.insert(schema.friends).values({
|
||||
userId: friendRequest.fromId,
|
||||
friendId: friendRequest.toId,
|
||||
createdAt: now,
|
||||
}).run();
|
||||
db.transaction((tx) => {
|
||||
tx.insert(schema.friends).values({
|
||||
userId: friendRequest.fromId,
|
||||
friendId: friendRequest.toId,
|
||||
createdAt: now,
|
||||
}).run();
|
||||
|
||||
// Get the accepting user's data for the WS event
|
||||
tx.update(schema.friendRequests)
|
||||
.set({ status })
|
||||
.where(eq(schema.friendRequests.id, id))
|
||||
.run();
|
||||
});
|
||||
|
||||
// WS broadcast AFTER transaction commits
|
||||
const acceptingUser = db.select().from(schema.users).where(eq(schema.users.id, request.userId)).get();
|
||||
if (acceptingUser) {
|
||||
const friend: Friend = {
|
||||
@@ -228,14 +235,14 @@ export async function socialRoutes(app: FastifyInstance): Promise<void> {
|
||||
requestId: id,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// For declined, just update the status (single write, no transaction needed)
|
||||
db.update(schema.friendRequests)
|
||||
.set({ status })
|
||||
.where(eq(schema.friendRequests.id, id))
|
||||
.run();
|
||||
}
|
||||
|
||||
// Update request status
|
||||
db.update(schema.friendRequests)
|
||||
.set({ status })
|
||||
.where(eq(schema.friendRequests.id, id))
|
||||
.run();
|
||||
|
||||
return reply.code(200).send({ success: true });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user