diff --git a/app/components/HoverPreview/HoverPreviewIssue.tsx b/app/components/HoverPreview/HoverPreviewIssue.tsx index 2fd4ba3c9b..01369bc6f9 100644 --- a/app/components/HoverPreview/HoverPreviewIssue.tsx +++ b/app/components/HoverPreview/HoverPreviewIssue.tsx @@ -5,7 +5,6 @@ import { Backticks } from "@shared/components/Backticks"; import { IssueStatusIcon } from "@shared/components/IssueStatusIcon"; import { richExtensions } from "@shared/editor/nodes"; import type { UnfurlResourceType, UnfurlResponse } from "@shared/types"; -import { IntegrationService } from "@shared/types"; import { Avatar } from "~/components/Avatar"; import Editor from "~/components/Editor"; import Flex from "~/components/Flex"; @@ -37,13 +36,6 @@ function HoverPreviewIssue({ ref, }: Props) { const authorName = author.name; - const urlObj = new URL(url); - const service = - urlObj.hostname === "linear.app" - ? IntegrationService.Linear - : urlObj.hostname === "github.com" - ? IntegrationService.GitHub - : IntegrationService.GitLab; return ( @@ -52,11 +44,7 @@ function HoverPreviewIssue({ - <StyledIssueStatusIcon - service={service} - state={state} - size={18} - /> + <StyledIssueStatusIcon state={state} size={18} /> <span> <Backticks content={title} />  <Text type="tertiary">{id}</Text> diff --git a/app/hooks/useEmbeds.ts b/app/hooks/useEmbeds.ts index 267770abb4..128edff078 100644 --- a/app/hooks/useEmbeds.ts +++ b/app/hooks/useEmbeds.ts @@ -5,6 +5,7 @@ import embeds from "@shared/editor/embeds"; import { IntegrationType, TeamPreference } from "@shared/types"; import type Integration from "~/models/Integration"; import Logger from "~/utils/Logger"; +import { Hook, PluginManager } from "~/utils/PluginManager"; import useCurrentTeam from "./useCurrentTeam"; import useStores from "./useStores"; @@ -34,6 +35,8 @@ export default function useEmbeds(loadIfMissing = false) { } }, [integrations, loadIfMissing]); + // Plugins may register instance URL resolvers after integrations load. + const pluginsLoaded = PluginManager.isLoaded; const disabledEmbeds = (team?.getPreference(TeamPreference.DisabledEmbeds) as string[]) || []; @@ -46,6 +49,9 @@ export default function useEmbeds(loadIfMissing = false) { if (integration?.settings) { e.settings = integration.settings; + e.instanceUrl = PluginManager.getHooks(Hook.InstanceUrl) + .find((hook) => hook.value.service === e.name) + ?.value.url(integration.settings); } e.disabled = disabledEmbeds.includes(e.id); @@ -53,6 +59,6 @@ export default function useEmbeds(loadIfMissing = false) { return e; }), // oxlint-disable-next-line react-hooks/exhaustive-deps - [integrations.orderedData, team?.preferences] + [integrations.orderedData, team?.preferences, pluginsLoaded] ); } diff --git a/app/typings/react.d.ts b/app/typings/react.d.ts index 81397514a2..5099ef2bc4 100644 --- a/app/typings/react.d.ts +++ b/app/typings/react.d.ts @@ -5,7 +5,6 @@ import "react"; declare module "react" { namespace JSX { interface IntrinsicElements { - "zapier-app-directory": any; "em-emoji": any; } } diff --git a/app/utils/PluginManager.ts b/app/utils/PluginManager.ts index 6f6d4beec1..b9c8363c22 100644 --- a/app/utils/PluginManager.ts +++ b/app/utils/PluginManager.ts @@ -1,6 +1,13 @@ import { isArray, sortBy } from "es-toolkit/compat"; import { action, makeObservable, observable, runInAction } from "mobx"; import type { IObservableArray } from "mobx"; +import type { + IntegrationService, + IntegrationSettings, + IntegrationType, + MentionType, +} from "@shared/types"; +import type Integration from "~/models/Integration"; import type Team from "~/models/Team"; import type User from "~/models/User"; import type { LazyComponent } from "~/components/LazyLoad"; @@ -15,6 +22,8 @@ export enum Hook { Settings = "settings", Imports = "imports", Icon = "icon", + MentionProvider = "mentionProvider", + InstanceUrl = "instanceUrl", } /** @@ -46,6 +55,25 @@ type PluginValueMap = { action: React.ReactElement; }; [Hook.Icon]: React.ElementType; + [Hook.MentionProvider]: { + /** The integration service that this provider handles. */ + service: IntegrationService; + /** Whether the URL belongs to the integration. */ + isMentionable: (url: URL, integration: Integration) => boolean; + /** The mention type of the URL, or undefined if it cannot be mentioned. */ + mentionType: ( + url: URL, + integration: Integration + ) => MentionType | undefined; + }; + [Hook.InstanceUrl]: { + /** The integration service that this resolver is used for. */ + service: IntegrationService; + /** Returns the URL of the instance that the integration is configured with. */ + url: ( + settings: IntegrationSettings<IntegrationType.Embed> + ) => string | undefined; + }; }; export type Plugin<T extends Hook> = { diff --git a/app/utils/mention.test.ts b/app/utils/mention.test.ts new file mode 100644 index 0000000000..51ca3b39f1 --- /dev/null +++ b/app/utils/mention.test.ts @@ -0,0 +1,53 @@ +import { IntegrationService, MentionType } from "@shared/types"; +import type Integration from "~/models/Integration"; +import { determineMentionType, isURLMentionable } from "./mention"; +import { Hook, PluginManager } from "./PluginManager"; + +const buildIntegration = (service: IntegrationService) => + ({ service, settings: {} }) as unknown as Integration; + +describe("mention", () => { + beforeAll(() => { + PluginManager.add({ + id: "test-mention-provider", + name: "Test mention provider", + type: Hook.MentionProvider, + value: { + service: IntegrationService.GitHub, + isMentionable: (url) => url.hostname === "example.com", + mentionType: (url) => + url.pathname.startsWith("/issues/") ? MentionType.Issue : undefined, + }, + }); + }); + + it("uses the provider registered for the integration service", () => { + const integration = buildIntegration(IntegrationService.GitHub); + const url = new URL("https://example.com/issues/1"); + + expect(isURLMentionable({ url, integration })).toBe(true); + expect(determineMentionType({ url, integration })).toBe(MentionType.Issue); + }); + + it("returns the provider result for URLs it does not recognize", () => { + const integration = buildIntegration(IntegrationService.GitHub); + + expect( + isURLMentionable({ url: new URL("https://other.com/"), integration }) + ).toBe(false); + expect( + determineMentionType({ + url: new URL("https://example.com/about"), + integration, + }) + ).toBeUndefined(); + }); + + it("does not mention URLs for services without a provider", () => { + const integration = buildIntegration(IntegrationService.Slack); + const url = new URL("https://example.com/issues/1"); + + expect(isURLMentionable({ url, integration })).toBe(false); + expect(determineMentionType({ url, integration })).toBeUndefined(); + }); +}); diff --git a/app/utils/mention.ts b/app/utils/mention.ts index 5d8bb16341..e8fa9c878e 100644 --- a/app/utils/mention.ts +++ b/app/utils/mention.ts @@ -1,74 +1,17 @@ -import type { IntegrationSettings, IntegrationType } from "@shared/types"; -import { IntegrationService, MentionType } from "@shared/types"; +import type { MentionType } from "@shared/types"; import type Integration from "~/models/Integration"; - -const gitlabSystemPaths = new Set([ - "explore", - "help", - "admin", - "dashboard", - "users", - "groups", - "projects", - "snippets", - "search", - "-", -]); - -const giteaSystemPaths = new Set([ - "-", - ".well-known", - "admin", - "api", - "assets", - "attachments", - "avatars", - "explore", - "issues", - "login", - "milestones", - "notifications", - "org", - "pulls", - "repo", - "user", -]); +import { Hook, PluginManager } from "~/utils/PluginManager"; /** - * Returns the path segments of a URL relative to a connected Gitea instance, - * which can be served from a sub-path. + * Returns the mention provider registered for the service of an integration. * - * @param url the URL to split. - * @param integration the Gitea integration. - * @returns the path segments, or undefined if the URL is not on the instance. + * @param integration the integration to find a provider for. + * @returns the provider, or undefined if no plugin handles the service. */ -const giteaPathParts = ( - url: URL, - integration: Integration -): string[] | undefined => { - const settings = - integration.settings as IntegrationSettings<IntegrationType.Embed>; - if (!settings.gitea?.installation) { - return undefined; - } - - let base: URL; - try { - base = new URL(settings.gitea.url); - } catch { - return undefined; - } - - const basePath = base.pathname.replace(/\/+$/, ""); - if ( - url.host !== base.host || - (basePath && !url.pathname.startsWith(`${basePath}/`)) - ) { - return undefined; - } - - return url.pathname.slice(basePath.length).split("/").filter(Boolean); -}; +const getMentionProvider = (integration: Integration) => + PluginManager.getHooks(Hook.MentionProvider).find( + (hook) => hook.value.service === integration.service + )?.value; /** * Checks whether a URL can be converted to a mention for the given @@ -83,49 +26,8 @@ export const isURLMentionable = ({ }: { url: URL; integration: Integration; -}): boolean => { - const { hostname, pathname } = url; - - switch (integration.service) { - case IntegrationService.GitHub: { - return hostname === "github.com"; - } - - case IntegrationService.Linear: { - const pathParts = pathname.split("/"); - const settings = - integration.settings as IntegrationSettings<IntegrationType.Embed>; - - return ( - hostname === "linear.app" && - settings.linear?.workspace.key === pathParts[1] // ensure installed workspace key matches with the provided url. - ); - } - - case IntegrationService.GitLab: { - const settings = - integration.settings as IntegrationSettings<IntegrationType.Embed>; - let gitlabHostname: string | undefined; - try { - gitlabHostname = settings.gitlab?.url - ? new URL(settings.gitlab.url).hostname - : undefined; - } catch { - // Invalid URL stored in settings - return false; - } - - return hostname === "gitlab.com" || hostname === gitlabHostname; - } - - case IntegrationService.Gitea: { - return !!giteaPathParts(url, integration); - } - - default: - return false; - } -}; +}): boolean => + getMentionProvider(integration)?.isMentionable(url, integration) ?? false; /** * Determines the type of mention a URL represents for the given integration, @@ -140,74 +42,5 @@ export const determineMentionType = ({ }: { url: URL; integration: Integration; -}): MentionType | undefined => { - const { pathname } = url; - const pathParts = pathname.split("/"); - - switch (integration.service) { - case IntegrationService.GitHub: { - const type = pathParts[3]; - return type === "pull" - ? MentionType.PullRequest - : type === "issues" - ? MentionType.Issue - : type === "projects" - ? MentionType.Project - : undefined; - } - - case IntegrationService.Linear: { - const type = pathParts[2]; - return type === "issue" - ? MentionType.Issue - : type === "project" - ? MentionType.Project - : undefined; - } - - case IntegrationService.GitLab: { - const hasShowParam = url.searchParams.has("show"); - - if ( - /\/-\/merge_requests\/\d+/.test(pathname) || - (/\/-\/merge_requests\/?$/.test(pathname) && hasShowParam) - ) { - return MentionType.PullRequest; - } - if ( - /\/-\/(issues|work_items)\/\d+/.test(pathname) || - (/\/-\/(issues|work_items)\/?$/.test(pathname) && hasShowParam) - ) { - return MentionType.Issue; - } - if (!pathname.includes("/-/")) { - const parts = pathname.split("/").filter(Boolean); - if (parts.length >= 2 && !gitlabSystemPaths.has(parts[0])) { - return MentionType.Project; - } - } - return undefined; - } - - case IntegrationService.Gitea: { - const parts = giteaPathParts(url, integration); - if (!parts || parts.length < 2 || giteaSystemPaths.has(parts[0])) { - return undefined; - } - if (parts.length === 2) { - return MentionType.Project; - } - if (!/^\d+$/.test(parts[3] ?? "")) { - return undefined; - } - return parts[2] === "pulls" - ? MentionType.PullRequest - : parts[2] === "issues" - ? MentionType.Issue - : undefined; - } - - default: - return; - } -}; +}): MentionType | undefined => + getMentionProvider(integration)?.mentionType(url, integration); diff --git a/plugins/azure/server/auth/azure.ts b/plugins/azure/server/auth/azure.ts index 9705f811e9..40b6394f5d 100644 --- a/plugins/azure/server/auth/azure.ts +++ b/plugins/azure/server/auth/azure.ts @@ -8,7 +8,6 @@ import { toError } from "@shared/utils/error"; import { slugifyDomain } from "@shared/utils/domains"; import { parseEmail } from "@shared/utils/email"; import accountProvisioner from "@server/commands/accountProvisioner"; -import { MicrosoftGraphError } from "@server/errors"; import passportMiddleware from "@server/middlewares/passport"; import type { User } from "@server/models"; import type { AuthenticationResult } from "@server/types"; @@ -30,6 +29,7 @@ import AttachmentHelper from "@server/models/helpers/AttachmentHelper"; import { AttachmentPreset } from "@shared/types"; import { UserFlag } from "@server/models/User"; import { getEmailVerified } from "./getEmailVerified"; +import { MicrosoftGraphError } from "../errors"; const router = new Router(); const scopes: string[] = []; diff --git a/plugins/azure/server/errors.ts b/plugins/azure/server/errors.ts new file mode 100644 index 0000000000..2fd8fd0903 --- /dev/null +++ b/plugins/azure/server/errors.ts @@ -0,0 +1,10 @@ +import httpErrors from "http-errors"; + +export function MicrosoftGraphError( + message = "Microsoft Graph API did not return required fields" +) { + return httpErrors(400, message, { + id: "graph_error", + isReportable: false, + }); +} diff --git a/plugins/azure/server/index.ts b/plugins/azure/server/index.ts index eddc61bb65..6730cb8b8e 100644 --- a/plugins/azure/server/index.ts +++ b/plugins/azure/server/index.ts @@ -1,5 +1,6 @@ import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import AzureClient from "./azure"; import router from "./auth/azure"; import env from "./env"; @@ -9,6 +10,11 @@ if (enabled) { PluginManager.add({ ...config, type: Hook.AuthProvider, - value: { router, id: config.id, allowEmailMatch: true }, + value: { + router, + id: config.id, + allowEmailMatch: true, + oauthClient: () => new AzureClient(), + }, }); } diff --git a/server/typings/outlinewiki__passport-azure-ad-oauth2.d.ts b/plugins/azure/server/typings/outlinewiki__passport-azure-ad-oauth2.d.ts similarity index 100% rename from server/typings/outlinewiki__passport-azure-ad-oauth2.d.ts rename to plugins/azure/server/typings/outlinewiki__passport-azure-ad-oauth2.d.ts diff --git a/plugins/diagrams/client/index.tsx b/plugins/diagrams/client/index.tsx index 76ac8c93f2..a8a12a64d1 100644 --- a/plugins/diagrams/client/index.tsx +++ b/plugins/diagrams/client/index.tsx @@ -1,4 +1,4 @@ -import { UserRole } from "@shared/types"; +import { IntegrationService, UserRole } from "@shared/types"; import { createLazyComponent } from "~/components/LazyLoad"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; @@ -17,4 +17,12 @@ PluginManager.add([ enabled: (_, user) => user.role === UserRole.Admin, }, }, + { + ...config, + type: Hook.InstanceUrl, + value: { + service: IntegrationService.Diagrams, + url: (settings) => settings.diagrams?.url, + }, + }, ]); diff --git a/plugins/diagrams/server/diagrams.test.ts b/plugins/diagrams/server/diagrams.test.ts new file mode 100644 index 0000000000..640f34d449 --- /dev/null +++ b/plugins/diagrams/server/diagrams.test.ts @@ -0,0 +1,65 @@ +import { IntegrationService, IntegrationType } from "@shared/types"; +import { + buildAdmin, + buildGuestUser, + buildIntegration, + buildTeam, +} from "@server/test/factories"; +import { getTestServer } from "@server/test/support"; + +const server = getTestServer(); + +describe("#integrations.create", () => { + it("should accept the settings of a diagrams integration", async () => { + const admin = await buildAdmin(); + + const res = await server.post("/api/integrations.create", admin, { + body: { + type: IntegrationType.Embed, + service: IntegrationService.Diagrams, + settings: { diagrams: { url: "https://diagrams.example.com/" } }, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.settings).toEqual({ + diagrams: { url: "https://diagrams.example.com/" }, + }); + }); + + it("should fail with status 400 bad request for invalid diagrams settings", async () => { + const admin = await buildAdmin(); + + const res = await server.post("/api/integrations.create", admin, { + body: { + type: IntegrationType.Embed, + service: IntegrationService.Diagrams, + settings: { diagrams: { url: "not a url" } }, + }, + }); + expect(res.status).toEqual(400); + }); +}); + +describe("#integrations.list", () => { + it("should expose diagrams settings to guests", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const guest = await buildGuestUser({ teamId: team.id }); + await buildIntegration({ + userId: admin.id, + teamId: team.id, + service: IntegrationService.Diagrams, + type: IntegrationType.Embed, + settings: { diagrams: { url: "https://diagrams.example.com/" } }, + }); + + const res = await server.post("/api/integrations.list", guest); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data[0].settings).toEqual({ + diagrams: { url: "https://diagrams.example.com/" }, + }); + }); +}); diff --git a/plugins/diagrams/server/index.ts b/plugins/diagrams/server/index.ts new file mode 100644 index 0000000000..cfa962e70c --- /dev/null +++ b/plugins/diagrams/server/index.ts @@ -0,0 +1,15 @@ +import { z } from "zod"; +import { IntegrationService } from "@shared/types"; +import { Hook, PluginManager } from "@server/utils/PluginManager"; + +PluginManager.add({ + type: Hook.IntegrationSettings, + value: { + service: IntegrationService.Diagrams, + keys: ["diagrams"], + visibleToGuests: true, + schema: z.object({ + diagrams: z.object({ url: z.url() }), + }), + }, +}); diff --git a/plugins/diagrams/shared/types.ts b/plugins/diagrams/shared/types.ts new file mode 100644 index 0000000000..0f8b2ec2e0 --- /dev/null +++ b/plugins/diagrams/shared/types.ts @@ -0,0 +1,9 @@ +declare module "@shared/types" { + interface EmbedIntegrationSettings { + diagrams?: { + url: string; + }; + } +} + +export {}; diff --git a/plugins/discord/server/index.ts b/plugins/discord/server/index.ts index e2144f5977..0936aa6a17 100644 --- a/plugins/discord/server/index.ts +++ b/plugins/discord/server/index.ts @@ -1,5 +1,6 @@ import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import DiscordClient from "./discord"; import router from "./auth/discord"; import env from "./env"; @@ -9,6 +10,10 @@ if (enabled) { PluginManager.add({ ...config, type: Hook.AuthProvider, - value: { router, id: config.id }, + value: { + router, + id: config.id, + oauthClient: () => new DiscordClient(), + }, }); } diff --git a/plugins/figma/server/api/figma.test.ts b/plugins/figma/server/api/figma.test.ts index 816343caa1..40cfadd2fa 100644 --- a/plugins/figma/server/api/figma.test.ts +++ b/plugins/figma/server/api/figma.test.ts @@ -1,3 +1,6 @@ +import { IntegrationService, IntegrationType } from "@shared/types"; +import type { User } from "@server/models"; +import { Integration } from "@server/models"; import { buildUser } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -41,3 +44,36 @@ describe("#figma.callback", () => { expect(res.status).toEqual(400); }); }); + +describe("#integrations.info", () => { + const buildFigmaAccount = (owner: User) => + Integration.create<Integration<IntegrationType.LinkedAccount>>({ + userId: owner.id, + teamId: owner.teamId, + service: IntegrationService.Figma, + type: IntegrationType.LinkedAccount, + settings: { + figma: { + account: { + id: "123", + name: "owner", + email: "owner@example.com", + avatarUrl: "https://example.com/avatar.png", + }, + }, + }, + }); + + it("should expose figma account settings to the owner", async () => { + const owner = await buildUser(); + const integration = await buildFigmaAccount(owner); + + const res = await server.post("/api/integrations.info", owner, { + body: { id: integration.id }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.settings.figma.account.email).toEqual("owner@example.com"); + }); +}); diff --git a/plugins/figma/server/api/figma.ts b/plugins/figma/server/api/figma.ts index 241da3501f..4cbb73be0f 100644 --- a/plugins/figma/server/api/figma.ts +++ b/plugins/figma/server/api/figma.ts @@ -99,6 +99,7 @@ router.get( new UploadIntegrationLogoTask().schedule({ integrationId: integration.id, logoUrl: figmaAccount.img_url, + settingsPath: ["figma", "account", "avatarUrl"], }) ); diff --git a/plugins/figma/server/index.ts b/plugins/figma/server/index.ts index 738aa2efb8..a54c1c2da0 100644 --- a/plugins/figma/server/index.ts +++ b/plugins/figma/server/index.ts @@ -1,3 +1,4 @@ +import { IntegrationService } from "@shared/types"; import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; import router from "./api/figma"; @@ -5,6 +6,11 @@ import env from "./env"; import { Figma } from "./figma"; import { Minute } from "@shared/utils/time"; +PluginManager.add({ + type: Hook.IntegrationSettings, + value: { service: IntegrationService.Figma, keys: ["figma"] }, +}); + const enabled = !!env.FIGMA_CLIENT_ID && !!env.FIGMA_CLIENT_SECRET; if (enabled) { diff --git a/plugins/figma/shared/types.ts b/plugins/figma/shared/types.ts new file mode 100644 index 0000000000..d0cba589f3 --- /dev/null +++ b/plugins/figma/shared/types.ts @@ -0,0 +1,14 @@ +declare module "@shared/types" { + interface LinkedAccountIntegrationSettings { + figma?: { + account: { + id: string; + name: string; + email: string; + avatarUrl: string; + }; + }; + } +} + +export {}; diff --git a/plugins/gitea/client/index.tsx b/plugins/gitea/client/index.tsx index 07a3c47374..5ab91fdd6f 100644 --- a/plugins/gitea/client/index.tsx +++ b/plugins/gitea/client/index.tsx @@ -1,6 +1,9 @@ +import type { IntegrationSettings, IntegrationType } from "@shared/types"; +import { IntegrationService } from "@shared/types"; import { createLazyComponent } from "~/components/LazyLoad"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; +import { GiteaUtils } from "../shared/GiteaUtils"; import Icon from "./components/Icon"; PluginManager.add([ @@ -15,4 +18,26 @@ PluginManager.add([ component: createLazyComponent(() => import("./Settings")), }, }, + { + ...config, + type: Hook.MentionProvider, + value: { + service: IntegrationService.Gitea, + isMentionable: (url, integration) => { + const settings = + integration.settings as IntegrationSettings<IntegrationType.Embed>; + return ( + !!settings.gitea?.installation && + !!GiteaUtils.pathParts(url, settings.gitea.url) + ); + }, + mentionType: (url, integration) => { + const settings = + integration.settings as IntegrationSettings<IntegrationType.Embed>; + return settings.gitea?.installation + ? GiteaUtils.mentionType(url, settings.gitea.url) + : undefined; + }, + }, + }, ]); diff --git a/plugins/gitea/server/index.ts b/plugins/gitea/server/index.ts index fc3e82fe5f..9cc5b475c8 100644 --- a/plugins/gitea/server/index.ts +++ b/plugins/gitea/server/index.ts @@ -1,3 +1,4 @@ +import { IntegrationService } from "@shared/types"; import { Minute } from "@shared/utils/time"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; @@ -6,6 +7,10 @@ import router from "./api/gitea"; import { Gitea } from "./gitea"; PluginManager.add([ + { + type: Hook.IntegrationSettings, + value: { service: IntegrationService.Gitea, keys: ["gitea"] }, + }, { ...config, type: Hook.API, diff --git a/plugins/gitea/shared/GiteaUtils.ts b/plugins/gitea/shared/GiteaUtils.ts index 5358f434d2..7da94b623f 100644 --- a/plugins/gitea/shared/GiteaUtils.ts +++ b/plugins/gitea/shared/GiteaUtils.ts @@ -148,6 +148,35 @@ export class GiteaUtils { } } + /** + * Returns the path segments of a URL relative to a Gitea instance. + * + * @param url - the URL to split. + * @param baseUrl - the URL of the Gitea instance, which may include a path. + * @returns the path segments, or undefined if the URL is not on the instance. + */ + public static pathParts(url: URL, baseUrl: string): string[] | undefined { + let base: URL; + + try { + base = new URL(baseUrl); + } catch { + return; + } + + if (url.host !== base.host) { + return; + } + + // Instances can be served from a sub-path, e.g. https://example.com/gitea + const basePath = base.pathname.replace(/\/+$/, ""); + if (basePath && !url.pathname.startsWith(`${basePath}/`)) { + return; + } + + return url.pathname.slice(basePath.length).split("/").filter(Boolean); + } + /** * Parses a Gitea URL and extracts resource identifiers. * @@ -160,30 +189,15 @@ export class GiteaUtils { baseUrl: string ): GiteaResource | undefined { let parsed: URL; - let base: URL; try { parsed = new URL(url); - base = new URL(baseUrl); } catch { return; } - if (parsed.host !== base.host) { - return; - } - - // Instances can be served from a sub-path, e.g. https://example.com/gitea - const basePath = base.pathname.replace(/\/+$/, ""); - if (basePath && !parsed.pathname.startsWith(`${basePath}/`)) { - return; - } - - const parts = parsed.pathname - .slice(basePath.length) - .split("/") - .filter(Boolean); - if (parts.length < 2 || this.systemPaths.has(parts[0])) { + const parts = this.pathParts(parsed, baseUrl); + if (!parts || parts.length < 2 || this.systemPaths.has(parts[0])) { return; } diff --git a/plugins/gitea/shared/types.ts b/plugins/gitea/shared/types.ts new file mode 100644 index 0000000000..db244a8a85 --- /dev/null +++ b/plugins/gitea/shared/types.ts @@ -0,0 +1,13 @@ +declare module "@shared/types" { + interface EmbedIntegrationSettings { + gitea?: { + url: string; + installation?: { + id: number; + account: { id: number; name: string; avatarUrl: string }; + }; + }; + } +} + +export {}; diff --git a/plugins/github/client/index.tsx b/plugins/github/client/index.tsx index dea29467f2..1e0772d9ba 100644 --- a/plugins/github/client/index.tsx +++ b/plugins/github/client/index.tsx @@ -1,6 +1,8 @@ +import { IntegrationService } from "@shared/types"; import { createLazyComponent } from "~/components/LazyLoad"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; +import { GitHubUtils } from "../shared/GitHubUtils"; import Icon from "./Icon"; PluginManager.add([ @@ -15,4 +17,13 @@ PluginManager.add([ component: createLazyComponent(() => import("./Settings")), }, }, + { + ...config, + type: Hook.MentionProvider, + value: { + service: IntegrationService.GitHub, + isMentionable: (url) => url.hostname === "github.com", + mentionType: (url) => GitHubUtils.mentionType(url), + }, + }, ]); diff --git a/plugins/github/server/api/github.test.ts b/plugins/github/server/api/github.test.ts index 8dfe626a79..5352211a23 100644 --- a/plugins/github/server/api/github.test.ts +++ b/plugins/github/server/api/github.test.ts @@ -1,6 +1,13 @@ -import { IntegrationService } from "@shared/types"; +import { IntegrationService, IntegrationType } from "@shared/types"; import { Integration } from "@server/models"; -import { buildAdmin, buildUser, buildViewer } from "@server/test/factories"; +import { + buildAdmin, + buildGuestUser, + buildIntegration, + buildTeam, + buildUser, + buildViewer, +} from "@server/test/factories"; import { getTestServer } from "@server/test/support"; import { GitHub, InstallationAccess } from "../github"; import { SetupAction } from "./schema"; @@ -136,3 +143,60 @@ describe("#github.callback", () => { expect(res.status).toEqual(403); }); }); + +describe("#integrations.list", () => { + it("should expose the github settings of the integration", async () => { + const team = await buildTeam(); + const member = await buildUser({ teamId: team.id }); + const installation = { + id: 1, + account: { id: 2, name: "outline", avatarUrl: "https://example.com" }, + }; + await buildIntegration({ + userId: member.id, + teamId: team.id, + service: IntegrationService.GitHub, + type: IntegrationType.Embed, + settings: { + github: { installation }, + url: "https://example.com", + }, + }); + + const res = await server.post("/api/integrations.list", member); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data[0].settings).toEqual({ + url: "https://example.com", + github: { installation }, + }); + }); + + it("should not expose github settings to guests", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const guest = await buildGuestUser({ teamId: team.id }); + await buildIntegration({ + userId: admin.id, + teamId: team.id, + service: IntegrationService.GitHub, + type: IntegrationType.Embed, + settings: { + url: "https://example.com", + github: { + installation: { + id: 123, + account: { id: 456, name: "acme", avatarUrl: "" }, + }, + }, + }, + }); + + const res = await server.post("/api/integrations.list", guest); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data[0].settings).toEqual({ url: "https://example.com" }); + }); +}); diff --git a/plugins/github/server/index.ts b/plugins/github/server/index.ts index 34e2d4ac6f..a537cb5874 100644 --- a/plugins/github/server/index.ts +++ b/plugins/github/server/index.ts @@ -1,3 +1,4 @@ +import { IntegrationService } from "@shared/types"; import { Minute } from "@shared/utils/time"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; @@ -9,6 +10,11 @@ import { GitHub } from "./github"; import GitHubWebhookTask from "./tasks/GitHubWebhookTask"; import { uninstall } from "./uninstall"; +PluginManager.add({ + type: Hook.IntegrationSettings, + value: { service: IntegrationService.GitHub, keys: ["github"] }, +}); + const enabled = !!env.GITHUB_CLIENT_ID && !!env.GITHUB_CLIENT_SECRET && diff --git a/plugins/github/shared/types.ts b/plugins/github/shared/types.ts new file mode 100644 index 0000000000..afa4ef7abc --- /dev/null +++ b/plugins/github/shared/types.ts @@ -0,0 +1,12 @@ +declare module "@shared/types" { + interface EmbedIntegrationSettings { + github?: { + installation: { + id: number; + account: { id: number; name: string; avatarUrl: string }; + }; + }; + } +} + +export {}; diff --git a/plugins/gitlab/client/index.tsx b/plugins/gitlab/client/index.tsx index 410af586cd..63199bd846 100644 --- a/plugins/gitlab/client/index.tsx +++ b/plugins/gitlab/client/index.tsx @@ -1,6 +1,9 @@ +import type { IntegrationSettings, IntegrationType } from "@shared/types"; +import { IntegrationService } from "@shared/types"; import { createLazyComponent } from "~/components/LazyLoad"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; +import { GitLabUtils } from "../shared/GitLabUtils"; import Icon from "./components/Icon"; PluginManager.add([ @@ -15,4 +18,27 @@ PluginManager.add([ component: createLazyComponent(() => import("./Settings")), }, }, + { + ...config, + type: Hook.MentionProvider, + value: { + service: IntegrationService.GitLab, + isMentionable: (url, integration) => { + const settings = + integration.settings as IntegrationSettings<IntegrationType.Embed>; + let hostname: string | undefined; + try { + hostname = settings.gitlab?.url + ? new URL(settings.gitlab.url).hostname + : undefined; + } catch { + return false; + } + + return url.hostname === "gitlab.com" || url.hostname === hostname; + }, + // The host is already checked by isMentionable. + mentionType: (url) => GitLabUtils.mentionType(url, url.origin), + }, + }, ]); diff --git a/plugins/gitlab/server/api/gitlab.test.ts b/plugins/gitlab/server/api/gitlab.test.ts index c86d69a611..5e66c4c85d 100644 --- a/plugins/gitlab/server/api/gitlab.test.ts +++ b/plugins/gitlab/server/api/gitlab.test.ts @@ -1,4 +1,5 @@ -import { buildUser, buildViewer } from "@server/test/factories"; +import { IntegrationService, IntegrationType } from "@shared/types"; +import { buildAdmin, buildUser, buildViewer } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; const server = getTestServer(); @@ -58,3 +59,22 @@ describe("#gitlab.callback", () => { expect(res.status).toEqual(403); }); }); + +describe("#integrations.create", () => { + it("should accept the settings of a gitlab integration", async () => { + const admin = await buildAdmin(); + + const res = await server.post("/api/integrations.create", admin, { + body: { + type: IntegrationType.Embed, + service: IntegrationService.GitLab, + settings: { gitlab: { url: "https://gitlab.example.com" } }, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.settings).toEqual({ + gitlab: { url: "https://gitlab.example.com" }, + }); + }); +}); diff --git a/plugins/gitlab/server/api/schema.ts b/plugins/gitlab/server/api/schema.ts index d6fdff9280..56bd7c0611 100644 --- a/plugins/gitlab/server/api/schema.ts +++ b/plugins/gitlab/server/api/schema.ts @@ -39,3 +39,19 @@ export const GitLabConnectSchema = BaseSchema.extend({ }); export type GitLabConnectReq = z.infer<typeof GitLabConnectSchema>; + +export const GitLabSettingsSchema = z.object({ + gitlab: z.object({ + url: z.url().optional(), + installation: z + .object({ + id: z.number(), + account: z.object({ + id: z.number(), + name: z.string(), + avatarUrl: z.string(), + }), + }) + .optional(), + }), +}); diff --git a/plugins/gitlab/server/index.ts b/plugins/gitlab/server/index.ts index 8f4c3c8166..8e533b13c4 100644 --- a/plugins/gitlab/server/index.ts +++ b/plugins/gitlab/server/index.ts @@ -1,13 +1,23 @@ +import { IntegrationService } from "@shared/types"; import { Minute } from "@shared/utils/time"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; import { GitLabUtils } from "../shared/GitLabUtils"; import { GitLabIssueProvider } from "./GitLabIssueProvider"; import router from "./api/gitlab"; +import { GitLabSettingsSchema } from "./api/schema"; import { GitLab } from "./gitlab"; import GitLabWebhookTask from "./tasks/GitLabWebhookTask"; PluginManager.add([ + { + type: Hook.IntegrationSettings, + value: { + service: IntegrationService.GitLab, + keys: ["gitlab"], + schema: GitLabSettingsSchema, + }, + }, { ...config, type: Hook.API, diff --git a/plugins/gitlab/shared/types.ts b/plugins/gitlab/shared/types.ts new file mode 100644 index 0000000000..fbefbdd3ce --- /dev/null +++ b/plugins/gitlab/shared/types.ts @@ -0,0 +1,13 @@ +declare module "@shared/types" { + interface EmbedIntegrationSettings { + gitlab?: { + url?: string; + installation?: { + id: number; + account: { id: number; name: string; avatarUrl: string }; + }; + }; + } +} + +export {}; diff --git a/plugins/google/server/auth/google.ts b/plugins/google/server/auth/google.ts index 93701a1828..a03b2d0929 100644 --- a/plugins/google/server/auth/google.ts +++ b/plugins/google/server/auth/google.ts @@ -8,10 +8,7 @@ import { toError } from "@shared/utils/error"; import { languages } from "@shared/i18n"; import { slugifyDomain } from "@shared/utils/domains"; import accountProvisioner from "@server/commands/accountProvisioner"; -import { - GmailAccountCreationError, - TeamDomainRequiredError, -} from "@server/errors"; +import { TeamDomainRequiredError } from "@server/errors"; import passportMiddleware from "@server/middlewares/passport"; import { AuthenticationProvider, User } from "@server/models"; import type { AuthenticationResult } from "@server/types"; @@ -26,6 +23,7 @@ import { import config from "../../plugin.json"; import env from "../env"; import { createContext } from "@server/context"; +import { GmailAccountCreationError } from "../errors"; const router = new Router(); diff --git a/plugins/google/server/errors.ts b/plugins/google/server/errors.ts new file mode 100644 index 0000000000..25c970284b --- /dev/null +++ b/plugins/google/server/errors.ts @@ -0,0 +1,10 @@ +import httpErrors from "http-errors"; + +export function GmailAccountCreationError( + message = "Cannot create account using personal Gmail address" +) { + return httpErrors(400, message, { + id: "gmail_account_creation", + isReportable: false, + }); +} diff --git a/plugins/google/server/index.ts b/plugins/google/server/index.ts index a21fce2436..babf221a17 100644 --- a/plugins/google/server/index.ts +++ b/plugins/google/server/index.ts @@ -1,5 +1,6 @@ import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import GoogleClient from "./google"; import router from "./auth/google"; import env from "./env"; @@ -10,7 +11,12 @@ if (enabled) { { ...config, type: Hook.AuthProvider, - value: { router, id: config.id, allowEmailMatch: true }, + value: { + router, + id: config.id, + allowEmailMatch: true, + oauthClient: () => new GoogleClient(), + }, }, ]); } diff --git a/server/typings/passport-google-oauth2.d.ts b/plugins/google/server/typings/passport-google-oauth2.d.ts similarity index 100% rename from server/typings/passport-google-oauth2.d.ts rename to plugins/google/server/typings/passport-google-oauth2.d.ts diff --git a/app/components/Icons/GoogleIcon.tsx b/plugins/googleanalytics/client/GoogleIcon.tsx similarity index 100% rename from app/components/Icons/GoogleIcon.tsx rename to plugins/googleanalytics/client/GoogleIcon.tsx diff --git a/plugins/googleanalytics/client/Settings.tsx b/plugins/googleanalytics/client/Settings.tsx index 674f9617e9..2bac037e83 100644 --- a/plugins/googleanalytics/client/Settings.tsx +++ b/plugins/googleanalytics/client/Settings.tsx @@ -11,7 +11,7 @@ import { IntegrationScene } from "~/scenes/Settings/components/IntegrationScene" import SettingRow from "~/scenes/Settings/components/SettingRow"; import Button from "~/components/Button"; import Heading from "~/components/Heading"; -import GoogleIcon from "~/components/Icons/GoogleIcon"; +import GoogleIcon from "./GoogleIcon"; import Input from "~/components/Input"; import Text from "~/components/Text"; import useStores from "~/hooks/useStores"; diff --git a/plugins/iframely/server/iframely.test.ts b/plugins/iframely/server/iframely.test.ts new file mode 100644 index 0000000000..21ae340820 --- /dev/null +++ b/plugins/iframely/server/iframely.test.ts @@ -0,0 +1,150 @@ +// @vitest-isolate true +import type { Mock } from "vitest"; +import { randomString } from "@shared/random"; +import { UnfurlResourceType } from "@shared/types"; +import { Hour } from "@shared/utils/time"; +import type { User } from "@server/models"; +import Redis from "@server/storage/redis"; +import { buildGuestUser, buildUser } from "@server/test/factories"; +import { getTestServer } from "@server/test/support"; +import { RedisPrefixHelper } from "@server/utils/RedisPrefixHelper"; +import Iframely from "./iframely"; + +vi.spyOn(Iframely, "requestResource").mockImplementation(() => + Promise.resolve({}) +); + +const server = getTestServer(); + +describe("#urls.unfurl", () => { + let user: User; + beforeEach(async () => { + user = await buildUser(); + }); + + it("should succeed with status 200 ok for a valid external url", async () => { + (Iframely.requestResource as Mock).mockResolvedValue( + Promise.resolve({ + url: "https://www.flickr.com", + type: "rich", + meta: { + title: "Flickr", + description: + "The safest and most inclusive global community of photography enthusiasts. The best place for inspiration, connection, and sharing!", + }, + links: { + thumbnail: [ + { + href: "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/671aaf5d51c929e483e8b26d_Open%20Graph%20Home.jpg", + type: "image/jpg", + rel: ["twitter", "thumbnail", "ssl", "og"], + content_length: 412824, + media: { + width: 1200, + height: 630, + }, + }, + ], + icon: [ + { + href: "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/67167dd041b0982f0f230dab_flickr-webclip.png", + rel: ["apple-touch-icon", "icon", "ssl"], + type: "image/png", + }, + ], + }, + }) + ); + + const res = await server.post("/api/urls.unfurl", user, { + body: { + url: "https://www.flickr.com", + }, + }); + + expect(res.status).toEqual(200); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.url).toEqual("https://www.flickr.com"); + expect(body.type).toEqual(UnfurlResourceType.URL); + expect(body.title).toEqual("Flickr"); + expect(body.description).toEqual( + "The safest and most inclusive global community of photography enthusiasts. The best place for inspiration, connection, and sharing!" + ); + expect(body.thumbnailUrl).toEqual( + "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/671aaf5d51c929e483e8b26d_Open%20Graph%20Home.jpg" + ); + }); + + it("should succeed with status 204 no content for a non-existing external url", async () => { + (Iframely.requestResource as Mock).mockResolvedValue( + Promise.resolve({ + status: 404, + error: + "Iframely could not fetch the given URL. The content is no longer available at the origin.", + }) + ); + + const res = await server.post("/api/urls.unfurl", user, { + body: { + url: "https://random.url", + }, + }); + + expect(res.status).toEqual(204); + }); + + it("should cache a permanent external url error for a day", async () => { + (Iframely.requestResource as Mock).mockResolvedValue({ + status: 403, + error: "Iframely could not fetch the given URL.", + }); + const url = `https://${randomString(8)}.example.com`; + + const res = await server.post("/api/urls.unfurl", user, { + body: { url }, + }); + + expect(res.status).toEqual(204); + const ttl = await Redis.defaultClient.ttl( + RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url) + ); + expect(ttl).toBeGreaterThan(Hour.seconds); + }); + + it("should cache a transient external url error for a minute", async () => { + (Iframely.requestResource as Mock).mockResolvedValue({ + status: 418, + error: "Iframely timed out fetching the given URL.", + }); + const url = `https://${randomString(8)}.example.com`; + + const res = await server.post("/api/urls.unfurl", user, { + body: { url }, + }); + + expect(res.status).toEqual(204); + const ttl = await Redis.defaultClient.ttl( + RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url) + ); + expect(ttl).toBeGreaterThan(0); + expect(ttl).toBeLessThanOrEqual(60); + }); + + it("should not unfurl external urls for guests", async () => { + const guest = await buildGuestUser({ teamId: user.teamId }); + const callCount = (Iframely.requestResource as Mock).mock.calls.length; + + const res = await server.post("/api/urls.unfurl", guest, { + body: { + url: "https://github.com/acme/private/issues/1", + }, + }); + + expect(res.status).toEqual(204); + expect((Iframely.requestResource as Mock).mock.calls.length).toEqual( + callCount + ); + }); +}); diff --git a/plugins/linear/client/index.tsx b/plugins/linear/client/index.tsx index c55a835c94..0f0d0eb39d 100644 --- a/plugins/linear/client/index.tsx +++ b/plugins/linear/client/index.tsx @@ -1,6 +1,9 @@ +import type { IntegrationSettings, IntegrationType } from "@shared/types"; +import { IntegrationService } from "@shared/types"; import { createLazyComponent } from "~/components/LazyLoad"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; +import { LinearUtils } from "../shared/LinearUtils"; import Icon from "./Icon"; PluginManager.add([ @@ -15,4 +18,22 @@ PluginManager.add([ component: createLazyComponent(() => import("./Settings")), }, }, + { + ...config, + type: Hook.MentionProvider, + value: { + service: IntegrationService.Linear, + isMentionable: (url, integration) => { + const settings = + integration.settings as IntegrationSettings<IntegrationType.Embed>; + + // Ensure the installed workspace key matches the URL. + return ( + url.hostname === "linear.app" && + settings.linear?.workspace.key === url.pathname.split("/")[1] + ); + }, + mentionType: (url) => LinearUtils.mentionType(url), + }, + }, ]); diff --git a/plugins/linear/server/api/linear.ts b/plugins/linear/server/api/linear.ts index 16a4b7c65b..27ac66a89d 100644 --- a/plugins/linear/server/api/linear.ts +++ b/plugins/linear/server/api/linear.ts @@ -105,6 +105,7 @@ router.get( await new UploadIntegrationLogoTask().schedule({ integrationId: integration.id, logoUrl: workspace.logoUrl, + settingsPath: ["linear", "workspace", "logoUrl"], }); } }); diff --git a/plugins/linear/server/index.ts b/plugins/linear/server/index.ts index a94f896472..b0f648e338 100644 --- a/plugins/linear/server/index.ts +++ b/plugins/linear/server/index.ts @@ -1,3 +1,4 @@ +import { IntegrationService } from "@shared/types"; import { Minute } from "@shared/utils/time"; import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; @@ -8,6 +9,11 @@ import { Linear } from "./linear"; import UploadIntegrationLogoTask from "@server/queues/tasks/UploadIntegrationLogoTask"; import { uninstall } from "./uninstall"; +PluginManager.add({ + type: Hook.IntegrationSettings, + value: { service: IntegrationService.Linear, keys: ["linear"] }, +}); + const enabled = !!env.LINEAR_CLIENT_ID && !!env.LINEAR_CLIENT_SECRET; if (enabled) { diff --git a/plugins/linear/shared/types.ts b/plugins/linear/shared/types.ts new file mode 100644 index 0000000000..04efc0561a --- /dev/null +++ b/plugins/linear/shared/types.ts @@ -0,0 +1,9 @@ +declare module "@shared/types" { + interface EmbedIntegrationSettings { + linear?: { + workspace: { id: string; name: string; key: string; logoUrl?: string }; + }; + } +} + +export {}; diff --git a/plugins/notion/server/api/notion.test.ts b/plugins/notion/server/api/notion.test.ts index 845e7c3d8e..66936e333a 100644 --- a/plugins/notion/server/api/notion.test.ts +++ b/plugins/notion/server/api/notion.test.ts @@ -1,5 +1,19 @@ -import { buildUser, buildViewer } from "@server/test/factories"; +import { + CollectionPermission, + ImportState, + IntegrationService, + IntegrationType, +} from "@shared/types"; +import { Integration } from "@server/models"; +import { + buildAdmin, + buildImport, + buildIntegration, + buildUser, + buildViewer, +} from "@server/test/factories"; import { getTestServer } from "@server/test/support"; +import type { NotionImportInput } from "../../shared/schema"; const server = getTestServer(); @@ -58,3 +72,72 @@ describe("#notion.callback", () => { expect(res.status).toEqual(403); }); }); + +describe("#imports.create", () => { + it("should create an import", async () => { + const admin = await buildAdmin(); + const integration = await Integration.create< + Integration<IntegrationType.Import> + >({ + service: IntegrationService.Notion, + type: IntegrationType.Import, + userId: admin.id, + teamId: admin.teamId, + settings: { + externalWorkspace: { + id: "testId", + name: "testWorkspaceName", + }, + }, + }); + const input: NotionImportInput = [{ permission: undefined }]; + + const res = await server.post("/api/imports.create", admin, { + body: { + integrationId: integration.id, + service: IntegrationService.Notion, + input, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.id).toBeTruthy(); + expect(body.data.name).toEqual("testWorkspaceName"); + expect(body.data.state).toEqual(ImportState.Created); + expect(body.data.service).toEqual(IntegrationService.Notion); + expect(body.data.createdById).toEqual(admin.id); + }); + + it("should not allow more than one active import at a time", async () => { + const admin = await buildAdmin(); + const integration = await buildIntegration({ + userId: admin.id, + teamId: admin.teamId, + service: IntegrationService.Notion, + type: IntegrationType.Import, + settings: { + externalWorkspace: { id: "ws-1", name: "Test Workspace" }, + // oxlint-disable-next-line @typescript-eslint/no-explicit-any + } as any, + }); + const input: NotionImportInput = [ + { permission: CollectionPermission.Read }, + ]; + await buildImport({ + createdById: admin.id, + teamId: admin.teamId, + integrationId: integration.id, + }); + + const res = await server.post("/api/imports.create", admin, { + body: { + integrationId: integration.id, + service: IntegrationService.Notion, + input, + }, + }); + + expect(res.status).toEqual(422); + }); +}); diff --git a/plugins/notion/server/index.ts b/plugins/notion/server/index.ts index db80e25850..7ad7cbe433 100644 --- a/plugins/notion/server/index.ts +++ b/plugins/notion/server/index.ts @@ -1,10 +1,22 @@ +import { z } from "zod"; +import { IntegrationService } from "@shared/types"; +import { IntegrationImportBodySchema } from "@server/routes/api/imports/schema"; import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; import router from "./api/notion"; +import { NotionImportInputItemSchema } from "../shared/schema"; import env from "./env"; import { NotionImportsProcessor } from "./processors/NotionImportsProcessor"; import NotionAPIImportTask from "./tasks/NotionAPIImportTask"; +PluginManager.add({ + type: Hook.ImportSchema, + value: IntegrationImportBodySchema.extend({ + service: z.literal(IntegrationService.Notion), + input: z.array(NotionImportInputItemSchema), + }), +}); + const enabled = !!env.NOTION_CLIENT_ID && !!env.NOTION_CLIENT_SECRET; if (enabled) { diff --git a/plugins/notion/server/processors/NotionImportsProcessor.ts b/plugins/notion/server/processors/NotionImportsProcessor.ts index a23675dad7..b2b512c0ec 100644 --- a/plugins/notion/server/processors/NotionImportsProcessor.ts +++ b/plugins/notion/server/processors/NotionImportsProcessor.ts @@ -1,5 +1,8 @@ import type { Transaction } from "sequelize"; -import type { NotionImportInput, NotionImportTaskInput } from "@shared/schema"; +import type { + NotionImportInput, + NotionImportTaskInput, +} from "../../shared/schema"; import { IntegrationService } from "@shared/types"; import type { Import, ImportTask } from "@server/models"; import { Integration } from "@server/models"; diff --git a/plugins/notion/shared/schema.ts b/plugins/notion/shared/schema.ts new file mode 100644 index 0000000000..cffbf8ac45 --- /dev/null +++ b/plugins/notion/shared/schema.ts @@ -0,0 +1,33 @@ +import { z } from "zod"; +import { + BaseImportInputItemSchema, + BaseImportTaskInputItemSchema, +} from "@shared/schema"; +import type { IntegrationService } from "@shared/types"; +import { PageType } from "./types"; + +export const NotionImportInputItemSchema = BaseImportInputItemSchema.extend({ + type: z.enum(PageType).optional(), + externalId: z.string().optional(), +}); + +export type NotionImportInput = z.infer<typeof NotionImportInputItemSchema>[]; + +export const NotionImportTaskInputItemSchema = + BaseImportTaskInputItemSchema.extend({ + type: z.enum(PageType), + }); + +export type NotionImportTaskInput = z.infer< + typeof NotionImportTaskInputItemSchema +>[]; + +declare module "@shared/schema" { + interface ImportInputMap { + [IntegrationService.Notion]: NotionImportInput; + } + + interface ImportTaskInputMap { + [IntegrationService.Notion]: NotionImportTaskInput; + } +} diff --git a/plugins/oidc/server/auth/oidcRouter.ts b/plugins/oidc/server/auth/oidcRouter.ts index bbdcb114e7..9eed7a9c85 100644 --- a/plugins/oidc/server/auth/oidcRouter.ts +++ b/plugins/oidc/server/auth/oidcRouter.ts @@ -9,10 +9,7 @@ import { parseEmail } from "@shared/utils/email"; import { getSupportedLanguage } from "@shared/utils/language"; import { isBase64Url } from "@shared/utils/urls"; import accountProvisioner from "@server/commands/accountProvisioner"; -import { - OIDCMalformedUserInfoError, - AuthenticationError, -} from "@server/errors"; +import { AuthenticationError } from "@server/errors"; import Logger from "@server/logging/Logger"; import passportMiddleware from "@server/middlewares/passport"; import type { User } from "@server/models"; @@ -32,6 +29,7 @@ import env from "../env"; import { OIDCStrategy } from "./OIDCStrategy"; import { resolveProviderId } from "./resolveProviderId"; import { createContext } from "@server/context"; +import { OIDCMalformedUserInfoError } from "../errors"; export interface OIDCEndpoints { authorizationURL: string; diff --git a/plugins/oidc/server/errors.ts b/plugins/oidc/server/errors.ts new file mode 100644 index 0000000000..542087b47e --- /dev/null +++ b/plugins/oidc/server/errors.ts @@ -0,0 +1,10 @@ +import httpErrors from "http-errors"; + +export function OIDCMalformedUserInfoError( + message = "User profile information malformed" +) { + return httpErrors(400, message, { + id: "malformed_user_info", + isReportable: false, + }); +} diff --git a/plugins/oidc/server/index.ts b/plugins/oidc/server/index.ts index 0d7c95aeaa..3b2a304686 100644 --- a/plugins/oidc/server/index.ts +++ b/plugins/oidc/server/index.ts @@ -1,6 +1,7 @@ import Logger from "@server/logging/Logger"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import OIDCClient from "./oidc"; import router from "./auth/oidc"; import env from "./env"; @@ -27,7 +28,11 @@ if (enabled) { { ...config, type: Hook.AuthProvider, - value: { router, id: config.id }, + value: { + router, + id: config.id, + oauthClient: () => new OIDCClient(), + }, name: env.OIDC_DISPLAY_NAME || config.name, }, ]); diff --git a/plugins/slab/server/index.ts b/plugins/slab/server/index.ts index 9dd94b8c42..1cb46a2c99 100644 --- a/plugins/slab/server/index.ts +++ b/plugins/slab/server/index.ts @@ -1,9 +1,18 @@ +import { z } from "zod"; +import { IntegrationService } from "@shared/types"; +import { AttachmentImportBodySchema } from "@server/routes/api/imports/schema"; import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; import { SlabImportsProcessor } from "./processors/SlabImportsProcessor"; import SlabAPIImportTask from "./tasks/SlabAPIImportTask"; PluginManager.add([ + { + type: Hook.ImportSchema, + value: AttachmentImportBodySchema.extend({ + service: z.literal(IntegrationService.Slab), + }), + }, { ...config, type: Hook.Processor, diff --git a/plugins/slab/server/slab.test.ts b/plugins/slab/server/slab.test.ts new file mode 100644 index 0000000000..44d03c5b54 --- /dev/null +++ b/plugins/slab/server/slab.test.ts @@ -0,0 +1,27 @@ +import { ImportState, IntegrationService } from "@shared/types"; +import { buildAdmin, buildAttachment } from "@server/test/factories"; +import { getTestServer } from "@server/test/support"; + +const server = getTestServer(); + +describe("#imports.create", () => { + it("should create a slab import from an uploaded file", async () => { + const admin = await buildAdmin(); + const attachment = await buildAttachment({ + teamId: admin.teamId, + userId: admin.id, + }); + + const res = await server.post("/api/imports.create", admin, { + body: { + attachmentId: attachment.id, + service: IntegrationService.Slab, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.state).toEqual(ImportState.Created); + expect(body.data.service).toEqual(IntegrationService.Slab); + }); +}); diff --git a/plugins/slab/shared/types.ts b/plugins/slab/shared/types.ts new file mode 100644 index 0000000000..1d4fb9dae4 --- /dev/null +++ b/plugins/slab/shared/types.ts @@ -0,0 +1,21 @@ +import type { + MarkdownImportInput, + MarkdownImportScratch, + MarkdownImportTaskInput, +} from "@shared/schema"; +import type { IntegrationService } from "@shared/types"; + +// Slab exports are imported through the Markdown importer. +declare module "@shared/schema" { + interface ImportInputMap { + [IntegrationService.Slab]: MarkdownImportInput; + } + + interface ImportScratchMap { + [IntegrationService.Slab]: MarkdownImportScratch; + } + + interface ImportTaskInputMap { + [IntegrationService.Slab]: MarkdownImportTaskInput; + } +} diff --git a/server/typings/passport-slack-oauth2.d.ts b/plugins/slack/server/typings/passport-slack-oauth2.d.ts similarity index 100% rename from server/typings/passport-slack-oauth2.d.ts rename to plugins/slack/server/typings/passport-slack-oauth2.d.ts diff --git a/plugins/slack/shared/types.ts b/plugins/slack/shared/types.ts new file mode 100644 index 0000000000..a7de37b7f9 --- /dev/null +++ b/plugins/slack/shared/types.ts @@ -0,0 +1,7 @@ +declare module "@shared/types" { + interface LinkedAccountIntegrationSettings { + slack?: { serviceTeamId: string; serviceUserId: string }; + } +} + +export {}; diff --git a/plugins/zapier/client/typings.d.ts b/plugins/zapier/client/typings.d.ts new file mode 100644 index 0000000000..37624f7ae5 --- /dev/null +++ b/plugins/zapier/client/typings.d.ts @@ -0,0 +1,11 @@ +import "react"; + +// React 19 resolves intrinsic elements from React.JSX rather than the global +// JSX namespace, so custom elements must be declared here. +declare module "react" { + namespace JSX { + interface IntrinsicElements { + "zapier-app-directory": any; + } + } +} diff --git a/server/errors.test.ts b/server/errors.test.ts index b40b388898..1cfb8d4952 100644 --- a/server/errors.test.ts +++ b/server/errors.test.ts @@ -40,16 +40,7 @@ describe("errors", () => { name: "EmailAuthenticationRequiredError", fn: errors.EmailAuthenticationRequiredError, }, - { name: "MicrosoftGraphError", fn: errors.MicrosoftGraphError }, { name: "TeamDomainRequiredError", fn: errors.TeamDomainRequiredError }, - { - name: "GmailAccountCreationError", - fn: errors.GmailAccountCreationError, - }, - { - name: "OIDCMalformedUserInfoError", - fn: errors.OIDCMalformedUserInfoError, - }, { name: "AuthenticationProviderDisabledError", fn: errors.AuthenticationProviderDisabledError, diff --git a/server/errors.ts b/server/errors.ts index 7a28badfb9..1c60e9f95e 100644 --- a/server/errors.ts +++ b/server/errors.ts @@ -195,15 +195,6 @@ export function EmailAuthenticationRequiredError( }); } -export function MicrosoftGraphError( - message = "Microsoft Graph API did not return required fields" -) { - return httpErrors(400, message, { - id: "graph_error", - isReportable: false, - }); -} - export function TeamDomainRequiredError( message = "Unable to determine workspace from current domain or subdomain" ) { @@ -213,24 +204,6 @@ export function TeamDomainRequiredError( }); } -export function GmailAccountCreationError( - message = "Cannot create account using personal Gmail address" -) { - return httpErrors(400, message, { - id: "gmail_account_creation", - isReportable: false, - }); -} - -export function OIDCMalformedUserInfoError( - message = "User profile information malformed" -) { - return httpErrors(400, message, { - id: "malformed_user_info", - isReportable: false, - }); -} - export function AuthenticationProviderDisabledError( message = "Authentication method has been disabled by an admin", redirectPath = "/" diff --git a/server/logging/sentry.ts b/server/logging/sentry.ts index 979de39e7b..816b15069e 100644 --- a/server/logging/sentry.ts +++ b/server/logging/sentry.ts @@ -72,7 +72,6 @@ function loadSentry(dsn: string): typeof SentryNode { // Authentication "UnauthorizedError", "TeamDomainRequiredError", - "GmailAccountCreationError", "AuthRedirectError", "UserSuspendedError", "TooManyRequestsError", diff --git a/server/models/AuthenticationProvider.ts b/server/models/AuthenticationProvider.ts index 3da97b2681..3f0425748a 100644 --- a/server/models/AuthenticationProvider.ts +++ b/server/models/AuthenticationProvider.ts @@ -26,11 +26,6 @@ import Team from "./Team"; import UserAuthentication from "./UserAuthentication"; import Length from "./validators/Length"; -// TODO: Avoid this hardcoding of plugins -import AzureClient from "plugins/azure/server/azure"; -import DiscordClient from "plugins/discord/server/discord"; -import GoogleClient from "plugins/google/server/google"; -import OIDCClient from "plugins/oidc/server/oidc"; import type { APIContext } from "@server/types"; import type { DestroyOptions } from "sequelize"; @@ -119,18 +114,9 @@ class AuthenticationProvider extends Model< * @returns A configured OAuthClient instance */ get oauthClient() { - switch (this.name) { - case "google": - return new GoogleClient(); - case "azure": - return new AzureClient(); - case "oidc": - return new OIDCClient(); - case "discord": - return new DiscordClient(); - default: - return undefined; - } + return AuthenticationHelper.providers + .find((p) => p.value.id === this.name) + ?.value.oauthClient?.(); } /** diff --git a/server/models/Integration.ts b/server/models/Integration.ts index 97139b42a9..225d24c3fa 100644 --- a/server/models/Integration.ts +++ b/server/models/Integration.ts @@ -18,6 +18,7 @@ import IntegrationAuthentication from "@server/models/IntegrationAuthentication" import Team from "@server/models/Team"; import User from "@server/models/User"; import ParanoidModel from "@server/models/base/ParanoidModel"; +import { Hook, PluginManager } from "@server/utils/PluginManager"; @Scopes(() => ({ withAuthentication: { @@ -118,19 +119,7 @@ class Integration<T = unknown> extends ParanoidModel< case IntegrationType.Embed: { const settings = this .settings as IntegrationSettings<IntegrationType.Embed>; - - // Guests only need the URLs used to render and edit embeds. - if (viewer?.isGuest) { - return { url: settings?.url, diagrams: settings?.diagrams }; - } - return { - url: settings?.url, - github: settings?.github, - gitlab: settings?.gitlab, - gitea: settings?.gitea, - linear: settings?.linear, - diagrams: settings?.diagrams, - }; + return { url: settings?.url, ...this.presentPluginSettings(viewer) }; } case IntegrationType.Analytics: { const settings = this @@ -151,15 +140,31 @@ class Integration<T = unknown> extends ParanoidModel< if (!viewer || viewer.id !== this.userId) { return undefined; } - const settings = this - .settings as IntegrationSettings<IntegrationType.LinkedAccount>; - return { figma: settings?.figma }; + return this.presentPluginSettings(); } default: return undefined; } } + /** + * Picks the settings keys that plugins registered for this service. + * + * @param viewer the user the settings are presented to, if any. + * @returns the registered settings, keyed by name. + */ + private presentPluginSettings(viewer?: User) { + const settings = this.settings as Record<string, unknown> | null; + const keys = PluginManager.getHooks(Hook.IntegrationSettings) + .filter( + (hook) => + hook.value.service === this.service && + (!viewer?.isGuest || hook.value.visibleToGuests) + ) + .flatMap((hook) => hook.value.keys); + return Object.fromEntries(keys.map((key) => [key, settings?.[key]])); + } + // hooks @AfterDestroy diff --git a/server/queues/tasks/UploadIntegrationLogoTask.test.ts b/server/queues/tasks/UploadIntegrationLogoTask.test.ts new file mode 100644 index 0000000000..fed4385fe7 --- /dev/null +++ b/server/queues/tasks/UploadIntegrationLogoTask.test.ts @@ -0,0 +1,25 @@ +import { IntegrationService, IntegrationType } from "@shared/types"; +import { buildIntegration, buildUser } from "@server/test/factories"; +import UploadIntegrationLogoTask from "./UploadIntegrationLogoTask"; + +describe("UploadIntegrationLogoTask", () => { + it("should not change settings for jobs without a settings path", async () => { + const user = await buildUser(); + const settings = { url: "https://example.com" }; + const integration = await buildIntegration({ + userId: user.id, + teamId: user.teamId, + service: IntegrationService.Linear, + type: IntegrationType.Embed, + settings, + }); + + await new UploadIntegrationLogoTask().perform({ + integrationId: integration.id, + logoUrl: "https://example.com/logo.png", + }); + + await integration.reload(); + expect(integration.settings).toEqual(settings); + }); +}); diff --git a/server/queues/tasks/UploadIntegrationLogoTask.ts b/server/queues/tasks/UploadIntegrationLogoTask.ts index 04a35892ba..9db1954675 100644 --- a/server/queues/tasks/UploadIntegrationLogoTask.ts +++ b/server/queues/tasks/UploadIntegrationLogoTask.ts @@ -1,20 +1,17 @@ -import type { IntegrationType } from "@shared/types"; -import { IntegrationService, AttachmentPreset } from "@shared/types"; +import { cloneDeep, set } from "es-toolkit/compat"; +import { AttachmentPreset } from "@shared/types"; import attachmentCreator from "@server/commands/attachmentCreator"; import { createContext } from "@server/context"; import { Integration, User } from "@server/models"; import { BaseTask, TaskPriority } from "@server/queues/tasks/base/BaseTask"; -const SupportedIntegrations = [ - IntegrationService.Linear, - IntegrationService.Figma, -]; - type Props = { /** The integrationId to operate on */ integrationId: string; /** The original logoUrl from third-party service */ logoUrl: string; + /** The path in the integration settings to store the uploaded url at, missing on older jobs */ + settingsPath?: string[]; }; /** @@ -23,10 +20,15 @@ type Props = { */ export default class UploadIntegrationLogoTask extends BaseTask<Props> { public async perform(props: Props) { + const { settingsPath } = props; + if (!settingsPath) { + return; + } + const integration = await Integration.scope("withAuthentication").findByPk( props.integrationId ); - if (!integration || !SupportedIntegrations.includes(integration.service)) { + if (!integration) { return; } @@ -52,23 +54,11 @@ export default class UploadIntegrationLogoTask extends BaseTask<Props> { return; } - switch (integration.service) { - case IntegrationService.Linear: - ( - integration as Integration<IntegrationType.Embed> - ).settings.linear!.workspace.logoUrl = attachment.url; - break; - case IntegrationService.Figma: - ( - integration as Integration<IntegrationType.LinkedAccount> - ).settings.figma!.account.avatarUrl = attachment.url; - break; - default: - throw new Error( - `Unsupported integration service: ${integration.service}` - ); // This should never happen - } - + integration.settings = set( + cloneDeep(integration.settings ?? {}), + settingsPath, + attachment.url + ); integration.changed("settings", true); await integration.save(); } diff --git a/server/routes/api/imports/imports.test.ts b/server/routes/api/imports/imports.test.ts index ef38970c06..0a244baa72 100644 --- a/server/routes/api/imports/imports.test.ts +++ b/server/routes/api/imports/imports.test.ts @@ -1,17 +1,9 @@ -import type { NotionImportInput } from "@shared/schema"; -import { - CollectionPermission, - ImportableIntegrationService, - ImportState, - IntegrationService, - IntegrationType, -} from "@shared/types"; +import { ImportState, IntegrationService } from "@shared/types"; import type { Import } from "@server/models"; -import { Integration } from "@server/models"; import { buildAdmin, + buildAttachment, buildImport, - buildIntegration, buildUser, } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -19,71 +11,41 @@ import { getTestServer } from "@server/test/support"; const server = getTestServer(); describe("#imports.create", () => { - it("should create an import", async () => { + it("should create an import from an uploaded file", async () => { const admin = await buildAdmin(); - const integration = await Integration.create< - Integration<IntegrationType.Import> - >({ - service: IntegrationService.Notion, - type: IntegrationType.Import, - userId: admin.id, + const attachment = await buildAttachment({ teamId: admin.teamId, - settings: { - externalWorkspace: { - id: "testId", - name: "testWorkspaceName", - }, - }, + userId: admin.id, }); - const input: NotionImportInput = [{ permission: undefined }]; const res = await server.post("/api/imports.create", admin, { body: { - integrationId: integration.id, - service: IntegrationService.Notion, - input, + attachmentId: attachment.id, + service: IntegrationService.Markdown, }, }); const body = await res.json(); expect(res.status).toEqual(200); - expect(body.data.id).toBeTruthy(); - expect(body.data.name).toEqual("testWorkspaceName"); expect(body.data.state).toEqual(ImportState.Created); - expect(body.data.service).toEqual(IntegrationService.Notion); - expect(body.data.createdById).toEqual(admin.id); + expect(body.data.service).toEqual(IntegrationService.Markdown); }); - it("should not allow more than one active import at a time", async () => { + it("should reject an unknown service", async () => { const admin = await buildAdmin(); - const integration = await buildIntegration({ + const attachment = await buildAttachment({ + teamId: admin.teamId, userId: admin.id, - teamId: admin.teamId, - service: IntegrationService.Notion, - type: IntegrationType.Import, - settings: { - externalWorkspace: { id: "ws-1", name: "Test Workspace" }, - // oxlint-disable-next-line @typescript-eslint/no-explicit-any - } as any, - }); - const input: NotionImportInput = [ - { permission: CollectionPermission.Read }, - ]; - await buildImport({ - createdById: admin.id, - teamId: admin.teamId, - integrationId: integration.id, }); const res = await server.post("/api/imports.create", admin, { body: { - integrationId: integration.id, - service: ImportableIntegrationService.Notion, - input, + attachmentId: attachment.id, + service: "unknown", }, }); - expect(res.status).toEqual(422); + expect(res.status).toEqual(400); }); it("should require authentication", async () => { diff --git a/server/routes/api/imports/imports.ts b/server/routes/api/imports/imports.ts index 446ed3e914..4d4fbd1742 100644 --- a/server/routes/api/imports/imports.ts +++ b/server/routes/api/imports/imports.ts @@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto"; import { truncate } from "es-toolkit/compat"; import type { WhereOptions } from "sequelize"; import type { IntegrationType } from "@shared/types"; -import { ImportState, IntegrationService, UserRole } from "@shared/types"; +import { ImportState, UserRole } from "@shared/types"; import { ImportValidation } from "@shared/validations"; import auth from "@server/middlewares/authentication"; import { rateLimiter } from "@server/middlewares/rateLimiter"; @@ -32,12 +32,7 @@ router.post( authorize(user, "createImport", user.team); - if ( - body.service === IntegrationService.Markdown || - body.service === IntegrationService.Slab || - body.service === IntegrationService.OKF || - body.service === IntegrationService.JSON - ) { + if ("attachmentId" in body) { const attachment = await Attachment.findByPk(body.attachmentId, { rejectOnEmpty: true, }); diff --git a/server/routes/api/imports/schema.ts b/server/routes/api/imports/schema.ts index e113b6b4d5..0e8c6fc98a 100644 --- a/server/routes/api/imports/schema.ts +++ b/server/routes/api/imports/schema.ts @@ -1,12 +1,40 @@ import { z } from "zod"; -import { NotionImportInputItemSchema } from "@shared/schema"; +import { BaseImportInputItemSchema } from "@shared/schema"; import { CollectionPermission, ImportableIntegrationService, IntegrationService, } from "@shared/types"; +import { Hook, PluginManager } from "@server/utils/PluginManager"; import { BaseSchema } from "../schema"; +/** The body to import an uploaded file. */ +export const AttachmentImportBodySchema = z.object({ + service: z.enum(ImportableIntegrationService), + attachmentId: z.uuid(), + permission: z.enum(CollectionPermission).optional(), +}); + +/** The body to import from a connected integration. */ +export const IntegrationImportBodySchema = z.object({ + service: z.enum(ImportableIntegrationService), + integrationId: z.uuid(), + input: z.array(BaseImportInputItemSchema), +}); + +/** A body that an import plugin accepts in imports.create. */ +export type ImportsCreateBody = + | z.infer<typeof AttachmentImportBodySchema> + | z.infer<typeof IntegrationImportBodySchema>; + +// Resolved on first parse, after plugins have registered their schemas. +const pluginBodies = z.lazy((): z.ZodType<ImportsCreateBody> => { + const schemas = PluginManager.getHooks(Hook.ImportSchema).map( + (hook) => hook.value + ); + return schemas.length ? z.union(schemas) : z.never(); +}); + const BaseIdSchema = z.object({ /** Id of the import */ id: z.uuid(), @@ -28,36 +56,16 @@ const ImportsSortParamsSchema = z.object({ .transform((val) => (val !== "ASC" ? "DESC" : val)), }); -const BaseBodySchema = z.object({ - integrationId: z.uuid(), -}); - export const ImportsCreateSchema = BaseSchema.extend({ - body: z.discriminatedUnion("service", [ - BaseBodySchema.extend({ - service: z.literal(IntegrationService.Notion), - input: z.array(NotionImportInputItemSchema), - }), - z.object({ - service: z.literal(IntegrationService.Markdown), - attachmentId: z.uuid(), - permission: z.enum(CollectionPermission).optional(), - }), - z.object({ - service: z.literal(IntegrationService.Slab), - attachmentId: z.uuid(), - permission: z.enum(CollectionPermission).optional(), - }), - z.object({ - service: z.literal(IntegrationService.OKF), - attachmentId: z.uuid(), - permission: z.enum(CollectionPermission).optional(), - }), - z.object({ - service: z.literal(IntegrationService.JSON), - attachmentId: z.uuid(), - permission: z.enum(CollectionPermission).optional(), + body: z.union([ + AttachmentImportBodySchema.extend({ + service: z.enum([ + IntegrationService.Markdown, + IntegrationService.OKF, + IntegrationService.JSON, + ]), }), + pluginBodies, ]), }); diff --git a/server/routes/api/integrations/integrations.test.ts b/server/routes/api/integrations/integrations.test.ts index 4c70547a9e..ff47c567a0 100644 --- a/server/routes/api/integrations/integrations.test.ts +++ b/server/routes/api/integrations/integrations.test.ts @@ -283,16 +283,10 @@ describe("#integrations.list", () => { await buildIntegration({ userId: admin.id, teamId: team.id, - service: IntegrationService.GitHub, + service: IntegrationService.Diagrams, type: IntegrationType.Embed, settings: { url: "https://example.com", - github: { - installation: { - id: 123, - account: { id: 456, name: "acme", avatarUrl: "" }, - }, - }, }, }); await buildIntegration({ @@ -314,32 +308,22 @@ describe("#integrations.list", () => { expect(body.data.length).toEqual(1); expect(body.data[0].type).toEqual(IntegrationType.Embed); expect(body.data[0].settings.url).toEqual("https://example.com"); - expect(body.data[0].settings.github).toBeUndefined(); }); }); describe("#integrations.info", () => { - const buildFigmaAccount = (owner: User) => + const buildLinkedAccount = (owner: User) => Integration.create<Integration<IntegrationType.LinkedAccount>>({ userId: owner.id, teamId: owner.teamId, service: IntegrationService.Figma, type: IntegrationType.LinkedAccount, - settings: { - figma: { - account: { - id: "123", - name: "owner", - email: "owner@example.com", - avatarUrl: "https://example.com/avatar.png", - }, - }, - }, + settings: {}, }); it("should expose linked account settings to the owner", async () => { const owner = await buildUser(); - const integration = await buildFigmaAccount(owner); + const integration = await buildLinkedAccount(owner); const res = await server.post("/api/integrations.info", owner, { body: { id: integration.id }, @@ -347,13 +331,13 @@ describe("#integrations.info", () => { const body = await res.json(); expect(res.status).toEqual(200); - expect(body.data.settings.figma.account.email).toEqual("owner@example.com"); + expect(body.data.settings).toEqual({}); }); it("should not allow other users to read a linked account", async () => { const owner = await buildUser(); const admin = await buildAdmin({ teamId: owner.teamId }); - const integration = await buildFigmaAccount(owner); + const integration = await buildLinkedAccount(owner); const res = await server.post("/api/integrations.info", admin, { body: { id: integration.id }, diff --git a/server/routes/api/integrations/schema.ts b/server/routes/api/integrations/schema.ts index 06eb0f9f26..b473f46673 100644 --- a/server/routes/api/integrations/schema.ts +++ b/server/routes/api/integrations/schema.ts @@ -1,12 +1,22 @@ import { z } from "zod"; +import type { EmbedIntegrationSettings } from "@shared/types"; import { IntegrationService, IntegrationType, UserCreatableIntegrationService, } from "@shared/types"; import { Integration } from "@server/models"; +import { Hook, PluginManager } from "@server/utils/PluginManager"; import { BaseSchema } from "../schema"; +// Resolved on first parse, after plugins have registered their schemas. +const pluginSettings = z.lazy((): z.ZodType<EmbedIntegrationSettings> => { + const schemas = PluginManager.getHooks(Hook.IntegrationSettings).flatMap( + (hook) => (hook.value.schema ? [hook.value.schema] : []) + ); + return schemas.length ? z.union(schemas) : z.never(); +}); + export const IntegrationsListSchema = BaseSchema.extend({ body: z.object({ /** Integrations sorting direction */ @@ -58,29 +68,8 @@ export const IntegrationsCreateSchema = BaseSchema.extend({ scriptName: z.string().optional(), }) ) - .or( - z.object({ - diagrams: z.object({ url: z.url() }), - }) - ) - .or( - z.object({ - gitlab: z.object({ - url: z.url().optional(), - installation: z - .object({ - id: z.number(), - account: z.object({ - id: z.number(), - name: z.string(), - avatarUrl: z.string(), - }), - }) - .optional(), - }), - }) - ) .or(z.object({ serviceTeamId: z.string() })) + .or(pluginSettings) .optional(), }), }); @@ -109,29 +98,8 @@ export const IntegrationsUpdateSchema = BaseSchema.extend({ scriptName: z.string().optional(), }) ) - .or( - z.object({ - diagrams: z.object({ url: z.url() }), - }) - ) - .or( - z.object({ - gitlab: z.object({ - url: z.url().optional(), - installation: z - .object({ - id: z.number(), - account: z.object({ - id: z.number(), - name: z.string(), - avatarUrl: z.string(), - }), - }) - .optional(), - }), - }) - ) .or(z.object({ serviceTeamId: z.string() })) + .or(pluginSettings) .optional(), /** Integration events */ diff --git a/server/routes/api/urls/urls.test.ts b/server/routes/api/urls/urls.test.ts index 4874844aa1..87029375da 100644 --- a/server/routes/api/urls/urls.test.ts +++ b/server/routes/api/urls/urls.test.ts @@ -1,8 +1,6 @@ // @vitest-isolate true -import type { Mock } from "vitest"; import { randomString } from "@shared/random"; import { UnfurlResourceType } from "@shared/types"; -import { Hour } from "@shared/utils/time"; import { createContext } from "@server/context"; import env from "@server/env"; import type { User } from "@server/models"; @@ -12,13 +10,9 @@ import { buildDocument, buildShare, buildTeam, - buildGuestUser, buildUser, } from "@server/test/factories"; -import Redis from "@server/storage/redis"; import { getTestServer } from "@server/test/support"; -import { RedisPrefixHelper } from "@server/utils/RedisPrefixHelper"; -import Iframely from "plugins/iframely/server/iframely"; const resolveCname = vi.hoisted( () => @@ -49,10 +43,6 @@ vi.mock("dns", () => ({ resolveCname, })); -vi.spyOn(Iframely, "requestResource").mockImplementation(() => - Promise.resolve({}) -); - const server = getTestServer(); describe("#urls.unfurl", () => { @@ -504,132 +494,6 @@ Install instructions here.` }); expect(res.status).toEqual(204); }); - - it("should succeed with status 200 ok for a valid external url", async () => { - (Iframely.requestResource as Mock).mockResolvedValue( - Promise.resolve({ - url: "https://www.flickr.com", - type: "rich", - meta: { - title: "Flickr", - description: - "The safest and most inclusive global community of photography enthusiasts. The best place for inspiration, connection, and sharing!", - }, - links: { - thumbnail: [ - { - href: "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/671aaf5d51c929e483e8b26d_Open%20Graph%20Home.jpg", - type: "image/jpg", - rel: ["twitter", "thumbnail", "ssl", "og"], - content_length: 412824, - media: { - width: 1200, - height: 630, - }, - }, - ], - icon: [ - { - href: "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/67167dd041b0982f0f230dab_flickr-webclip.png", - rel: ["apple-touch-icon", "icon", "ssl"], - type: "image/png", - }, - ], - }, - }) - ); - - const res = await server.post("/api/urls.unfurl", user, { - body: { - url: "https://www.flickr.com", - }, - }); - - expect(res.status).toEqual(200); - const body = await res.json(); - - expect(res.status).toEqual(200); - expect(body.url).toEqual("https://www.flickr.com"); - expect(body.type).toEqual(UnfurlResourceType.URL); - expect(body.title).toEqual("Flickr"); - expect(body.description).toEqual( - "The safest and most inclusive global community of photography enthusiasts. The best place for inspiration, connection, and sharing!" - ); - expect(body.thumbnailUrl).toEqual( - "https://combo.staticflickr.com/66a031f9fc343c5e42d965ca/671aaf5d51c929e483e8b26d_Open%20Graph%20Home.jpg" - ); - }); - - it("should not unfurl external urls for guests", async () => { - const guest = await buildGuestUser({ teamId: user.teamId }); - const callCount = (Iframely.requestResource as Mock).mock.calls.length; - - const res = await server.post("/api/urls.unfurl", guest, { - body: { - url: "https://github.com/acme/private/issues/1", - }, - }); - - expect(res.status).toEqual(204); - expect((Iframely.requestResource as Mock).mock.calls.length).toEqual( - callCount - ); - }); - - it("should succeed with status 204 no content for a non-existing external url", async () => { - (Iframely.requestResource as Mock).mockResolvedValue( - Promise.resolve({ - status: 404, - error: - "Iframely could not fetch the given URL. The content is no longer available at the origin.", - }) - ); - - const res = await server.post("/api/urls.unfurl", user, { - body: { - url: "https://random.url", - }, - }); - - expect(res.status).toEqual(204); - }); - - it("should cache a permanent external url error for a day", async () => { - (Iframely.requestResource as Mock).mockResolvedValue({ - status: 403, - error: "Iframely could not fetch the given URL.", - }); - const url = `https://${randomString(8)}.example.com`; - - const res = await server.post("/api/urls.unfurl", user, { - body: { url }, - }); - - expect(res.status).toEqual(204); - const ttl = await Redis.defaultClient.ttl( - RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url) - ); - expect(ttl).toBeGreaterThan(Hour.seconds); - }); - - it("should cache a transient external url error for a minute", async () => { - (Iframely.requestResource as Mock).mockResolvedValue({ - status: 418, - error: "Iframely timed out fetching the given URL.", - }); - const url = `https://${randomString(8)}.example.com`; - - const res = await server.post("/api/urls.unfurl", user, { - body: { url }, - }); - - expect(res.status).toEqual(204); - const ttl = await Redis.defaultClient.ttl( - RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url) - ); - expect(ttl).toBeGreaterThan(0); - expect(ttl).toBeLessThanOrEqual(60); - }); }); describe("#urls.checkEmbed", () => { diff --git a/server/utils/BaseIssueProvider.ts b/server/utils/BaseIssueProvider.ts index 49bdc7aaf2..191ba2471e 100644 --- a/server/utils/BaseIssueProvider.ts +++ b/server/utils/BaseIssueProvider.ts @@ -1,14 +1,11 @@ import type { IssueSource } from "@shared/schema"; -import type { - IntegrationType, - IssueTrackerIntegrationService, -} from "@shared/types"; +import type { IntegrationService, IntegrationType } from "@shared/types"; import type { Integration } from "@server/models"; export abstract class BaseIssueProvider { - service: IssueTrackerIntegrationService; + service: IntegrationService; - constructor(service: IssueTrackerIntegrationService) { + constructor(service: IntegrationService) { this.service = service; } diff --git a/server/utils/PluginManager.ts b/server/utils/PluginManager.ts index 734d707c57..57bc72f391 100644 --- a/server/utils/PluginManager.ts +++ b/server/utils/PluginManager.ts @@ -2,8 +2,15 @@ import path from "node:path"; import { glob } from "glob"; import type Router from "@koa/router"; import { isArray, sortBy } from "es-toolkit/compat"; -import type { Concrete } from "@shared/types"; +import type { z } from "zod"; +import type { + Concrete, + EmbedIntegrationSettings, + IntegrationService, + LinkedAccountIntegrationSettings, +} from "@shared/types"; import type BaseEmail from "@server/emails/templates/BaseEmail"; +import type { ImportsCreateBody } from "@server/routes/api/imports/schema"; import env from "@server/env"; import Logger from "@server/logging/Logger"; import type BaseProcessor from "@server/queues/processors/BaseProcessor"; @@ -13,6 +20,7 @@ import type { UnfurlSignature, UninstallSignature, } from "@server/types"; +import type OAuthClient from "./oauth"; import type { BaseIssueProvider } from "./BaseIssueProvider"; import type { GroupSyncProvider } from "./GroupSyncProvider"; import type { BaseSearchProvider } from "./BaseSearchProvider"; @@ -36,6 +44,8 @@ export enum Hook { API = "api", AuthProvider = "authProvider", EmailTemplate = "emailTemplate", + ImportSchema = "importSchema", + IntegrationSettings = "integrationSettings", IssueProvider = "issueProvider", Processor = "processor", SearchProvider = "searchProvider", @@ -60,9 +70,25 @@ type PluginValueMap = { * different Google Workspace domain, can sign in to a matching account. */ allowEmailMatch?: boolean; + /** Creates a client to validate and refresh the provider's access tokens. */ + oauthClient?: () => OAuthClient; }; // oxlint-disable-next-line @typescript-eslint/no-explicit-any -- typeof BaseEmail<EmailProps> isn't assignable from BaseEmail<Subtype>; plugins register heterogeneous template Props. [Hook.EmailTemplate]: Concrete<typeof BaseEmail<any>>; + [Hook.ImportSchema]: z.ZodType<ImportsCreateBody>; + [Hook.IntegrationSettings]: { + /** The integration service that the settings belong to. */ + service: IntegrationService; + /** The settings keys that are presented to clients. */ + keys: ( + | keyof EmbedIntegrationSettings + | keyof LinkedAccountIntegrationSettings + )[]; + /** Whether guests can see the keys, which only render and edit embeds. */ + visibleToGuests?: boolean; + /** A schema to validate settings given through the integrations API. */ + schema?: z.ZodType<EmbedIntegrationSettings>; + }; [Hook.IssueProvider]: BaseIssueProvider; [Hook.MentionProvider]: MentionSignature; [Hook.Processor]: Concrete<typeof BaseProcessor>; diff --git a/shared/components/IssueStatusIcon/GitHubIssueStatusIcon.tsx b/shared/components/IssueStatusIcon/DefaultIssueStatusIcon.tsx similarity index 84% rename from shared/components/IssueStatusIcon/GitHubIssueStatusIcon.tsx rename to shared/components/IssueStatusIcon/DefaultIssueStatusIcon.tsx index 8c5c0785f0..815b8f0a1f 100644 --- a/shared/components/IssueStatusIcon/GitHubIssueStatusIcon.tsx +++ b/shared/components/IssueStatusIcon/DefaultIssueStatusIcon.tsx @@ -1,10 +1,17 @@ import React from "react"; import type { BaseIconProps } from "."; -export function GitHubIssueStatusIcon(props: BaseIconProps) { +/** + * A status icon for issue trackers that do not register their own. + * + * @param props the issue state and display options. + * @returns the icon, or null for an unknown state. + */ +export function DefaultIssueStatusIcon(props: BaseIconProps) { const { state, className, size = 16 } = props; switch (state.name) { + case "opened": case "open": return ( <svg diff --git a/shared/components/IssueStatusIcon/GitLabIssueStatusIcon.tsx b/shared/components/IssueStatusIcon/GitLabIssueStatusIcon.tsx deleted file mode 100644 index 73a0b39bdf..0000000000 --- a/shared/components/IssueStatusIcon/GitLabIssueStatusIcon.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import React from "react"; -import type { BaseIconProps } from "."; - -export function GitLabIssueStatusIcon(props: BaseIconProps) { - const { state, className, size = 16 } = props; - - switch (state.name) { - case "opened": - case "open": - return ( - <svg - viewBox="0 0 16 16" - width={size} - height={size} - fill={state.color} - className={className} - > - <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" /> - <path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Z" /> - </svg> - ); - case "closed": - return ( - <svg - viewBox="0 0 16 16" - width={size} - height={size} - fill={state.color} - className={className} - > - <path d="M11.28 6.78a.75.75 0 0 0-1.06-1.06L7.25 8.69 5.78 7.22a.75.75 0 0 0-1.06 1.06l2 2a.75.75 0 0 0 1.06 0l3.5-3.5Z" /> - <path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0Zm-1.5 0a6.5 6.5 0 1 0-13 0 6.5 6.5 0 0 0 13 0Z" /> - </svg> - ); - default: - return null; - } -} diff --git a/shared/components/IssueStatusIcon/LinearIssueStatusIcon.tsx b/shared/components/IssueStatusIcon/WorkflowStatusIcon.tsx similarity index 93% rename from shared/components/IssueStatusIcon/LinearIssueStatusIcon.tsx rename to shared/components/IssueStatusIcon/WorkflowStatusIcon.tsx index af68e1b441..82c35437ba 100644 --- a/shared/components/IssueStatusIcon/LinearIssueStatusIcon.tsx +++ b/shared/components/IssueStatusIcon/WorkflowStatusIcon.tsx @@ -12,7 +12,13 @@ enum StateType { Canceled = "canceled", } -export function LinearIssueStatusIcon(props: BaseIconProps) { +/** + * A status icon for issues in a workflow, which shows the state type and progress. + * + * @param props the issue state and display options. + * @returns the icon. + */ +export function WorkflowStatusIcon(props: BaseIconProps) { const theme = useTheme(); const { state, size = 16 } = props; const percentage = diff --git a/shared/components/IssueStatusIcon/index.tsx b/shared/components/IssueStatusIcon/index.tsx index a79dd30b0e..11069b5c39 100644 --- a/shared/components/IssueStatusIcon/index.tsx +++ b/shared/components/IssueStatusIcon/index.tsx @@ -1,14 +1,8 @@ import * as React from "react"; import styled from "styled-components"; -import type { - IssueTrackerIntegrationService, - UnfurlResourceType, - UnfurlResponse, -} from "../../types"; -import { IntegrationService } from "../../types"; -import { GitHubIssueStatusIcon } from "./GitHubIssueStatusIcon"; -import { LinearIssueStatusIcon } from "./LinearIssueStatusIcon"; -import { GitLabIssueStatusIcon } from "./GitLabIssueStatusIcon"; +import type { UnfurlResourceType, UnfurlResponse } from "../../types"; +import { DefaultIssueStatusIcon } from "./DefaultIssueStatusIcon"; +import { WorkflowStatusIcon } from "./WorkflowStatusIcon"; export type BaseIconProps = { state: UnfurlResponse[UnfurlResourceType.Issue]["state"]; @@ -16,30 +10,25 @@ export type BaseIconProps = { size?: number; }; -type Props = BaseIconProps & { - service: IssueTrackerIntegrationService; -}; - -export function IssueStatusIcon(props: Props) { +/** + * Renders the status icon of an issue. Issues with a workflow state type show + * its progress, others show whether they are open or closed. + * + * @param props the issue state and display options. + * @returns the icon. + */ +export function IssueStatusIcon(props: BaseIconProps) { return ( <Icon size={props.size} className={props.className}> - {getIcon(props)} + {props.state.type ? ( + <WorkflowStatusIcon {...props} /> + ) : ( + <DefaultIssueStatusIcon {...props} /> + )} </Icon> ); } -function getIcon(props: Props) { - switch (props.service) { - case IntegrationService.GitHub: - case IntegrationService.Gitea: - return <GitHubIssueStatusIcon {...props} />; - case IntegrationService.Linear: - return <LinearIssueStatusIcon {...props} />; - case IntegrationService.GitLab: - return <GitLabIssueStatusIcon {...props} />; - } -} - const Icon = styled.span<{ size?: number }>` display: inline-flex; flex-shrink: 0; diff --git a/shared/editor/components/Mentions.tsx b/shared/editor/components/Mentions.tsx index 1b7353d8bd..1ff4c8a592 100644 --- a/shared/editor/components/Mentions.tsx +++ b/shared/editor/components/Mentions.tsx @@ -27,7 +27,6 @@ import useShare from "../../hooks/useShare"; import env from "../../env"; import theme from "../../styles/theme"; import { - IntegrationService, UnfurlResourceType, type JSONValue, type UnfurlResponse, @@ -362,19 +361,6 @@ export const MentionIssue = observer((props: IssuePrProps) => { const issue = unfurl as UnfurlResponse[UnfurlResourceType.Issue]; - let service = IntegrationService.GitLab; - try { - const parsedUrl = new URL(issue.url); - service = - parsedUrl.hostname === "github.com" - ? IntegrationService.GitHub - : parsedUrl.hostname === "linear.app" - ? IntegrationService.Linear - : IntegrationService.GitLab; - } catch { - // Invalid URL in unfurl data, default to GitLab - } - return ( <a {...attrs} @@ -386,7 +372,7 @@ export const MentionIssue = observer((props: IssuePrProps) => { rel="noopener noreferrer nofollow" > <Flex align="center" gap={6}> - <IssueStatusIcon size={14} service={service} state={issue.state} /> + <IssueStatusIcon size={14} state={issue.state} /> <Flex align="center" gap={4}> <Text> <Backticks content={issue.title} /> diff --git a/shared/editor/embeds/index.tsx b/shared/editor/embeds/index.tsx index 92df78a34f..d6a2dfcecb 100644 --- a/shared/editor/embeds/index.tsx +++ b/shared/editor/embeds/index.tsx @@ -90,6 +90,8 @@ export class EmbedDescriptor { component?: React.FunctionComponent<EmbedProps>; /** The integration settings, if any */ settings?: IntegrationSettings<IntegrationType.Embed>; + /** The URL of the instance that the integration is configured with, if any */ + instanceUrl?: string; /** Whether this embed has been disabled by the team admin */ disabled?: boolean; diff --git a/shared/editor/extensions/Diagrams.ts b/shared/editor/extensions/Diagrams.ts index 6a16eb54be..59e54cc6e7 100644 --- a/shared/editor/extensions/Diagrams.ts +++ b/shared/editor/extensions/Diagrams.ts @@ -272,9 +272,8 @@ export default class Diagrams extends Extension { ); const uiTheme = this.editor.props.theme.isDark ? "dark" : "atlas"; return ( - sanitizeUrl( - integration?.settings?.diagrams?.url ?? "https://embed.diagrams.net/" - ) + `?embed=1&ui=${uiTheme}&spin=1&modified=unsavedChanges&proto=json` + sanitizeUrl(integration?.instanceUrl ?? "https://embed.diagrams.net/") + + `?embed=1&ui=${uiTheme}&spin=1&modified=unsavedChanges&proto=json` ); } diff --git a/shared/schema.ts b/shared/schema.ts index 81b6077a46..4667f4bb2a 100644 --- a/shared/schema.ts +++ b/shared/schema.ts @@ -1,29 +1,17 @@ import { z } from "zod"; -import type { - IntegrationService, - ProsemirrorData, - ProsemirrorDoc, -} from "./types"; +import type { ProsemirrorData, ProsemirrorDoc } from "./types"; import { CollectionPermission, type ImportableIntegrationService, - IssueTrackerIntegrationService, + IntegrationService, } from "./types"; -import { PageType } from "plugins/notion/shared/types"; -const BaseImportInputItemSchema = z.object({ +export const BaseImportInputItemSchema = z.object({ permission: z.enum(CollectionPermission).optional(), }); export type BaseImportInput = z.infer<typeof BaseImportInputItemSchema>[]; -export const NotionImportInputItemSchema = BaseImportInputItemSchema.extend({ - type: z.enum(PageType).optional(), - externalId: z.string().optional(), -}); - -export type NotionImportInput = z.infer<typeof NotionImportInputItemSchema>[]; - export const MarkdownImportInputItemSchema = BaseImportInputItemSchema.extend({ externalId: z.string(), }); @@ -38,17 +26,15 @@ export const JSONImportInputItemSchema = BaseImportInputItemSchema.extend({ export type JSONImportInput = z.infer<typeof JSONImportInputItemSchema>[]; +/** The import input by service, plugins augment it with their importers. */ +export interface ImportInputMap { + [IntegrationService.Markdown]: MarkdownImportInput; + [IntegrationService.OKF]: MarkdownImportInput; + [IntegrationService.JSON]: JSONImportInput; +} + export type ImportInput<T extends ImportableIntegrationService> = - T extends IntegrationService.Notion - ? NotionImportInput - : T extends - | IntegrationService.Markdown - | IntegrationService.Slab - | IntegrationService.OKF - ? MarkdownImportInput - : T extends IntegrationService.JSON - ? JSONImportInput - : BaseImportInput; + T extends keyof ImportInputMap ? ImportInputMap[T] : BaseImportInput; export const BaseImportTaskInputItemSchema = z.object({ externalId: z.string(), @@ -60,15 +46,6 @@ export type BaseImportTaskInput = z.infer< typeof BaseImportTaskInputItemSchema >[]; -export const NotionImportTaskInputItemSchema = - BaseImportTaskInputItemSchema.extend({ - type: z.enum(PageType), - }); - -export type NotionImportTaskInput = z.infer< - typeof NotionImportTaskInputItemSchema ->[]; - /** * Manifest entry describing a single attachment discovered during the * Markdown zip bootstrap phase. The `id` is a pre-assigned UUID used both @@ -134,14 +111,14 @@ export interface JSONImportScratch { * isn't part of any single task's input. Cleared when the import flips to * `Completed`. */ -export type ImportScratch<T extends ImportableIntegrationService> = T extends - | IntegrationService.Markdown - | IntegrationService.Slab - | IntegrationService.OKF - ? MarkdownImportScratch - : T extends IntegrationService.JSON - ? JSONImportScratch - : never; +export interface ImportScratchMap { + [IntegrationService.Markdown]: MarkdownImportScratch; + [IntegrationService.OKF]: MarkdownImportScratch; + [IntegrationService.JSON]: JSONImportScratch; +} + +export type ImportScratch<T extends ImportableIntegrationService> = + T extends keyof ImportScratchMap ? ImportScratchMap[T] : never; /** * Per-page task input. Generated by the bootstrap task and consumed by @@ -215,17 +192,17 @@ export type JSONImportTaskInput = ( | JSONPageImportTaskInputItem )[]; +/** The import task input by service, plugins augment it with their importers. */ +export interface ImportTaskInputMap { + [IntegrationService.Markdown]: MarkdownImportTaskInput; + [IntegrationService.OKF]: MarkdownImportTaskInput; + [IntegrationService.JSON]: JSONImportTaskInput; +} + export type ImportTaskInput<T extends ImportableIntegrationService> = - T extends IntegrationService.Notion - ? NotionImportTaskInput - : T extends - | IntegrationService.Markdown - | IntegrationService.Slab - | IntegrationService.OKF - ? MarkdownImportTaskInput - : T extends IntegrationService.JSON - ? JSONImportTaskInput - : BaseImportTaskInput; + T extends keyof ImportTaskInputMap + ? ImportTaskInputMap[T] + : BaseImportTaskInput; // No reason to be here except for co-location with import task input. export type ImportTaskOutput = { @@ -252,7 +229,7 @@ export const IssueSource = z.object({ id: z.string().nonempty(), name: z.string().nonempty(), }), - service: z.enum(IssueTrackerIntegrationService), + service: z.enum(IntegrationService), }); export type IssueSource = z.infer<typeof IssueSource>; diff --git a/shared/types/integrations.ts b/shared/types/integrations.ts index 9579764b2a..119e2a133f 100644 --- a/shared/types/integrations.ts +++ b/shared/types/integrations.ts @@ -53,23 +53,6 @@ export const ImportableIntegrationService = { JSON: IntegrationService.JSON, } as const; -/** Integration services that are issue trackers. */ -export type IssueTrackerIntegrationService = Extract< - IntegrationService, - | IntegrationService.GitHub - | IntegrationService.GitLab - | IntegrationService.Gitea - | IntegrationService.Linear ->; - -/** Runtime values of `IssueTrackerIntegrationService`. */ -export const IssueTrackerIntegrationService = { - GitHub: IntegrationService.GitHub, - GitLab: IntegrationService.GitLab, - Gitea: IntegrationService.Gitea, - Linear: IntegrationService.Linear, -} as const; - /** Integration services that can be created through the API. */ export type UserCreatableIntegrationService = Extract< IntegrationService, @@ -91,37 +74,28 @@ export const UserCreatableIntegrationService = { GitLab: IntegrationService.GitLab, } as const; +/** The settings stored on an embed integration, plugins augment it with their keys. */ +export interface EmbedIntegrationSettings { + url?: string; +} + +/** The settings stored on a linked account integration, plugins augment it with their keys. */ +// oxlint-disable-next-line typescript/no-empty-object-type -- populated by module augmentation. +export interface LinkedAccountIntegrationSettings {} + +// Mapped copies, because interfaces are not assignable to JSON index signatures. +type EmbedSettings = Pick< + EmbedIntegrationSettings, + keyof EmbedIntegrationSettings +>; +type LinkedAccountSettings = Pick< + LinkedAccountIntegrationSettings, + keyof LinkedAccountIntegrationSettings +>; + /** The settings stored on an integration, by integration type. */ export type IntegrationSettings<T> = T extends IntegrationType.Embed - ? { - url?: string; - github?: { - installation: { - id: number; - account: { id: number; name: string; avatarUrl: string }; - }; - }; - gitlab?: { - url?: string; - installation?: { - id: number; - account: { id: number; name: string; avatarUrl: string }; - }; - }; - gitea?: { - url: string; - installation?: { - id: number; - account: { id: number; name: string; avatarUrl: string }; - }; - }; - linear?: { - workspace: { id: string; name: string; key: string; logoUrl?: string }; - }; - diagrams?: { - url: string; - }; - } + ? EmbedSettings : T extends IntegrationType.Analytics ? { measurementId: string; instanceUrl?: string; scriptName?: string } : T extends IntegrationType.Post @@ -133,56 +107,10 @@ export type IntegrationSettings<T> = T extends IntegrationType.Embed externalWorkspace: { id: string; name: string; iconUrl?: string }; } : T extends IntegrationType.LinkedAccount - ? { - slack?: { serviceTeamId: string; serviceUserId: string }; - figma?: { - account: { - id: string; - name: string; - email: string; - avatarUrl: string; - }; - }; - } + ? LinkedAccountSettings : | { url: string } - | { - github?: { - installation: { - id: number; - account: { - id?: number; - name: string; - avatarUrl?: string; - }; - }; - }; - gitlab?: { - url?: string; - installation?: { - id: number; - account: { - id?: number; - name: string; - avatarUrl?: string; - }; - }; - }; - gitea?: { - url: string; - installation?: { - id: number; - account: { - id?: number; - name: string; - avatarUrl?: string; - }; - }; - }; - diagrams?: { - url: string; - }; - } + | EmbedSettings | { serviceTeamId: string } | { measurementId: string;