@@ -116,7 +116,7 @@ export const STREAMS: StreamDef[] = [
116116 prefixes : [ 'om_' ] ,
117117 columns : [
118118 { name : 'id' , type : 'text' , path : 'id' } ,
119- { name : 'user_id' , type : 'text' , path : 'user_id' , index : true } ,
119+ { name : 'user_id' , type : 'text' , path : 'user_id' , index : true , references : 'users' } ,
120120 { name : 'organization_id' , type : 'text' , path : 'organization_id' , index : true } ,
121121 { name : 'status' , type : 'text' , path : 'status' } ,
122122 { name : 'role_slug' , type : 'text' , path : [ 'role' , 'slug' ] } ,
@@ -183,8 +183,8 @@ export const STREAMS: StreamDef[] = [
183183 { name : 'email' , type : 'text' , path : 'email' , index : true } ,
184184 { name : 'state' , type : 'text' , path : 'state' } ,
185185 { name : 'organization_id' , type : 'text' , path : 'organization_id' , index : true } ,
186- { name : 'inviter_user_id' , type : 'text' , path : 'inviter_user_id' } ,
187- { name : 'accepted_user_id' , type : 'text' , path : 'accepted_user_id' } ,
186+ { name : 'inviter_user_id' , type : 'text' , path : 'inviter_user_id' , references : 'users' } ,
187+ { name : 'accepted_user_id' , type : 'text' , path : 'accepted_user_id' , references : 'users' } ,
188188 { name : 'role_slug' , type : 'text' , path : [ 'role' , 'slug' ] } ,
189189 { name : 'expires_at' , type : 'timestamptz' , path : 'expires_at' } ,
190190 { name : 'accepted_at' , type : 'timestamptz' , path : 'accepted_at' } ,
@@ -251,7 +251,7 @@ export const STREAMS: StreamDef[] = [
251251 { name : 'event' , type : 'text' , path : 'event' , index : true } ,
252252 { name : 'type' , type : 'text' , path : 'type' } ,
253253 { name : 'status' , type : 'text' , path : 'status' } ,
254- { name : 'user_id' , type : 'text' , path : 'user_id' , index : true } ,
254+ { name : 'user_id' , type : 'text' , path : 'user_id' , index : true , references : 'users' } ,
255255 { name : 'email' , type : 'text' , path : 'email' , index : true } ,
256256 { name : 'ip_address' , type : 'text' , path : 'ip_address' } ,
257257 { name : 'user_agent' , type : 'text' , path : 'user_agent' } ,
@@ -271,7 +271,7 @@ export const STREAMS: StreamDef[] = [
271271 prefixes : [ 'session_' ] ,
272272 columns : [
273273 { name : 'id' , type : 'text' , path : 'id' } ,
274- { name : 'user_id' , type : 'text' , path : 'user_id' , index : true } ,
274+ { name : 'user_id' , type : 'text' , path : 'user_id' , index : true , references : 'users' } ,
275275 { name : 'organization_id' , type : 'text' , path : 'organization_id' , index : true } ,
276276 { name : 'status' , type : 'text' , path : 'status' } ,
277277 { name : 'auth_method' , type : 'text' , path : 'auth_method' } ,
@@ -591,6 +591,48 @@ export const STREAMS: StreamDef[] = [
591591 } ,
592592] satisfies CatalogStreamDef [ ]
593593
594+ /**
595+ * Whether `col` holds a user id: declared with `references: 'users'`, or
596+ * named `user_id`, which an `extraColumns` entry may be without declaring
597+ * anything, and which always counted.
598+ */
599+ const namesUser = ( col : ColumnDef ) : boolean => col . references === 'users' || col . name === 'user_id'
600+
601+ /**
602+ * The columns of `stream` that name a user, and the `users` stream's own
603+ * key: a membership's or sign-in's `user_id`, an invitation's inviter and
604+ * the user who accepted it, an extra column declared as referencing
605+ * `users`. `redact` erases a row that names the subject in any of them.
606+ */
607+ export function userReferenceColumns ( stream : StreamDef ) : ColumnDef [ ] {
608+ return stream . columns . filter ( ( c ) => namesUser ( c ) || ( stream . name === 'users' && stream . key . includes ( c . name ) ) )
609+ }
610+
611+ /**
612+ * The payload paths that name a user, each as its steps under the object:
613+ * `['user_id']`, or `['metadata', 'owner_user_id']` for a nested extra
614+ * column. Every stream of the catalog contributes, whether selected or
615+ * not, because the archive keeps every event type the tail subscribed to;
616+ * `streams` adds the extra columns of the selection. An event's `data`
617+ * carries the object, so `redact` scrubs archived and quarantined events
618+ * by `data` followed by each.
619+ */
620+ export function userReferencePaths ( streams : StreamDef [ ] ) : string [ ] [ ] {
621+ const seen = new Set < string > ( )
622+ const out : string [ ] [ ] = [ ]
623+ for ( const stream of [ ...STREAMS , ...streams ] ) {
624+ for ( const col of stream . columns ) {
625+ if ( ! namesUser ( col ) ) continue
626+ const path = Array . isArray ( col . path ) ? col . path : [ col . path ]
627+ const key = path . join ( '\u0000' )
628+ if ( seen . has ( key ) ) continue
629+ seen . add ( key )
630+ out . push ( path )
631+ }
632+ }
633+ return out
634+ }
635+
594636export const streamByName = ( name : string ) : StreamDef | undefined => STREAMS . find ( ( s ) => s . name === name )
595637
596638/**
0 commit comments