Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

32.1 KB
Raw Blame
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoIdLength } from "@cap/database/helpers";
import {
	comments,
	organizationMembers,
	organizations,
	sharedVideos,
	spaces,
	spaceVideos,
	users,
	videoEdits,
	videos,
	videoUploads,
	videoViewerGrants,
} from "@cap/database/schema";
import type { VideoMetadata } from "@cap/database/types";
import { buildEnv, serverEnv } from "@cap/env";
import { userIsPro } from "@cap/utils";
import {
	Database,
	ImageUploads,
	provideOptionalAuth,
	resolveEffectiveVideoRules,
	Videos,
} from "@cap/web-backend";
import { VideosPolicy } from "@cap/web-backend/src/Videos/VideosPolicy";
import {
	Comment,
	type ImageUpload,
	type Organisation,
	type Policy,
	type Video,
} from "@cap/web-domain";
import { and, eq, type InferSelectModel, isNull, sql } from "drizzle-orm";
import { Effect, Option } from "effect";
import type { Metadata } from "next";
import { headers } from "next/headers";
import { notFound } from "next/navigation";
import { after } from "next/server";
import {
	getDashboardSpacesData,
	type OrganizationSettings,
	type Spaces,
} from "@/app/(org)/dashboard/dashboard-data";
import { isAiConfigured } from "@/lib/ai/provider";
import { completeDesktopSegmentsManifestAndQueue } from "@/lib/desktop-segments-recovery";
import { isEarlyInstantPlaybackEnabled } from "@/lib/early-instant-playback";
import {
	getPendingLoomProcessing,
	startDeferredLoomProcessing,
} from "@/lib/loom-deferred-processing";
import { createNotification } from "@/lib/Notification";
import {
	canManageOrganizationSettings,
	getEffectiveOrganizationRole,
} from "@/lib/permissions/roles";
import { resolveDefaultPlaybackSpeed } from "@/lib/playback-speed";
import { getPublicShareVideo } from "@/lib/public-share-video";
import * as EffectRuntime from "@/lib/server";
import { runPromise } from "@/lib/server";
import { getSharePageBranding } from "@/lib/share-branding";
import { parseShareCallToAction } from "@/lib/share-call-to-action";
import { getShareDashboardDestination } from "@/lib/share-dashboard-destination";
import {
	canStartPlaybackInHtml,
	getSharePlaybackSource,
} from "@/lib/share-playback";
import { buildShareVideoMetadata } from "@/lib/share-video-metadata";
import { resolveShareWebUrl } from "@/lib/share-web-url";
import { isVideoOverShareableLinkLimit } from "@/lib/shareable-link-quota";
import {
	isIframelyCrawlerUserAgent,
	isSocialCrawlerUserAgent,
	SOCIAL_REFERRER_DOMAINS,
} from "@/lib/social-crawlers";
import { transcribeVideo } from "@/lib/transcribe";
import { canUserDownloadVideo } from "@/lib/video-download-permissions";
import { isEditSourceKey } from "@/lib/video-edit-processing";
import {
	areEditSpecsEquivalent,
	createIdentityEditSpec,
} from "@/lib/video-edits";
import { getVideoViewCount } from "@/lib/video-view-count";
import { optionFromTOrFirst } from "@/utils/effect";
import { isAiGenerationEnabled } from "@/utils/flags";
import {
	AccessRestrictedState,
	PrivateVideoState,
	SignInRequiredState,
} from "./_components/AccessStates";
import { PasswordGate } from "./_components/PasswordGate";
import { PendingRecordingShare } from "./_components/PendingRecordingShare";
import { ShareHeader } from "./_components/ShareHeader";
import { Share } from "./Share";

const VIEW_NOTIFICATION_DELAY_MS = 2 * 60 * 1000;
const VIDEO_ID_PATTERN = /^[0-9abcdefghjkmnpqrstvwxyz]+$/;

type ShareVideoSearchParams = {
	[key: string]: string | string[] | undefined;
};

const isValidVideoIdParam = (videoId: string) =>
	videoId.length === nanoIdLength && VIDEO_ID_PATTERN.test(videoId);

const hasRecordingStoppedParam = (searchParams: ShareVideoSearchParams) => {
	const recordingStoppedParam = Array.isArray(searchParams.recordingStopped)
		? searchParams.recordingStopped[0]
		: searchParams.recordingStopped;

	return recordingStoppedParam === "1" || recordingStoppedParam === "true";
};

function toShareVideo<
	T extends {
		password: unknown;
		ownerId: unknown;
		organizationTombstoneAt: Date | null;
	},
