-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroomSync.ts
More file actions
396 lines (365 loc) · 13.2 KB
/
Copy pathroomSync.ts
File metadata and controls
396 lines (365 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import type { RealtimeChannel } from '@supabase/supabase-js';
import {
CURRENT_SCHEMA_VERSION,
isBrick,
sanitizeTitle,
validateBaseplateBounds,
type BaseplateBounds,
type Brick,
type BrickColor,
type BrickShape,
type Rotation,
} from '@brick/shared';
import { useEditorStore } from '../state/editorStore';
import { commandStack } from '../state/commandStack';
import { useToastStore } from '../state/toastStore';
import {
closePasswordPrompt,
requestPassword,
usePasswordPrompt,
} from '../state/passwordPromptStore';
import { supabase, type BrickRow, type RoomRow } from './supabase';
import { ensureAnonymousSession } from './auth';
import { checkMembership, rpcJoinRoom } from './roomPassword';
/**
* Room lifecycle:
* connect(id) → status='connecting' → hydrate from DB → status='connected'
* → subscribe to realtime → apply inbound events
* disconnect() → unsubscribe, clear roomId/status, leave scene as-is
*
* Outbound writes are fire-and-forget (see roomWrites.ts). Inbound events
* are applied via withRemoteApply() so the outbound wrapper can skip them.
*/
type SessionHandle = {
channel: RealtimeChannel;
roomId: string;
userId: string;
};
let session: SessionHandle | null = null;
export async function connectToRoom(roomId: string): Promise<boolean> {
const client = supabase;
if (!client) {
useToastStore.getState().show('Supabase not configured', 'error');
return false;
}
if (session?.roomId === roomId) return true;
if (session) await disconnectRoom();
const store = useEditorStore.getState();
store.setRoomStatus('connecting');
// 0) Ensure we have an anonymous auth session. RLS policies + RPC execute
// grants are keyed to `authenticated`, so every request needs a JWT.
const userId = await ensureAnonymousSession();
if (!userId) {
store.setRoomStatus('error');
useToastStore.getState().show('Sign-in failed — check your connection', 'error');
return false;
}
// 1) Ensure the room exists. First visitor seeds it with the current local
// state; subsequent visitors just read.
const existing = await client.from('rooms').select('*').eq('id', roomId).maybeSingle();
let roomRow: RoomRow | null = (existing.data as RoomRow | null) ?? null;
if (!roomRow) {
const current = store.serializeCreation();
const insert = await client
.from('rooms')
.insert({
id: roomId,
title: current.title,
baseplate_bounds: current.baseplateBounds,
})
.select('*')
.single();
if (insert.error || !insert.data) {
console.warn('[room] create failed:', insert.error);
store.setRoomStatus('error');
useToastStore.getState().show('Could not create room', 'error');
return false;
}
roomRow = insert.data as RoomRow;
// Seed the room with any bricks currently sitting in the local scene so
// "start sharing a work-in-progress" preserves the player's draft.
const locals = Array.from(store.bricks.values());
if (locals.length > 0) {
const rows = locals.map((b) => brickToRow(b, roomId));
const bulk = await client.from('bricks').insert(rows);
if (bulk.error) console.warn('[room] seed bricks failed:', bulk.error);
}
}
// At this point roomRow is non-null — TypeScript needs a final assert
// because the narrow above is inside a conditional branch.
if (!roomRow) {
store.setRoomStatus('error');
return false;
}
// 1.5) Password gate. Room-metadata SELECT is allowed for any authenticated
// user, but bricks + writes are blocked unless we're in room_members. Prompt
// until the user enters the right password or cancels.
if (roomRow.password_hash) {
const alreadyMember = await checkMembership(roomId, userId);
if (!alreadyMember) {
const ok = await promptUntilJoined(roomId);
if (!ok) {
store.setRoomStatus('idle');
return false;
}
}
}
// 2) Hydrate the scene from room metadata + all its bricks.
const bricksRes = await client.from('bricks').select('*').eq('room_id', roomId);
if (bricksRes.error) {
console.warn('[room] fetch bricks failed:', bricksRes.error);
store.setRoomStatus('error');
useToastStore.getState().show('Could not load room', 'error');
return false;
}
const rawRows = (bricksRes.data as BrickRow[] | null) ?? [];
// Realtime and REST responses are untrusted — filter out anything that
// doesn't match our schema. A malicious row shouldn't poison the scene.
const hydrated: Brick[] = [];
for (const row of rawRows) {
const brick = rowToBrickSafe(row);
if (brick) hydrated.push(brick);
}
const snapshotTitle = sanitizeTitle(roomRow.title) ?? 'Untitled Creation';
const snapshotBounds = validateBaseplateBounds(roomRow.baseplate_bounds) ?? store.baseplateBounds;
const snapshotCreated = Date.parse(roomRow.created_at) || Date.now();
store.withRemoteApply(() => {
useEditorStore.getState().loadCreation({
version: CURRENT_SCHEMA_VERSION,
title: snapshotTitle,
createdAt: snapshotCreated,
bricks: hydrated,
baseplateBounds: snapshotBounds,
});
});
commandStack.clear();
// 3) Subscribe to realtime changes filtered by room_id.
// Payloads are validated against the shared schema before being applied —
// never trust peer data directly, even though RLS gates writes server-side.
const channel = client
.channel(`room:${roomId}`)
.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'bricks', filter: `room_id=eq.${roomId}` },
(payload) => applyBrickInsert(payload.new),
)
.on(
'postgres_changes',
{ event: 'DELETE', schema: 'public', table: 'bricks', filter: `room_id=eq.${roomId}` },
(payload) => applyBrickDelete((payload.old as { id?: unknown }).id),
)
.on(
'postgres_changes',
{ event: 'UPDATE', schema: 'public', table: 'bricks', filter: `room_id=eq.${roomId}` },
(payload) => applyBrickUpdate(payload.new),
)
.on(
'postgres_changes',
{ event: 'UPDATE', schema: 'public', table: 'rooms', filter: `id=eq.${roomId}` },
(payload) => applyRoomUpdate(payload.new),
);
await new Promise<void>((resolve) => {
channel.subscribe((status) => {
if (
status === 'SUBSCRIBED' ||
status === 'CHANNEL_ERROR' ||
status === 'TIMED_OUT' ||
status === 'CLOSED'
) {
resolve();
}
});
});
session = { channel, roomId, userId };
store.setRoomId(roomId);
store.setRoomStatus('connected');
store.setRoomPasswordState(roomRow.password_hash !== null, roomRow.password_set_at);
useToastStore.getState().show(`Joined room ${roomId}`, 'success');
return true;
}
/**
* Loops the password prompt until the RPC accepts a guess or the user
* cancels. Wrong attempts keep the modal open with an error; a cancel
* during an in-flight RPC is caught by the "still open" guard so we don't
* spuriously re-open the prompt.
*/
async function promptUntilJoined(roomId: string): Promise<boolean> {
for (;;) {
const password = await requestPassword(roomId);
if (password === null) {
closePasswordPrompt();
return false;
}
const ok = await rpcJoinRoom(roomId, password);
if (ok) {
closePasswordPrompt();
return true;
}
// User cancelled while the RPC was running — stop looping.
if (!usePasswordPrompt.getState().open) return false;
usePasswordPrompt.getState().setError('Wrong password');
}
}
export async function disconnectRoom(): Promise<void> {
if (!session) return;
const ch = session.channel;
session = null;
await ch.unsubscribe();
const store = useEditorStore.getState();
store.setRoomId(null);
store.setRoomStatus('idle');
store.setRoomPasswordState(false, null);
}
export function currentRoomId(): string | null {
return session?.roomId ?? null;
}
// -------------------- Event appliers --------------------
function applyBrickInsert(raw: unknown): void {
const brick = rowToBrickSafe(raw);
if (!brick) return;
const store = useEditorStore.getState();
if (store.bricks.has(brick.id)) return; // our own echo
store.withRemoteApply(() => {
useEditorStore.getState().restoreBrick(brick);
});
}
function applyBrickUpdate(raw: unknown): void {
const brick = rowToBrickSafe(raw);
if (!brick) return;
const store = useEditorStore.getState();
const existing = store.bricks.get(brick.id);
if (!existing) {
// Update for a brick we don't have locally — treat as late-
// arriving insert so we stay in sync.
store.withRemoteApply(() => {
useEditorStore.getState().restoreBrick(brick);
});
return;
}
// Already identical (our own echo, or a no-op)?
if (
existing.gx === brick.gx &&
existing.gy === brick.gy &&
existing.gz === brick.gz &&
existing.rotation === brick.rotation &&
existing.color === brick.color &&
existing.shape === brick.shape &&
(existing.transparent === true) === (brick.transparent === true)
) {
return;
}
store.withRemoteApply(() => {
useEditorStore.getState().updateBrick(brick.id, {
shape: brick.shape,
color: brick.color,
gx: brick.gx,
gy: brick.gy,
gz: brick.gz,
rotation: brick.rotation,
transparent: brick.transparent === true,
});
});
}
function applyBrickDelete(id: unknown): void {
if (typeof id !== 'string' || id.length === 0 || id.length > 64) return;
const store = useEditorStore.getState();
if (!store.bricks.has(id)) return; // already gone locally
store.withRemoteApply(() => {
useEditorStore.getState().removeBrickById(id);
});
}
function applyRoomUpdate(raw: unknown): void {
if (!raw || typeof raw !== 'object') return;
const row = raw as Partial<RoomRow>;
const store = useEditorStore.getState();
const nextTitle = sanitizeTitle(row.title);
if (nextTitle !== null && store.title !== nextTitle) {
store.withRemoteApply(() => {
useEditorStore.getState().setTitle(nextTitle);
});
}
const remote = validateBaseplateBounds(row.baseplate_bounds);
if (remote) {
const local = store.baseplateBounds;
if (
local.minGx !== remote.minGx ||
local.maxGx !== remote.maxGx ||
local.minGz !== remote.minGz ||
local.maxGz !== remote.maxGz
) {
store.withRemoteApply(() => {
useEditorStore.setState({ baseplateBounds: remote });
});
}
}
// Password rotation = kick signal. Re-check membership; if we were removed
// while the password is still set, the modal comes up again. Pre-existing
// members keep their access through the rotation.
const nextPwSetAt = typeof row.password_set_at === 'string' ? row.password_set_at : null;
const nextHasPw = row.password_hash !== null && row.password_hash !== undefined;
if (nextPwSetAt !== store.roomPasswordSetAt) {
store.setRoomPasswordState(nextHasPw, nextPwSetAt);
void handlePasswordRotation(nextHasPw);
}
}
/**
* Triggered when the room's password_set_at timestamp changes. If the room
* is still protected we re-check our membership; losing it means we've been
* kicked and must re-authenticate. A rotation that simply removes the
* password leaves our session intact.
*/
async function handlePasswordRotation(hasPassword: boolean): Promise<void> {
const current = session;
if (!current) return;
if (!hasPassword) return; // password was removed; still in the room, no action.
const stillMember = await checkMembership(current.roomId, current.userId);
if (stillMember) return;
// We've been removed. Unsubscribe + clear scene, then loop the prompt.
const roomId = current.roomId;
useToastStore.getState().show('Room password changed — re-enter to rejoin', 'info');
await disconnectRoom();
void connectToRoom(roomId);
}
// -------------------- Row <-> Brick conversions --------------------
/**
* Coerce an untrusted row into a Brick. Returns null for anything that
* doesn't pass the shared-schema validator — colour outside the palette,
* unknown shape, non-finite coords, etc. Protects against a peer that
* ignores the client and writes garbage straight to the DB.
*/
function rowToBrickSafe(raw: unknown): Brick | null {
if (!raw || typeof raw !== 'object') return null;
const row = raw as Partial<BrickRow>;
const candidate = {
id: row.id,
shape: row.shape as BrickShape | undefined,
color: row.color as BrickColor | undefined,
gx: row.gx,
gy: row.gy,
gz: row.gz,
rotation: row.rotation as Rotation | undefined,
transparent: row.transparent === true ? true : undefined,
};
return isBrick(candidate) ? candidate : null;
}
/**
* Row payload for INSERT. `created_at` is deliberately omitted so the DB
* `default now()` fills it — passing an empty string makes Postgres try
* to parse '' as a timestamptz and explode with SQLSTATE 22007.
*/
export function brickToRow(brick: Brick, roomId: string): Omit<BrickRow, 'created_at'> {
return {
id: brick.id,
room_id: roomId,
shape: brick.shape,
color: brick.color,
gx: brick.gx,
gy: brick.gy,
gz: brick.gz,
rotation: brick.rotation,
transparent: brick.transparent === true,
};
}
export function boundsMatch(a: BaseplateBounds, b: BaseplateBounds): boolean {
return a.minGx === b.minGx && a.maxGx === b.maxGx && a.minGz === b.minGz && a.maxGz === b.maxGz;
}