Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web): add Slack link unfurl processing
Richie McIlroyd3346fb69821
diff --git a/apps/web/__tests__/unit/slack-unfurl.test.ts b/apps/web/__tests__/unit/slack-unfurl.test.tsnew file mode 100644index 000000000..510cb4256--- /dev/null+++ b/apps/web/__tests__/unit/slack-unfurl.test.ts@@ -0,0 +1,200 @@+import { Video } from "@cap/web-domain";+import { describe, expect, it, vi } from "vitest";+import type { PublicShareVideo } from "@/lib/public-share-video";+import {+ buildSlackVideoBlock,+ parseSlackEventPayload,+ processSlackEvent,+} from "@/lib/slack/unfurl";++const video: PublicShareVideo = {+ id: Video.VideoId.make("abc123"),+ name: "Cap walkthrough",+ ownerId: "owner123",+ ownerName: "Richie",+ sourceType: "desktopMP4",+ width: 1920,+ height: 1080,+ duration: 64,+};++const payload = {+ type: "event_callback" as const,+ team_id: "T123",+ event_id: "E123",+ event: {+ type: "link_shared" as const,+ channel: "C123",+ message_ts: "123.456",+ unfurl_id: "U123",+ source: "conversations_history",+ links: [+ {+ domain: "cap.so",+ url: "https://cap.so/s/abc123",+ },+ ],+ },+};++describe("Slack video unfurls", () => {+ it("parses link and uninstall events without accepting malformed payloads", () => {+ expect(parseSlackEventPayload(payload)).toEqual(payload);+ expect(+ parseSlackEventPayload({+ type: "event_callback",+ team_id: "T123",+ event: { type: "app_uninstalled" },+ }),+ ).toMatchObject({+ type: "event_callback",+ team_id: "T123",+ event: { type: "app_uninstalled" },+ });+ expect(parseSlackEventPayload({ type: "event_callback" })).toBeNull();+ });++ it("builds a Slack-compliant navigation-free video block URL", () => {+ expect(+ buildSlackVideoBlock({+ video,+ shareUrl: "https://cap.so/s/abc123",+ webUrl: "https://cap.so",+ }),+ ).toMatchObject({+ type: "video",+ title_url: "https://cap.so/s/abc123",+ video_url: "https://cap.so/embed/abc123?autoplay=true&slack=true",+ thumbnail_url: "https://cap.so/api/video/og?videoId=abc123",+ provider_name: "Cap",+ author_name: "Richie",+ });+ });++ it("keeps video block text within Slack's strict limits", () => {+ const block = buildSlackVideoBlock({+ video: {+ ...video,+ name: "V".repeat(250),+ ownerName: "A".repeat(80),+ },+ shareUrl: "https://cap.so/s/abc123",+ webUrl: "https://cap.so",+ });++ expect(block.title.text).toHaveLength(199);+ expect(block.author_name).toHaveLength(49);+ });++ it("publishes only eligible links using the event unfurl locator", async () => {+ const sendUnfurl = vi.fn(async () => undefined);+ await processSlackEvent({+ payload,+ webUrl: "https://cap.so",+ dependencies: {+ deleteInstallation: vi.fn(async () => undefined),+ getInstallationToken: vi.fn(async () => "xoxb-token"),+ getVideo: vi.fn(async () => video),+ sendUnfurl,+ },+ });++ expect(sendUnfurl).toHaveBeenCalledWith({+ token: "xoxb-token",+ event: {+ channel: "C123",+ messageTs: "123.456",+ unfurlId: "U123",+ source: "conversations_history",+ },+ unfurls: {+ "https://cap.so/s/abc123": {+ blocks: [expect.objectContaining({ type: "video" })],+ },+ },+ });+ });++ it("uses the canonical Cap recording URL for legacy cap.link unfurls", async () => {+ const sendUnfurl = vi.fn(async () => undefined);+ await processSlackEvent({+ payload: {+ ...payload,+ event: {+ ...payload.event,+ links: [+ {+ domain: "cap.link",+ url: "https://cap.link/abc123",+ },+ ],+ },+ },+ webUrl: "https://cap.so",+ dependencies: {+ deleteInstallation: vi.fn(async () => undefined),+ getInstallationToken: vi.fn(async () => "xoxb-token"),+ getVideo: vi.fn(async () => video),+ sendUnfurl,+ },+ });++ expect(sendUnfurl).toHaveBeenCalledWith(+ expect.objectContaining({+ unfurls: {+ "https://cap.link/abc123": {+ blocks: [+ expect.objectContaining({+ title_url: "https://cap.so/s/abc123",+ }),+ ],+ },+ },+ }),+ );+ });++ it("degrades silently when a link is restricted or the app is not installed", async () => {+ const sendUnfurl = vi.fn(async () => undefined);+ await processSlackEvent({+ payload,+ webUrl: "https://cap.so",+ dependencies: {+ deleteInstallation: vi.fn(async () => undefined),+ getInstallationToken: vi.fn(async () => "xoxb-token"),+ getVideo: vi.fn(async () => null),+ sendUnfurl,+ },+ });+ await processSlackEvent({+ payload,+ webUrl: "https://cap.so",+ dependencies: {+ deleteInstallation: vi.fn(async () => undefined),+ getInstallationToken: vi.fn(async () => null),+ getVideo: vi.fn(async () => video),+ sendUnfurl,+ },+ });+ expect(sendUnfurl).not.toHaveBeenCalled();+ });++ it("removes credentials when Slack reports an uninstall", async () => {+ const deleteInstallation = vi.fn(async () => undefined);+ await processSlackEvent({+ payload: {+ type: "event_callback",+ team_id: "T123",+ event: { type: "app_uninstalled" },+ },+ webUrl: "https://cap.so",+ dependencies: {+ deleteInstallation,+ getInstallationToken: vi.fn(async () => null),+ getVideo: vi.fn(async () => null),+ sendUnfurl: vi.fn(async () => undefined),+ },+ });+ expect(deleteInstallation).toHaveBeenCalledWith("T123");+ });+});diff --git a/apps/web/lib/slack/unfurl.ts b/apps/web/lib/slack/unfurl.tsnew file mode 100644index 000000000..425f0b27e--- /dev/null+++ b/apps/web/lib/slack/unfurl.ts@@ -0,0 +1,221 @@+import type { Video } from "@cap/web-domain";+import { parseCapShareUrl } from "@/lib/oembed";+import {+ getPublicShareVideo,+ type PublicShareVideo,+} from "@/lib/public-share-video";+import { sendSlackUnfurl } from "./client";+import {+ deleteSlackInstallationByTeamId,+ getSlackInstallationToken,+} from "./installations";++type SlackLink = {+ domain: string;+ url: string;+};++export type SlackLinkSharedEvent = {+ type: "link_shared";+ channel: string;+ message_ts: string;+ unfurl_id?: string;+ source?: string;+ links: SlackLink[];+};++export type SlackEventPayload =+ | {+ type: "event_callback";+ team_id: string;+ event_id?: string;+ event: SlackLinkSharedEvent | { type: "app_uninstalled" };+ }+ | {+ type: "url_verification";+ challenge: string;+ };++const truncate = (value: string, maxLength: number) =>+ Array.from(value).slice(0, maxLength).join("");++const formatDuration = (duration: number | null) => {+ if (!duration || duration <= 0) return "Watch this recording on Cap";+ const seconds = Math.round(duration);+ const minutes = Math.floor(seconds / 60);+ const remainder = seconds % 60;+ return `Watch this ${minutes}:${remainder.toString().padStart(2, "0")} recording on Cap`;+};++export const buildSlackVideoBlock = ({+ video,+ shareUrl,+ webUrl,+}: {+ video: PublicShareVideo;+ shareUrl: string;+ webUrl: string;+}) => {+ const title = truncate(video.name.trim() || "Cap recording", 199);+ const embedUrl = new URL(`/embed/${video.id}`, webUrl);+ embedUrl.searchParams.set("autoplay", "true");+ embedUrl.searchParams.set("slack", "true");+ const thumbnailUrl = new URL("/api/video/og", webUrl);+ thumbnailUrl.searchParams.set("videoId", video.id);++ return {+ type: "video",+ title: {+ type: "plain_text",+ text: title,+ emoji: true,+ },+ title_url: shareUrl,+ description: {+ type: "plain_text",+ text: formatDuration(video.duration),+ emoji: true,+ },+ video_url: embedUrl.toString(),+ alt_text: truncate(`Watch ${video.name}`, 200),+ thumbnail_url: thumbnailUrl.toString(),+ provider_name: "Cap",+ ...(video.ownerName ? { author_name: truncate(video.ownerName, 49) } : {}),+ };+};++const isRecord = (value: unknown): value is Record<string, unknown> =>+ Boolean(value) && typeof value === "object" && !Array.isArray(value);++export const parseSlackEventPayload = (+ value: unknown,+): SlackEventPayload | null => {+ if (!isRecord(value) || typeof value.type !== "string") return null;+ if (+ value.type === "url_verification" &&+ typeof value.challenge === "string"+ ) {+ return { type: "url_verification", challenge: value.challenge };+ }+ if (+ value.type !== "event_callback" ||+ typeof value.team_id !== "string" ||+ !isRecord(value.event) ||+ typeof value.event.type !== "string"+ ) {+ return null;+ }+ if (value.event.type === "app_uninstalled") {+ return {+ type: "event_callback",+ team_id: value.team_id,+ event_id: typeof value.event_id === "string" ? value.event_id : undefined,+ event: { type: "app_uninstalled" },+ };+ }+ if (+ value.event.type !== "link_shared" ||+ typeof value.event.channel !== "string" ||+ typeof value.event.message_ts !== "string" ||+ !Array.isArray(value.event.links)+ ) {+ return null;+ }++ const links = value.event.links+ .filter(isRecord)+ .filter(+ (link): link is SlackLink =>+ typeof link.domain === "string" && typeof link.url === "string",+ );+ if (links.length === 0) return null;++ return {+ type: "event_callback",+ team_id: value.team_id,+ event_id: typeof value.event_id === "string" ? value.event_id : undefined,+ event: {+ type: "link_shared",+ channel: value.event.channel,+ message_ts: value.event.message_ts,+ unfurl_id:+ typeof value.event.unfurl_id === "string"+ ? value.event.unfurl_id+ : undefined,+ source:+ typeof value.event.source === "string" ? value.event.source : undefined,+ links,+ },+ };+};++type ProcessSlackEventDependencies = {+ deleteInstallation: typeof deleteSlackInstallationByTeamId;+ getInstallationToken: typeof getSlackInstallationToken;+ getVideo: typeof getPublicShareVideo;+ sendUnfurl: typeof sendSlackUnfurl;+};++const defaultDependencies: ProcessSlackEventDependencies = {+ deleteInstallation: deleteSlackInstallationByTeamId,+ getInstallationToken: getSlackInstallationToken,+ getVideo: getPublicShareVideo,+ sendUnfurl: sendSlackUnfurl,+};++export const processSlackEvent = async ({+ payload,+ webUrl,+ dependencies = defaultDependencies,+}: {+ payload: Exclude<SlackEventPayload, { type: "url_verification" }>;+ webUrl: string;+ dependencies?: ProcessSlackEventDependencies;+}) => {+ if (payload.event.type === "app_uninstalled") {+ await dependencies.deleteInstallation(payload.team_id);+ return;+ }++ const token = await dependencies.getInstallationToken(payload.team_id);+ if (!token) return;++ const uniqueLinks = [+ ...new Map(payload.event.links.map((link) => [link.url, link])).values(),+ ].slice(0, 5);+ const resolved = await Promise.all(+ uniqueLinks.map(async (link) => {+ const videoId = parseCapShareUrl(link.url);+ if (!videoId) return null;+ const video = await dependencies.getVideo(videoId as Video.VideoId);+ if (!video) return null;+ const shareUrl = new URL(`/s/${video.id}`, webUrl).toString();+ const blocks: unknown[] = [+ buildSlackVideoBlock({ video, shareUrl, webUrl }),+ ];+ return [+ link.url,+ {+ blocks,+ },+ ] as const;+ }),+ );+ const unfurls = Object.fromEntries(+ resolved.filter(+ (entry): entry is NonNullable<typeof entry> => entry !== null,+ ),+ );+ if (Object.keys(unfurls).length === 0) return;++ await dependencies.sendUnfurl({+ token,+ event: {+ channel: payload.event.channel,+ messageTs: payload.event.message_ts,+ unfurlId: payload.event.unfurl_id,+ source: payload.event.source,+ },+ unfurls,+ });+};