Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix(integrations/slack): prevent channel list fetch from timing out
Marc Seitz9a36dc7764e9
diff --git a/lib/integrations/slack/client.ts b/lib/integrations/slack/client.tsindex 20e260a8..1c1f8d51 100644--- a/lib/integrations/slack/client.ts+++ b/lib/integrations/slack/client.ts@@ -6,6 +6,23 @@ import {} from "@/lib/integrations/slack/types";import { decryptSlackToken } from "@/lib/integrations/slack/utils";+// Slack `conversations.list` is rate limited (Tier 2). Without a `limit` it+// returns small pages, so a large workspace requires hundreds of sequential+// requests and can blow past the function timeout. These guards keep a single+// fetch bounded in both work and wall-clock time.+const SLACK_CHANNELS_PAGE_LIMIT = 999; // Slack max is 1000+const SLACK_CHANNELS_MAX_PAGES = 50; // up to ~50k channels+const SLACK_CHANNELS_DEADLINE_MS = 45_000; // overall budget, well under maxDuration+const SLACK_CHANNELS_MAX_RETRY_PER_PAGE = 2; // bounded 429 retries+const SLACK_CHANNELS_MAX_RETRY_AFTER_MS = 15_000; // cap a single backoff++export class SlackRateLimitError extends Error {+ constructor(message = "Slack rate limit exceeded") {+ super(message);+ this.name = "SlackRateLimitError";+ }+}+export class SlackClient {private clientId: string;private clientSecret: string;@@ -149,29 +166,59 @@ export class SlackClient {const channels: SlackChannel[] = [];let cursor: string | undefined = undefined;+ let pages = 0;+ const deadline = Date.now() + SLACK_CHANNELS_DEADLINE_MS;do {+ if (Date.now() >= deadline) {+ console.warn(+ `[slack] getChannels hit time budget after ${pages} page(s); returning ${channels.length} channel(s) so far`,+ );+ break;+ }+const url = new URL(`${this.baseUrl}/conversations.list`);url.searchParams.set("types", "public_channel,private_channel");url.searchParams.set("exclude_archived", "true");+ url.searchParams.set("limit", String(SLACK_CHANNELS_PAGE_LIMIT));if (cursor) url.searchParams.set("cursor", cursor);const requestUrl = url.toString();- const ac = new AbortController();- const to = setTimeout(() => ac.abort(), 10000);- const resp = await fetch(requestUrl, {- headers: {- Authorization: `Bearer ${decryptedToken}`,- "Content-Type": "application/json",- },- signal: ac.signal,- }).finally(() => clearTimeout(to));- // Basic 429 handling- if (resp.status === 429) {- const retry = Number(resp.headers.get("retry-after") || 1);- await new Promise((r) => setTimeout(r, retry * 1000));- continue;++ // Fetch a single page with bounded 429 retries.+ let resp: Response | undefined;+ for (let attempt = 0; ; attempt++) {+ const ac = new AbortController();+ const to = setTimeout(() => ac.abort(), 10000);+ resp = await fetch(requestUrl, {+ headers: {+ Authorization: `Bearer ${decryptedToken}`,+ "Content-Type": "application/json",+ },+ signal: ac.signal,+ }).finally(() => clearTimeout(to));++ if (resp.status !== 429) break;++ // Rate limited: give up rather than sleep into the function timeout.+ if (+ attempt >= SLACK_CHANNELS_MAX_RETRY_PER_PAGE ||+ Date.now() >= deadline+ ) {+ throw new SlackRateLimitError(+ "Slack rate limit exceeded while listing channels (conversations.list)",+ );+ }++ const retryAfterSec = Number(resp.headers.get("retry-after") || 1);+ const waitMs = Math.min(+ retryAfterSec * 1000,+ SLACK_CHANNELS_MAX_RETRY_AFTER_MS,+ Math.max(0, deadline - Date.now()),+ );+ await new Promise((r) => setTimeout(r, waitMs));}+if (!resp.ok)throw new Error(`Failed to get channels: ${resp.status} ${resp.statusText}`,@@ -188,7 +235,8 @@ export class SlackClient {})),);cursor = data.response_metadata?.next_cursor || undefined;- } while (cursor);+ pages++;+ } while (cursor && pages < SLACK_CHANNELS_MAX_PAGES);return channels;}diff --git a/lib/integrations/slack/utils.ts b/lib/integrations/slack/utils.tsindex af0489be..8fe4eb25 100644--- a/lib/integrations/slack/utils.ts+++ b/lib/integrations/slack/utils.ts@@ -1,5 +1,11 @@import crypto from "crypto";+/**+ * Redis cache key for a team's fetched Slack channel list.+ */+export const slackChannelsCacheKey = (teamId: string) =>+ `slack:channels:${teamId}`;+/*** Derives a 32-byte encryption key from the environment variable using SHA-256* @param keyMaterial - The raw key material from environmentdiff --git a/lib/swr/use-slack-channels.ts b/lib/swr/use-slack-channels.tsindex 21a29cae..a21b9d57 100644--- a/lib/swr/use-slack-channels.ts+++ b/lib/swr/use-slack-channels.ts@@ -1,14 +1,16 @@+import { useCallback } from "react";+import { useTeam } from "@/context/team-context";import useSWR from "swr";import { SlackChannel } from "@/lib/integrations/slack/types";import { fetcher } from "@/lib/utils";+type SlackChannelsResponse = { channels: SlackChannel[] };+export function useSlackChannels({ enabled = true }: { enabled?: boolean }) {const { currentTeamId: teamId } = useTeam();- const { data, error, isLoading, mutate } = useSWR<{- channels: SlackChannel[];- }>(+ const { data, error, isLoading, mutate } = useSWR<SlackChannelsResponse>(enabled && teamId? `/api/teams/${teamId}/integrations/slack/channels`: null,@@ -23,10 +25,21 @@ export function useSlackChannels({ enabled = true }: { enabled?: boolean }) {},);+ // Force a fresh fetch from Slack, bypassing the server-side cache, and write+ // the result back into the SWR cache without an extra revalidation.+ const refresh = useCallback(async () => {+ if (!teamId) return;+ const res = await fetcher(+ `/api/teams/${teamId}/integrations/slack/channels?refresh=true`,+ );+ await mutate(res as SlackChannelsResponse, { revalidate: false });+ }, [teamId, mutate]);+return {channels: data?.channels || [],error,loading: isLoading && !data,mutate,+ refresh,};}diff --git a/pages/api/teams/[teamId]/integrations/slack/channels.ts b/pages/api/teams/[teamId]/integrations/slack/channels.tsindex 69c2dbc1..51226dea 100644--- a/pages/api/teams/[teamId]/integrations/slack/channels.ts+++ b/pages/api/teams/[teamId]/integrations/slack/channels.ts@@ -4,16 +4,32 @@ import { authOptions } from "@/pages/api/auth/[...nextauth]";import { getServerSession } from "next-auth/next";import { z } from "zod";-import { getSlackClient } from "@/lib/integrations/slack/client";+import {+ SlackRateLimitError,+ getSlackClient,+} from "@/lib/integrations/slack/client";import { getSlackEnv } from "@/lib/integrations/slack/env";import { SlackCredential } from "@/lib/integrations/slack/types";+import { slackChannelsCacheKey } from "@/lib/integrations/slack/utils";import prisma from "@/lib/prisma";+import { redis } from "@/lib/redis";import { CustomUser } from "@/lib/types";export const config = {maxDuration: 300,};+// Channel lists rarely change, so cache them to avoid re-paginating Slack's+// rate-limited conversations.list on every settings page visit / SWR retry.+const CHANNELS_CACHE_TTL_SECONDS = 600; // 10 minutes++type AvailableChannel = {+ id: string;+ name: string;+ is_private: boolean;+ is_member: boolean;+};+export default async function handler(req: NextApiRequest,res: NextApiResponse,@@ -59,13 +75,27 @@ export default async function handler(return res.status(404).json({ error: "Slack integration not found" });}+ const cacheKey = slackChannelsCacheKey(teamId);+ const skipCache = req.query.refresh === "true" || req.query.refresh === "1";++ if (!skipCache) {+ try {+ const cached = await redis.get<AvailableChannel[]>(cacheKey);+ if (cached) {+ return res.status(200).json({ channels: cached, cached: true });+ }+ } catch (cacheError) {+ console.error("Slack channels cache read failed:", cacheError);+ }+ }+try {const slackClient = getSlackClient();const channels = await slackClient.getChannels((integration.credentials as SlackCredential).accessToken,);- const availableChannels = channels+ const availableChannels: AvailableChannel[] = channels.filter((channel) => !channel.is_archived).map((channel) => ({id: channel.id,@@ -75,8 +105,23 @@ export default async function handler(})).sort((a, b) => a.name.localeCompare(b.name));+ try {+ await redis.set(cacheKey, availableChannels, {+ ex: CHANNELS_CACHE_TTL_SECONDS,+ });+ } catch (cacheError) {+ console.error("Slack channels cache write failed:", cacheError);+ }+return res.status(200).json({ channels: availableChannels });} catch (slackError) {+ if (slackError instanceof SlackRateLimitError) {+ return res.status(429).json({+ error:+ "Slack is rate limiting channel requests. Please wait a moment and refresh.",+ });+ }+if (slackError instanceof Error &&slackError.message.includes("missing_scope")diff --git a/pages/api/teams/[teamId]/integrations/slack/index.ts b/pages/api/teams/[teamId]/integrations/slack/index.tsindex b06191e3..c2bc1478 100644--- a/pages/api/teams/[teamId]/integrations/slack/index.ts+++ b/pages/api/teams/[teamId]/integrations/slack/index.ts@@ -10,7 +10,9 @@ import {SlackCredentialPublic,} from "@/lib/integrations/slack/types";import { uninstallSlackIntegration } from "@/lib/integrations/slack/uninstall";+import { slackChannelsCacheKey } from "@/lib/integrations/slack/utils";import prisma from "@/lib/prisma";+import { redis } from "@/lib/redis";import { CustomUser } from "@/lib/types";const channelConfigSchema = z.object({@@ -227,6 +229,12 @@ async function handleDelete(},});+ try {+ await redis.del(slackChannelsCacheKey(teamId));+ } catch (cacheError) {+ console.error("Slack channels cache invalidation failed:", cacheError);+ }+if (!uninstallResult.ok) {return res.status(200).json({message:diff --git a/pages/settings/slack.tsx b/pages/settings/slack.tsxindex 0addba0a..1d89f8cd 100644--- a/pages/settings/slack.tsx+++ b/pages/settings/slack.tsx@@ -65,6 +65,7 @@ export default function SlackSettings() {loading: loadingChannels,error: channelsError,mutate: mutateChannels,+ refresh: refreshChannels,} = useSlackChannels({enabled: !!integration,});@@ -363,14 +364,14 @@ export default function SlackSettings() {const handleRefreshChannels = useCallback(async () => {setRefreshingChannels(true);try {- await mutateChannels();+ await refreshChannels();toast.success("Channel list refreshed");} catch (error) {toast.error("Failed to refresh channel list");} finally {setRefreshingChannels(false);}- }, [mutateChannels]);+ }, [refreshChannels]);return (<AppLayout>