>(row: T) {
	const {
		password: _password,
		ownerId: _ownerId,
		organizationTombstoneAt: _organizationTombstoneAt,
		...video
	} = row;
	return video;
}

// Helper function to fetch shared spaces data for a video
// The longest the page waits for the playback URL beyond its other queries.
const SERVER_PLAYBACK_WAIT_MS = 50;

function resolveWithin<T>(promise: Promise<T>, ms: number): Promise<T | null> {
	return new Promise((resolve) => {
		const timer = setTimeout(() => resolve(null), ms);
		promise.then(
			(value) => {
				clearTimeout(timer);
				resolve(value);
			},
			() => {
				clearTimeout(timer);
				resolve(null);
			},
		);
	});
}

async function getSharedSpacesForVideo(videoId: Video.VideoId) {
	// Space-level and organization-level sharing are independent queries.
	const [spaceSharing, orgSharing] = await Promise.all([
		db()
			.select({
				id: spaces.id,
				name: spaces.name,
				organizationId: spaces.organizationId,
				iconUrl: spaces.iconUrl,
				settings: spaces.settings,
				hasPassword: sql`${spaces.password} IS NOT NULL`.mapWith(Boolean),
			})
			.from(spaceVideos)
			.innerJoin(spaces, eq(spaceVideos.spaceId, spaces.id))
			.innerJoin(organizations, eq(spaces.organizationId, organizations.id))
			.where(eq(spaceVideos.videoId, videoId)),
		db()
			.select({
				id: organizations.id,
				name: organizations.name,
				organizationId: organizations.id,
				iconUrl: organizations.iconUrl,
			})
			.from(sharedVideos)
			.innerJoin(
				organizations,
				eq(sharedVideos.organizationId, organizations.id),
			)
			.where(eq(sharedVideos.videoId, videoId)),
	]);

	const sharedSpaces: Array<{
		id: string;
		name: string;
		organizationId: string;
		iconUrl?: string;
		settings?: OrganizationSettings | null;
		hasPassword?: boolean;
	}> = [];

	// Add space-level sharing
	spaceSharing.forEach((space) => {
		sharedSpaces.push({
			id: space.id,
			name: space.name,
			organizationId: space.organizationId,
			iconUrl: space.iconUrl || undefined,
			settings: space.settings,
			hasPassword: space.hasPassword,
		});
	});

	// Add organization-level sharing
	orgSharing.forEach((org) => {
		sharedSpaces.push({
			id: org.id,
			name: org.name,
			organizationId: org.organizationId,
			iconUrl: org.iconUrl || undefined,
			settings: null,
			hasPassword: false,
		});
	});

	return {
		sharedSpaces,
		sharedOrganizations: orgSharing.map(({ id, name }) => ({ id, name })),
	};
}

function PolicyDeniedView({
	reason,
	videoId,
}: {
	reason?: string;
	videoId: Video.VideoId;
}) {
	if (reason === "email_restriction_login_required") {
		return <SignInRequiredState videoId={videoId} />;
	}
	if (reason === "email_restriction_denied") {
		return <AccessRestrictedState videoId={videoId} />;
	}
	return <PrivateVideoState videoId={videoId} />;
}

const renderPolicyDenied = (videoId: Video.VideoId, reason?: string) =>
	Effect.succeed(
		<PolicyDeniedView key={videoId} videoId={videoId} reason={reason} />,
	);

const renderNoSuchElement = (awaitRecording: boolean) =>
	awaitRecording
		? Effect.succeed(<PendingRecordingShare />)
		: Effect.sync(() => notFound());

const getShareVideoPageCatchers = (
	videoId: Video.VideoId,
	awaitRecording: boolean,
) => ({
	PolicyDenied: (e: Policy.PolicyDeniedError) =>
		renderPolicyDenied(videoId, e.reason),
	NoSuchElementException: () => renderNoSuchElement(awaitRecording),
});

