Barazo AppView backend
barazo.forum
1import {
2 pgTable,
3 pgPolicy,
4 text,
5 timestamp,
6 integer,
7 jsonb,
8 boolean,
9 index,
10 primaryKey,
11} from 'drizzle-orm/pg-core'
12import { sql } from 'drizzle-orm'
13import { appRole } from './roles.js'
14
15// ---------------------------------------------------------------------------
16// Global user preferences (stored in PostgreSQL for MVP, will sync to PDS later)
17// ---------------------------------------------------------------------------
18
19export const userPreferences = pgTable('user_preferences', {
20 did: text('did').primaryKey(),
21 maturityLevel: text('maturity_level', {
22 enum: ['sfw', 'mature'],
23 })
24 .notNull()
25 .default('sfw'),
26 declaredAge: integer('declared_age'),
27 mutedWords: jsonb('muted_words').$type<string[]>().notNull().default([]),
28 blockedDids: jsonb('blocked_dids').$type<string[]>().notNull().default([]),
29 mutedDids: jsonb('muted_dids').$type<string[]>().notNull().default([]),
30 crossPostBluesky: boolean('cross_post_bluesky').notNull().default(false),
31 crossPostFrontpage: boolean('cross_post_frontpage').notNull().default(false),
32 crossPostScopesGranted: boolean('cross_post_scopes_granted').notNull().default(false),
33 updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
34})
35
36// ---------------------------------------------------------------------------
37// Per-community preference overrides
38// ---------------------------------------------------------------------------
39
40export const userCommunityPreferences = pgTable(
41 'user_community_preferences',
42 {
43 did: text('did').notNull(),
44 communityDid: text('community_did').notNull(),
45 maturityOverride: text('maturity_override', {
46 enum: ['sfw', 'mature'],
47 }),
48 mutedWords: jsonb('muted_words').$type<string[]>(),
49 blockedDids: jsonb('blocked_dids').$type<string[]>(),
50 mutedDids: jsonb('muted_dids').$type<string[]>(),
51 notificationPrefs: jsonb('notification_prefs').$type<{
52 replies: boolean
53 reactions: boolean
54 mentions: boolean
55 modActions: boolean
56 }>(),
57 updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
58 },
59 (table) => [
60 primaryKey({ columns: [table.did, table.communityDid] }),
61 index('user_community_prefs_did_idx').on(table.did),
62 index('user_community_prefs_community_idx').on(table.communityDid),
63 pgPolicy('tenant_isolation', {
64 as: 'permissive',
65 to: appRole,
66 for: 'all',
67 using: sql`community_did = current_setting('app.current_community_did', true)`,
68 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
69 }),
70 ]
71).enableRLS()