Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web): add space collection visibility action
Richie McIlroy79caae5af55b
diff --git a/apps/web/actions/collections/visibility.ts b/apps/web/actions/collections/visibility.tsnew file mode 100644index 000000000..b5871c6bc--- /dev/null+++ b/apps/web/actions/collections/visibility.ts@@ -0,0 +1,98 @@+"use server";++import { db } from "@cap/database";+import { getCurrentUser } from "@cap/database/auth/session";+import { spaces } from "@cap/database/schema";+import { PublicCollection, Space } from "@cap/web-domain";+import { eq, type SQL, sql } from "drizzle-orm";+import { Either, Schema } from "effect";+import { revalidatePath } from "next/cache";+import { isOrganizationOwnerPro } from "@/lib/org-pro";+import { requireSpaceManager } from "../organization/space-authorization";++const decodeSettingsPatch = Schema.decodeUnknownEither(+ PublicCollection.PublicPageSettingsUpdate,+);++/**+ * Toggles a space's public collection link and/or its public-page presentation+ * settings from the dashboard space page. Enabling public (or customizing the+ * page) requires the org owner to be on Pro; un-publishing is always allowed.+ *+ * `settings` is a partial patch merged into the stored `settings.publicPage`,+ * mirroring the folder path (FolderUpdate RPC).+ */+export async function setSpaceCollectionVisibility(input: {+ spaceId: string;+ public?: boolean;+ settings?: PublicCollection.PublicPageSettingsUpdate;+}) {+ const user = await getCurrentUser();+ if (!user) return { success: false, error: "Unauthorized" };++ // Server actions are publicly callable; the TS parameter types are+ // compile-time only, so validate everything before it reaches the database.+ if (typeof input.spaceId !== "string" || input.spaceId.length === 0) {+ return { success: false, error: "Invalid request" };+ }+ if (input.public !== undefined && typeof input.public !== "boolean") {+ return { success: false, error: "Invalid request" };+ }++ let settingsPatch: PublicCollection.PublicPageSettingsUpdate | undefined;+ if (input.settings !== undefined) {+ const decoded = decodeSettingsPatch(input.settings);+ if (Either.isLeft(decoded)) {+ return { success: false, error: "Invalid public page settings" };+ }+ settingsPatch = decoded.right;+ }++ const id = Space.SpaceId.make(input.spaceId);++ const [[space], access] = await Promise.all([+ db()+ .select({+ organizationId: spaces.organizationId,+ public: spaces.public,+ })+ .from(spaces)+ .where(eq(spaces.id, id))+ .limit(1),+ requireSpaceManager(user.id, id).catch(() => null),+ ]);++ if (!space) return { success: false, error: "Space not found" };+ if (!access) return { success: false, error: "Unauthorized" };++ const enablingPublic = input.public === true && !space.public;+ const changingSettings = settingsPatch !== undefined;++ if (+ (enablingPublic || changingSettings) &&+ !(await isOrganizationOwnerPro(space.organizationId))+ ) {+ return {+ success: false,+ error: "Upgrade to Cap Pro to create a public collection link",+ };+ }++ const update: { public?: boolean; settings?: SQL } = {};+ if (input.public !== undefined) update.public = input.public;+ if (settingsPatch !== undefined) {+ // Atomic merge so concurrent patches (and the logo upload action, which+ // also writes settings.publicPage) can't overwrite each other's keys.+ update.settings = sql`JSON_MERGE_PATCH(COALESCE(${spaces.settings}, '{}'), CAST(${JSON.stringify(+ { publicPage: settingsPatch },+ )} AS JSON))`;+ }++ if (Object.keys(update).length > 0)+ await db().update(spaces).set(update).where(eq(spaces.id, id));++ revalidatePath("/dashboard");+ revalidatePath(`/dashboard/spaces/${id}`);+ revalidatePath(`/c/${id}`);+ return { success: true };+}