Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web-backend): add effective video rules resolution helpers

Richie McIlroy4476bc53ee47
diff --git a/packages/web-backend/src/Videos/EffectiveVideoRules.ts b/packages/web-backend/src/Videos/EffectiveVideoRules.ts
new file mode 100644
index 000000000..e276d3c6a
--- /dev/null
+++ b/packages/web-backend/src/Videos/EffectiveVideoRules.ts
@@ -0,0 +1,102 @@
+export type ViewerSettingKey =
+ | "disableSummary"
+ | "disableCaptions"
+ | "disableChapters"
+ | "disableReactions"
+ | "disableTranscript"
+ | "disableComments";
+
+export type ViewerSettings = Partial<Record<ViewerSettingKey, boolean>>;
+
+export type SpaceRuleInput = {
+ id: string;
+ name: string;
+ settings?: ViewerSettings | null;
+ hasPassword?: boolean;
+ password?: string | null;
+};
+
+export type SpaceRuleSource = {
+ id: string;
+ name: string;
+};
+
+export type EffectiveVideoRules = {
+ settings: Required<ViewerSettings>;
+ inheritedSettings: Partial<Record<ViewerSettingKey, SpaceRuleSource[]>>;
+ inheritedPasswordSources: SpaceRuleSource[];
+ hasInheritedPassword: boolean;
+};
+
+const settingKeys: ViewerSettingKey[] = [
+ "disableSummary",
+ "disableCaptions",
+ "disableChapters",
+ "disableReactions",
+ "disableTranscript",
+ "disableComments",
+];
+
+const emptySettings: Required<ViewerSettings> = {
+ disableSummary: false,
+ disableCaptions: false,
+ disableChapters: false,
+ disableReactions: false,
+ disableTranscript: false,
+ disableComments: false,
+};
+
+export function resolveEffectiveVideoRules({
+ videoSettings,
+ organizationSettings,
+ spaces,
+}: {
+ videoSettings?: ViewerSettings | null;
+ organizationSettings?: ViewerSettings | null;
+ spaces: SpaceRuleInput[];
+}): EffectiveVideoRules {
+ const inheritedSettings: Partial<
+ Record<ViewerSettingKey, SpaceRuleSource[]>
+ > = {};
+ const settings = { ...emptySettings };
+
+ for (const key of settingKeys) {
+ const sources = spaces
+ .filter((space) => space.settings?.[key] === true)
+ .map((space) => ({ id: space.id, name: space.name }));
+
+ if (sources.length > 0) {
+ settings[key] = true;
+ inheritedSettings[key] = sources;
+ } else {
+ settings[key] =
+ videoSettings?.[key] ?? organizationSettings?.[key] ?? false;
+ }
+ }
+
+ const inheritedPasswordSources = spaces
+ .filter((space) => space.hasPassword || Boolean(space.password))
+ .map((space) => ({ id: space.id, name: space.name }));
+
+ return {
+ settings,
+ inheritedSettings,
+ inheritedPasswordSources,
+ hasInheritedPassword: inheritedPasswordSources.length > 0,
+ };
+}
+
+export function collectPasswordHashes({
+ videoPassword,
+ spacePasswords,
+}: {
+ videoPassword?: string | null;
+ spacePasswords: Array<{ password?: string | null }>;
+}) {
+ return [
+ ...(videoPassword ? [videoPassword] : []),
+ ...spacePasswords
+ .map((space) => space.password)
+ .filter((password): password is string => Boolean(password)),
+ ];
+}
diff --git a/packages/web-backend/src/index.ts b/packages/web-backend/src/index.ts
index 81bb1e6ba..24f5bab91 100644
--- a/packages/web-backend/src/index.ts
+++ b/packages/web-backend/src/index.ts
@@ -25,6 +25,15 @@ export {
} from "./Storage/StorageRepo.ts";
export { Tinybird } from "./Tinybird/index.ts";
export { Users } from "./Users/index.ts";
+export {
+ collectPasswordHashes,
+ type EffectiveVideoRules,
+ resolveEffectiveVideoRules,
+ type SpaceRuleInput,
+ type SpaceRuleSource,
+ type ViewerSettingKey,
+ type ViewerSettings,
+} from "./Videos/EffectiveVideoRules.ts";
export { Videos } from "./Videos/index.ts";
export {
buildCanView,