The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
LinkedIn app: import image, video and audio attachments (#29357)
martmull4b24128dd43c
diff --git a/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-deadline-margin-milliseconds.constant.ts b/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-deadline-margin-milliseconds.constant.tsnew file mode 100644index 00000000000..ae1404a51cd--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-deadline-margin-milliseconds.constant.ts@@ -0,0 +1 @@+export const LINKEDIN_ATTACHMENT_DEADLINE_MARGIN_MILLISECONDS = 10 * 1000;diff --git a/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-max-size-bytes.constant.ts b/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-max-size-bytes.constant.tsnew file mode 100644index 00000000000..2e5b446e1ee--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/constants/linkedin-attachment-max-size-bytes.constant.ts@@ -0,0 +1 @@+export const LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES = 25 * 1024 * 1024;diff --git a/packages/twenty-apps/public/linkedin/src/constants/linkedin-handle-webhook-event-timeout-seconds.constant.ts b/packages/twenty-apps/public/linkedin/src/constants/linkedin-handle-webhook-event-timeout-seconds.constant.tsnew file mode 100644index 00000000000..9cf4b2650a1--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/constants/linkedin-handle-webhook-event-timeout-seconds.constant.ts@@ -0,0 +1 @@+export const LINKEDIN_HANDLE_WEBHOOK_EVENT_TIMEOUT_SECONDS = 30;diff --git a/packages/twenty-apps/public/linkedin/src/constants/linkedin-imported-attachment-types.constant.ts b/packages/twenty-apps/public/linkedin/src/constants/linkedin-imported-attachment-types.constant.tsnew file mode 100644index 00000000000..4430709e1f3--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/constants/linkedin-imported-attachment-types.constant.ts@@ -0,0 +1,4 @@+import { type UnipileMessageAttachment } from 'src/unipile/types/unipile-message-attachment.type';++export const LINKEDIN_IMPORTED_ATTACHMENT_TYPES: UnipileMessageAttachment['type'][] =+ ['img', 'video', 'audio'];diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-handle-webhook-event.test.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-handle-webhook-event.test.tsindex 2e72cf29dc1..e269cb0ec45 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-handle-webhook-event.test.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-handle-webhook-event.test.ts@@ -12,6 +12,7 @@ const mocks = vi.hoisted(() => ({kvSet: vi.fn(),getUnipileUser: vi.fn(),getUnipileChat: vi.fn(),+ downloadUnipileMessageAttachment: vi.fn(),query: vi.fn(),mutation: vi.fn(),}));@@ -43,6 +44,10 @@ vi.mock('src/unipile/utils/get-unipile-chat.util', () => ({getUnipileChat: mocks.getUnipileChat,}));+vi.mock('src/unipile/utils/download-unipile-message-attachment.util', () => ({+ downloadUnipileMessageAttachment: mocks.downloadUnipileMessageAttachment,+}));+const { linkedinHandleWebhookEventHandler } =await import('src/logic-functions/linkedin-handle-webhook-event');@@ -130,6 +135,40 @@ describe('linkedinHandleWebhookEventHandler', () => {});});+ it('imports the image of a received message', async () => {+ mocks.downloadUnipileMessageAttachment.mockResolvedValue(+ Buffer.from('png'),+ );+ const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => {});++ await expect(+ linkedinHandleWebhookEventHandler(+ buildMessageEvent({+ sender_id: DAPHNE.id,+ sender: DAPHNE,+ is_sender: false,+ text: '',+ attachments: [+ { id: 'attachment-1', type: 'img', mimetype: 'image/png' },+ ],+ }),+ ),+ ).resolves.toEqual({ handled: true });++ expect(+ mocks.downloadUnipileMessageAttachment,+ ).toHaveBeenCalledExactlyOnceWith({+ accountId: 'acc_1',+ chatId: 'chat-1',+ messageId: 'message-1',+ attachmentId: 'attachment-1',+ maxSizeBytes: 25 * 1024 * 1024,+ deadlineAtMs: expect.any(Number),+ });++ consoleLog.mockRestore();+ });+it('imports a group message from a stranger when another member is a contact', async () => {const ada = {id: 'ACoADA',diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-import-history.test.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-import-history.test.tsindex 85123c9dbdb..623da976870 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-import-history.test.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/__tests__/linkedin-import-history.test.ts@@ -92,12 +92,14 @@ const IMPORTED = {ingestedMessageCount: 12,matchedPersonCount: 1,createdPersonCount: 1,+ importedAttachmentCount: 2,};const SKIPPED = {ingestedMessageCount: 0,matchedPersonCount: 0,createdPersonCount: 0,+ importedAttachmentCount: 0,skippedReason: 'NO_CONTACT',};@@ -140,6 +142,9 @@ describe('linkedinImportHistoryHandler', () => {accountId: 'acc_1',chatId: 'chat-2',});+ expect(mocks.ingestLinkedinMessages).toHaveBeenCalledWith(+ expect.objectContaining({ unipileAccountId: 'acc_1' }),+ );expect(mocks.kvStore.get(PROGRESS_KEY)).toEqual({completedChunkCount: 3,completedConversationCount: 42,@@ -148,7 +153,7 @@ describe('linkedinImportHistoryHandler', () => {});expect(console.log).toHaveBeenCalledWith(expect.stringMatching(- /chunk 2: 2 chats .*1 conversations imported, 12 messages, 1 people matched, 1 people created, skipped 1 without contact/,+ /chunk 2: 2 chats .*1 conversations imported, 12 messages, 1 people matched, 1 people created, 2 attachments, skipped 1 without contact/,),);});@@ -386,6 +391,7 @@ describe('linkedinImportHistoryHandler', () => {ingestedMessageCount: 3,matchedPersonCount: 1,createdPersonCount: 0,+ importedAttachmentCount: 0,});await linkedinImportHistoryHandler({ ...PAYLOAD, resumeIndex: 1 });diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-handle-webhook-event.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-handle-webhook-event.tsindex ad6a58a7086..7311775118b 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-handle-webhook-event.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-handle-webhook-event.ts@@ -5,6 +5,8 @@ import {} from 'twenty-sdk/logic-function';import { isDefined } from 'twenty-sdk/utils';+import { LINKEDIN_ATTACHMENT_DEADLINE_MARGIN_MILLISECONDS } from 'src/constants/linkedin-attachment-deadline-margin-milliseconds.constant';+import { LINKEDIN_HANDLE_WEBHOOK_EVENT_TIMEOUT_SECONDS } from 'src/constants/linkedin-handle-webhook-event-timeout-seconds.constant';import { LINKEDIN_HANDLE_WEBHOOK_EVENT_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';import { findLinkedinConnectionByUnipileAccountId } from 'src/logic-functions/utils/find-linkedin-connection-by-unipile-account-id.util';import { getLinkedinChatCounterparts } from 'src/logic-functions/utils/get-linkedin-chat-counterparts.util';@@ -21,10 +23,12 @@ const ingestUnipileMessage = async ({connectedAccountId,unipileAccountId,message,+ attachmentDeadlineAtMs,}: {connectedAccountId: string;unipileAccountId: string;message: UnipileMessage;+ attachmentDeadlineAtMs: number;}): Promise<LinkedinWebhookEventResult> => {const [channel] = await listMessageChannels({ connectedAccountId });@@ -48,6 +52,8 @@ const ingestUnipileMessage = async ({: [...chatCounterparts, message.sender];const { ingestedMessageCount } = await ingestLinkedinMessages({+ unipileAccountId,+ attachmentDeadlineAtMs,channel,settings,chat,@@ -72,6 +78,10 @@ const ingestUnipileMessage = async ({export const linkedinHandleWebhookEventHandler = async (event: UnipileWebhookEvent,): Promise<LinkedinWebhookEventResult> => {+ const attachmentDeadlineAtMs =+ Date.now() ++ LINKEDIN_HANDLE_WEBHOOK_EVENT_TIMEOUT_SECONDS * 1000 -+ LINKEDIN_ATTACHMENT_DEADLINE_MARGIN_MILLISECONDS;const connection = await findLinkedinConnectionByUnipileAccountId(event.account_id,);@@ -89,6 +99,7 @@ export const linkedinHandleWebhookEventHandler = async (connectedAccountId: connection.id,unipileAccountId: event.account_id,message: event.payload,+ attachmentDeadlineAtMs,});case 'account.status.disconnected':case 'account.status.errored':@@ -108,6 +119,6 @@ export default defineLogicFunction({name: 'linkedin-handle-webhook-event',description:'Imports new LinkedIn messages into the connection message channel and flags connections LinkedIn signed out.',- timeoutSeconds: 30,+ timeoutSeconds: LINKEDIN_HANDLE_WEBHOOK_EVENT_TIMEOUT_SECONDS,handler: linkedinHandleWebhookEventHandler,});diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-import-history.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-import-history.tsindex 2832603e8c5..f75b8b9e705 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-import-history.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/linkedin-import-history.ts@@ -8,6 +8,7 @@ import {} from 'twenty-sdk/logic-function';import { isDefined } from 'twenty-sdk/utils';+import { LINKEDIN_ATTACHMENT_DEADLINE_MARGIN_MILLISECONDS } from 'src/constants/linkedin-attachment-deadline-margin-milliseconds.constant';import { LINKEDIN_IMPORT_HISTORY_TIMEOUT_SECONDS } from 'src/constants/linkedin-import-history-timeout-seconds.constant';import { LINKEDIN_IMPORT_HISTORY_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';import { type LinkedinHistoryImportProgress } from 'src/logic-functions/types/linkedin-history-import-progress.type';@@ -43,6 +44,7 @@ type LinkedinImportHistoryTotals = {messageCount: number;matchedPersonCount: number;createdPersonCount: number;+ attachmentCount: number;skippedChatCounts: Record<NonNullable<LinkedinIngestResult['skippedReason']>,number@@ -83,6 +85,8 @@ export const linkedinImportHistoryHandler = async (payload: LinkedinImportHistoryJobPayload,context?: Pick<LogicFunctionExecutionContext, 'retryCount' | 'maxRetries'>,): Promise<LinkedinImportHistoryResult> => {+ const invokedAt = Date.now();+if ((await getActiveLinkedinHistoryImportState(payload)) === null) {return { status: 'CANCELLED' };}@@ -103,6 +107,10 @@ export const linkedinImportHistoryHandler = async (}const startedAt = Date.now();+ const attachmentDeadlineAtMs =+ invokedAt ++ LINKEDIN_IMPORT_HISTORY_TIMEOUT_SECONDS * 1000 -+ LINKEDIN_ATTACHMENT_DEADLINE_MARGIN_MILLISECONDS;const jobId = getLinkedinImportHistoryJobId(payload);let importedConversationCount = startProgress.currentChunkImportedCount ?? 0;let processedChatCount = startProgress.currentChunkProcessedCount;@@ -112,6 +120,7 @@ export const linkedinImportHistoryHandler = async (messageCount: 0,matchedPersonCount: 0,createdPersonCount: 0,+ attachmentCount: 0,skippedChatCounts: { GROUP_CHATS_OFF: 0, NO_MESSAGES: 0, NO_CONTACT: 0 },};@@ -214,6 +223,8 @@ export const linkedinImportHistoryHandler = async (cursor: chatCursor,});const ingestResult = await ingestLinkedinMessages({+ unipileAccountId,+ attachmentDeadlineAtMs,channel,settings,chat,@@ -224,6 +235,7 @@ export const linkedinImportHistoryHandler = async (totals.messageCount += ingestResult.ingestedMessageCount;totals.matchedPersonCount += ingestResult.matchedPersonCount;totals.createdPersonCount += ingestResult.createdPersonCount;+ totals.attachmentCount += ingestResult.importedAttachmentCount;isChatImported ||= ingestResult.ingestedMessageCount > 0;if (isDefined(ingestResult.skippedReason) && !isDefined(chatCursor)) {@@ -315,7 +327,7 @@ export const linkedinImportHistoryHandler = async (`[linkedin] history run ${runId} chunk ${chunkIndex}: ` +`${chats.length} chats (last messages ${chats[0]?.last_message_timestamp ?? '-'} to ${chats[chats.length - 1]?.last_message_timestamp ?? '-'}), ` +`${importedConversationCount} conversations imported, ${totals.messageCount} messages, ` +- `${totals.matchedPersonCount} people matched, ${totals.createdPersonCount} people created, ` ++ `${totals.matchedPersonCount} people matched, ${totals.createdPersonCount} people created, ${totals.attachmentCount} attachments, ` +`skipped ${totals.skippedChatCounts.NO_CONTACT} without contact, ${totals.skippedChatCounts.GROUP_CHATS_OFF} group chats, ${totals.skippedChatCounts.NO_MESSAGES} without messages, ` +`in ${Math.round((Date.now() - startedAt) / 1000)}s`,);diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-ingest-result.type.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-ingest-result.type.tsindex e47b231b482..291361e1e0a 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-ingest-result.type.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-ingest-result.type.ts@@ -2,5 +2,6 @@ export type LinkedinIngestResult = {ingestedMessageCount: number;matchedPersonCount: number;createdPersonCount: number;+ importedAttachmentCount: number;skippedReason?: 'GROUP_CHATS_OFF' | 'NO_MESSAGES' | 'NO_CONTACT';};diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-message-attachment-file.type.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-message-attachment-file.type.tsnew file mode 100644index 00000000000..ff93f2a494d--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/types/linkedin-message-attachment-file.type.ts@@ -0,0 +1,9 @@+import { type UnipileMessageAttachment } from 'src/unipile/types/unipile-message-attachment.type';++export type LinkedinMessageAttachmentFile = {+ messageExternalId: string;+ attachmentId: string;+ type: UnipileMessageAttachment['type'];+ mimeType: string;+ content: Buffer;+};diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/create-linkedin-person.util.test.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/create-linkedin-person.util.test.tsindex d619147c474..81dd48817f5 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/create-linkedin-person.util.test.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/create-linkedin-person.util.test.ts@@ -64,11 +64,28 @@ describe('createLinkedinPerson', () => {await expect(createLinkedinPerson(DAPHNE)).resolves.toBe('existing-id');});- it('rethrows when nobody was created for the profile', async () => {+ it('creates the person with a new id when its id belongs to a deleted person', async () => {+ mocks.mutation+ .mockRejectedValueOnce(+ new Error(+ 'A duplicate entry was detected: unique constraint person.person_pkey was violated',+ ),+ )+ .mockResolvedValueOnce({ createPerson: { id: 'new-id' } });+ mocks.query.mockResolvedValue({ people: { edges: [] } });++ await expect(createLinkedinPerson(DAPHNE)).resolves.toBe('new-id');+ expect(getCreatedPersonId()).toBeDefined();+ expect(+ mocks.mutation.mock.calls[1][0].createPerson.__args.data,+ ).not.toHaveProperty('id');+ });++ it('rethrows when the person cannot be created at all', async () => {mocks.mutation.mockRejectedValue(new Error('Forbidden'));mocks.query.mockResolvedValue({ people: { edges: [] } });await expect(createLinkedinPerson(DAPHNE)).rejects.toThrow('Forbidden');- expect(getCreatedPersonId()).toBeDefined();+ expect(mocks.mutation).toHaveBeenCalledOnce();});});diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/import-linkedin-message-attachments.util.test.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/import-linkedin-message-attachments.util.test.tsnew file mode 100644index 00000000000..aba1f95e954--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/import-linkedin-message-attachments.util.test.ts@@ -0,0 +1,203 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { type LinkedinMessageAttachmentFile } from 'src/logic-functions/types/linkedin-message-attachment-file.type';+import { type UnipileMessage } from 'src/unipile/types/unipile-message.type';+import { type UnipileMessageAttachment } from 'src/unipile/types/unipile-message-attachment.type';+import { UnipileRateLimitError } from 'src/unipile/utils/request-unipile.util';++const mocks = vi.hoisted(() => ({+ downloadUnipileMessageAttachment: vi.fn(),+}));++vi.mock('src/unipile/utils/download-unipile-message-attachment.util', () => ({+ downloadUnipileMessageAttachment: mocks.downloadUnipileMessageAttachment,+}));++const { importLinkedinMessageAttachments } =+ await import('src/logic-functions/utils/import-linkedin-message-attachments.util');++const MAX_SIZE_BYTES = 25 * 1024 * 1024;++const buildMessage = (+ attachments: UnipileMessageAttachment[],+): UnipileMessage => ({+ id: 'message-1',+ chat_id: 'chat-1',+ sender_id: 'ACoPRIYA',+ timestamp: '2026-10-29T07:44:08.382Z',+ is_sender: false,+ is_deleted: false,+ is_event: false,+ provider: 'linkedin',+ attachments,+});++const importAttachments = async ({+ attachments,+ deadlineAtMs = Date.now() + 60_000,+}: {+ attachments: UnipileMessageAttachment[];+ deadlineAtMs?: number;+}) => {+ const files: LinkedinMessageAttachmentFile[] = [];+ const importedCount = await importLinkedinMessageAttachments({+ unipileAccountId: 'acc_1',+ messages: [buildMessage(attachments)],+ deadlineAtMs,+ onAttachmentFile: async (file) => {+ files.push(file);+ },+ });++ return { files, importedCount };+};++describe('importLinkedinMessageAttachments', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.spyOn(console, 'warn').mockImplementation(() => {});+ mocks.downloadUnipileMessageAttachment.mockResolvedValue(+ Buffer.from('downloaded'),+ );+ });++ it('downloads images, videos and audio', async () => {+ const deadlineAtMs = Date.now() + 60_000;+ const { files, importedCount } = await importAttachments({+ attachments: [+ { id: 'img-1', type: 'img', mimetype: 'image/jpeg' },+ { id: 'video-1', type: 'video', mimetype: 'video/mp4' },+ { id: 'audio-1', type: 'audio', mimetype: 'audio/mpeg' },+ ],+ deadlineAtMs,+ });++ expect(importedCount).toBe(3);+ expect(files.map(({ attachmentId }) => attachmentId)).toEqual([+ 'img-1',+ 'video-1',+ 'audio-1',+ ]);+ expect(mocks.downloadUnipileMessageAttachment).toHaveBeenCalledWith({+ accountId: 'acc_1',+ chatId: 'chat-1',+ messageId: 'message-1',+ attachmentId: 'img-1',+ maxSizeBytes: MAX_SIZE_BYTES,+ deadlineAtMs,+ });+ expect(files[0]).toMatchObject({+ messageExternalId: 'message-1',+ mimeType: 'image/jpeg',+ content: Buffer.from('downloaded'),+ });+ });++ it('uses the inline content instead of downloading it', async () => {+ const { files } = await importAttachments({+ attachments: [+ {+ id: 'img-1',+ type: 'img',+ mimetype: 'image/png',+ content: Buffer.from('inline').toString('base64'),+ },+ ],+ });++ expect(files[0]?.content.toString()).toBe('inline');+ expect(mocks.downloadUnipileMessageAttachment).not.toHaveBeenCalled();+ });++ it('skips other, unavailable and oversized attachments', async () => {+ await expect(+ importAttachments({+ attachments: [+ { id: 'file-1', type: 'file', mimetype: 'application/pdf' },+ { id: 'post-1', type: 'shared_content', mimetype: 'text/html' },+ {+ id: 'img-1',+ type: 'img',+ mimetype: 'image/png',+ is_unavailable: true,+ },+ {+ id: 'video-1',+ type: 'video',+ mimetype: 'video/mp4',+ file_size: 500 * 1024 * 1024,+ },+ ],+ }),+ ).resolves.toEqual({ files: [], importedCount: 0 });+ expect(mocks.downloadUnipileMessageAttachment).not.toHaveBeenCalled();+ });++ it('drops inline content over the size limit when no size was listed', async () => {+ await expect(+ importAttachments({+ attachments: [+ {+ id: 'video-1',+ type: 'video',+ mimetype: 'video/mp4',+ content: Buffer.alloc(MAX_SIZE_BYTES + 1).toString('base64'),+ },+ ],+ }),+ ).resolves.toEqual({ files: [], importedCount: 0 });+ });++ it('drops a file that fails to download and keeps the others', async () => {+ mocks.downloadUnipileMessageAttachment.mockRejectedValueOnce(+ new Error('Unipile download message attachment failed (404)'),+ );++ const { files } = await importAttachments({+ attachments: [+ { id: 'img-1', type: 'img', mimetype: 'image/png' },+ { id: 'img-2', type: 'img', mimetype: 'image/png' },+ ],+ });++ expect(files.map(({ attachmentId }) => attachmentId)).toEqual(['img-2']);+ });++ it('skips the remaining files once the deadline is reached', async () => {+ vi.useFakeTimers({ toFake: ['Date'] });+ vi.setSystemTime(Date.parse('2026-10-29T08:00:00.000Z'));+ mocks.downloadUnipileMessageAttachment.mockImplementation(async () => {+ vi.advanceTimersByTime(2_000);++ return Buffer.from('downloaded');+ });++ try {+ const { files, importedCount } = await importAttachments({+ attachments: [+ { id: 'img-1', type: 'img', mimetype: 'image/png' },+ { id: 'img-2', type: 'img', mimetype: 'image/png' },+ ],+ deadlineAtMs: Date.now() + 1_000,+ });++ expect(importedCount).toBe(1);+ expect(files.map(({ attachmentId }) => attachmentId)).toEqual(['img-1']);+ expect(mocks.downloadUnipileMessageAttachment).toHaveBeenCalledOnce();+ } finally {+ vi.useRealTimers();+ }+ });++ it('lets a rate limit through so the import can pause', async () => {+ mocks.downloadUnipileMessageAttachment.mockRejectedValue(+ new UnipileRateLimitError('rate limited', 3600),+ );++ await expect(+ importAttachments({+ attachments: [{ id: 'img-1', type: 'img', mimetype: 'image/png' }],+ }),+ ).rejects.toBeInstanceOf(UnipileRateLimitError);+ });+});diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/ingest-linkedin-messages.util.test.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/ingest-linkedin-messages.util.test.tsindex 87b6c876539..0a94aef046c 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/ingest-linkedin-messages.util.test.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/__tests__/ingest-linkedin-messages.util.test.ts@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({ingestMessages: vi.fn(),matchLinkedinParticipantPersonId: vi.fn(),createLinkedinPerson: vi.fn(),+ importLinkedinMessageAttachments: vi.fn(),}));vi.mock('twenty-sdk/logic-function', () => ({@@ -25,9 +26,18 @@ vi.mock('src/logic-functions/utils/create-linkedin-person.util', () => ({createLinkedinPerson: mocks.createLinkedinPerson,}));+vi.mock(+ 'src/logic-functions/utils/import-linkedin-message-attachments.util',+ () => ({+ importLinkedinMessageAttachments: mocks.importLinkedinMessageAttachments,+ }),+);+const { ingestLinkedinMessages } =await import('src/logic-functions/utils/ingest-linkedin-messages.util');+const DEADLINE_AT_MS = Date.parse('2026-10-29T08:00:00.000Z');+const PRIYA = {id: 'ACoPRIYA',display_name: 'Priya Nair',@@ -74,6 +84,8 @@ const ingest = ({chat?: typeof ONE_TO_ONE_CHAT | typeof GROUP_CHAT;} = {}) =>ingestLinkedinMessages({+ unipileAccountId: 'acc_1',+ attachmentDeadlineAtMs: DEADLINE_AT_MS,channel: { id: 'channel-id', handle: 'https://www.linkedin.com/in/me' },settings,chat,@@ -91,6 +103,7 @@ describe('ingestLinkedinMessages', () => {vi.clearAllMocks();mocks.matchLinkedinParticipantPersonId.mockResolvedValue(undefined);mocks.createLinkedinPerson.mockResolvedValue('created-id');+ mocks.importLinkedinMessageAttachments.mockResolvedValue(0);});it('attaches the matched person to the participant', async () => {@@ -179,5 +192,30 @@ describe('ingestLinkedinMessages', () => {).resolves.toMatchObject({ ingestedMessageCount: 0 });expect(mocks.createLinkedinPerson).not.toHaveBeenCalled();expect(mocks.ingestMessages).not.toHaveBeenCalled();+ expect(mocks.importLinkedinMessageAttachments).not.toHaveBeenCalled();+ });++ it('imports the attachments of the ingested messages', async () => {+ const message = {+ ...buildMessage(true),+ attachments: [+ { id: 'attachment-1', type: 'img' as const, mimetype: 'image/png' },+ ],+ };++ mocks.importLinkedinMessageAttachments.mockResolvedValue(1);++ await expect(ingest({ messages: [message] })).resolves.toMatchObject({+ ingestedMessageCount: 1,+ importedAttachmentCount: 1,+ });+ expect(+ mocks.importLinkedinMessageAttachments,+ ).toHaveBeenCalledExactlyOnceWith({+ unipileAccountId: 'acc_1',+ messages: [message],+ deadlineAtMs: DEADLINE_AT_MS,+ onAttachmentFile: expect.any(Function),+ });});});diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/create-linkedin-person.util.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/create-linkedin-person.util.tsindex a6a634cca14..ffc7e679da9 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/create-linkedin-person.util.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/create-linkedin-person.util.ts@@ -5,6 +5,42 @@ import { findPersonIdsByLinkedinHandle } from 'src/logic-functions/utils/find-peimport { getLinkedinPersonId } from 'src/logic-functions/utils/get-linkedin-person-id.util';import { getLinkedinProfileSlug } from 'src/logic-functions/utils/get-linkedin-profile-slug.util';+const insertLinkedinPerson = async ({+ id,+ firstName,+ lastName,+ handle,+}: {+ id?: string;+ firstName: string;+ lastName: string;+ handle: string;+}): Promise<string> => {+ const { createPerson } = await new CoreApiClient({+ runAs: 'application',+ }).mutation({+ createPerson: {+ __args: {+ data: {+ ...(isDefined(id) ? { id } : {}),+ name: { firstName, lastName },+ linkedinLink: { primaryLinkUrl: handle },+ },+ },+ id: true,+ },+ });++ if (!isDefined(createPerson)) {+ throw new Error(`Could not create the person for ${handle}`);+ }++ return createPerson.id;+};++const isPersonIdTakenError = (error: unknown): boolean =>+ error instanceof Error && error.message.includes('person_pkey');+export const createLinkedinPerson = async ({firstName,lastName,@@ -23,28 +59,18 @@ export const createLinkedinPerson = async ({const id = await getLinkedinPersonId(slug);try {- const { createPerson } = await new CoreApiClient({- runAs: 'application',- }).mutation({- createPerson: {- __args: {- data: {- id,- name: { firstName, lastName },- linkedinLink: { primaryLinkUrl: handle },- },- },- id: true,- },+ const createdPersonId = await insertLinkedinPerson({+ id,+ firstName,+ lastName,+ handle,});- if (isDefined(createPerson)) {- console.log(- `[linkedin] created person ${createPerson.id} for ${handle} (slug ${slug}, requested id ${id})`,- );+ console.log(+ `[linkedin] created person ${createdPersonId} for ${handle} (slug ${slug})`,+ );- return createPerson.id;- }+ return createdPersonId;} catch (error) {const [existingPersonId] = await findPersonIdsByLinkedinHandle(handle);@@ -56,8 +82,20 @@ export const createLinkedinPerson = async ({return existingPersonId;}- throw error;- }+ if (!isPersonIdTakenError(error)) {+ throw error;+ }- throw new Error(`Could not create the person for ${handle}`);+ const createdPersonId = await insertLinkedinPerson({+ firstName,+ lastName,+ handle,+ });++ console.log(+ `[linkedin] created person ${createdPersonId} for ${handle} with a new id, ${id} is taken: ${error instanceof Error ? error.message : String(error)}`,+ );++ return createdPersonId;+ }};diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/import-linkedin-message-attachments.util.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/import-linkedin-message-attachments.util.tsnew file mode 100644index 00000000000..f54b5896c06--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/import-linkedin-message-attachments.util.ts@@ -0,0 +1,87 @@+import { isNonEmptyString } from '@sniptt/guards';+import { isDefined } from 'twenty-sdk/utils';++import { LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES } from 'src/constants/linkedin-attachment-max-size-bytes.constant';+import { LINKEDIN_IMPORTED_ATTACHMENT_TYPES } from 'src/constants/linkedin-imported-attachment-types.constant';+import { type LinkedinMessageAttachmentFile } from 'src/logic-functions/types/linkedin-message-attachment-file.type';+import { type UnipileMessage } from 'src/unipile/types/unipile-message.type';+import { type UnipileMessageAttachment } from 'src/unipile/types/unipile-message-attachment.type';+import { downloadUnipileMessageAttachment } from 'src/unipile/utils/download-unipile-message-attachment.util';+import { UnipileRateLimitError } from 'src/unipile/utils/request-unipile.util';++const isImportableAttachment = (+ attachment: UnipileMessageAttachment,+): boolean =>+ LINKEDIN_IMPORTED_ATTACHMENT_TYPES.includes(attachment.type) &&+ !attachment.is_unavailable &&+ (!isDefined(attachment.file_size) ||+ attachment.file_size <= LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES);++export const importLinkedinMessageAttachments = async ({+ unipileAccountId,+ messages,+ deadlineAtMs,+ onAttachmentFile,+}: {+ unipileAccountId: string;+ messages: UnipileMessage[];+ deadlineAtMs: number;+ onAttachmentFile: (file: LinkedinMessageAttachmentFile) => Promise<void>;+}): Promise<number> => {+ let importedCount = 0;++ for (const message of messages) {+ for (const attachment of (message.attachments ?? []).filter(+ isImportableAttachment,+ )) {+ if (Date.now() >= deadlineAtMs) {+ console.warn(+ `[linkedin] chat ${message.chat_id}: attachment import reached the job deadline, remaining files skipped`,+ );++ return importedCount;+ }++ try {+ const content = isNonEmptyString(attachment.content)+ ? Buffer.from(attachment.content, 'base64')+ : await downloadUnipileMessageAttachment({+ accountId: unipileAccountId,+ chatId: message.chat_id,+ messageId: message.id,+ attachmentId: attachment.id,+ maxSizeBytes: LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES,+ deadlineAtMs,+ });++ if (content.byteLength > LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES) {+ console.warn(+ `[linkedin] attachment ${attachment.id} of message ${message.id} not imported: larger than ${LINKEDIN_ATTACHMENT_MAX_SIZE_BYTES} bytes`,+ );++ continue;+ }++ await onAttachmentFile({+ messageExternalId: message.id,+ attachmentId: attachment.id,+ type: attachment.type,+ mimeType: attachment.mimetype,+ content,+ });++ importedCount += 1;+ } catch (error) {+ if (error instanceof UnipileRateLimitError) {+ throw error;+ }++ console.warn(+ `[linkedin] attachment ${attachment.id} of message ${message.id} not imported: ${error instanceof Error ? error.message : String(error)}`,+ );+ }+ }+ }++ return importedCount;+};diff --git a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/ingest-linkedin-messages.util.ts b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/ingest-linkedin-messages.util.tsindex 1d1097a86c8..012497791ce 100644--- a/packages/twenty-apps/public/linkedin/src/logic-functions/utils/ingest-linkedin-messages.util.ts+++ b/packages/twenty-apps/public/linkedin/src/logic-functions/utils/ingest-linkedin-messages.util.ts@@ -9,6 +9,7 @@ import { INGEST_MESSAGES_MAX_BATCH_SIZE } from 'src/constants/ingest-messages-maimport { buildLinkedinChatSubject } from 'src/logic-functions/utils/build-linkedin-chat-subject.util';import { buildLinkedinProfileHandle } from 'src/logic-functions/utils/build-linkedin-profile-handle.util';import { createLinkedinPerson } from 'src/logic-functions/utils/create-linkedin-person.util';+import { importLinkedinMessageAttachments } from 'src/logic-functions/utils/import-linkedin-message-attachments.util';import { isLinkedinContactCreationAllowed } from 'src/logic-functions/utils/is-linkedin-contact-creation-allowed.util';import { isLinkedinGroupChat } from 'src/logic-functions/utils/is-linkedin-group-chat.util';import { mapUnipileMessageToIngestMessage } from 'src/logic-functions/utils/map-unipile-message-to-ingest-message.util';@@ -96,12 +97,16 @@ const resolvePersonIdByHandle = async ({};export const ingestLinkedinMessages = async ({+ unipileAccountId,+ attachmentDeadlineAtMs,channel,settings,chat,counterparts,messages,}: {+ unipileAccountId: string;+ attachmentDeadlineAtMs: number;channel: Pick<AppMessageChannel, 'id' | 'handle'>;settings: LinkedinSettings;chat: UnipileChat;@@ -114,6 +119,7 @@ export const ingestLinkedinMessages = async ({ingestedMessageCount: 0,matchedPersonCount: 0,createdPersonCount: 0,+ importedAttachmentCount: 0,};if (isGroupChat && !settings.areGroupChatsImported) {@@ -183,9 +189,26 @@ export const ingestLinkedinMessages = async ({});}+ const importedAttachmentCount = await importLinkedinMessageAttachments({+ unipileAccountId,+ messages: ingestableMessages,+ deadlineAtMs: attachmentDeadlineAtMs,+ onAttachmentFile: async ({+ messageExternalId,+ type,+ mimeType,+ content,+ }) => {+ console.log(+ `[linkedin] message ${messageExternalId}: imported ${type} attachment (${mimeType}, ${content.byteLength}B), not stored yet`,+ );+ },+ });+return {ingestedMessageCount: ingestMessagesInput.length,matchedPersonCount,createdPersonCount,+ importedAttachmentCount,};};diff --git a/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message-attachment.type.ts b/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message-attachment.type.tsnew file mode 100644index 00000000000..0784426ee94--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message-attachment.type.ts@@ -0,0 +1,8 @@+export type UnipileMessageAttachment = {+ id: string;+ type: 'img' | 'video' | 'audio' | 'file' | 'shared_content' | 'contact_card';+ mimetype: string;+ file_size?: number;+ is_unavailable?: boolean;+ content?: string;+};diff --git a/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message.type.ts b/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message.type.tsindex 182a1a36298..faf25521b5f 100644--- a/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message.type.ts+++ b/packages/twenty-apps/public/linkedin/src/unipile/types/unipile-message.type.ts@@ -1,3 +1,4 @@+import { type UnipileMessageAttachment } from 'src/unipile/types/unipile-message-attachment.type';import { type UnipileProvider } from 'src/unipile/types/unipile-provider.type';import { type UnipileUser } from 'src/unipile/types/unipile-user.type';@@ -12,4 +13,5 @@ export type UnipileMessage = {is_deleted: boolean;is_event: boolean;provider: UnipileProvider;+ attachments?: UnipileMessageAttachment[];};diff --git a/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/download-unipile-message-attachment.util.test.ts b/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/download-unipile-message-attachment.util.test.tsnew file mode 100644index 00000000000..aaf9cfab397--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/download-unipile-message-attachment.util.test.ts@@ -0,0 +1,80 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({ getAttachment: vi.fn() }));++vi.mock('@unipile/sdk', () => ({+ UnipileMessaging: class {+ getAttachment = mocks.getAttachment;+ },+}));++vi.mock('src/unipile/utils/get-unipile-request-options.util', () => ({+ getUnipileRequestOptions: () => ({ auth: 'key', throwOnError: true }),+}));++const { downloadUnipileMessageAttachment } =+ await import('src/unipile/utils/download-unipile-message-attachment.util');++const streamOf = ({+ chunks,+ onCancel,+}: {+ chunks: string[];+ onCancel?: () => void;+}) =>+ new ReadableStream<Uint8Array>({+ start(controller) {+ for (const chunk of chunks) {+ controller.enqueue(Buffer.from(chunk));+ }+ controller.close();+ },+ cancel: onCancel,+ });++const download = (maxSizeBytes: number) =>+ downloadUnipileMessageAttachment({+ accountId: 'acc_1',+ chatId: 'chat-1',+ messageId: 'message-1',+ attachmentId: 'attachment-1',+ maxSizeBytes,+ deadlineAtMs: Date.now() + 60_000,+ });++describe('downloadUnipileMessageAttachment', () => {+ beforeEach(() => {+ mocks.getAttachment.mockReset();+ });++ it('returns the downloaded file', async () => {+ mocks.getAttachment.mockResolvedValue({+ data: streamOf({ chunks: ['ab', 'cd'] }),+ });++ await expect(download(10)).resolves.toEqual(Buffer.from('abcd'));+ expect(mocks.getAttachment).toHaveBeenCalledWith(+ expect.objectContaining({+ parseAs: 'stream',+ signal: expect.any(AbortSignal),+ path: {+ account_id: 'acc_1',+ chat_id: 'chat-1',+ message_id: 'message-1',+ attachment_id: 'attachment-1',+ },+ }),+ );+ });++ it('stops a download that goes over the size limit', async () => {+ const onCancel = vi.fn();++ mocks.getAttachment.mockResolvedValue({+ data: streamOf({ chunks: ['abc', 'def', 'ghi'], onCancel }),+ });++ await expect(download(4)).rejects.toThrow('larger than 4 bytes');+ expect(onCancel).toHaveBeenCalledOnce();+ });+});diff --git a/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/request-unipile.util.test.ts b/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/request-unipile.util.test.tsindex e4498d33fc3..43ae0afb802 100644--- a/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/request-unipile.util.test.ts+++ b/packages/twenty-apps/public/linkedin/src/unipile/utils/__tests__/request-unipile.util.test.ts@@ -44,6 +44,17 @@ describe('requestUnipile', () => {expect(mocks.wait).not.toHaveBeenCalled();});+ it('hands back a rate limit whose wait would end after the deadline', async () => {+ const request = vi.fn().mockRejectedValue(RATE_LIMITED_ERROR);++ await expect(+ requestUnipile('download message attachment', request, {+ deadlineAtMs: Date.now() + 10_000,+ }),+ ).rejects.toMatchObject({ name: 'UnipileRateLimitError' });+ expect(mocks.wait).not.toHaveBeenCalled();+ });+it('gives up after 5 waits', async () => {const request = vi.fn().mockRejectedValue(RATE_LIMITED_ERROR);diff --git a/packages/twenty-apps/public/linkedin/src/unipile/utils/download-unipile-message-attachment.util.ts b/packages/twenty-apps/public/linkedin/src/unipile/utils/download-unipile-message-attachment.util.tsnew file mode 100644index 00000000000..a7bca8bfd93--- /dev/null+++ b/packages/twenty-apps/public/linkedin/src/unipile/utils/download-unipile-message-attachment.util.ts@@ -0,0 +1,62 @@+import { UnipileMessaging } from '@unipile/sdk';++import { getUnipileRequestOptions } from 'src/unipile/utils/get-unipile-request-options.util';+import { requestUnipile } from 'src/unipile/utils/request-unipile.util';++export const downloadUnipileMessageAttachment = async ({+ accountId,+ chatId,+ messageId,+ attachmentId,+ maxSizeBytes,+ deadlineAtMs,+}: {+ accountId: string;+ chatId: string;+ messageId: string;+ attachmentId: string;+ maxSizeBytes: number;+ deadlineAtMs: number;+}): Promise<Buffer> => {+ const signal = AbortSignal.timeout(Math.max(deadlineAtMs - Date.now(), 1));+ const { data } = await requestUnipile(+ 'download message attachment',+ () =>+ new UnipileMessaging().getAttachment({+ ...getUnipileRequestOptions(),+ parseAs: 'stream',+ signal,+ path: {+ account_id: accountId,+ chat_id: chatId,+ message_id: messageId,+ attachment_id: attachmentId,+ },+ }),+ { deadlineAtMs },+ );++ if (!(data instanceof ReadableStream)) {+ throw new Error('Unipile returned no attachment content');+ }++ const reader: ReadableStreamDefaultReader<Uint8Array> = data.getReader();+ const chunks: Uint8Array[] = [];+ let sizeBytes = 0;+ let readResult = await reader.read();++ while (!readResult.done) {+ sizeBytes += readResult.value.byteLength;++ if (sizeBytes > maxSizeBytes) {+ await reader.cancel();++ throw new Error(`Attachment is larger than ${maxSizeBytes} bytes`);+ }++ chunks.push(readResult.value);+ readResult = await reader.read();+ }++ return Buffer.concat(chunks);+};diff --git a/packages/twenty-apps/public/linkedin/src/unipile/utils/request-unipile.util.ts b/packages/twenty-apps/public/linkedin/src/unipile/utils/request-unipile.util.tsindex 4a1ac368c5e..3b23344e061 100644--- a/packages/twenty-apps/public/linkedin/src/unipile/utils/request-unipile.util.ts+++ b/packages/twenty-apps/public/linkedin/src/unipile/utils/request-unipile.util.ts@@ -1,4 +1,5 @@import { isNumber, isObject, isString } from '@sniptt/guards';+import { isDefined } from 'twenty-sdk/utils';import { wait } from 'src/utils/wait.util';@@ -68,6 +69,7 @@ const getRateLimitWaitSeconds = (error: UnipileRequestError): number => {export const requestUnipile = async <TResult>(requestName: string,request: () => Promise<TResult>,+ { deadlineAtMs }: { deadlineAtMs?: number } = {},): Promise<TResult> => {for (let waitCount = 0; ; waitCount++) {try {@@ -86,7 +88,9 @@ export const requestUnipile = async <TResult>(if (waitCount >= UNIPILE_RATE_LIMIT_MAX_WAITS ||- waitSeconds > UNIPILE_RATE_LIMIT_MAX_WAIT_SECONDS+ waitSeconds > UNIPILE_RATE_LIMIT_MAX_WAIT_SECONDS ||+ (isDefined(deadlineAtMs) &&+ Date.now() + waitSeconds * 1000 > deadlineAtMs)) {throw new UnipileRateLimitError(requestError.message, waitSeconds);}