Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(webhooks): trigger webhooks and save activity log on auto-tagging (#538)

Corentin Thomasset73ab9e8ab58a
diff --git a/.changeset/nine-eggs-count.md b/.changeset/nine-eggs-count.md
new file mode 100644
index 00000000..3fd58c45
--- /dev/null
+++ b/.changeset/nine-eggs-count.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-server": patch
+---
+
+Save document activity log when auto tagging rule is applied
diff --git a/.changeset/tricky-apes-rule.md b/.changeset/tricky-apes-rule.md
new file mode 100644
index 00000000..0c23a862
--- /dev/null
+++ b/.changeset/tricky-apes-rule.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-server": patch
+---
+
+Trigger tag-added webhooks when auto tagging rule is applied
diff --git a/apps/papra-client/.nvmrc b/apps/papra-client/.nvmrc
new file mode 100644
index 00000000..8fdd954d
--- /dev/null
+++ b/apps/papra-client/.nvmrc
@@ -0,0 +1 @@
+22
\ No newline at end of file
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.test.ts b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
index 0061b7f7..62954640 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.test.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
@@ -11,6 +11,8 @@ import { createTaggingRulesRepository } from '../tagging-rules/tagging-rules.rep
import { createTagsRepository } from '../tags/tags.repository';
import { documentsTagsTable } from '../tags/tags.table';
import { createInMemoryTaskServices } from '../tasks/tasks.test-utils';
+import { createWebhookRepository } from '../webhooks/webhook.repository';
+import { createDocumentActivityRepository } from './document-activity/document-activity.repository';
import { documentActivityLogTable } from './document-activity/document-activity.table';
import { createDocumentAlreadyExistsError, createDocumentSizeTooLargeError } from './documents.errors';
import { createDocumentsRepository } from './documents.repository';
@@ -554,6 +556,9 @@ describe('documents usecases', () => {
storageKey: 'organization-1/originals/document-1.txt',
});
+ const webhookRepository = createWebhookRepository({ db });
+ const documentActivityRepository = createDocumentActivityRepository({ db });
+
await extractAndSaveDocumentFileContent({
documentId: 'document-1',
organizationId: 'organization-1',
@@ -561,6 +566,8 @@ describe('documents usecases', () => {
documentsStorageService,
taggingRulesRepository,
tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
});
const documentRecords = await db.select().from(documentsTable);
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.ts b/apps/papra-server/src/modules/documents/documents.usecases.ts
index 00e0e3ea..f1d4050a 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.ts
@@ -140,6 +140,8 @@ export async function createDocument({
newDocumentStorageKey: newFileStorageContext.storageKey,
tagsRepository,
taggingRulesRepository,
+ webhookRepository,
+ documentActivityRepository,
documentsStorageService,
logger,
})
@@ -232,6 +234,8 @@ async function handleExistingDocument({
documentsRepository,
tagsRepository,
taggingRulesRepository,
+ webhookRepository,
+ documentActivityRepository,
documentsStorageService,
newDocumentStorageKey,
logger,
@@ -243,6 +247,8 @@ async function handleExistingDocument({
documentsRepository: DocumentsRepository;
tagsRepository: TagsRepository;
taggingRulesRepository: TaggingRulesRepository;
+ webhookRepository: WebhookRepository;
+ documentActivityRepository: DocumentActivityRepository;
documentsStorageService: DocumentStorageService;
newDocumentStorageKey: string;
logger: Logger;
@@ -260,7 +266,7 @@ async function handleExistingDocument({
documentsRepository.restoreDocument({ documentId: existingDocument.id, organizationId, name: fileName, userId }),
]);
- await applyTaggingRules({ document: restoredDocument, taggingRulesRepository, tagsRepository });
+ await applyTaggingRules({ document: restoredDocument, taggingRulesRepository, tagsRepository, webhookRepository, documentActivityRepository });
return { document: restoredDocument };
}
@@ -484,6 +490,8 @@ export async function extractAndSaveDocumentFileContent({
ocrLanguages,
taggingRulesRepository,
tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
}: {
documentId: string;
ocrLanguages?: string[];
@@ -492,6 +500,8 @@ export async function extractAndSaveDocumentFileContent({
documentsStorageService: DocumentStorageService;
taggingRulesRepository: TaggingRulesRepository;
tagsRepository: TagsRepository;
+ webhookRepository: WebhookRepository;
+ documentActivityRepository: DocumentActivityRepository;
}) {
const { document } = await documentsRepository.getDocumentById({ documentId, organizationId });
@@ -517,7 +527,7 @@ export async function extractAndSaveDocumentFileContent({
throw createDocumentNotFoundError();
}
- await applyTaggingRules({ document: updatedDocument, taggingRulesRepository, tagsRepository });
+ await applyTaggingRules({ document: updatedDocument, taggingRulesRepository, tagsRepository, webhookRepository, documentActivityRepository });
return { document: updatedDocument };
}
diff --git a/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts b/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
index 3f4052dc..b78acbdf 100644
--- a/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
+++ b/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
@@ -3,6 +3,8 @@ import type { TaskServices } from '../../tasks/tasks.services';
import type { DocumentStorageService } from '../storage/documents.storage.services';
import { createTaggingRulesRepository } from '../../tagging-rules/tagging-rules.repository';
import { createTagsRepository } from '../../tags/tags.repository';
+import { createWebhookRepository } from '../../webhooks/webhook.repository';
+import { createDocumentActivityRepository } from '../document-activity/document-activity.repository';
import { createDocumentsRepository } from '../documents.repository';
import { extractAndSaveDocumentFileContent } from '../documents.usecases';
@@ -15,6 +17,8 @@ export async function registerExtractDocumentFileContentTask({ taskServices, db,
const documentsRepository = createDocumentsRepository({ db });
const taggingRulesRepository = createTaggingRulesRepository({ db });
const tagsRepository = createTagsRepository({ db });
+ const webhookRepository = createWebhookRepository({ db });
+ const documentActivityRepository = createDocumentActivityRepository({ db });
// TODO: remove type cast
const { documentId, organizationId, ocrLanguages } = data as { documentId: string; organizationId: string; ocrLanguages: string[] };
@@ -27,6 +31,8 @@ export async function registerExtractDocumentFileContentTask({ taskServices, db,
documentsStorageService,
taggingRulesRepository,
tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
});
},
});
diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts
index 686ba7a9..750b7c20 100644
--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts
+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts
@@ -1,11 +1,13 @@
import { eq } from 'drizzle-orm';
import { describe, expect, test } from 'vitest';
import { createInMemoryDatabase } from '../app/database/database.test-utils';
+import { createDocumentActivityRepository } from '../documents/document-activity/document-activity.repository';
import { documentsTable } from '../documents/documents.table';
import { createTestLogger } from '../shared/logger/logger.test-utils';
import { isNil } from '../shared/utils';
import { createTagsRepository } from '../tags/tags.repository';
import { documentsTagsTable } from '../tags/tags.table';
+import { createWebhookRepository } from '../webhooks/webhook.repository';
import { createTaggingRulesRepository } from './tagging-rules.repository';
import { applyTaggingRules } from './tagging-rules.usecases';
@@ -33,8 +35,10 @@ describe('tagging-rules usecases', () => {
const taggingRulesRepository = createTaggingRulesRepository({ db });
const tagsRepository = createTagsRepository({ db });
+ const webhookRepository = createWebhookRepository({ db });
+ const documentActivityRepository = createDocumentActivityRepository({ db });
- await applyTaggingRules({ document, taggingRulesRepository, tagsRepository, logger });
+ await applyTaggingRules({ document, taggingRulesRepository, tagsRepository, webhookRepository, documentActivityRepository, logger });
const documentTags = await db.select().from(documentsTagsTable);
@@ -73,8 +77,10 @@ describe('tagging-rules usecases', () => {
const taggingRulesRepository = createTaggingRulesRepository({ db });
const tagsRepository = createTagsRepository({ db });
+ const webhookRepository = createWebhookRepository({ db });
+ const documentActivityRepository = createDocumentActivityRepository({ db });
- await applyTaggingRules({ document, taggingRulesRepository, tagsRepository });
+ await applyTaggingRules({ document, taggingRulesRepository, tagsRepository, webhookRepository, documentActivityRepository });
const documentTags = await db.select().from(documentsTagsTable);
@@ -96,8 +102,10 @@ describe('tagging-rules usecases', () => {
const taggingRulesRepository = createTaggingRulesRepository({ db });
const tagsRepository = createTagsRepository({ db });
+ const webhookRepository = createWebhookRepository({ db });
+ const documentActivityRepository = createDocumentActivityRepository({ db });
- await applyTaggingRules({ document, taggingRulesRepository, tagsRepository });
+ await applyTaggingRules({ document, taggingRulesRepository, tagsRepository, webhookRepository, documentActivityRepository });
const documentTags = await db.select().from(documentsTagsTable);
diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.ts
index d5a5bc32..3ccb6793 100644
--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.ts
+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.ts
@@ -1,12 +1,15 @@
+import type { DocumentActivityRepository } from '../documents/document-activity/document-activity.repository';
import type { Document } from '../documents/documents.types';
import type { Logger } from '../shared/logger/logger';
import type { TagsRepository } from '../tags/tags.repository';
+import type { WebhookRepository } from '../webhooks/webhook.repository';
import type { TaggingRuleOperatorValidatorRegistry } from './conditions/tagging-rule-conditions.registry';
import type { TaggingRulesRepository } from './tagging-rules.repository';
import type { TaggingRuleField, TaggingRuleOperator } from './tagging-rules.types';
import { safely, safelySync } from '@corentinth/chisels';
import { uniq } from 'lodash-es';
import { createLogger } from '../shared/logger/logger';
+import { addTagToDocument } from '../tags/tags.usecases';
import { createTaggingRuleOperatorValidatorRegistry } from './conditions/tagging-rule-conditions.registry';
import { getDocumentFieldValue } from './tagging-rules.models';
@@ -55,6 +58,8 @@ export async function applyTaggingRules({
taggingRulesRepository,
tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
taggingRuleOperatorValidatorRegistry = createTaggingRuleOperatorValidatorRegistry(),
logger = createLogger({ namespace: 'tagging-rules' }),
}: {
@@ -63,6 +68,8 @@ export async function applyTaggingRules({
taggingRulesRepository: TaggingRulesRepository;
taggingRuleOperatorValidatorRegistry?: TaggingRuleOperatorValidatorRegistry;
tagsRepository: TagsRepository;
+ webhookRepository: WebhookRepository;
+ documentActivityRepository: DocumentActivityRepository;
logger?: Logger;
}) {
const { taggingRules } = await taggingRulesRepository.getOrganizationEnabledTaggingRules({ organizationId: document.organizationId });
@@ -84,16 +91,26 @@ export async function applyTaggingRules({
const tagIdsToApply: string[] = uniq(taggingRulesToApplyActions.flatMap(taggingRule => taggingRule.actions.map(action => action.tagId)));
- const appliedTagIds = await Promise.all(tagIdsToApply.map(async (tagId) => {
- const [, error] = await safely(async () => tagsRepository.addTagToDocument({ tagId, documentId: document.id }));
+ const tagsToApply = await tagsRepository.getTagsByIds({ tagIds: tagIdsToApply, organizationId: document.organizationId });
+
+ const appliedTagIds = await Promise.all(tagsToApply.tags.map(async (tag) => {
+ const [, error] = await safely(async () => addTagToDocument({
+ tagId: tag.id,
+ documentId: document.id,
+ organizationId: document.organizationId,
+ tag,
+ tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
+ }));
if (error) {
- logger.error({ error, tagId, documentId: document.id }, 'Failed to add tag to document');
+ logger.error({ error, tagId: tag.id, documentId: document.id }, 'Failed to add tag to document');
return;
}
- return tagId;
+ return tag.id;
}));
logger.info({
diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.ts
index c58ed7be..db7d61e7 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.ts
@@ -1,7 +1,7 @@
import type { Database } from '../app/database/database.types';
import type { DbInsertableTag } from './tags.types';
import { injectArguments, safely } from '@corentinth/chisels';
-import { and, eq, getTableColumns, sql } from 'drizzle-orm';
+import { and, eq, getTableColumns, inArray, sql } from 'drizzle-orm';
import { get } from 'lodash-es';
import { documentsTable } from '../documents/documents.table';
import { isUniqueConstraintError } from '../shared/db/constraints.models';
@@ -16,6 +16,7 @@ export function createTagsRepository({ db }: { db: Database }) {
{
getOrganizationTags,
getTagById,
+ getTagsByIds,
createTag,
deleteTag,
updateTag,
@@ -57,6 +58,24 @@ async function getTagById({ tagId, organizationId, db }: { tagId: string; organi
return { tag };
}
+async function getTagsByIds({ tagIds, organizationId, db }: { tagIds: string[]; organizationId: string; db: Database }) {
+ if (tagIds.length === 0) {
+ return { tags: [] };
+ }
+
+ const tags = await db
+ .select()
+ .from(tagsTable)
+ .where(
+ and(
+ inArray(tagsTable.id, tagIds),
+ eq(tagsTable.organizationId, organizationId),
+ ),
+ );
+
+ return { tags };
+}
+
async function createTag({ tag, db }: { tag: DbInsertableTag; db: Database }) {
const [result, error] = await safely(db.insert(tagsTable).values(tag).returning());
diff --git a/apps/papra-server/src/modules/tags/tags.routes.ts b/apps/papra-server/src/modules/tags/tags.routes.ts
index eaddd446..af4a2281 100644
--- a/apps/papra-server/src/modules/tags/tags.routes.ts
+++ b/apps/papra-server/src/modules/tags/tags.routes.ts
@@ -17,6 +17,7 @@ import { deferTriggerWebhooks } from '../webhooks/webhook.usecases';
import { createTagNotFoundError } from './tags.errors';
import { createTagsRepository } from './tags.repository';
import { tagColorSchema, tagIdSchema } from './tags.schemas';
+import { addTagToDocument } from './tags.usecases';
export function registerTagsRoutes(context: RouteDefinitionContext) {
setupCreateNewTagRoute(context);
@@ -184,21 +185,15 @@ function setupAddTagToDocumentRoute({ app, db }: RouteDefinitionContext) {
throw createTagNotFoundError();
}
- await tagsRepository.addTagToDocument({ tagId, documentId });
-
- deferTriggerWebhooks({
- webhookRepository,
- organizationId,
- event: 'document:tag:added',
- payload: { documentId, organizationId, tagId, tagName: tag.name },
- });
-
- deferRegisterDocumentActivityLog({
+ await addTagToDocument({
+ tagId,
documentId,
- event: 'tagged',
+ organizationId,
userId,
+ tag,
+ tagsRepository,
+ webhookRepository,
documentActivityRepository,
- tagId,
});
return context.body(null, 204);
diff --git a/apps/papra-server/src/modules/tags/tags.usecases.ts b/apps/papra-server/src/modules/tags/tags.usecases.ts
index e69de29b..74b8c176 100644
--- a/apps/papra-server/src/modules/tags/tags.usecases.ts
+++ b/apps/papra-server/src/modules/tags/tags.usecases.ts
@@ -0,0 +1,45 @@
+import type { DocumentActivityRepository } from '../documents/document-activity/document-activity.repository';
+import type { WebhookRepository } from '../webhooks/webhook.repository';
+import type { TagsRepository } from './tags.repository';
+import type { Tag } from './tags.types';
+import { deferRegisterDocumentActivityLog } from '../documents/document-activity/document-activity.usecases';
+import { deferTriggerWebhooks } from '../webhooks/webhook.usecases';
+
+export async function addTagToDocument({
+ tagId,
+ documentId,
+ organizationId,
+ userId,
+ tag,
+
+ tagsRepository,
+ webhookRepository,
+ documentActivityRepository,
+}: {
+ tagId: string;
+ documentId: string;
+ organizationId: string;
+ userId?: string;
+ tag: Tag;
+
+ tagsRepository: TagsRepository;
+ webhookRepository: WebhookRepository;
+ documentActivityRepository: DocumentActivityRepository;
+}) {
+ await tagsRepository.addTagToDocument({ tagId, documentId });
+
+ deferTriggerWebhooks({
+ webhookRepository,
+ organizationId,
+ event: 'document:tag:added',
+ payload: { documentId, organizationId, tagId, tagName: tag.name },
+ });
+
+ deferRegisterDocumentActivityLog({
+ documentId,
+ event: 'tagged',
+ userId,
+ documentActivityRepository,
+ tagId,
+ });
+}