Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add mobile HTTP API route

Richie McIlroy6c2d63d07f7c
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
new file mode 100644
index 000000000..af24edec1
--- /dev/null
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -0,0 +1,1409 @@
+import crypto from "node:crypto";
+import { authOptions } from "@cap/database/auth/auth-options";
+import { isEmailAllowedForSignup } from "@cap/database/auth/domain-utils";
+import { hashPassword } from "@cap/database/crypto";
+import { sendEmail } from "@cap/database/emails/config";
+import { OTPEmail } from "@cap/database/emails/otp-email";
+import { nanoId } from "@cap/database/helpers";
+import * as Db from "@cap/database/schema";
+import { serverEnv } from "@cap/env";
+import {
+ Database,
+ getCurrentUser,
+ ImageUploads,
+ Storage,
+ Videos,
+ VideosRepo,
+} from "@cap/web-backend";
+import {
+ Comment,
+ CurrentUser,
+ Folder,
+ Mobile,
+ type Organisation,
+ User,
+ Video,
+} from "@cap/web-domain";
+import {
+ HttpApiBuilder,
+ HttpApiError,
+ HttpServerResponse,
+} from "@effect/platform";
+import { and, count, desc, eq, isNull, or, sql } from "drizzle-orm";
+import { Effect, Exit, Layer, Option } from "effect";
+import { revalidatePath } from "next/cache";
+import { createNotification } from "@/lib/Notification";
+import { apiToHandler } from "@/lib/server";
+import { startVideoProcessingWorkflow } from "@/lib/video-processing";
+
+export const dynamic = "force-dynamic";
+
+type CapRow = {
+ id: Video.VideoId;
+ name: string;
+ createdAt: Date;
+ updatedAt: Date;
+ ownerName: string | null;
+ duration: number | null;
+ folderId: Folder.FolderId | null;
+ public: boolean;
+ hasPassword: boolean;
+ commentCount: number;
+ reactionCount: number;
+ uploadVideoId: Video.VideoId | null;
+ uploadUploaded: number | null;
+ uploadTotal: number | null;
+ uploadPhase: Video.UploadPhase | null;
+ processingProgress: number | null;
+ processingMessage: string | null;
+ processingError: string | null;
+ metadata: unknown;
+ transcriptionStatus:
+ | "PROCESSING"
+ | "COMPLETE"
+ | "ERROR"
+ | "SKIPPED"
+ | "NO_AUDIO"
+ | null;
+};
+
+type MobileCapSummary = (typeof Mobile.MobileCapSummary)["Type"];
+type MobileFolder = (typeof Mobile.MobileFolder)["Type"];
+type MobileOrganization = (typeof Mobile.MobileOrganization)["Type"];
+type MobileFolderCreateInput = (typeof Mobile.MobileFolderCreateInput)["Type"];
+type MobileUploadCreateInput = (typeof Mobile.MobileUploadCreateInput)["Type"];
+
+const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
+const emailCodePattern = /^\d{6}$/;
+const emailCodeTtlMs = 10 * 60 * 1000;
+
+const toIsoString = (value: Date) => value.toISOString();
+
+const normalizeEmail = (email: string) => email.trim().toLowerCase();
+
+const hashEmailCode = (code: string) =>
+ crypto
+ .createHash("sha256")
+ .update(`${code}${serverEnv().NEXTAUTH_SECRET}`)
+ .digest("hex");
+
+const sendMobileEmailCode = async (email: string, code: string) => {
+ if (!serverEnv().RESEND_API_KEY) {
+ console.log("");
+ console.log("Cap mobile verification code");
+ console.log(`Email: ${email}`);
+ console.log(`Code: ${code}`);
+ console.log("Expires in: 10 minutes");
+ console.log("");
+ return;
+ }
+
+ await sendEmail({
+ email,
+ subject: "Your Cap Verification Code",
+ react: OTPEmail({ code, email }),
+ });
+};
+
+const getEmailAuthAdapter = () => {
+ const adapter = authOptions().adapter;
+ const { createUser, getUserByEmail, updateUser } = adapter ?? {};
+
+ if (!createUser || !getUserByEmail || !updateUser) {
+ throw new Error("Email auth adapter is not configured");
+ }
+
+ return { createUser, getUserByEmail, updateUser };
+};
+
+const createOrUpdateEmailUser = async (email: string) => {
+ const { createUser, getUserByEmail, updateUser } = getEmailAuthAdapter();
+ const existingUser = await getUserByEmail(email);
+
+ if (existingUser) {
+ return updateUser({
+ id: existingUser.id,
+ emailVerified: new Date(),
+ });
+ }
+
+ return createUser({
+ email,
+ emailVerified: new Date(),
+ image: null,
+ name: null,
+ });
+};
+
+const parseBearerToken = (authorization: string | undefined) => {
+ if (!authorization) return null;
+ const [scheme, token] = authorization.split(" ");
+ if (scheme?.toLowerCase() !== "bearer" || !token) return null;
+ return token;
+};
+
+const parsePositiveInteger = (
+ value: string | undefined,
+ fallback: number,
+ max: number,
+) => {
+ const parsed = Number(value);
+ if (!Number.isFinite(parsed) || parsed < 1) return fallback;
+ return Math.min(Math.trunc(parsed), max);
+};
+
+const getMetadataRecord = (metadata: unknown): Record<string, unknown> => {
+ if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) {
+ return {};
+ }
+ return metadata as Record<string, unknown>;
+};
+
+const getMetadataString = (metadata: Record<string, unknown>, key: string) => {
+ const value = metadata[key];
+ return typeof value === "string" && value.length > 0 ? value : null;
+};
+
+const getMetadataChapters = (metadata: Record<string, unknown>) => {
+ const chapters = metadata.chapters;
+ if (!Array.isArray(chapters)) return [];
+
+ return chapters.flatMap((chapter) => {
+ if (!chapter || typeof chapter !== "object" || Array.isArray(chapter)) {
+ return [];
+ }
+ const value = chapter as Record<string, unknown>;
+ const title = value.title;
+ const start = value.start;
+ if (typeof title !== "string" || typeof start !== "number") return [];
+ return [{ title, start }];
+ });
+};
+
+const getDeploymentOrigin = () => {
+ const webUrl = serverEnv().WEB_URL;
+ const vercelEnv = serverEnv().VERCEL_ENV;
+
+ if (!vercelEnv || vercelEnv === "production") return webUrl;
+
+ if (vercelEnv === "preview") {
+ const branchHost = serverEnv().VERCEL_BRANCH_URL_HOST;
+ if (branchHost?.endsWith(".vercel.app")) return `https://${branchHost}`;
+ }
+
+ return webUrl;
+};
+
+const getFileExtension = (input: MobileUploadCreateInput) => {
+ const fileNameExtension = input.fileName.split(".").at(-1)?.toLowerCase();
+ if (
+ fileNameExtension &&
+ fileNameExtension !== input.fileName.toLowerCase() &&
+ /^[a-z0-9]+$/.test(fileNameExtension)
+ ) {
+ return fileNameExtension;
+ }
+
+ if (input.contentType.includes("quicktime")) return "mov";
+ if (input.contentType.includes("webm")) return "webm";
+ if (input.contentType.includes("matroska")) return "mkv";
+ if (input.contentType.includes("x-msvideo")) return "avi";
+ if (input.contentType.includes("x-m4v")) return "m4v";
+ return "mp4";
+};
+
+const getUploadTitle = (fileName: string) => {
+ const title = fileName.replace(/\.[^/.]+$/, "").trim();
+ return title.length > 0 ? title : "Mobile Upload";
+};
+
+const toMobileCapSummary = (
+ row: CapRow,
+ thumbnailUrl: string | null,
+ viewCount: number,
+): MobileCapSummary => ({
+ id: row.id,
+ shareUrl: `${serverEnv().WEB_URL}/s/${row.id}`,
+ title: row.name,
+ createdAt: toIsoString(row.createdAt),
+ updatedAt: toIsoString(row.updatedAt),
+ ownerName: row.ownerName ?? "",
+ durationSeconds: row.duration,
+ thumbnailUrl,
+ folderId: row.folderId,
+ public: row.public,
+ protected: row.hasPassword,
+ viewCount,
+ commentCount: Number(row.commentCount),
+ reactionCount: Number(row.reactionCount),
+ upload: row.uploadVideoId
+ ? {
+ uploaded: Number(row.uploadUploaded ?? 0),
+ total: Number(row.uploadTotal ?? 0),
+ phase: row.uploadPhase ?? "uploading",
+ processingProgress: Number(row.processingProgress ?? 0),
+ processingMessage: row.processingMessage,
+ processingError: row.processingError,
+ }
+ : null,
+});
+
+const withMappedErrors = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
+ effect.pipe(
+ Effect.catchTags({
+ DatabaseError: () => new HttpApiError.InternalServerError(),
+ NoSuchElementException: () => new HttpApiError.NotFound(),
+ PolicyDenied: () => new HttpApiError.Forbidden(),
+ S3Error: () => new HttpApiError.InternalServerError(),
+ StorageError: () => new HttpApiError.InternalServerError(),
+ UnknownException: () => new HttpApiError.InternalServerError(),
+ VerifyVideoPasswordError: () => new HttpApiError.Forbidden(),
+ VideoNotFoundError: () => new HttpApiError.NotFound(),
+ }),
+ );
+
+const ensureEmailSignInAllowed = Effect.fn("Mobile.ensureEmailSignInAllowed")(
+ function* (email: string) {
+ if (!emailPattern.test(email)) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ const allowedDomains = serverEnv().CAP_ALLOWED_SIGNUP_DOMAINS;
+ if (!allowedDomains) return;
+
+ const database = yield* Database;
+ const [existingUser] = yield* database.use((db) =>
+ db
+ .select({ id: Db.users.id })
+ .from(Db.users)
+ .where(eq(Db.users.email, email))
+ .limit(1),
+ );
+
+ if (!existingUser && !isEmailAllowedForSignup(email, allowedDomains)) {
+ return yield* Effect.fail(new HttpApiError.Forbidden());
+ }
+ },
+);
+
+const createMobileApiKey = Effect.fn("Mobile.createMobileApiKey")(function* (
+ userId: User.UserId,
+) {
+ const database = yield* Database;
+ const apiKey = crypto.randomUUID();
+ yield* database.use((db) =>
+ db.insert(Db.authApiKeys).values({
+ id: apiKey,
+ userId,
+ }),
+ );
+
+ return {
+ type: "api_key" as const,
+ apiKey,
+ userId,
+ };
+});
+
+const requestEmailSession = Effect.fn("Mobile.requestEmailSession")(function* (
+ rawEmail: string,
+) {
+ const email = normalizeEmail(rawEmail);
+ yield* ensureEmailSignInAllowed(email);
+
+ const code = crypto.randomInt(100000, 1000000).toString();
+ const token = hashEmailCode(code);
+ const expires = new Date(Date.now() + emailCodeTtlMs);
+ const database = yield* Database;
+
+ yield* database.use(async (db) => {
+ const [existingToken] = await db
+ .select({ identifier: Db.verificationTokens.identifier })
+ .from(Db.verificationTokens)
+ .where(eq(Db.verificationTokens.identifier, email))
+ .limit(1);
+
+ if (existingToken) {
+ await db
+ .update(Db.verificationTokens)
+ .set({ token, expires })
+ .where(eq(Db.verificationTokens.identifier, email));
+ return;
+ }
+
+ await db.insert(Db.verificationTokens).values({
+ identifier: email,
+ token,
+ expires,
+ });
+ });
+
+ yield* Effect.tryPromise({
+ try: () => sendMobileEmailCode(email, code),
+ catch: () => new HttpApiError.InternalServerError(),
+ });
+
+ return { success: true as const };
+});
+
+const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* ({
+ email: rawEmail,
+ code: rawCode,
+}: (typeof Mobile.MobileEmailSessionVerifyInput)["Type"]) {
+ const email = normalizeEmail(rawEmail);
+ const code = rawCode.trim();
+
+ if (!emailPattern.test(email) || !emailCodePattern.test(code)) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ yield* ensureEmailSignInAllowed(email);
+
+ const database = yield* Database;
+ const token = hashEmailCode(code);
+ const [verificationToken] = yield* database.use((db) =>
+ db
+ .select()
+ .from(Db.verificationTokens)
+ .where(
+ and(
+ eq(Db.verificationTokens.identifier, email),
+ eq(Db.verificationTokens.token, token),
+ ),
+ )
+ .limit(1),
+ );
+
+ if (!verificationToken) {
+ return yield* Effect.fail(new HttpApiError.Forbidden());
+ }
+
+ yield* database.use((db) =>
+ db
+ .delete(Db.verificationTokens)
+ .where(
+ and(
+ eq(Db.verificationTokens.identifier, email),
+ eq(Db.verificationTokens.token, token),
+ ),
+ ),
+ );
+
+ if (verificationToken.expires.valueOf() < Date.now()) {
+ return yield* Effect.fail(new HttpApiError.Forbidden());
+ }
+
+ const user = yield* Effect.tryPromise({
+ try: () => createOrUpdateEmailUser(email),
+ catch: () => new HttpApiError.InternalServerError(),
+ });
+
+ return yield* createMobileApiKey(User.UserId.make(user.id));
+});
+
+const getAccessibleOrganizations = Effect.fn(
+ "Mobile.getAccessibleOrganizations",
+)(function* (userId: User.UserId) {
+ const database = yield* Database;
+ const imageUploads = yield* ImageUploads;
+
+ const rows = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.organizations.id,
+ name: Db.organizations.name,
+ ownerId: Db.organizations.ownerId,
+ iconUrl: Db.organizations.iconUrl,
+ role: Db.organizationMembers.role,
+ })
+ .from(Db.organizations)
+ .leftJoin(
+ Db.organizationMembers,
+ and(
+ eq(Db.organizationMembers.organizationId, Db.organizations.id),
+ eq(Db.organizationMembers.userId, userId),
+ ),
+ )
+ .where(
+ and(
+ isNull(Db.organizations.tombstoneAt),
+ or(
+ eq(Db.organizations.ownerId, userId),
+ eq(Db.organizationMembers.userId, userId),
+ ),
+ ),
+ ),
+ );
+
+ return yield* Effect.forEach(
+ rows,
+ (row) =>
+ Effect.gen(function* () {
+ const role: MobileOrganization["role"] =
+ row.ownerId === userId ? "owner" : (row.role ?? "member");
+ const iconUrl = row.iconUrl
+ ? yield* imageUploads.resolveImageUrl(row.iconUrl)
+ : null;
+
+ return {
+ id: row.id,
+ name: row.name,
+ iconUrl,
+ role,
+ };
+ }),
+ { concurrency: 5 },
+ );
+});
+
+const getRootFolders = Effect.fn("Mobile.getRootFolders")(function* (
+ organizationId: Organisation.OrganisationId,
+) {
+ const user = yield* CurrentUser;
+ const database = yield* Database;
+
+ const rows = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.folders.id,
+ name: Db.folders.name,
+ color: Db.folders.color,
+ parentId: Db.folders.parentId,
+ videoCount: sql<number>`(
+ SELECT COUNT(*)
+ FROM ${Db.videos}
+ WHERE ${Db.videos.folderId} = ${Db.folders.id}
+ AND ${Db.videos.ownerId} = ${user.id}
+ AND ${Db.videos.orgId} = ${organizationId}
+ )`,
+ })
+ .from(Db.folders)
+ .where(
+ and(
+ eq(Db.folders.organizationId, organizationId),
+ eq(Db.folders.createdById, user.id),
+ isNull(Db.folders.parentId),
+ isNull(Db.folders.spaceId),
+ ),
+ ),
+ );
+
+ return rows satisfies MobileFolder[];
+});
+
+const assertOrganizationAccess = Effect.fn("Mobile.assertOrganizationAccess")(
+ function* (organizationId: Organisation.OrganisationId) {
+ const user = yield* CurrentUser;
+ const organizations = yield* getAccessibleOrganizations(user.id);
+ const hasAccess = organizations.some((org) => org.id === organizationId);
+ if (!hasAccess) return yield* Effect.fail(new HttpApiError.Forbidden());
+ },
+);
+
+const getBootstrap = Effect.fn("Mobile.getBootstrap")(function* () {
+ const user = yield* CurrentUser;
+ const database = yield* Database;
+ const imageUploads = yield* ImageUploads;
+
+ const [userRow] = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.users.id,
+ name: Db.users.name,
+ email: Db.users.email,
+ image: Db.users.image,
+ activeOrganizationId: Db.users.activeOrganizationId,
+ })
+ .from(Db.users)
+ .where(eq(Db.users.id, user.id)),
+ );
+ if (!userRow) return yield* Effect.fail(new HttpApiError.Unauthorized());
+
+ const organizations = yield* getAccessibleOrganizations(user.id);
+ const activeOrganization =
+ organizations.find((org) => org.id === userRow.activeOrganizationId) ??
+ organizations[0] ??
+ null;
+ const activeOrganizationId = activeOrganization?.id ?? null;
+ const rootFolders = activeOrganizationId
+ ? yield* getRootFolders(activeOrganizationId)
+ : [];
+ const imageUrl = userRow.image
+ ? yield* imageUploads.resolveImageUrl(userRow.image)
+ : null;
+
+ return {
+ user: {
+ id: userRow.id,
+ name: userRow.name,
+ email: userRow.email,
+ imageUrl,
+ activeOrganizationId: activeOrganizationId ?? user.activeOrganizationId,
+ },
+ organizations,
+ activeOrganizationId,
+ rootFolders,
+ };
+});
+
+const getCapRows = Effect.fn("Mobile.getCapRows")(function* ({
+ folderId,
+ page,
+ limit,
+}: {
+ folderId: Folder.FolderId | null;
+ page: number;
+ limit: number;
+}) {
+ const user = yield* CurrentUser;
+ const database = yield* Database;
+ const offset = (page - 1) * limit;
+ const folderFilter = folderId
+ ? eq(Db.videos.folderId, folderId)
+ : isNull(Db.videos.folderId);
+ const whereClause = and(
+ eq(Db.videos.ownerId, user.id),
+ eq(Db.videos.orgId, user.activeOrganizationId),
+ folderFilter,
+ isNull(Db.organizations.tombstoneAt),
+ );
+
+ const [totalRow] = yield* database.use((db) =>
+ db
+ .select({ value: count() })
+ .from(Db.videos)
+ .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
+ .where(whereClause),
+ );
+
+ const rows = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.videos.id,
+ name: Db.videos.name,
+ createdAt: Db.videos.createdAt,
+ updatedAt: Db.videos.updatedAt,
+ ownerName: Db.users.name,
+ duration: Db.videos.duration,
+ folderId: Db.videos.folderId,
+ public: Db.videos.public,
+ hasPassword: sql<boolean>`${Db.videos.password} IS NOT NULL`.mapWith(
+ Boolean,
+ ),
+ commentCount: sql<number>`COUNT(DISTINCT CASE WHEN ${Db.comments.type} = 'text' THEN ${Db.comments.id} END)`,
+ reactionCount: sql<number>`COUNT(DISTINCT CASE WHEN ${Db.comments.type} = 'emoji' THEN ${Db.comments.id} END)`,
+ uploadVideoId: Db.videoUploads.videoId,
+ uploadUploaded: Db.videoUploads.uploaded,
+ uploadTotal: Db.videoUploads.total,
+ uploadPhase: Db.videoUploads.phase,
+ processingProgress: Db.videoUploads.processingProgress,
+ processingMessage: Db.videoUploads.processingMessage,
+ processingError: Db.videoUploads.processingError,
+ metadata: Db.videos.metadata,
+ transcriptionStatus: Db.videos.transcriptionStatus,
+ })
+ .from(Db.videos)
+ .leftJoin(Db.comments, eq(Db.videos.id, Db.comments.videoId))
+ .leftJoin(Db.users, eq(Db.videos.ownerId, Db.users.id))
+ .leftJoin(Db.videoUploads, eq(Db.videos.id, Db.videoUploads.videoId))
+ .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
+ .where(whereClause)
+ .groupBy(
+ Db.videos.id,
+ Db.videos.name,
+ Db.videos.createdAt,
+ Db.videos.updatedAt,
+ Db.users.name,
+ Db.videos.duration,
+ Db.videos.folderId,
+ Db.videos.public,
+ Db.videos.password,
+ Db.videoUploads.videoId,
+ Db.videoUploads.uploaded,
+ Db.videoUploads.total,
+ Db.videoUploads.phase,
+ Db.videoUploads.processingProgress,
+ Db.videoUploads.processingMessage,
+ Db.videoUploads.processingError,
+ Db.videos.metadata,
+ Db.videos.transcriptionStatus,
+ )
+ .orderBy(desc(Db.videos.effectiveCreatedAt))
+ .limit(limit)
+ .offset(offset),
+ );
+
+ return { rows, total: totalRow?.value ?? 0 };
+});
+
+const getCapsList = Effect.fn("Mobile.getCapsList")(function* (
+ params: (typeof Mobile.MobileCapsListParams)["Type"],
+) {
+ const page = parsePositiveInteger(params.page, 1, 10_000);
+ const limit = parsePositiveInteger(params.limit, 20, 50);
+ const folderId = params.folderId
+ ? Folder.FolderId.make(params.folderId)
+ : null;
+ const videos = yield* Videos;
+ const user = yield* CurrentUser;
+
+ const [{ rows, total }, folders] = yield* Effect.all([
+ getCapRows({ folderId, page, limit }),
+ folderId ? Effect.succeed([]) : getRootFolders(user.activeOrganizationId),
+ ]);
+ const analyticsExits = yield* videos
+ .getAnalyticsBulk(rows.map((row) => row.id))
+ .pipe(Effect.catchAll(() => Effect.succeed([])));
+ const viewCounts = new Map<Video.VideoId, number>();
+
+ rows.forEach((row, index) => {
+ const result = analyticsExits[index];
+ viewCounts.set(
+ row.id,
+ result && Exit.isSuccess(result) ? result.value.count : 0,
+ );
+ });
+
+ const caps = yield* Effect.forEach(
+ rows,
+ (row) =>
+ videos.getThumbnailURL(row.id).pipe(
+ Effect.map(Option.getOrNull),
+ Effect.catchAll(() => Effect.succeed(null)),
+ Effect.map((thumbnailUrl) =>
+ toMobileCapSummary(row, thumbnailUrl, viewCounts.get(row.id) ?? 0),
+ ),
+ ),
+ { concurrency: 5 },
+ );
+
+ return {
+ folders,
+ caps,
+ page,
+ limit,
+ total,
+ hasMore: page * limit < total,
+ };
+});
+
+const createMobileFolder = Effect.fn("Mobile.createFolder")(function* (
+ input: MobileFolderCreateInput,
+) {
+ const user = yield* CurrentUser;
+ const name = input.name.trim();
+ if (!name) return yield* Effect.fail(new HttpApiError.BadRequest());
+
+ const organizationId = user.activeOrganizationId;
+ yield* assertOrganizationAccess(organizationId);
+
+ const color = input.color ?? "normal";
+ const id = Folder.FolderId.make(nanoId());
+ const database = yield* Database;
+
+ yield* database.use((db) =>
+ db.insert(Db.folders).values({
+ id,
+ name,
+ color,
+ organizationId,
+ createdById: user.id,
+ parentId: null,
+ spaceId: null,
+ }),
+ );
+
+ yield* Effect.sync(() => {
+ revalidatePath("/dashboard/caps");
+ });
+
+ return {
+ id,
+ name,
+ color,
+ parentId: null,
+ videoCount: 0,
+ };
+});
+
+const getCapById = Effect.fn("Mobile.getCapById")(function* (
+ videoId: Video.VideoId,
+) {
+ const user = yield* CurrentUser;
+ const database = yield* Database;
+ const videos = yield* Videos;
+
+ const [row] = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.videos.id,
+ name: Db.videos.name,
+ createdAt: Db.videos.createdAt,
+ updatedAt: Db.videos.updatedAt,
+ ownerName: Db.users.name,
+ duration: Db.videos.duration,
+ folderId: Db.videos.folderId,
+ public: Db.videos.public,
+ hasPassword: sql<boolean>`${Db.videos.password} IS NOT NULL`.mapWith(
+ Boolean,
+ ),
+ commentCount: sql<number>`COUNT(DISTINCT CASE WHEN ${Db.comments.type} = 'text' THEN ${Db.comments.id} END)`,
+ reactionCount: sql<number>`COUNT(DISTINCT CASE WHEN ${Db.comments.type} = 'emoji' THEN ${Db.comments.id} END)`,
+ uploadVideoId: Db.videoUploads.videoId,
+ uploadUploaded: Db.videoUploads.uploaded,
+ uploadTotal: Db.videoUploads.total,
+ uploadPhase: Db.videoUploads.phase,
+ processingProgress: Db.videoUploads.processingProgress,
+ processingMessage: Db.videoUploads.processingMessage,
+ processingError: Db.videoUploads.processingError,
+ metadata: Db.videos.metadata,
+ transcriptionStatus: Db.videos.transcriptionStatus,
+ })
+ .from(Db.videos)
+ .leftJoin(Db.comments, eq(Db.videos.id, Db.comments.videoId))
+ .leftJoin(Db.users, eq(Db.videos.ownerId, Db.users.id))
+ .leftJoin(Db.videoUploads, eq(Db.videos.id, Db.videoUploads.videoId))
+ .where(and(eq(Db.videos.id, videoId), eq(Db.videos.ownerId, user.id)))
+ .groupBy(
+ Db.videos.id,
+ Db.videos.name,
+ Db.videos.createdAt,
+ Db.videos.updatedAt,
+ Db.users.name,
+ Db.videos.duration,
+ Db.videos.folderId,
+ Db.videos.public,
+ Db.videos.password,
+ Db.videoUploads.videoId,
+ Db.videoUploads.uploaded,
+ Db.videoUploads.total,
+ Db.videoUploads.phase,
+ Db.videoUploads.processingProgress,
+ Db.videoUploads.processingMessage,
+ Db.videoUploads.processingError,
+ Db.videos.metadata,
+ Db.videos.transcriptionStatus,
+ ),
+ );
+
+ if (!row) return yield* Effect.fail(new HttpApiError.NotFound());
+
+ const thumbnailUrl = yield* videos.getThumbnailURL(row.id).pipe(
+ Effect.map(Option.getOrNull),
+ Effect.catchAll(() => Effect.succeed(null)),
+ );
+ const analytics = yield* videos.getAnalytics(row.id).pipe(
+ Effect.map((result) => result.count),
+ Effect.catchAll(() => Effect.succeed(0)),
+ );
+
+ return { row, cap: toMobileCapSummary(row, thumbnailUrl, analytics) };
+});
+
+const getComments = Effect.fn("Mobile.getComments")(function* (
+ videoId: Video.VideoId,
+) {
+ const database = yield* Database;
+ const imageUploads = yield* ImageUploads;
+
+ const rows = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.comments.id,
+ videoId: Db.comments.videoId,
+ type: Db.comments.type,
+ content: Db.comments.content,
+ timestamp: Db.comments.timestamp,
+ parentCommentId: Db.comments.parentCommentId,
+ createdAt: Db.comments.createdAt,
+ updatedAt: Db.comments.updatedAt,
+ authorId: Db.comments.authorId,
+ authorName: Db.users.name,
+ authorImage: Db.users.image,
+ })
+ .from(Db.comments)
+ .leftJoin(Db.users, eq(Db.comments.authorId, Db.users.id))
+ .where(eq(Db.comments.videoId, videoId))
+ .orderBy(Db.comments.createdAt),
+ );
+
+ return yield* Effect.forEach(
+ rows,
+ (row) =>
+ Effect.gen(function* () {
+ const imageUrl = row.authorImage
+ ? yield* imageUploads
+ .resolveImageUrl(row.authorImage)
+ .pipe(Effect.catchAll(() => Effect.succeed(null)))
+ : null;
+
+ return {
+ id: row.id,
+ videoId: row.videoId,
+ type: row.type,
+ content: row.content,
+ timestamp: row.timestamp,
+ parentCommentId: row.parentCommentId,
+ createdAt: toIsoString(row.createdAt),
+ updatedAt: toIsoString(row.updatedAt),
+ author: {
+ id: row.authorId,
+ name: row.authorName,
+ imageUrl,
+ },
+ };
+ }),
+ { concurrency: 5 },
+ );
+});
+
+const getCapDetail = Effect.fn("Mobile.getCapDetail")(function* (
+ videoId: Video.VideoId,
+) {
+ const { row, cap } = yield* getCapById(videoId);
+ const metadata = getMetadataRecord(row.metadata);
+ const comments = yield* getComments(videoId);
+
+ return {
+ cap,
+ summary: getMetadataString(metadata, "summary"),
+ chapters: getMetadataChapters(metadata),
+ transcriptionStatus: row.transcriptionStatus,
+ comments,
+ shareUrl: `${serverEnv().WEB_URL}/s/${videoId}`,
+ };
+});
+
+const createMobileComment = Effect.fn("Mobile.createComment")(function* ({
+ videoId,
+ content,
+ timestamp,
+ parentCommentId,
+ type,
+}: {
+ videoId: Video.VideoId;
+ content: string;
+ timestamp: number | null;
+ parentCommentId: Comment.CommentId | null;
+ type: "text" | "emoji";
+}) {
+ const user = yield* CurrentUser;
+ yield* getCapById(videoId);
+
+ const trimmedContent = content.trim();
+ if (trimmedContent.length === 0) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ const id = Comment.CommentId.make(nanoId());
+ const now = new Date();
+ const database = yield* Database;
+ yield* database.use((db) =>
+ db.insert(Db.comments).values({
+ id,
+ authorId: user.id,
+ type,
+ content: trimmedContent,
+ videoId,
+ timestamp,
+ parentCommentId,
+ createdAt: now,
+ updatedAt: now,
+ }),
+ );
+
+ const notificationType = parentCommentId
+ ? "reply"
+ : type === "emoji"
+ ? "reaction"
+ : "comment";
+
+ yield* Effect.tryPromise(() =>
+ createNotification({
+ type: notificationType,
+ videoId,
+ authorId: user.id,
+ comment: { id, content: trimmedContent },
+ parentCommentId: parentCommentId ?? undefined,
+ }),
+ ).pipe(Effect.catchAll(() => Effect.void));
+
+ const comments = yield* getComments(videoId);
+ const created = comments.find((comment) => comment.id === id);
+ if (!created)
+ return yield* Effect.fail(new HttpApiError.InternalServerError());
+ return created;
+});
+
+const getPlayback = Effect.fn("Mobile.getPlayback")(function* (
+ videoId: Video.VideoId,
+) {
+ const videos = yield* Videos;
+ const storage = yield* Storage;
+ const [video] = yield* videos.getByIdForViewing(videoId).pipe(
+ Effect.flatten,
+ Effect.catchTag("NoSuchElementException", () => new Video.NotFoundError()),
+ );
+ const [bucket] = yield* storage.getAccessForVideo(video);
+ const source = Video.Video.getSource(video);
+
+ const transcriptKey = `${video.ownerId}/${video.id}/transcription.vtt`;
+ const transcriptUrl = yield* bucket.headObject(transcriptKey).pipe(
+ Effect.flatMap(() => bucket.getSignedObjectUrl(transcriptKey)),
+ Effect.catchAll(() => Effect.succeed(null)),
+ );
+
+ if (source instanceof Video.Mp4Source) {
+ const url = yield* bucket.getSignedObjectUrl(source.getFileKey());
+ return { kind: "mp4" as const, url, transcriptUrl };
+ }
+
+ if (source instanceof Video.M3U8Source) {
+ const url = yield* bucket.getSignedObjectUrl(source.getPlaylistFileKey());
+ return { kind: "hls" as const, url, transcriptUrl };
+ }
+
+ if (source instanceof Video.SegmentsSource) {
+ return {
+ kind: "hls" as const,
+ url: `${serverEnv().WEB_URL}/api/playlist?videoId=${video.id}&videoType=segments-master`,
+ transcriptUrl,
+ };
+ }
+
+ return yield* Effect.fail(new HttpApiError.NotFound());
+});
+
+const createUpload = Effect.fn("Mobile.createUpload")(function* (
+ input: MobileUploadCreateInput,
+) {
+ const user = yield* CurrentUser;
+ const organizationId = input.organizationId ?? user.activeOrganizationId;
+ yield* assertOrganizationAccess(organizationId);
+
+ if (!input.contentType.startsWith("video/")) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ const database = yield* Database;
+ const storage = yield* Storage;
+ const repo = yield* VideosRepo;
+ const folderId = input.folderId;
+
+ if (folderId) {
+ const [folder] = yield* database.use((db) =>
+ db
+ .select({ id: Db.folders.id })
+ .from(Db.folders)
+ .where(
+ and(
+ eq(Db.folders.id, folderId),
+ eq(Db.folders.organizationId, organizationId),
+ eq(Db.folders.createdById, user.id),
+ isNull(Db.folders.spaceId),
+ ),
+ ),
+ );
+ if (!folder) return yield* Effect.fail(new HttpApiError.NotFound());
+ }
+
+ const writable = yield* storage.getWritableAccessForUser(
+ user.id,
+ organizationId,
+ );
+ const videoId = yield* repo.create({
+ ownerId: user.id,
+ orgId: organizationId,
+ name: getUploadTitle(input.fileName),
+ public: serverEnv().CAP_VIDEOS_DEFAULT_PUBLIC,
+ source: { type: "webMP4" },
+ bucketId: writable.bucketId,
+ storageIntegrationId: writable.storageIntegrationId,
+ folderId: Option.fromNullable(folderId),
+ width: Option.fromNullable(input.width),
+ height: Option.fromNullable(input.height),
+ duration: Option.fromNullable(input.durationSeconds),
+ metadata: Option.none(),
+ transcriptionStatus: Option.none(),
+ });
+
+ yield* database.use((db) =>
+ db.insert(Db.videoUploads).values({
+ videoId,
+ total: input.contentLength ?? 0,
+ mode: "singlepart",
+ }),
+ );
+
+ const rawFileKey = `${user.id}/${videoId}/raw-upload.${getFileExtension(input)}`;
+ const upload = yield* writable.access.createUploadTarget(rawFileKey, {
+ contentType: input.contentType,
+ method: "put",
+ fields: {
+ "Content-Type": input.contentType,
+ "x-amz-meta-userid": user.id,
+ "x-amz-meta-source": "cap-mobile-ios",
+ },
+ });
+ const { cap } = yield* getCapById(videoId);
+
+ return {
+ id: videoId,
+ shareUrl: `${serverEnv().WEB_URL}/s/${videoId}`,
+ rawFileKey,
+ upload,
+ cap,
+ };
+});
+
+const ApiLive = HttpApiBuilder.api(Mobile.MobileApiContract).pipe(
+ Layer.provide(
+ HttpApiBuilder.group(Mobile.MobileApiContract, "mobile", (handlers) =>
+ Effect.gen(function* () {
+ const videos = yield* Videos;
+ const database = yield* Database;
+
+ return handlers
+ .handle("getAuthConfig", () =>
+ Effect.succeed({
+ googleAuthAvailable: Boolean(serverEnv().GOOGLE_CLIENT_ID),
+ workosAuthAvailable: Boolean(serverEnv().WORKOS_CLIENT_ID),
+ }),
+ )
+ .handle("requestSession", ({ request, urlParams }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* getCurrentUser;
+ if (Option.isNone(user)) {
+ const redirectOrigin = getDeploymentOrigin();
+ const requestUrl = new URL(request.url);
+ const loginRedirectUrl = new URL(`${redirectOrigin}/login`);
+ loginRedirectUrl.searchParams.set(
+ "next",
+ new URL(
+ `${redirectOrigin}${requestUrl.pathname}${requestUrl.search}`,
+ ).toString(),
+ );
+ if (urlParams.provider === "google") {
+ loginRedirectUrl.searchParams.set(
+ "mobileProvider",
+ "google",
+ );
+ } else if (urlParams.provider === "workos") {
+ loginRedirectUrl.searchParams.set(
+ "mobileProvider",
+ "workos",
+ );
+ if (urlParams.organizationId) {
+ loginRedirectUrl.searchParams.set(
+ "organizationId",
+ urlParams.organizationId,
+ );
+ }
+ }
+ return HttpServerResponse.redirect(
+ loginRedirectUrl.toString(),
+ );
+ }
+
+ const session = yield* createMobileApiKey(user.value.id);
+
+ if (urlParams.redirectUri) {
+ const redirectUrl = new URL(urlParams.redirectUri);
+ redirectUrl.searchParams.set("api_key", session.apiKey);
+ redirectUrl.searchParams.set("user_id", user.value.id);
+ return HttpServerResponse.redirect(redirectUrl.toString());
+ }
+
+ return session;
+ }),
+ ),
+ )
+ .handle("requestEmailSession", ({ payload }) =>
+ withMappedErrors(requestEmailSession(payload.email)),
+ )
+ .handle("verifyEmailSession", ({ payload }) =>
+ withMappedErrors(verifyEmailSession(payload)),
+ )
+ .handle("revokeSession", ({ headers }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const token = parseBearerToken(headers.authorization);
+ if (!token)
+ return yield* Effect.fail(new HttpApiError.Unauthorized());
+ yield* database.use((db) =>
+ db.delete(Db.authApiKeys).where(eq(Db.authApiKeys.id, token)),
+ );
+ return { success: true as const };
+ }),
+ ),
+ )
+ .handle("bootstrap", () => withMappedErrors(getBootstrap()))
+ .handle("setActiveOrganization", ({ payload }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ yield* assertOrganizationAccess(payload.organizationId);
+ yield* database.use((db) =>
+ db
+ .update(Db.users)
+ .set({ activeOrganizationId: payload.organizationId })
+ .where(eq(Db.users.id, user.id)),
+ );
+ return yield* getBootstrap();
+ }),
+ ),
+ )
+ .handle("listCaps", ({ urlParams }) =>
+ withMappedErrors(getCapsList(urlParams)),
+ )
+ .handle("createFolder", ({ payload }) =>
+ withMappedErrors(createMobileFolder(payload)),
+ )
+ .handle("getCap", ({ path }) =>
+ withMappedErrors(getCapDetail(path.id)),
+ )
+ .handle("updateCapSharing", ({ path, payload }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ yield* getCapById(path.id);
+ yield* database.use((db) =>
+ db
+ .update(Db.videos)
+ .set({ public: payload.public })
+ .where(
+ and(
+ eq(Db.videos.id, path.id),
+ eq(Db.videos.ownerId, user.id),
+ ),
+ ),
+ );
+ const { cap } = yield* getCapById(path.id);
+ return cap;
+ }),
+ ),
+ )
+ .handle("updateCapTitle", ({ path, payload }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ yield* getCapById(path.id);
+ const title = payload.title.trim();
+ if (!title) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ yield* database.use((db) =>
+ db
+ .update(Db.videos)
+ .set({ name: title })
+ .where(
+ and(
+ eq(Db.videos.id, path.id),
+ eq(Db.videos.ownerId, user.id),
+ ),
+ ),
+ );
+ yield* Effect.sync(() => {
+ revalidatePath("/dashboard/caps");
+ revalidatePath("/dashboard/shared-caps");
+ revalidatePath(`/s/${path.id}`);
+ });
+ const { cap } = yield* getCapById(path.id);
+ return cap;
+ }),
+ ),
+ )
+ .handle("updateCapPassword", ({ path, payload }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ yield* getCapById(path.id);
+ const trimmedPassword = payload.password?.trim() ?? null;
+ const nextPassword = trimmedPassword
+ ? yield* Effect.tryPromise({
+ try: () => hashPassword(trimmedPassword),
+ catch: () => new HttpApiError.InternalServerError(),
+ })
+ : null;
+
+ yield* database.use((db) =>
+ db
+ .update(Db.videos)
+ .set({ password: nextPassword })
+ .where(
+ and(
+ eq(Db.videos.id, path.id),
+ eq(Db.videos.ownerId, user.id),
+ ),
+ ),
+ );
+ const { cap } = yield* getCapById(path.id);
+ return cap;
+ }),
+ ),
+ )
+ .handle("deleteCap", ({ path }) =>
+ withMappedErrors(
+ videos
+ .delete(path.id)
+ .pipe(Effect.map(() => ({ success: true as const }))),
+ ),
+ )
+ .handle("getPlayback", ({ path }) =>
+ withMappedErrors(getPlayback(path.id)),
+ )
+ .handle("getDownload", ({ path }) =>
+ withMappedErrors(
+ videos.getDownloadInfo(path.id).pipe(
+ Effect.flatMap(
+ Option.match({
+ onNone: () => Effect.fail(new HttpApiError.NotFound()),
+ onSome: (info) =>
+ Effect.succeed({
+ fileName: info.fileName,
+ url: info.downloadUrl,
+ }),
+ }),
+ ),
+ ),
+ ),
+ )
+ .handle("createComment", ({ path, payload }) =>
+ withMappedErrors(
+ createMobileComment({
+ videoId: path.id,
+ content: payload.content,
+ timestamp: payload.timestamp,
+ parentCommentId: payload.parentCommentId ?? null,
+ type: "text",
+ }),
+ ),
+ )
+ .handle("deleteComment", ({ path }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ const result = yield* database.use((db) =>
+ db
+ .delete(Db.comments)
+ .where(
+ and(
+ eq(Db.comments.id, path.id),
+ eq(Db.comments.authorId, user.id),
+ ),
+ ),
+ );
+ const affectedRows = Array.isArray(result)
+ ? (result[0]?.affectedRows ?? 0)
+ : 0;
+ if (affectedRows === 0) {
+ return yield* Effect.fail(new HttpApiError.NotFound());
+ }
+ return { success: true as const };
+ }),
+ ),
+ )
+ .handle("createReaction", ({ path, payload }) =>
+ withMappedErrors(
+ createMobileComment({
+ videoId: path.id,
+ content: payload.content,
+ timestamp: payload.timestamp,
+ parentCommentId: null,
+ type: "emoji",
+ }),
+ ),
+ )
+ .handle("createUpload", ({ payload }) =>
+ withMappedErrors(createUpload(payload)),
+ )
+ .handle("updateUploadProgress", ({ path, payload }) =>
+ withMappedErrors(
+ videos
+ .updateUploadProgress({
+ videoId: path.id,
+ uploaded: Math.max(0, Math.trunc(payload.uploaded)),
+ total: Math.max(0, Math.trunc(payload.total)),
+ updatedAt: new Date(),
+ })
+ .pipe(Effect.map(() => ({ success: true as const }))),
+ ),
+ )
+ .handle("completeUpload", ({ path, payload }) =>
+ withMappedErrors(
+ Effect.gen(function* () {
+ const user = yield* CurrentUser;
+ const [video] = yield* database.use((db) =>
+ db
+ .select({
+ id: Db.videos.id,
+ ownerId: Db.videos.ownerId,
+ bucketId: Db.videos.bucket,
+ })
+ .from(Db.videos)
+ .where(
+ and(
+ eq(Db.videos.id, path.id),
+ eq(Db.videos.ownerId, user.id),
+ ),
+ ),
+ );
+ if (!video)
+ return yield* Effect.fail(new HttpApiError.NotFound());
+
+ const prefix = `${user.id}/${path.id}/`;
+ if (!payload.rawFileKey.startsWith(prefix)) {
+ return yield* Effect.fail(new HttpApiError.BadRequest());
+ }
+
+ if (payload.contentLength !== undefined) {
+ yield* database.use((db) =>
+ db
+ .update(Db.videoUploads)
+ .set({
+ uploaded: payload.contentLength,
+ total: payload.contentLength,
+ updatedAt: new Date(),
+ })
+ .where(eq(Db.videoUploads.videoId, path.id)),
+ );
+ }
+
+ yield* Effect.tryPromise(() =>
+ startVideoProcessingWorkflow({
+ videoId: path.id,
+ userId: user.id,
+ rawFileKey: payload.rawFileKey,
+ bucketId: video.bucketId,
+ processingMessage: "Starting video processing...",
+ startFailureMessage:
+ "Video uploaded, but processing could not start.",
+ mode: "singlepart",
+ }),
+ ).pipe(
+ Effect.catchAll((error) =>
+ Effect.logError(error).pipe(
+ Effect.flatMap(() =>
+ Effect.fail(new HttpApiError.InternalServerError()),
+ ),
+ ),
+ ),
+ );
+
+ return { success: true as const };
+ }),
+ ),
+ );
+ }),
+ ),
+ ),
+);
+
+const handler = apiToHandler(ApiLive);
+
+export const GET = handler;
+export const POST = handler;
+export const PATCH = handler;
+export const DELETE = handler;