Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(server): use original destination addresses when available for intake emails (#655)

Corentin Thomasset08f4a1cd0582
diff --git a/.changeset/evil-papayas-sell.md b/.changeset/evil-papayas-sell.md
new file mode 100644
index 00000000..07584414
--- /dev/null
+++ b/.changeset/evil-papayas-sell.md
@@ -0,0 +1,5 @@
+---
+"@papra/docker": patch
+---
+
+Intake email edge case: use original destination addresses when available for intake emails when forwarded
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
index f7b80407..c2fe10e4 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
-import { buildEmailAddress, getEmailUsername, getIsFromAllowedOrigin, parseEmailAddress } from './intake-emails.models';
+import { buildEmailAddress, getEmailUsername, getIsFromAllowedOrigin, getRecipientAddresses, parseEmailAddress } from './intake-emails.models';
describe('intake-emails models', () => {
describe('getEmailUsername', () => {
@@ -60,4 +60,38 @@ describe('intake-emails models', () => {
expect(parseEmailAddress({ email: '[email protected]' })).to.eql({ username: 'foo', domain: 'example.fr', plusPart: 'bar+baz' });
});
});
+
+ describe('getRecipientAddresses', () => {
+ test(`the recipient addresses are the addresses the email was sent to, they are either in the to or originalTo fields, deduplicated
+ there is a difference between to and originalTo as originalTo contains the actual email address used whereas to can be
+ modified by mailing lists or email forwarding (like proton or cowmail)`, () => {
+ const email = {
+ to: [
+ { address: '[email protected]' },
+ { address: '[email protected]' },
+ ],
+ originalTo: [
+ { address: '[email protected]' },
+ { address: '[email protected]' },
+ ],
+ };
+
+ expect(getRecipientAddresses({ email })).to.eql(['[email protected]', '[email protected]', '[email protected]']);
+ });
+
+ test('email addresses are deduplicated in a case insensitive way', () => {
+ const email = {
+ to: [
+ { address: '[email protected]' },
+ { address: '[email protected]' },
+ ],
+ originalTo: [
+ { address: '[email protected]' },
+ { address: '[email protected]' },
+ ],
+ };
+
+ expect(getRecipientAddresses({ email })).to.eql(['[email protected]', '[email protected]', '[email protected]']);
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
index 9b866229..b365c8bd 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
@@ -1,5 +1,5 @@
import { createError } from '../shared/errors/errors';
-import { isDefined, isNil } from '../shared/utils';
+import { isDefined, isNil, uniq } from '../shared/utils';
export function buildEmailAddress({
username,
@@ -59,3 +59,12 @@ export function getIsFromAllowedOrigin({
.map(allowedOrigin => allowedOrigin.toLowerCase())
.includes(origin.toLowerCase());
}
+
+export function getRecipientAddresses({ email }: { email: {
+ to: { address: string }[];
+ originalTo: { address: string }[];
+}; }): string[] {
+ const { to, originalTo } = email;
+
+ return uniq([...to, ...originalTo].map(({ address }) => address.toLowerCase()));
+}
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
index 66a3b10a..45150dbf 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
@@ -17,6 +17,7 @@ import { validateFormData, validateJsonBody, validateParams } from '../shared/va
import { createSubscriptionsRepository } from '../subscriptions/subscriptions.repository';
import { createUsersRepository } from '../users/users.repository';
import { INTAKE_EMAILS_INGEST_ROUTE } from './intake-emails.constants';
+import { getRecipientAddresses } from './intake-emails.models';
import { createIntakeEmailsRepository } from './intake-emails.repository';
import { allowedOriginsSchema, intakeEmailIdSchema, intakeEmailsIngestionMetaSchema, parseJson } from './intake-emails.schemas';
import { createIntakeEmailsServices } from './intake-emails.services';
@@ -162,7 +163,7 @@ function setupIngestIntakeEmailRoute({ app, db, config, trackingServices, taskSe
async (context) => {
const { email, 'attachments[]': attachments = [] } = context.req.valid('form');
const fromAddress = email.from.address;
- const recipientsAddresses = email.to.map(({ address }) => address);
+ const recipientsAddresses = getRecipientAddresses({ email });
addLogContext({ fromAddress, recipientsAddresses });
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.schemas.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.schemas.ts
index baf98267..b7acd4da 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.schemas.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.schemas.ts
@@ -11,6 +11,7 @@ export const emailInfoSchema = z.object({
export const intakeEmailsIngestionMetaSchema = z.object({
from: emailInfoSchema,
to: z.array(emailInfoSchema),
+ originalTo: z.array(emailInfoSchema).optional().default([]),
// cc: z.array(emailInfoSchema).optional(),
// subject: z.string(),
// text: z.string().optional(),
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
index 8c835ff7..9ae37555 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
@@ -123,7 +123,7 @@ describe('intake-emails usecases', () => {
});
expect(loggerTransport.getLogs({ excludeTimestampMs: true })).to.eql([
- { level: 'info', message: 'Intake email not found', namespace: 'test', data: { } },
+ { level: 'info', message: 'Intake email not found', namespace: 'test', data: { recipientAddress: '[email protected]' } },
]);
expect(await db.select().from(documentsTable)).to.eql([]);
});
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
index c10a5e1e..b891195b 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
@@ -89,7 +89,7 @@ export async function ingestEmailForRecipient({
const { intakeEmail } = await intakeEmailsRepository.getIntakeEmailByEmailAddress({ emailAddress: recipientAddress });
if (!intakeEmail) {
- logger.info('Intake email not found');
+ logger.info({ recipientAddress }, 'Intake email not found');
return;
}
diff --git a/apps/papra-server/src/modules/shared/uniq.bench.ts b/apps/papra-server/src/modules/shared/uniq.bench.ts
new file mode 100644
index 00000000..f271745c
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/uniq.bench.ts
@@ -0,0 +1,58 @@
+import { uniq } from 'lodash-es';
+import { bench, describe } from 'vitest';
+
+describe.skip('uniq', () => {
+ const array = Array.from({ length: 10_000 }, (_, i) => i % 100);
+
+ bench('array.from(new Set(array))', () => {
+ const result = Array.from(new Set(array));
+ void result;
+ });
+
+ bench('[...new Set(array)]', () => {
+ const result = [...new Set(array)];
+ void result;
+ });
+
+ bench('for of with set', () => {
+ const seen = new Set<number>();
+ const result: number[] = [];
+ for (const item of array) {
+ if (!seen.has(item)) {
+ seen.add(item);
+ result.push(item);
+ }
+ }
+ void result;
+ });
+
+ bench('for of with object', () => {
+ const seen: Record<number, boolean> = {};
+ const result: number[] = [];
+ for (const item of array) {
+ if (seen[item] === undefined) {
+ seen[item] = true;
+ result.push(item);
+ }
+ }
+ void result;
+ });
+
+ bench('for loop with object', () => {
+ const seen: Record<number, boolean> = {};
+ const result: number[] = [];
+ for (let i = 0; i < array.length; i++) {
+ const item = array[i];
+ if (item !== undefined && seen[item] === undefined) {
+ seen[item] = true;
+ result.push(item);
+ }
+ }
+ void result;
+ });
+
+ bench('lodash uniq', () => {
+ const result = uniq(array);
+ void result;
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/utils.test.ts b/apps/papra-server/src/modules/shared/utils.test.ts
index b88e7e8d..86e8732d 100644
--- a/apps/papra-server/src/modules/shared/utils.test.ts
+++ b/apps/papra-server/src/modules/shared/utils.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
-import { isDefined, isNil, isNilOrEmptyString, isNonEmptyString, isString, nullifyPositiveInfinity, omitUndefined } from './utils';
+import { isDefined, isNil, isNilOrEmptyString, isNonEmptyString, isString, nullifyPositiveInfinity, omitUndefined, uniq, uniqBy } from './utils';
describe('utils', () => {
describe('omitUndefined', () => {
@@ -107,4 +107,32 @@ describe('utils', () => {
expect(isNilOrEmptyString([])).toBe(false);
});
});
+
+ describe('uniq', () => {
+ test('returns an array with unique values', () => {
+ expect(uniq([1, 2, 2, 3, 3, 3])).toEqual([1, 2, 3]);
+ expect(uniq(['a', 'b', 'a', 'c', 'b'])).toEqual(['a', 'b', 'c']);
+ expect(uniq([])).toEqual([]);
+ });
+ });
+
+ describe('uniqBy', () => {
+ test('returns an array with unique values based on an extracted key', () => {
+ const data = [
+ { id: 1, name: 'Alice' },
+ { id: 2, name: 'Bob' },
+ { id: 1, name: 'Alice' },
+ { id: 3, name: 'Charlie' },
+ { id: 2, name: 'Bob' },
+ ];
+
+ const result = uniqBy(data, item => item.id);
+
+ expect(result).toEqual([
+ { id: 1, name: 'Alice' },
+ { id: 2, name: 'Bob' },
+ { id: 3, name: 'Charlie' },
+ ]);
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/shared/utils.ts b/apps/papra-server/src/modules/shared/utils.ts
index 79c32271..e19b630d 100644
--- a/apps/papra-server/src/modules/shared/utils.ts
+++ b/apps/papra-server/src/modules/shared/utils.ts
@@ -31,3 +31,20 @@ export function isNonEmptyString(value: unknown): value is string {
export function nullifyPositiveInfinity(value: number): number | null {
return value === Number.POSITIVE_INFINITY ? null : value;
}
+
+export function uniqBy<T, K extends string | number | symbol>(array: T[], keyGetter: (item: T) => K): T[] {
+ const seen: Record<string | number | symbol, boolean> = {};
+ const result: T[] = [];
+
+ for (const item of array) {
+ const key = keyGetter(item);
+ if (seen[key] === undefined) {
+ seen[key] = true;
+ result.push(item);
+ }
+ }
+
+ return result;
+}
+
+export const uniq = <T extends string | number | symbol>(array: T[]): T[] => uniqBy(array, item => item);