Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { registerMessagingHandlers } from './socket/messaging.js';
import { app } from './app.js';
import { redis as appRedis } from './lib/redis.js';
import { setSocketServer } from './lib/socket.js';
import { setOnline, setOffline, refreshPresence, isOnline } from './services/presence.js';
import {
cleanupStaleSockets,
reconcileBoot,
Expand Down Expand Up @@ -106,7 +107,6 @@ io.use(socketAuthMiddleware);

io.on('connection', async (socket: AuthSocket) => {
const userId = socket.auth!.userId;
const deviceId = socket.auth!.deviceId;
console.log('User connected:', userId, socket.id);

socket.data['userId'] = userId;
Expand Down Expand Up @@ -173,12 +173,6 @@ io.on('connection', async (socket: AuthSocket) => {
await socket.join(m.conversationId);
}

const user = await db.query.users.findFirst({
where: eq(users.id, userId),
columns: { presenceVisible: true },
});
const presenceVisible = user?.presenceVisible ?? true;

if (appRedis) {
await registerPresenceSocket(appRedis, userId, deviceId, socket.id);
await cleanupStaleSockets(io, appRedis, userId, socket.id);
Expand All @@ -187,7 +181,12 @@ io.on('connection', async (socket: AuthSocket) => {
if (becameOnline && presenceVisible) {
for (const m of memberships) {
io.to(m.conversationId).emit('user_online', { userId });
io.to(m.conversationId).emit('presence_update', { userId, online: true });
io.to(m.conversationId).emit('presence_update', {
userId,
online: true,
status: 'online',
lastSeen: Date.now(),
});
}
await recordPresenceForCoMembers(
userId,
Expand Down
16 changes: 16 additions & 0 deletions apps/backend/src/services/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ export async function refreshPresenceSocket(
await registerPresenceSocket(redis, userId, deviceId, socketId);
}

export async function setOnline(redis: Redis, userId: string, socketId: string): Promise<boolean> {
const key = presenceKey(userId);
const debounceKey = `presence_debounce:${userId}`;

const count = await redis.scard(key);
await redis.sadd(key, socketId);
await redis.expire(key, PRESENCE_TTL);

if (count === 0) {
const debouncing = await redis.del(debounceKey);
if (debouncing === 1) {
return false; // Flap detected, don't broadcast online
}
return true; // First socket connected
}
return false;
/**
* Remove a socket mapping. Returns true when that device has no remaining
* tracked sockets, so callers may safely remove the device-level presence entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ export function ConversationListSidebar() {
handleOffline(data.userId);
}

function onPresenceUpdate(data: { userId: string; online: boolean }) {
if (data.online) {
function onPresenceUpdate(data: { userId: string; online?: boolean; status?: 'online' | 'offline'; lastSeen?: number }) {
const isOnline = data.status ? data.status === 'online' : !!data.online;
if (isOnline) {
handleOnline(data.userId);
} else {
handleOffline(data.userId);
Expand Down