diff --git a/.changeset/chatty-insects-notice.md b/.changeset/chatty-insects-notice.md new file mode 100644 index 0000000000000..0cf3ac56be86b --- /dev/null +++ b/.changeset/chatty-insects-notice.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': patch +--- + +Fixes Smarsh integration to support multiple file attachments per message. diff --git a/apps/meteor/app/smarsh-connector/server/functions/generateEml.ts b/apps/meteor/app/smarsh-connector/server/functions/generateEml.ts index ea9525442e469..7183e7d41a0c6 100644 --- a/apps/meteor/app/smarsh-connector/server/functions/generateEml.ts +++ b/apps/meteor/app/smarsh-connector/server/functions/generateEml.ts @@ -1,3 +1,4 @@ +import type { FileProp, MessageAttachment } from '@rocket.chat/core-typings'; import { MessageTypes } from '@rocket.chat/message-types'; import { Messages, SmarshHistory, Users, Rooms } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; @@ -25,6 +26,19 @@ function _getLink(attachment: { title_link: string }): string { return Meteor.absoluteUrl().replace(/\/$/, '') + __meteor_runtime_config__.ROOT_URL_PATH_PREFIX + url; } +function _processMessageFiles(files: FileProp[], attachments: MessageAttachment[] | undefined): { fileIds: string[]; displayText: string } { + const fileIds = files.map((f) => f._id); + + const fileLinks = + attachments + ?.filter((a): a is MessageAttachment & { title: string } => 'title' in a && a.title !== undefined) + .map((a) => `${a.title} (${_getLink({ title_link: 'title_link' in a ? a.title_link || '' : '' })})`) || []; + + const displayText = fileLinks.join(', '); + + return { fileIds, displayText }; +} + export const generateEml = async (): Promise => { setImmediate(async () => { const smarshMissingEmail = settings.get('Smarsh_MissingEmail_Email'); @@ -93,9 +107,14 @@ export const generateEml = async (): Promise => { } else { rows.push(`${message.msg} (${message.t})`); } + } else if (message.files?.length) { + const fileResult = _processMessageFiles(message.files, message.attachments); + fileResult.fileIds.forEach((id) => data.files.push(id)); + rows.push(fileResult.displayText); } else if (message.file) { - data.files.push(message.file._id); - rows.push(`${message?.attachments?.[0].title} (${_getLink({ title_link: message?.attachments?.[0].title_link || '' })})})`); + const fileResult = _processMessageFiles([message.file], message.attachments); + fileResult.fileIds.forEach((id) => data.files.push(id)); + rows.push(fileResult.displayText); } else if (message.attachments) { const attaches: string[] = []; message.attachments.forEach((a) => { @@ -123,9 +142,13 @@ export const generateEml = async (): Promise => { await SmarshHistory.updateOne( { _id: room._id }, { - _id: room._id, - lastRan: date, - lastResult: result, + $set: { + lastRan: date, + lastResult: result, + }, + $setOnInsert: { + _id: room._id, + }, }, { upsert: true }, );