Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): persist viewer rules and password on space creation

Richie McIlroy1335fcd394fa
diff --git a/apps/web/actions/organization/create-space.ts b/apps/web/actions/organization/create-space.ts
index 439619535..13f519de7 100644
--- a/apps/web/actions/organization/create-space.ts
+++ b/apps/web/actions/organization/create-space.ts
@@ -2,8 +2,10 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
+import { hashPassword } from "@cap/database/crypto";
import { nanoId } from "@cap/database/helpers";
import { spaceMembers, spaces } from "@cap/database/schema";
+import { userIsPro } from "@cap/utils";
import {
type ImageUpload,
Space,
@@ -15,6 +17,30 @@ import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { uploadSpaceIcon } from "./upload-space-icon";
+const settingKeys = [
+ "disableSummary",
+ "disableCaptions",
+ "disableChapters",
+ "disableReactions",
+ "disableTranscript",
+ "disableComments",
+] as const;
+
+const getSettingsFromFormData = (formData: FormData) =>
+ Object.fromEntries(
+ settingKeys.map((key) => [key, formData.get(key) === "true"]),
+ );
+
+const proSettingKeys = [
+ "disableSummary",
+ "disableChapters",
+ "disableTranscript",
+] as const;
+
+const hasProSettingsEnabled = (
+ settings: ReturnType<typeof getSettingsFromFormData>,
+) => proSettingKeys.some((key) => settings[key]);
+
interface CreateSpaceResponse {
success: boolean;
spaceId?: string;
@@ -37,6 +63,10 @@ export async function createSpace(
}
const name = formData.get("name") as string;
+ const passwordEnabled = formData.get("passwordEnabled") === "true";
+ const password = formData.get("password") as string | null;
+ const settings = getSettingsFromFormData(formData);
+ const canUseProFeatures = userIsPro(user);
if (!name) {
return {
@@ -45,6 +75,27 @@ export async function createSpace(
};
}
+ if (passwordEnabled && !password?.trim()) {
+ return {
+ success: false,
+ error: "Space password is required",
+ };
+ }
+
+ if (!canUseProFeatures && passwordEnabled) {
+ return {
+ success: false,
+ error: "Upgrade required to protect a space with a password",
+ };
+ }
+
+ if (!canUseProFeatures && hasProSettingsEnabled(settings)) {
+ return {
+ success: false,
+ error: "Upgrade required to change these viewer rules",
+ };
+ }
+
// Check for duplicate space name in the same organization
const existingSpace = await db()
.select({ id: spaces.id })
@@ -67,6 +118,10 @@ export async function createSpace(
// Generate the space ID early so we can use it in the file path
const spaceId = Space.SpaceId.make(nanoId());
let iconUrl: ImageUpload.ImageUrlOrKey | null = null;
+ const hashedPassword =
+ passwordEnabled && password?.trim()
+ ? await hashPassword(password.trim())
+ : null;
await db().transaction(async (tx) => {
// Create the space first
@@ -76,6 +131,8 @@ export async function createSpace(
organizationId: user.activeOrganizationId,
createdById: user.id,
iconUrl: null,
+ settings,
+ password: hashedPassword,
});
// --- Member Management Logic ---