Open History to see the last commit
1import { createRealtime, rawSql } from "@neon/realtime/server";
2import type { SceneRow, Viewer, Member } from "../shared/types";
3import type { Identity } from "./auth";
4export function realtime() {
5 const secret = process.env.REALTIME_SECRET;
6 if (!secret) throw new Error("REALTIME_SECRET is required");
7 return createRealtime({
8 secret,
9 db: decodeURIComponent(
10 new URL(process.env.DATABASE_URL!).pathname.slice(1),
11 ),
12 });
13}
14const access = `EXISTS(SELECT 1 FROM world_memberships m JOIN guest_sessions s ON s.guest=m.guest WHERE m.world=$1::uuid AND m.guest=$2::uuid AND m.active=1 AND s.id=$3::uuid AND s.revoked=0)`;
15export function sceneQuery(world: string, a: Identity) {
16 return rawSql<SceneRow>(
17 `
18 SELECT 'world'::text AS kind,w.id::text AS id,w.slug,w.title,w.generation,w.catalogue,w.status,
19 NULL::text AS key,NULL::text AS color,NULL::int AS x,NULL::int AS y,NULL::int AS z,NULL::int AS rotation,NULL::int AS revision,
20 NULL::text AS a,NULL::text AS b,NULL::text AS ca,NULL::text AS cb FROM worlds w WHERE w.id=$1::uuid AND ${access}
21 UNION ALL
22 SELECT 'part'::text,p.id::text,NULL::text,NULL::text,NULL::int,NULL::text,NULL::text,
23 p.key,p.color,p.x,p.y,p.z,p.rotation,p.revision,NULL::text,NULL::text,NULL::text,NULL::text FROM part_instances p WHERE p.world=$1::uuid AND p.deleted=0 AND ${access}
24 UNION ALL
25 SELECT 'connection'::text,e.id::text,NULL::text,NULL::text,NULL::int,NULL::text,NULL::text,
26 NULL::text,NULL::text,NULL::int,NULL::int,NULL::int,NULL::int,e.revision,e.a::text,e.b::text,e.ca,e.cb FROM part_connections e WHERE e.world=$1::uuid AND e.deleted=0 AND ${access}
27 `,
28 [world, a.guest, a.session],
29 );
30}
31export function presenceQuery(world: string, a: Identity) {
32 return rawSql<Viewer>(
33 `SELECT v.id::text,v.guest::text,g.name,g.color,v.selection,v.preview,v.cursor,v.generation,v.preview_at::text,v.cursor_at::text,v.expires_at::text FROM viewer_sessions v JOIN guests g ON g.id=v.guest WHERE v.world=$1::uuid AND ${access}`,
34 [world, a.guest, a.session],
35 );
36}
37export function historyQuery(world: string, a: Identity) {
38 return rawSql<{
39 undo: string | null;
40 redo: string | null;
41 generation: number;
42 }>(
43 `SELECT CASE WHEN h.generation=w.generation THEN h.undo->-1->>'id' ELSE NULL END AS undo,CASE WHEN h.generation=w.generation THEN h.redo->-1->>'id' ELSE NULL END AS redo,w.generation FROM guest_world_history h JOIN worlds w ON w.id=h.world WHERE h.world=$1::uuid AND h.guest=$2::uuid AND ${access}`,
44 [world, a.guest, a.session],
45 );
46}
47
48export function memberQuery(world: string, a: Identity) {
49 return rawSql<Member>(
50 `SELECT g.id::text,g.name,g.color,m.role,(w.owner=g.id) AS can_reset FROM world_memberships m JOIN guests g ON g.id=m.guest JOIN worlds w ON w.id=m.world WHERE m.world=$1::uuid AND m.guest=$2::uuid AND ${access}`,
51 [world, a.guest, a.session],
52 );
53}
54
55export function trustedRealtime(debugMode = false) {
56 if (!process.env.REALTIME_SECRET || !process.env.REALTIME_URL)
57 throw new Error("Trusted Realtime configuration is required");
58 return createRealtime({
59 secret: process.env.REALTIME_SECRET,
60 debugMode,
61 url: process.env.REALTIME_URL,
62 db: decodeURIComponent(
63 new URL(process.env.DATABASE_URL!).pathname.slice(1),
64 ),
65 });
66}