export async function generateMetadata(
	props: PageProps<"/s/[videoId]">,
): Promise<Metadata> {
	const params = await props.params;
	const searchParams = await props.searchParams;
	const videoId = params.videoId as Video.VideoId;
	const awaitRecording =
		isValidVideoIdParam(videoId) && hasRecordingStoppedParam(searchParams);

	const headersList = await headers();
	const referrer =
		headersList.get("x-referrer") || headersList.get("referer") || "";
	const requestUserAgent = headersList.get("user-agent") || "";
	const isAllowedReferrer = SOCIAL_REFERRER_DOMAINS.some((domain) =>
		referrer.includes(domain),
	);
	const canRenderSocialPreview =
		isAllowedReferrer || isSocialCrawlerUserAgent(requestUserAgent);
	const shouldAdvertiseIframelyPlayer =
		isIframelyCrawlerUserAgent(requestUserAgent) &&
		(await getPublicShareVideo(videoId).catch(() => null)) !== null;
	// Share pages also serve verified custom domains. Metadata has to point at
	// the host the visitor used, or Slack drops the preview image.
	const webUrl = await resolveShareWebUrl(headersList);
	const ogImageUrl = new URL(
		`/api/video/og?videoId=${videoId}`,
		webUrl,
	).toString();

	return Effect.flatMap(Videos, (v) => v.getByIdForViewing(videoId)).pipe(
		Effect.map(
			Option.match({
				onNone: () =>
					awaitRecording
						? {
								title: "Cap: Preparing Video",
								description: "This recording is being made available.",
								robots: "noindex, nofollow",
							}
						: notFound(),
				onSome: ([video]) => {
					return {
						...buildShareVideoMetadata({
							videoId,
							name: video.name,
							sourceType: video.source.type,
							width: Option.getOrNull(video.width),
							height: Option.getOrNull(video.height),
							webUrl,
							canonicalWebUrl: buildEnv.NEXT_PUBLIC_WEB_URL,
							advertiseIframelyPlayer: shouldAdvertiseIframelyPlayer,
						}),
						robots: canRenderSocialPreview
							? "index, follow"
							: "noindex, nofollow",
					};
				},
			}),
		),
		Effect.catchTags({
			PolicyDenied: () =>
				Effect.succeed({
					title: "Cap: This video is restricted",
					description: "This video has restricted access.",
					openGraph: {
						images: [{ url: ogImageUrl, width: 1200, height: 630 }],
					},
					robots: "noindex, nofollow",
				}),
			VerifyVideoPasswordError: () =>
				Effect.succeed({
					title: "Cap: Password Protected Video",
					description: "This video is password protected.",
					openGraph: {
						images: [{ url: ogImageUrl, width: 1200, height: 630 }],
					},
					twitter: {
						card: "summary_large_image",
						title: "Cap: Password Protected Video",
						description: "This video is password protected.",
						images: [ogImageUrl],
					},
					robots: "noindex, nofollow",
				}),
		}),
		provideOptionalAuth,
		EffectRuntime.runPromise,
	);
}

export default async function ShareVideoPage(props: PageProps<"/s/[videoId]">) {
	const params = await props.params;
	const searchParams = await props.searchParams;
	const videoId = params.videoId as Video.VideoId;
	const awaitRecording =
		isValidVideoIdParam(videoId) && hasRecordingStoppedParam(searchParams);

	return Effect.gen(function* () {
		const videosPolicy = yield* VideosPolicy;
		const loadVideo = () =>
			db()
				.select({
					id: videos.id,
					folderId: videos.folderId,
					name: videos.name,
					orgId: videos.orgId,
					createdAt: videos.createdAt,
					updatedAt: videos.updatedAt,
					effectiveCreatedAt: videos.effectiveCreatedAt,
					bucket: videos.bucket,
					storageIntegrationId: videos.storageIntegrationId,
					metadata: videos.metadata,
					public: videos.public,
					videoStartTime: videos.videoStartTime,
					audioStartTime: videos.audioStartTime,
					awsRegion: videos.awsRegion,
					awsBucket: videos.awsBucket,
					xStreamInfo: videos.xStreamInfo,
					jobId: videos.jobId,
					jobStatus: videos.jobStatus,
					isScreenshot: videos.isScreenshot,
					skipProcessing: videos.skipProcessing,
					transcriptionStatus: videos.transcriptionStatus,
					source: videos.source,
					videoSettings: videos.settings,
					width: videos.width,
					height: videos.height,
					duration: videos.duration,
					fps: videos.fps,
					firstViewEmailSentAt: videos.firstViewEmailSentAt,
					hasPassword: sql`${videos.password} IS NOT NULL`.mapWith(Boolean),
					sharedOrganization: {
						organizationId: sharedVideos.organizationId,
					},
					orgSettings: organizations.settings,
					allowedEmailDomain: organizations.allowedEmailDomain,
					organizationName: organizations.name,
					organizationIconUrl: organizations.iconUrl,
					shareableLinkIconUrl: organizations.shareableLinkIconUrl,
					hasActiveUpload:
						sql`${videoUploads.videoId} IS NOT NULL AND ${videos.isScreenshot} = false`.mapWith(
							Boolean,
						),
					activeUploadRawFileKey: videoUploads.rawFileKey,
					owner: users,
					ownerId: videos.ownerId,
					password: videos.password,
					organizationTombstoneAt: organizations.tombstoneAt,
				})
				.from(videos)
				.leftJoin(sharedVideos, eq(videos.id, sharedVideos.videoId))
				.innerJoin(users, eq(videos.ownerId, users.id))
				.leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
				.leftJoin(organizations, eq(videos.orgId, organizations.id))
				.where(eq(videos.id, videoId));
		const [row] = yield* Effect.promise(loadVideo);

		if (row) {
			yield* videosPolicy.canViewLoaded(row, Option.fromNullable(row.password));
		}

		return Option.fromNullable(
			row && row.organizationTombstoneAt === null ? toShareVideo(row) : null,
		);
	}).pipe(
		Effect.flatten,
		Effect.map((video) => ({ needsPassword: false, video }) as const),
		Effect.catchTag("VerifyVideoPasswordError", () =>
			Effect.succeed({ needsPassword: true } as const),
		),
		Effect.map((data) =>
			data.needsPassword ? (
				<PasswordGate key={videoId} videoId={videoId} />
			) : (
				// overflow-x-clip: the timeline view's theater strip breaks out to
				// 100vw, which overshoots by the scrollbar width on OSes with classic
				// scrollbars; clipping here keeps the page from gaining a horizontal
				// scroll without creating a new scroll container.
				// Desktop pins the page to the viewport so the comments rail can run
				// full height and the video column scrolls on its own; phones keep
				// ordinary document flow.
				<div
					key={videoId}
					className="flex overflow-x-clip flex-col min-h-screen bg-gray-2 lg:h-screen lg:min-h-0 lg:overflow-hidden"
				>
					<AuthorizedContent video={data.video} searchParams={searchParams} />
				</div>
			),
		),
		Effect.catchTags(getShareVideoPageCatchers(videoId, awaitRecording)),
		provideOptionalAuth,
		EffectRuntime.runPromise,
	);
}

