Skip to content
Open
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
2 changes: 1 addition & 1 deletion Architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ Two sweeps keep the keys honest without a disconnect:
- `clearPresenceState()` (`utils/redis.ts`) at startup (before accepting connections) and at graceful shutdown: as the sole node a full wipe, with live peers a scoped reap of the sockets missing from the cluster's adapter snapshot.
- `utils/presenceReaper.ts` every 60 seconds while serving, under the shared cluster lock: the same scoped reap, plus a reconciliation of the users themselves (one the DB column or `online_users` calls online with no live socket registered is taken offline too), followed by a `presence:update` offline to every server and DM room of each user it took offline, unless they registered a new socket by then.

Voice occupancy (`voice:channel_users`) is replayed to a socket on connect and to a member on joining a server (`replayVoiceState()`, stamped `source: 'connect'`), for the channels they can view; the client clears its lists on reconnect because the replay only names channels that have occupants, and ignores the snapshot for the channel it is re-joining itself, whose join answers with a fresher list.
Voice occupancy (`voice:channel_users`) is replayed to a socket on connect and to a member on joining a server (`replayVoiceState()`, stamped `source: 'connect'`), for the channels they can view; the client clears its lists on reconnect because the replay only names channels that have occupants, and ignores the snapshot for the channel it is re-joining itself, whose join answers with a fresher list. Every join, leave and reap of a channel's Redis mirror also assigns the channel's occupancy version in the same MULTI: a Lua script takes Redis's clock in microseconds and forces it above the value stored for the channel (`voice:channel:version:{id}`, kept a week after the channel's last change and never deleted with its state). `voice:user_joined`, `voice:user_left` and `voice:channel_users` carry it as the optional `occupancyVersion`: a snapshot carries the version it was read at, the list a joiner receives the version just below its own join. The client keeps, per channel, the version of the last list applied and each user's latest event since (`stores/voiceOccupancy.ts`): an event the list already reflects, or a list older than the one shown, changes nothing, and a newer list leaves the users whose latest event is newer than it as they are, so a snapshot read before a join or a leave never undoes it. An event or a list without a version applies as it did before versions.

### Email System

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
// @vitest-environment node
// A source scan, like mainLayoutBansWiring.test.ts: MainLayout's listener
// effect is mounted by no component test.
// A source scan of the wiring, like mainLayoutBansWiring.test.ts. The three
// voice occupancy handlers MainLayout registers live in
// services/voiceOccupancySocketHandlers.ts (their behaviour, with the real
// voice store: __tests__/services/voiceOccupancySocketHandlers.test.ts).
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

const LAYOUT = fileURLToPath(new URL('../../components/layout/MainLayout.tsx', import.meta.url));
const HANDLERS = fileURLToPath(new URL('../../services/voiceOccupancySocketHandlers.ts', import.meta.url));
const source = readFileSync(LAYOUT, 'utf8');
const handlers = readFileSync(HANDLERS, 'utf8');

function voiceUserLeftHandler(): string {
const start = source.indexOf('voiceUserLeft:');
expect(start, 'voiceUserLeft handler not found in MainLayout').toBeGreaterThan(-1);
const end = source.indexOf('voiceStateUpdate:', start);
expect(end, 'end of voiceUserLeft handler not found in MainLayout').toBeGreaterThan(start);
return source.slice(start, end);
const start = handlers.indexOf('userLeft:');
expect(start, 'userLeft handler not found').toBeGreaterThan(-1);
const end = handlers.indexOf('};', start);
expect(end, 'end of userLeft handler not found').toBeGreaterThan(start);
return handlers.slice(start, end);
}

describe('MainLayout hands a voice departure\'s epoch to the store', () => {
it('passes the epoch on, and a departure the store ignores ends the handler before the leave sound', () => {
const handler = voiceUserLeftHandler();
expect(handler).toMatch(/\{\s*channelId,\s*userId,\s*epoch\s*\}/);
const ignored = handler.search(/removeUserFromChannel\(channelId, userId, epoch\) === false\) return;/);
expect(handler).toMatch(/\{\s*channelId,\s*userId,\s*epoch,\s*occupancyVersion\s*\}/);
const ignored = handler.search(/removeUserFromChannel\(channelId, userId, epoch, occupancyVersion\) === false\) return;/);
expect(ignored, 'the store\'s answer is not checked').toBeGreaterThan(-1);
const sound = handler.indexOf('playLeaveSound()');
expect(sound).toBeGreaterThan(ignored);
// The store is asked once
expect(handler.match(/removeUserFromChannel\(/g) ?? []).toHaveLength(1);
});

it('a deleted space\'s voice lists go with what they reflected (stores/voiceOccupancy.ts)', () => {
const start = source.indexOf('serverDeleted:');
expect(start, 'serverDeleted handler not found in MainLayout').toBeGreaterThan(-1);
const handler = source.slice(start, source.indexOf('announcementInit:', start));
const cleared = handler.indexOf('useVoiceStore.setState({ channelUsers: newChannelUsers });');
const forgotten = handler.indexOf('forgetOccupancyVersions(voiceChannelIds);');
expect(cleared).toBeGreaterThan(-1);
expect(forgotten).toBeGreaterThan(cleared);
});

it('the event table routes voice:user_left to that handler', () => {
expect(source).toMatch(/\['voice:user_left', handlers\.voiceUserLeft\]/);
expect(source).toMatch(/voiceUserLeft: voiceOccupancySocketHandlers\.userLeft,/);
expect(source).toMatch(/\['voice:user_joined', handlers\.voiceUserJoined\]/);
expect(source).toMatch(/voiceUserJoined: voiceOccupancySocketHandlers\.userJoined,/);
expect(source).toMatch(/\['voice:channel_users', handlers\.voiceChannelUsers\]/);
expect(source).toMatch(/voiceChannelUsers: voiceOccupancySocketHandlers\.channelUsers,/);
});
});
12 changes: 12 additions & 0 deletions apps/desktop/src/__tests__/e2e/secureVoiceKeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,18 @@ describe('inbound keys', () => {
expect(frameSession.setRemoteKey).toHaveBeenCalledWith('peer', 0, expect.any(Uint8Array));
});

it('an announcement of the session already held changes nothing, however it is handed: no vet, no key, no rotation', async () => {
await withVettedPeer();
const watched = [fetchChannelDeviceLists, fetchDeviceList, encryptToDevice, socketEmit, frameSession.setLocalKey, frameSession.removeRemote];
for (const mock of watched) mock.mockClear();

onParticipantJoined(CH, { id: 'peer', deviceId: PEER_DEVICE, epoch: PEER_EPOCH }, { initialReplay: true });
onParticipantJoined(CH, { id: 'peer', deviceId: PEER_DEVICE, epoch: PEER_EPOCH }, { initialReplay: false });
await flush();

for (const mock of watched) expect(mock).not.toHaveBeenCalled();
});

it('drops keys with mismatched binding fields (scope, sender, device)', async () => {
await withVettedPeer();
for (const bad of [
Expand Down
Loading
Loading