Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/chatty-camels-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/model-typings': patch
'@rocket.chat/models': patch
'@rocket.chat/meteor': patch
---

Fixes `/sendEmailAttachment` to support sending multiple file attachments in a single email
77 changes: 46 additions & 31 deletions apps/meteor/server/features/EmailInbox/EmailInbox_Outgoing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isIMessageInbox } from '@rocket.chat/core-typings';
import type { IEmailInbox, IUser, IOmnichannelRoom, SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import type { IEmailInbox, IUser, IOmnichannelRoom, SlashCommandCallbackParams, IUpload } from '@rocket.chat/core-typings';
import { Messages, Uploads, LivechatRooms, Rooms, Users } from '@rocket.chat/models';
import { isTruthy } from '@rocket.chat/tools';
import { Match } from 'meteor/check';
import type Mail from 'nodemailer/lib/mailer';

Expand All @@ -22,6 +23,18 @@ const getRocketCatUser = async (): Promise<IUser | null> => Users.findOneById('r
const language = settings.get<string>('Language') || 'en';
const t = i18n.getFixedT(language);

async function buildMailAttachment(file: IUpload): Promise<Mail.Attachment | undefined> {
const buffer = await FileUpload.getBuffer(file);
if (!buffer) {
return;
}
return {
content: buffer,
contentType: file.type,
filename: file.name,
};
}

// TODO: change these messages with room notifications
const sendErrorReplyMessage = async (error: string, options: any) => {
if (!options?.rid || !options?.msgId) {
Expand Down Expand Up @@ -98,12 +111,16 @@ slashCommands.add({
}

const message = await Messages.findOneById(params.trim());
if (!message?.file) {
if (!message) {
return;
}

const room = await Rooms.findOneById<IOmnichannelRoom>(message.rid);
const fileRefs = (message.files || [message.file]).filter(isTruthy);
if (!fileRefs.length) {
return;
}

const room = await Rooms.findOneById<IOmnichannelRoom>(message.rid);
if (!room?.email) {
return;
}
Expand All @@ -118,37 +135,35 @@ slashCommands.add({
});
}

const file = await Uploads.findOneById(message.file._id);

if (!file) {
const files = await Uploads.find({ _id: { $in: fileRefs.map((f) => f._id) } }).toArray();
const emailAttachments = await Promise.all(files.map(buildMailAttachment));
const validAttachments = emailAttachments.filter((a): a is Mail.Attachment => Boolean(a));
if (validAttachments.length === 0) {
return;
}

const buffer = await FileUpload.getBuffer(file);
if (buffer) {
void sendEmail(
inbox,
{
to: room.email?.replyTo,
subject: room.email?.subject,
text: message?.attachments?.[0].description || '',
attachments: [
{
content: buffer,
contentType: file.type,
filename: file.name,
},
],
inReplyTo: Array.isArray(room.email?.thread) ? room.email?.thread[0] : room.email?.thread,
references: ([] as string[]).concat(room.email?.thread || []),
},
{
msgId: message._id,
sender: message.u.username,
rid: message.rid,
},
).then((info) => LivechatRooms.updateEmailThreadByRoomId(room._id, info.messageId));
}
const emailText =
message?.attachments
?.map((a) => a.description)
.filter(Boolean)
.join('\n\n') || '';

void sendEmail(
inbox,
{
to: room.email?.replyTo,
subject: room.email?.subject,
text: emailText,
attachments: validAttachments,
inReplyTo: Array.isArray(room.email?.thread) ? room.email?.thread[0] : room.email?.thread,
references: ([] as string[]).concat(room.email?.thread || []),
},
{
msgId: message._id,
sender: message.u.username,
rid: message.rid,
},
).then((info) => LivechatRooms.updateEmailThreadByRoomId(room._id, info.messageId));

await Messages.updateOne(
{ _id: message._id },
Expand Down
2 changes: 2 additions & 0 deletions packages/model-typings/src/models/IBaseUploadsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface IBaseUploadsModel<T extends IUpload> extends IBaseModel<T> {

confirmTemporaryFile(fileId: string, userId: string): Promise<Document | UpdateResult> | undefined;

findByIds(_ids: string[], options?: FindOptions<T>): FindCursor<T>;

findOneByName(name: string, options?: { session?: ClientSession }): Promise<T | null>;

findOneByRoomId(rid: string): Promise<T | null>;
Expand Down
10 changes: 10 additions & 0 deletions packages/models/src/models/BaseUploadModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export abstract class BaseUploadModelRaw extends BaseRaw<T> implements IBaseUplo
return this.updateOne(filter, update);
}

findByIds(_ids: string[], options?: FindOptions<T>): FindCursor<T> {
const query = {
_id: {
$in: _ids,
},
};

return this.find(query, options);
}

async findOneByName(name: string, options?: { session?: ClientSession }): Promise<T | null> {
return this.findOne<T>({ name }, { session: options?.session });
}
Expand Down
Loading