Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: address space rule review feedback

Richie McIlroydf3efeab4143
diff --git a/apps/web/actions/organization/create-space.ts b/apps/web/actions/organization/create-space.ts
index 13f519de7..132bb383f 100644
--- a/apps/web/actions/organization/create-space.ts
+++ b/apps/web/actions/organization/create-space.ts
@@ -15,32 +15,12 @@ import {
} from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import {
+ getSpaceSettingsFromFormData,
+ hasProSpaceSettingsEnabled,
+} from "./space-settings";
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;
@@ -65,7 +45,7 @@ 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 settings = getSpaceSettingsFromFormData(formData);
const canUseProFeatures = userIsPro(user);
if (!name) {
@@ -89,7 +69,7 @@ export async function createSpace(
};
}
- if (!canUseProFeatures && hasProSettingsEnabled(settings)) {
+ if (!canUseProFeatures && hasProSpaceSettingsEnabled(settings)) {
return {
success: false,
error: "Upgrade required to change these viewer rules",
diff --git a/apps/web/actions/organization/space-settings.ts b/apps/web/actions/organization/space-settings.ts
new file mode 100644
index 000000000..22627f7b8
--- /dev/null
+++ b/apps/web/actions/organization/space-settings.ts
@@ -0,0 +1,36 @@
+export const spaceSettingKeys = [
+ "disableSummary",
+ "disableCaptions",
+ "disableChapters",
+ "disableReactions",
+ "disableTranscript",
+ "disableComments",
+] as const;
+
+export type SpaceSettingKey = (typeof spaceSettingKeys)[number];
+export type SpaceSettings = Partial<Record<SpaceSettingKey, boolean>>;
+
+export const proSpaceSettingKeys = [
+ "disableSummary",
+ "disableChapters",
+ "disableTranscript",
+] as const;
+
+export const getSpaceSettingsFromFormData = (formData: FormData) =>
+ Object.fromEntries(
+ spaceSettingKeys.map((key) => [key, formData.get(key) === "true"]),
+ ) as Record<SpaceSettingKey, boolean>;
+
+export const hasProSpaceSettingsEnabled = (
+ settings: Record<SpaceSettingKey, boolean>,
+) => proSpaceSettingKeys.some((key) => settings[key]);
+
+export const preserveProSpaceSettings = (
+ submittedSettings: Record<SpaceSettingKey, boolean>,
+ existingSettings: SpaceSettings | null | undefined,
+) => ({
+ ...submittedSettings,
+ ...Object.fromEntries(
+ proSpaceSettingKeys.map((key) => [key, existingSettings?.[key] ?? false]),
+ ),
+});
diff --git a/apps/web/actions/organization/update-space.ts b/apps/web/actions/organization/update-space.ts
index d8232ca4c..7af003907 100644
--- a/apps/web/actions/organization/update-space.ts
+++ b/apps/web/actions/organization/update-space.ts
@@ -17,38 +17,12 @@ import { and, eq } from "drizzle-orm";
import { Effect, Option } from "effect";
import { revalidatePath } from "next/cache";
import { runPromise } from "@/lib/server";
+import {
+ getSpaceSettingsFromFormData,
+ preserveProSpaceSettings,
+} from "./space-settings";
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 preserveProSettings = (
- submittedSettings: ReturnType<typeof getSettingsFromFormData>,
- existingSettings: (typeof spaces.$inferSelect)["settings"],
-) => ({
- ...submittedSettings,
- ...Object.fromEntries(
- proSettingKeys.map((key) => [key, existingSettings?.[key] ?? false]),
- ),
-});
-
export async function updateSpace(formData: FormData) {
const user = await getCurrentUser();
if (!user) return { success: false, error: "Unauthorized" };
@@ -89,14 +63,14 @@ export async function updateSpace(formData: FormData) {
return { success: false, error: "Unauthorized" };
}
- const submittedSettings = getSettingsFromFormData(formData);
+ const submittedSettings = getSpaceSettingsFromFormData(formData);
const canUseProFeatures = userIsPro(user);
const settings = canUseProFeatures
? submittedSettings
- : preserveProSettings(submittedSettings, space.settings);
+ : preserveProSpaceSettings(submittedSettings, space.settings);
const spaceUpdate: {
name: string;
- settings: ReturnType<typeof getSettingsFromFormData>;
+ settings: ReturnType<typeof getSpaceSettingsFromFormData>;
password?: string | null;
} = { name, settings };
diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx
index 8d5681c4a..91a52c00e 100644
--- a/apps/web/app/(org)/dashboard/caps/page.tsx
+++ b/apps/web/app/(org)/dashboard/caps/page.tsx
@@ -241,6 +241,14 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
const videoIds = videoData.map((video) => video.id);
const sharedSpacesMap =
await getSharedSpacesForVideos(videoIds).pipe(runPromise);
+ const [organizationSettingsRow] = user.activeOrganizationId
+ ? await db()
+ .select({ settings: organizations.settings })
+ .from(organizations)
+ .where(eq(organizations.id, user.activeOrganizationId))
+ .limit(1)
+ : [];
+ const organizationSettings = organizationSettingsRow?.settings ?? null;
const processedVideoData = await Effect.all(
videoData.map(
@@ -252,7 +260,7 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
const sharedSpaces = sharedSpacesMap[video.id] ?? [];
const rules = resolveEffectiveVideoRules({
videoSettings: video.settings,
- organizationSettings: null,
+ organizationSettings,
spaces: sharedSpaces.filter((space) => !space.isOrg),
});
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
index e4d1727d3..851168e2f 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
@@ -307,13 +307,19 @@ export default async function SharedCapsPage(props: {
const sharedSpacesMap = await fetchSharedSpacesForVideos(
spaceVideoData.map((video) => Video.VideoId.make(video.id)),
);
+ const [organizationSettingsRow] = await db()
+ .select({ settings: organizations.settings })
+ .from(organizations)
+ .where(eq(organizations.id, space.organizationId))
+ .limit(1);
+ const organizationSettings = organizationSettingsRow?.settings ?? null;
const processedVideoData = spaceVideoData.map((video) => {
const { effectiveDate: _effectiveDate, ...videoWithoutEffectiveDate } =
video;
const sharedSpaces = sharedSpacesMap[video.id] ?? [];
const rules = resolveEffectiveVideoRules({
videoSettings: video.settings,
- organizationSettings: null,
+ organizationSettings,
spaces: sharedSpaces,
});
return {
@@ -432,13 +438,19 @@ export default async function SharedCapsPage(props: {
const sharedSpacesMap = await fetchSharedSpacesForVideos(
orgVideoData.map((video) => Video.VideoId.make(video.id)),
);
+ const [organizationSettingsRow] = await db()
+ .select({ settings: organizations.settings })
+ .from(organizations)
+ .where(eq(organizations.id, organization.id))
+ .limit(1);
+ const organizationSettings = organizationSettingsRow?.settings ?? null;
const processedVideoData = orgVideoData.map((video) => {
const { effectiveDate: _effectiveDate, ...videoWithoutEffectiveDate } =
video;
const sharedSpaces = sharedSpacesMap[video.id] ?? [];
const rules = resolveEffectiveVideoRules({
videoSettings: video.settings,
- organizationSettings: null,
+ organizationSettings,
spaces: sharedSpaces,
});
return {
diff --git a/apps/web/lib/folder.ts b/apps/web/lib/folder.ts
index a0aa5c1a8..f855a36b1 100644
--- a/apps/web/lib/folder.ts
+++ b/apps/web/lib/folder.ts
@@ -18,7 +18,7 @@ import {
} from "@cap/web-backend";
import type { ImageUpload, Organisation, Space, Video } from "@cap/web-domain";
import { CurrentUser, Folder } from "@cap/web-domain";
-import { and, desc, eq, isNull } from "drizzle-orm";
+import { and, desc, eq, inArray, isNull } from "drizzle-orm";
import { sql } from "drizzle-orm/sql";
import { Effect } from "effect";
@@ -188,6 +188,7 @@ export const getVideosByFolderId = Effect.fn(function* (
metadata: videos.metadata,
duration: videos.duration,
settings: videos.settings,
+ orgId: videos.orgId,
totalComments: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'text' THEN ${comments.id} END)`,
totalReactions: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'emoji' THEN ${comments.id} END)`,
sharedOrganizations: sql<
@@ -248,6 +249,7 @@ export const getVideosByFolderId = Effect.fn(function* (
videos.metadata,
videos.duration,
videos.settings,
+ videos.orgId,
videos.password,
users.name,
)
@@ -257,6 +259,25 @@ export const getVideosByFolderId = Effect.fn(function* (
// Fetch shared spaces data for all videos
const videoIds = videoData.map((video) => video.id);
const sharedSpacesMap = yield* getSharedSpacesForVideos(videoIds);
+ const orgIds = Array.from(new Set(videoData.map((video) => video.orgId)));
+ const organizationSettingsRows =
+ orgIds.length > 0
+ ? yield* db.use((db) =>
+ db
+ .select({
+ id: organizations.id,
+ settings: organizations.settings,
+ })
+ .from(organizations)
+ .where(inArray(organizations.id, orgIds)),
+ )
+ : [];
+ const organizationSettingsById = Object.fromEntries(
+ organizationSettingsRows.map((organization) => [
+ organization.id,
+ organization.settings,
+ ]),
+ );
// Process the video data to match the expected format
const processedVideoData = yield* Effect.all(
@@ -265,7 +286,7 @@ export const getVideosByFolderId = Effect.fn(function* (
const sharedSpaces = sharedSpacesMap[video.id] ?? [];
const rules = resolveEffectiveVideoRules({
videoSettings: video.settings,
- organizationSettings: null,
+ organizationSettings: organizationSettingsById[video.orgId] ?? null,
spaces: sharedSpaces.filter((space) => !space.isOrg),
});
const resolvedSharedSpaces = yield* Effect.all(