diff --git a/.env.test b/.env.test index 838abc2216..b74e70cbf6 100644 --- a/.env.test +++ b/.env.test @@ -18,6 +18,7 @@ GITHUB_CLIENT_ID=123; GITHUB_CLIENT_SECRET=123; GITHUB_APP_NAME=outline-test; GITHUB_APP_ID=123 +GITHUB_WEBHOOK_SECRET=test-webhook-secret GITHUB_APP_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA\n-----END RSA PRIVATE KEY-----" GITLAB_CLIENT_ID=123 diff --git a/app/models/Document.ts b/app/models/Document.ts index bd383ed1ba..43db0092c9 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -93,6 +93,8 @@ export default class Document extends ArchivableModel implements Searchable { fileName?: string; /** The id of the document this document was duplicated from. */ originalDocumentId?: string; + /** A link to the document in the external source. */ + externalUrl?: string; }; @computed diff --git a/app/scenes/Document/components/Insights.tsx b/app/scenes/Document/components/Insights.tsx index 9a95916ec8..dff9c3d428 100644 --- a/app/scenes/Document/components/Insights.tsx +++ b/app/scenes/Document/components/Insights.tsx @@ -3,6 +3,7 @@ import { useTranslation } from "react-i18next"; import styled from "styled-components"; import { s } from "@shared/styles"; import { stringToColor } from "@shared/utils/color"; +import { sanitizeUrl } from "@shared/utils/urls"; import type User from "~/models/User"; import { Avatar, AvatarSize } from "~/components/Avatar"; import Flex from "~/components/Flex"; @@ -20,6 +21,7 @@ type Props = { function Insights({ document }: Props) { const { t } = useTranslation(); const wrapperRef = useRef(null); + const sourceUrl = sanitizeUrl(document?.sourceMetadata?.externalUrl); // Move focus into the modal to account for lazy-loading useLayoutEffect(() => { @@ -60,6 +62,17 @@ function Insights({ document }: Props) { })} )} + {sourceUrl && ( +
  • + + {t("View original")} + +
  • + )} diff --git a/plugins/github/server/api/github.test.ts b/plugins/github/server/api/github.test.ts index 5352211a23..9bbfb4a208 100644 --- a/plugins/github/server/api/github.test.ts +++ b/plugins/github/server/api/github.test.ts @@ -1,3 +1,4 @@ +import crypto from "node:crypto"; import { IntegrationService, IntegrationType } from "@shared/types"; import { Integration } from "@server/models"; import { @@ -8,8 +9,9 @@ import { buildUser, buildViewer, } from "@server/test/factories"; -import { getTestServer } from "@server/test/support"; +import { getTestServer, mockTaskSchedule } from "@server/test/support"; import { GitHub, InstallationAccess } from "../github"; +import env from "../env"; import { SetupAction } from "./schema"; const server = getTestServer(); @@ -200,3 +202,40 @@ describe("#integrations.list", () => { expect(body.data[0].settings).toEqual({ url: "https://example.com" }); }); }); + +describe("#github.webhooks", () => { + const schedule = mockTaskSchedule(); + + // Spaces and non-ASCII text are not kept when the parsed body is stringified again. + const body = '{ "action": "opened", "title": "Caf\\u00e9 ☕" }'; + + const deliver = (signedBody: string) => + server.post("/api/github.webhooks", { + body, + headers: { + "content-type": "application/json", + "x-hub-signature-256": `sha256=${crypto + .createHmac("sha256", env.GITHUB_WEBHOOK_SECRET!) + .update(signedBody) + .digest("hex")}`, + }, + }); + + it("accepts a signature of the raw body", async () => { + const res = await deliver(body); + + expect(res.status).toEqual(202); + expect(schedule).toHaveBeenCalledWith( + expect.objectContaining({ + payload: { action: "opened", title: "Café ☕" }, + }) + ); + }); + + it("rejects a signature of the parsed body", async () => { + const res = await deliver(JSON.stringify(JSON.parse(body))); + + expect(res.status).toEqual(401); + expect(schedule).not.toHaveBeenCalled(); + }); +}); diff --git a/plugins/granola/client/Icon.tsx b/plugins/granola/client/Icon.tsx new file mode 100644 index 0000000000..1a2a0f2ebb --- /dev/null +++ b/plugins/granola/client/Icon.tsx @@ -0,0 +1,16 @@ +type Props = { + /** The size of the icon, 24px is default to match standard icons */ + size?: number; + /** The color of the icon, defaults to the current text color */ + fill?: string; +}; + +/** The Granola brand mark, sized to match the other integration icons. */ +export default function Icon({ size = 24, fill = "currentColor" }: Props) { + return ( + // The view box centers the mark at 16 of 24px, like the Figma icon. + + + + ); +} diff --git a/plugins/granola/client/Settings.tsx b/plugins/granola/client/Settings.tsx new file mode 100644 index 0000000000..58d3e2e08a --- /dev/null +++ b/plugins/granola/client/Settings.tsx @@ -0,0 +1,305 @@ +import { observer } from "mobx-react"; +import { EditIcon, PlusIcon, TrashIcon } from "outline-icons"; +import { useCallback } from "react"; +import { useTranslation, Trans } from "react-i18next"; +import { toast } from "sonner"; +import { IntegrationService, IntegrationType } from "@shared/types"; +import { errToString } from "@shared/utils/error"; +import { Avatar, AvatarSize } from "~/components/Avatar"; +import Button from "~/components/Button"; +import Flex from "~/components/Flex"; +import Heading from "~/components/Heading"; +import List from "~/components/List"; +import ListItem from "~/components/List/Item"; +import NudeButton from "~/components/NudeButton"; +import Text from "~/components/Text"; +import Time from "~/components/Time"; +import env from "~/env"; +import Tooltip from "~/components/Tooltip"; +import useCurrentUser from "~/hooks/useCurrentUser"; +import useRequest from "~/hooks/useRequest"; +import useStores from "~/hooks/useStores"; +import type Integration from "~/models/Integration"; +import type Policy from "~/models/Policy"; +import type User from "~/models/User"; +import { ConnectedButton } from "~/scenes/Settings/components/ConnectedButton"; +import { IntegrationScene } from "~/scenes/Settings/components/IntegrationScene"; +import type { PartialExcept } from "~/types"; +import { client } from "~/utils/ApiClient"; +import type { GranolaRule } from "../shared/types"; +import GranolaIcon from "./Icon"; +import { GranolaConnectButton } from "./components/GranolaButton"; +import RuleDialog from "./components/RuleDialog"; + +interface Connection { + id: string; + userId: string; + createdAt: string; +} + +interface ListResponse { + data: Connection[]; + users: PartialExcept[]; + policies: Policy[]; +} + +function Granola() { + const { integrations, users } = useStores(); + const user = useCurrentUser(); + const { t } = useTranslation(); + const appName = env.APP_NAME; + + const integration = integrations.find({ + type: IntegrationType.LinkedAccount, + service: IntegrationService.Granola, + userId: user.id, + }) as Integration | undefined; + const settings = integration?.settings?.granola; + + const fetchConnections = useCallback(async () => { + const res = await client.post("/granola.list"); + res.users.forEach((item) => users.add(item)); + // The rules come from the store, and another tab can change the connection. + res.data + .filter((item) => item.userId === user.id) + .forEach((item) => integrations.add(item)); + integrations.addPolicies(res.policies); + return res.data; + }, [users, integrations, user.id]); + const { data: connections, request: refresh } = useRequest( + fetchConnections, + true + ); + + const handleDisconnect = useCallback( + async (id: string) => { + await client.post("/integrations.delete", { id }); + if (integrations.get(id)) { + integrations.remove(id); + } + await refresh(); + }, + [integrations, refresh] + ); + + return ( + }> + Granola + + + Link your Granola account to {{ appName }} and your meeting notes will + show up as documents as soon as they’re ready. Everyone connects their + own account and chooses where their notes go. + + + + {connections && + (connections.length ? ( + <> + + + {t("Connected")} + {!integration && ( + } + onConnect={refresh} + /> + )} + + + + {connections.map((connection) => ( + + ))} + + + ) : ( +

    + } onConnect={refresh} /> +

    + ))} + + {integration && settings ? ( + + ) : null} +
    + ); +} + +interface ConnectionItemProps { + connection: Connection; + onDisconnect: (id: string) => Promise; +} + +const ConnectionItem = observer(function ConnectionItem({ + connection, + onDisconnect, +}: ConnectionItemProps) { + const { t } = useTranslation(); + const { users, policies } = useStores(); + const user = useCurrentUser(); + const owner = users.get(connection.userId); + const canDelete = policies.abilities(connection.id).delete; + const isOwn = connection.userId === user.id; + + return ( + + Connected on{" "} +