feat: forum, polymorphic comments, custom tiers, multi-invite, secrets encryption
Big feature batch driven by the super-audit findings and the user's follow-up requests. Schema migration 021 (folded into schema.sql Section 13): - forum_threads + polymorphic comments table keyed on (entity_type, entity_id). Entity types: forum_thread, proposal, incident, policy. Trigger maintains forum_threads.comment_count and last_comment_at. - space_tiers per-space with monthly_price_cents, billing_cadence, is_system, sort_order. Trigger seeds plus/basic/associate on every space INSERT and backfills existing spaces. space_members.tier_id FK added and backfilled from the legacy enum value. - space_role_labels (rename/recolor built-in roles per space). space_custom_roles + space_member_custom_roles for non-privileged extra labels. RLS gates by admin/board. - space_invites: multi-code invites with expires_at, max_uses, uses_count, is_enabled. Legacy spaces.invite_code backfilled as a permanent enabled invite. - secrets.encrypted_value bytea + encryption_version. Secrets RLS tightened. New comms_channels INSERT policy: any member can create; default channels are protected from deletion. - knowledge_base.render_markdown boolean (default true). Secrets encryption-at-rest: - AES-256-GCM in lib/secrets/crypto.ts. Master key from SECRETS_ENCRYPTION_KEY env (64 hex chars). - New revealSecret(id) server action: only path to plaintext, writes secret.revealed to activity_log. - /ops/page.tsx no longer selects value or encrypted_value on the list endpoint - the original screenshot bug where ciphertext was already in the page bundle before "Reveal" is fixed. UI: - /forum index, /forum/new, /forum/[id]. Pin/lock/delete actions for admin/board. - CommentThread component embedded on /proposals/[id], /incidents/[id], /policies/[slug]. Markdown rendering via react-markdown + remark-gfm. - /comms sidebar: inline "+ New channel" form. - /ops KB: row click opens markdown-rendering viewer modal. - /settings: new Tiers tab and Invites tab. Inline price edit and archive/delete on tiers; copy/enable/disable/delete on invites. - AtlasLogo replaces the terminal icon on the landing nav and footer. public/logo.svg wired as the favicon in app/layout.tsx metadata. - Sidebar has a Forum nav link. Server actions added: - lib/actions/forum.ts, comms.ts, tiers.ts, roles.ts, invites.ts. - lib/actions/secrets.ts rewritten with updateSecret + revealSecret. Auth flow: - joinSpace honors codes from space_invites first (enabled, expiry, use cap), falls back to legacy spaces.invite_code, increments uses_count on success. Env: - SECRETS_ENCRYPTION_KEY documented in .env.example and set on the Droplet's /opt/hackerspace-ops/.env.production. README: live site link added (https://hackerspace.sh). 266/266 unit tests pass. Production build clean. Migration 021 will be applied on the next deploy via /opt/hackerspace-ops/deploy.sh's idempotent migration runner.
This commit is contained in:
+37
-1
@@ -107,10 +107,38 @@ export async function joinSpace(formData: {
|
||||
// Use admin client for operations
|
||||
const admin = createAdminClient()
|
||||
|
||||
const code = formData.inviteCode.trim().toUpperCase()
|
||||
|
||||
// Try the new multi-code invites table first. Falls back to the legacy
|
||||
// spaces.invite_code (a permanent default) if the code isn't found there.
|
||||
let invite: { id: string; space_id: string; max_uses: number | null; uses_count: number; expires_at: string | null; is_enabled: boolean } | null = null
|
||||
const { data: inviteRow } = await admin
|
||||
.from('space_invites')
|
||||
.select('id, space_id, max_uses, uses_count, expires_at, is_enabled')
|
||||
.eq('code', code)
|
||||
.maybeSingle()
|
||||
if (inviteRow) invite = inviteRow as typeof invite
|
||||
|
||||
let spaceId: string | null = null
|
||||
if (invite) {
|
||||
if (!invite.is_enabled) return { error: 'This invite is disabled.' }
|
||||
if (invite.expires_at && new Date(invite.expires_at) < new Date()) return { error: 'This invite has expired.' }
|
||||
if (invite.max_uses !== null && invite.uses_count >= invite.max_uses) return { error: 'This invite has reached its use cap.' }
|
||||
spaceId = invite.space_id
|
||||
} else {
|
||||
const { data: legacy } = await admin
|
||||
.from('spaces')
|
||||
.select('id')
|
||||
.eq('invite_code', code)
|
||||
.maybeSingle()
|
||||
if (!legacy) return { error: 'Invalid invite code' }
|
||||
spaceId = legacy.id
|
||||
}
|
||||
|
||||
const { data: space, error: lookupErr } = await admin
|
||||
.from('spaces')
|
||||
.select('id, require_approval')
|
||||
.eq('invite_code', formData.inviteCode.trim().toUpperCase())
|
||||
.eq('id', spaceId)
|
||||
.single()
|
||||
|
||||
if (lookupErr || !space) return { error: 'Invalid invite code' }
|
||||
@@ -130,6 +158,14 @@ export async function joinSpace(formData: {
|
||||
|
||||
if (memberErr) return { error: memberErr.message }
|
||||
|
||||
// Increment invite usage counter for the multi-code path.
|
||||
if (invite) {
|
||||
await admin
|
||||
.from('space_invites')
|
||||
.update({ uses_count: invite.uses_count + 1 })
|
||||
.eq('id', invite.id)
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user