mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* wip * fix: Address review feedback on Granola integration - Limit the size of Granola API responses - Load the team when authorizing notes into personal collections - Add the user's connection from granola.list to the integrations store * feat: Connect Granola from a dialog * fix: Accept Granola webhook test events * Copy pass * Copy pass
113 lines
3.1 KiB
TypeScript
113 lines
3.1 KiB
TypeScript
import httpErrors from "http-errors";
|
|
import type { IntegrationType } from "@shared/types";
|
|
import documentCreator, {
|
|
authorizeDocumentCreate,
|
|
} from "@server/commands/documentCreator";
|
|
import { createContext } from "@server/context";
|
|
import Logger from "@server/logging/Logger";
|
|
import { Document, Integration, User } from "@server/models";
|
|
import { BaseTask } from "@server/queues/tasks/base/BaseTask";
|
|
import { sequelize } from "@server/storage/database";
|
|
import { Granola, GranolaApiError } from "../granola";
|
|
|
|
interface Props {
|
|
/** The Granola integration that received the note. */
|
|
integrationId: string;
|
|
/** The ID of the Granola note. */
|
|
noteId: string;
|
|
}
|
|
|
|
/**
|
|
* Creates a document from a Granola note in the location that the
|
|
* integration's rules choose.
|
|
*/
|
|
export default class GranolaNoteTask extends BaseTask<Props> {
|
|
public async perform({ integrationId, noteId }: Props) {
|
|
const integration = (await Integration.scope("withAuthentication").findByPk(
|
|
integrationId
|
|
)) as Integration<IntegrationType.LinkedAccount> | null;
|
|
const rules = integration?.settings?.granola?.rules;
|
|
if (!integration || !rules?.length) {
|
|
return;
|
|
}
|
|
|
|
// Personal collections need the team to authorize.
|
|
const user = await User.scope("withTeam").findByPk(integration.userId);
|
|
if (!user || user.isSuspended) {
|
|
return;
|
|
}
|
|
|
|
// Granola retries deliveries, so a note can arrive more than one time.
|
|
const existing = await Document.unscoped().findOne({
|
|
attributes: ["id"],
|
|
where: {
|
|
teamId: user.teamId,
|
|
createdById: user.id,
|
|
sourceMetadata: { externalId: noteId },
|
|
},
|
|
paranoid: false,
|
|
});
|
|
if (existing) {
|
|
return;
|
|
}
|
|
|
|
let note;
|
|
try {
|
|
note = await Granola.getNote(integration.authentication.token, noteId);
|
|
} catch (err) {
|
|
if (err instanceof GranolaApiError && err.status === 404) {
|
|
return;
|
|
}
|
|
throw err;
|
|
}
|
|
|
|
const rule = Granola.findRule(
|
|
rules,
|
|
(note.folder_membership ?? []).map((folder) => folder.id)
|
|
);
|
|
if (!rule) {
|
|
return;
|
|
}
|
|
|
|
const ctx = createContext({ user });
|
|
let location;
|
|
try {
|
|
location = await authorizeDocumentCreate(ctx, {
|
|
collectionId: rule.collectionId,
|
|
parentDocumentId: rule.documentId,
|
|
});
|
|
} catch (err) {
|
|
if (httpErrors.isHttpError(err) && err.status === 403) {
|
|
Logger.info("task", "Granola note destination is not available", {
|
|
integrationId,
|
|
noteId,
|
|
});
|
|
return;
|
|
}
|
|
throw err;
|
|
}
|
|
|
|
const { title, text, createdAt, externalUrl } = Granola.toDocument(
|
|
note,
|
|
user
|
|
);
|
|
|
|
await sequelize.transaction(async (transaction) => {
|
|
ctx.transaction = transaction;
|
|
await documentCreator(ctx, {
|
|
title,
|
|
text,
|
|
createdAt,
|
|
collectionId: location.collection?.id,
|
|
parentDocumentId: location.parentDocument?.id,
|
|
publish: true,
|
|
sourceMetadata: { externalId: note.id, externalUrl },
|
|
});
|
|
});
|
|
}
|
|
|
|
protected jobId({ integrationId, noteId }: Props) {
|
|
return `GranolaNoteTask-${integrationId}-${noteId}`;
|
|
}
|
|
}
|