async function AuthorizedContent({
	video,
	searchParams,
}: {
	video: Omit<
		InferSelectModel<typeof videos>,
		"password" | "settings" | "ownerId"
	> & {
		owner: InferSelectModel<typeof users>;
		sharedOrganization: { organizationId: Organisation.OrganisationId } | null;
		hasPassword: boolean;
		hasActiveUpload: boolean;
		activeUploadRawFileKey: string | null;
		orgSettings?: OrganizationSettings | null;
		allowedEmailDomain?: string | null;
		videoSettings?: OrganizationSettings | null;
		organizationName?: string | null;
		organizationIconUrl?: ImageUpload.ImageUrlOrKey | null;
		shareableLinkIconUrl?: ImageUpload.ImageUrlOrKey | null;
	};
	searchParams: ShareVideoSearchParams;
}) {
	// will have already been fetched if auth is required
	const user = await getCurrentUser();
	const videoId = video.id;
	let recoveredDesktopSegmentsUpload = false;

	if (
		user?.id === video.owner.id &&
		!video.isScreenshot &&
		video.source?.type === "desktopSegments" &&
		!video.hasActiveUpload &&
		serverEnv().MEDIA_SERVER_URL
	) {
		try {
			const result = await completeDesktopSegmentsManifestAndQueue({
				videoId,
				userId: user.id,
			});
			recoveredDesktopSegmentsUpload =
				result.status === "queued" ||
				result.status === "already-processing" ||
				result.status === "source-committing";
		} catch (error) {
			console.error(
				`[ShareVideoPage] Failed to recover desktop segments upload ${videoId}:`,
				error,
			);
		}
	}

	const hasActiveUpload =
		video.hasActiveUpload || recoveredDesktopSegmentsUpload;
	const canRegisterView =
		!hasActiveUpload &&
		Date.now() - video.updatedAt.getTime() >= VIEW_NOTIFICATION_DELAY_MS;

	const viewNotificationPromise =
		user && user.id !== video.owner.id && canRegisterView
			? createNotification({
					type: "view",
					videoId: video.id,
					authorId: user.id,
				}).catch((error) => {
					console.warn("Failed to create view notification:", error);
				})
			: Promise.resolve();

	const userId = user?.id;
	const commentId = optionFromTOrFirst(searchParams.comment).pipe(
		Option.map(Comment.CommentId.make),
	);
	const replyId = optionFromTOrFirst(searchParams.reply).pipe(
		Option.map(Comment.CommentId.make),
	);
	const recordingStopped = hasRecordingStoppedParam(searchParams);
	const earlySegmentPlayback =
		recordingStopped &&
		video.source?.type === "desktopSegments" &&
		isEarlyInstantPlaybackEnabled(video.owner.id);

	// Everything below is an independent round trip (DB or storage); each is
	// started here and awaited together further down, so the page pays for the
	// slowest one instead of the sum of all of them.
	const spacesDataPromise: Promise<Spaces[] | null> = user
		? getDashboardSpacesData(user).catch((error) => {
				console.error("Failed to fetch spaces data for sharing dialog:", error);
				return [];
			})
		: Promise.resolve(null);

	const sharedSpacesPromise = getSharedSpacesForVideo(videoId);
	const viewerCountPromise =
		user?.id === video.owner.id
			? db()
					.select({ count: sql<number>`count(*)`.mapWith(Number) })
					.from(videoViewerGrants)
					.where(
						and(
							eq(videoViewerGrants.videoId, videoId),
							isNull(videoViewerGrants.revokedAt),
						),
					)
					.then(([row]) => row?.count ?? 0)
			: Promise.resolve(0);

	const ownerIsPro = userIsPro(video.owner);

	// Fail-open: a broken count must never take the share page down.
	const overShareLimitPromise = ownerIsPro
		? Promise.resolve(false)
		: isVideoOverShareableLinkLimit({
				id: videoId,
				ownerId: video.owner.id,
				createdAt: video.createdAt,
				isScreenshot: video.isScreenshot,
			}).catch((error) => {
				console.error(
					`[ShareVideoPage] Shareable link quota check failed for ${videoId}:`,
					error,
				);
				return false;
			});
	const initialPlaybackSourcePromise =
		!video.isScreenshot &&
		!hasActiveUpload &&
		(video.source.type === "desktopMP4" || video.source.type === "webMP4")
			? overShareLimitPromise.then((overLimit) =>
					overLimit ? null : getSharePlaybackSource(video),
				)
			: undefined;
	const initialPlaybackUrlPromise = initialPlaybackSourcePromise?.then(
		(source) => source?.url ?? null,
	);
	// On Cap's own CDN the signed URL is ready as soon as the share-limit check
	// is (signing is local), so it is awaited with the rest of the page and
	// rendered into the HTML: the browser starts the video download before the
	// player hydrates. Custom buckets and storage integrations keep resolving it
	// on the client, behind the probe that falls back to the playlist route, and
	// so does this source if it is not ready in time.
	const serverPlaybackSourcePromise =
		buildEnv.NEXT_PUBLIC_IS_CAP === "true" &&
		!video.bucket &&
		!video.storageIntegrationId &&
		initialPlaybackSourcePromise &&
		canStartPlaybackInHtml((await headers()).get("user-agent"))
			? resolveWithin(initialPlaybackSourcePromise, SERVER_PLAYBACK_WAIT_MS)
			: Promise.resolve(null);

	const aiGenerationEnabledPromise = isAiGenerationEnabled(video.owner);
	const imagesPromise = Effect.gen(function* () {
		const imageUploads = yield* ImageUploads;
		const resolveImage = (
			image: ImageUpload.ImageUrlOrKey | null | undefined,
		) => (image ? imageUploads.resolveImageUrl(image) : Effect.succeed(null));
		return yield* Effect.all(
			{
				owner: resolveImage(video.owner.image),
				organization: resolveImage(video.organizationIconUrl),
				shareableLink: resolveImage(video.shareableLinkIconUrl),
			},
			{ concurrency: 3 },
		);
	}).pipe(runPromise);

	const screenshotImageUrlPromise = video.isScreenshot
		? Effect.flatMap(Videos, (videos) => videos.getThumbnailURL(videoId)).pipe(
				Effect.map(Option.getOrNull),
				provideOptionalAuth,
				runPromise,
			)
		: Promise.resolve(null);

	const customDomainPromise = (async () => {
		if (!user || user.id !== video.owner.id) {
			return { customDomain: null, domainVerified: false };
		}
		const activeOrganizationId = user.activeOrganizationId;
		if (!activeOrganizationId) {
			return { customDomain: null, domainVerified: false };
		}

		// Fetch the active org
		const orgArr = await db()
			.select({
				customDomain: organizations.customDomain,
				domainVerified: organizations.domainVerified,
			})
			.from(organizations)
			.where(eq(organizations.id, activeOrganizationId))
			.limit(1);

		const org = orgArr[0];
		if (
			org?.customDomain &&
			org.domainVerified !== null &&
			user.id === video.owner.id
		) {
			return { customDomain: org.customDomain, domainVerified: true };
		}
		return { customDomain: null, domainVerified: false };
	})();

	const userOrganizationsPromise = (async () => {
		if (!userId) return [];

		const [ownedOrganizations, memberOrganizations] = await Promise.all([
			db()
				.select({ id: organizations.id, name: organizations.name })
				.from(organizations)
				.where(eq(organizations.ownerId, userId)),
			db()
				.select({ id: organizations.id, name: organizations.name })
				.from(organizations)
				.innerJoin(
					organizationMembers,
					eq(organizations.id, organizationMembers.organizationId),
				)
				.where(eq(organizationMembers.userId, userId)),
		]);

		const allOrganizations = [...ownedOrganizations, ...memberOrganizations];
		const uniqueOrganizationIds = new Set();

		return allOrganizations.filter((organization) => {
			if (uniqueOrganizationIds.has(organization.id)) return false;
			uniqueOrganizationIds.add(organization.id);
			return true;
		});
	})();

	const membersListPromise = video.sharedOrganization?.organizationId
		? db()
				.select({ userId: organizationMembers.userId })
				.from(organizationMembers)
				.where(
					eq(
						organizationMembers.organizationId,
						video.sharedOrganization.organizationId,
					),
				)
		: Promise.resolve([]);

	const commentsPromise = Effect.gen(function* () {
		const db = yield* Database;
		const imageUploads = yield* ImageUploads;

		let toplLevelCommentId = Option.none<Comment.CommentId>();

		if (Option.isSome(replyId)) {
			const [parentComment] = yield* db.use((db) =>
				db
					.select({ parentCommentId: comments.parentCommentId })
					.from(comments)
					.where(eq(comments.id, replyId.value))
					.limit(1),
			);
			toplLevelCommentId = Option.fromNullable(parentComment?.parentCommentId);
		}

		const commentToBringToTheTop = Option.orElse(
			toplLevelCommentId,
			() => commentId,
		);

		return yield* db
			.use((db) =>
				db
					.select({
						id: comments.id,
						content: comments.content,
						timestamp: comments.timestamp,
						type: comments.type,
						authorId: comments.authorId,
						videoId: comments.videoId,
						createdAt: comments.createdAt,
						updatedAt: comments.updatedAt,
						parentCommentId: comments.parentCommentId,
						mediaKey: comments.mediaKey,
						mediaDuration: comments.mediaDuration,
						mediaMeta: comments.mediaMeta,
						authorName: users.name,
						authorImage: users.image,
					})
					.from(comments)
					.leftJoin(users, eq(comments.authorId, users.id))
					.where(eq(comments.videoId, videoId))
					.orderBy(
						Option.match(commentToBringToTheTop, {
							onSome: (commentId) =>
								sql`CASE WHEN ${comments.id} = ${commentId} THEN 0 ELSE 1 END, ${comments.createdAt}`,
							onNone: () => comments.createdAt,
						}),
					),
			)
			.pipe(
				Effect.map((comments) =>
					comments.map(
						Effect.fn(function* (c) {
							return Object.assign(c, {
								authorImage: yield* Option.fromNullable(c.authorImage).pipe(
									Option.map(imageUploads.resolveImageUrl),
									Effect.transposeOption,
									Effect.map(Option.getOrNull),
								),
							});
						}),
					),
				),
				Effect.flatMap(Effect.all),
			);
	}).pipe(EffectRuntime.runPromise);

	const viewsPromise = getVideoViewCount(videoId).then((v) => v.count);

	const videoOrganizationRolePromise = (async () => {
		if (!userId) return null;

		const [organizationAccess] = await db()
			.select({
				ownerId: organizations.ownerId,
				memberRole: organizationMembers.role,
			})
			.from(organizations)
			.leftJoin(
				organizationMembers,
				and(
					eq(organizationMembers.organizationId, organizations.id),
					eq(organizationMembers.userId, userId),
				),
			)
			.where(
				and(
					eq(organizations.id, video.orgId),
					isNull(organizations.tombstoneAt),
				),
			)
			.limit(1);

		if (!organizationAccess) return null;

		return getEffectiveOrganizationRole({
			userId,
			ownerId: organizationAccess.ownerId,
			memberRole: organizationAccess.memberRole,
		});
	})();

	const isVideoDownloadReady =
		!hasActiveUpload && video.source?.type !== "desktopSegments";

	const canDownloadVideoPromise =
		userId && isVideoDownloadReady
			? canUserDownloadVideo({
					userId,
					ownerId: video.owner.id,
					videoId,
				})
			: Promise.resolve(false);

	const dashboardDestinationPromise = getShareDashboardDestination({
		viewer: user
			? { id: user.id, activeOrganizationId: user.activeOrganizationId }
			: null,
		videoId,
		ownerId: video.owner.id,
		videoOrganizationId: video.orgId,
	}).catch((error) => {
		console.error(
			`[ShareVideoPage] Dashboard destination lookup failed for ${videoId}:`,
			error,
		);
		return null;
	});

	const videoHasEditsPromise = canDownloadVideoPromise.then((canDownload) => {
		if (!canDownload || video.isScreenshot) return false;
		return db()
			.select({ editSpec: videoEdits.editSpec })
			.from(videoEdits)
			.where(eq(videoEdits.videoId, videoId))
			.then(([videoEditRow]) =>
				videoEditRow
					? !areEditSpecsEquivalent(
							videoEditRow.editSpec,
							createIdentityEditSpec(videoEditRow.editSpec.sourceDuration),
						)
					: false,
			);
	});

	const [
		spacesData,
		{ sharedSpaces, sharedOrganizations },
		viewerCount,
		aiGenerationEnabled,
		screenshotImageUrl,
		membersList,
		userOrganizations,
		{ customDomain, domainVerified },
		videoOrganizationRole,
		canDownloadVideo,
		videoHasEdits,
		ownerIsOverShareLimit,
		resolvedImages,
		dashboardDestination,
		serverPlaybackSource,
	] = await Promise.all([
		spacesDataPromise,
		sharedSpacesPromise,
		viewerCountPromise,
		aiGenerationEnabledPromise,
		screenshotImageUrlPromise,
		membersListPromise,
		userOrganizationsPromise,
		customDomainPromise,
		videoOrganizationRolePromise,
		canDownloadVideoPromise,
		videoHasEditsPromise,
		overShareLimitPromise,
		imagesPromise,
		dashboardDestinationPromise,
		serverPlaybackSourcePromise,
		viewNotificationPromise,
	]);

	const rules = resolveEffectiveVideoRules({
		videoSettings: video.videoSettings,
		organizationSettings: video.orgSettings,
		spaces: sharedSpaces.filter((space) => space.id !== space.organizationId),
	});
	const env = serverEnv();
	const transcriptionGenerationAvailable =
		!video.isScreenshot &&
		Boolean(env.ASSEMBLY_API_KEY) &&
		!rules.settings.disableTranscript;
	const aiProviderAvailable = isAiConfigured();

	if (
		transcriptionGenerationAvailable &&
		!hasActiveUpload &&
		video.transcriptionStatus !== "COMPLETE" &&
		video.transcriptionStatus !== "PROCESSING" &&
		video.transcriptionStatus !== "SKIPPED" &&
		video.transcriptionStatus !== "NO_AUDIO" &&
		video.transcriptionStatus !== "ERROR"
	) {
		console.log("[ShareVideoPage] Starting transcription for video:", videoId);
		transcribeVideo(videoId, video.owner.id, aiGenerationEnabled).catch(
			(error) => {
				console.error(
					`[ShareVideoPage] Error transcribing video ${videoId}:`,
					error,
				);
			},
		);
	}

	const metadata = (video.metadata as VideoMetadata) || {};
	const deferredLoomRawFileKey = hasActiveUpload
		? null
		: getPendingLoomProcessing(metadata, video.owner.id, videoId);
	if (deferredLoomRawFileKey) {
		const rawFileKey = deferredLoomRawFileKey;
		after(() =>
			startDeferredLoomProcessing({
				videoId,
				ownerId: video.owner.id,
				bucketId: video.bucket,
				rawFileKey,
			}).catch((error) => {
				console.error(
					`[ShareVideoPage] Error starting deferred processing for ${videoId}:`,
					error,
				);
			}),
		);
	}
	const aiGenerationStatus = metadata.aiGenerationStatus || null;

	const initialAiData = {
		title: metadata.aiTitle || null,
		summary: metadata.summary || null,
		chapters: metadata.chapters || null,
		aiGenerationStatus,
	};

	const videoWithOrganizationInfo = {
		...video,
		hasActiveUpload,
		ownerIsOverShareLimit,
		owner: {
			id: video.owner.id,
			name: video.owner.name,
			isPro: ownerIsPro,
			image: resolvedImages.owner,
		},
		organization: {
			organizationMembers: membersList.map((member) => member.userId),
			organizationId: video.sharedOrganization?.organizationId ?? undefined,
		},
		sharedOrganizations: sharedOrganizations,
		password: null,
		folderId: user?.id === video.owner.id ? video.folderId : null,
		orgSettings: video.orgSettings || null,
		organizationName: video.organizationName,
		organizationIconUrl: resolvedImages.organization,
		shareableLinkIconUrl: resolvedImages.shareableLink,
		settings: rules.settings,
		hasInheritedPassword: rules.hasInheritedPassword,
		inheritedPasswordSources: rules.inheritedPasswordSources,
		inheritedSpaceSettings: rules.inheritedSettings,
		callToAction: ownerIsPro
			? parseShareCallToAction(video.videoSettings)
			: null,
	};
	const isEditProcessing =
		isEditSourceKey({
			ownerId: video.owner.id,
			videoId,
			rawFileKey: video.activeUploadRawFileKey,
		}) && !video.isScreenshot;

	const defaultPlaybackSpeed = resolveDefaultPlaybackSpeed(
		video.videoSettings?.defaultPlaybackSpeed,
		video.orgSettings?.defaultPlaybackSpeed,
	);

	const initialShareView =
		optionFromTOrFirst(searchParams.view).pipe(Option.getOrNull) ===
			"timeline" && !video.isScreenshot
			? ("timeline" as const)
			: ("classic" as const);
	const captionsInitiallyOff =
		optionFromTOrFirst(searchParams.captions).pipe(Option.getOrNull) === "off";
	// Recording media comments rides on the VIDEO OWNER's Pro plan; the
	// upload/create paths re-check server-side via canUseMediaComments.
	const canRecordMedia = Boolean(user) && videoWithOrganizationInfo.owner.isPro;

	// Bottom padding lives in Share (classic view only): the timeline view's
	// theater strip must end flush with the viewport, and Share is the one that
	// knows which view is active. Gutters moved into Share too — the comments
	// rail is flush to the viewport edge, so only the video column is centred
	// in a container.
	return (
		<div className="flex flex-col flex-1 min-h-0">
			<Share
				header={
					<ShareHeader
						canMoveToFolder={
							user?.id === video.owner.id && videoOrganizationRole !== null
						}
						data={{
							...videoWithOrganizationInfo,
							createdAt: video.metadata?.customCreatedAt
								? new Date(video.metadata.customCreatedAt)
								: video.createdAt,
						}}
						customDomain={customDomain}
						domainVerified={domainVerified}
						allowedEmailDomain={video.allowedEmailDomain}
						sharedOrganizations={
							videoWithOrganizationInfo.sharedOrganizations || []
						}
						sharedSpaces={sharedSpaces}
						viewerCount={viewerCount}
						userOrganizations={userOrganizations}
						spacesData={spacesData}
						branding={getSharePageBranding(videoWithOrganizationInfo)}
						canManageSharePageBranding={canManageOrganizationSettings(
							videoOrganizationRole,
						)}
						canDownload={canDownloadVideo}
						hasEdits={videoHasEdits}
						// Caught separately from the copy the sidebar consumes: the
						// header renders for everyone, and a failed count is worth
						// less than the header it would otherwise take down.
						views={viewsPromise.catch(() => null)}
						dashboardDestination={dashboardDestination}
					/>
				}
				dashboardDestination={dashboardDestination}
				data={videoWithOrganizationInfo}
				initialPlaybackUrl={initialPlaybackUrlPromise}
				serverPlaybackUrl={
					serverPlaybackSource?.defaultStorage ? serverPlaybackSource.url : null
				}
				screenshotImageUrl={screenshotImageUrl}
				videoSettings={videoWithOrganizationInfo.settings}
				comments={commentsPromise}
				views={viewsPromise}
				customDomain={customDomain}
				domainVerified={domainVerified}
				userOrganizations={userOrganizations}
				viewerId={user?.id ?? null}
				viewerSignedIn={user !== null}
				initialView={initialShareView}
				captionsInitiallyOff={captionsInitiallyOff}
				canRecordMedia={canRecordMedia}
				isEditProcessing={isEditProcessing}
				recordingStopped={recordingStopped}
				earlySegmentPlayback={earlySegmentPlayback}
				defaultPlaybackSpeed={defaultPlaybackSpeed}
				initialAiData={initialAiData}
				aiGenerationAvailable={aiGenerationEnabled && aiProviderAvailable}
				transcriptionGenerationAvailable={transcriptionGenerationAvailable}
			/>
		</div>
	);
}