Skip to content

Commit

Permalink
fix: rooms.export endpoint generates an empty export when given an …
Browse files Browse the repository at this point in the history
…invalid date (#32364)
  • Loading branch information
matheusbsilva137 committed May 20, 2024
1 parent 6ac3607 commit f83bd56
Show file tree
Hide file tree
Showing 10 changed files with 607 additions and 86 deletions.
6 changes: 6 additions & 0 deletions .changeset/ninety-rivers-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/rest-typings": minor
---

Fixed issue with "Export room as file" feature (`rooms.export` endpoint) generating an empty export when given an invalid date
20 changes: 4 additions & 16 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Media } from '@rocket.chat/core-services';
import type { IRoom, IUpload } from '@rocket.chat/core-typings';
import { Messages, Rooms, Users, Uploads } from '@rocket.chat/models';
import type { Notifications } from '@rocket.chat/rest-typings';
import { isGETRoomsNameExists, isRoomsImagesProps, isRoomsMuteUnmuteUserProps } from '@rocket.chat/rest-typings';
import { isGETRoomsNameExists, isRoomsImagesProps, isRoomsMuteUnmuteUserProps, isRoomsExportProps } from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';

import { isTruthy } from '../../../../lib/isTruthy';
Expand Down Expand Up @@ -599,15 +599,11 @@ API.v1.addRoute(

API.v1.addRoute(
'rooms.export',
{ authRequired: true },
{ authRequired: true, validateParams: isRoomsExportProps },
{
async post() {
const { rid, type } = this.bodyParams;

if (!rid || !type || !['email', 'file'].includes(type)) {
throw new Meteor.Error('error-invalid-params');
}

if (!(await hasPermissionAsync(this.userId, 'mail-messages', rid))) {
throw new Meteor.Error('error-action-not-allowed', 'Mailing is not allowed');
}
Expand All @@ -627,12 +623,8 @@ API.v1.addRoute(
const { dateFrom, dateTo } = this.bodyParams;
const { format } = this.bodyParams;

if (!['html', 'json'].includes(format || '')) {
throw new Meteor.Error('error-invalid-format');
}

const convertedDateFrom = new Date(dateFrom || '');
const convertedDateTo = new Date(dateTo || '');
const convertedDateFrom = dateFrom ? new Date(dateFrom) : new Date(0);
const convertedDateTo = dateTo ? new Date(dateTo) : new Date();
convertedDateTo.setDate(convertedDateTo.getDate() + 1);

if (convertedDateFrom > convertedDateTo) {
Expand All @@ -658,10 +650,6 @@ API.v1.addRoute(
throw new Meteor.Error('error-invalid-recipient');
}

if (messages?.length === 0) {
throw new Meteor.Error('error-invalid-messages');
}

const result = await dataExport.sendViaEmail(
{
rid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const FileExport = ({ formId, rid, exportOptions, onCancel }: FileExportProps) =
[t],
);

const handleExport = ({ type, dateFrom, dateTo, format }: MailExportFormValues) => {
const handleExport = ({ dateFrom, dateTo, format }: MailExportFormValues) => {
roomExportMutation.mutateAsync({
rid,
type,
dateFrom,
dateTo,
type: 'file',
...(dateFrom && { dateFrom }),
...(dateTo && { dateTo }),
format,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ const MailExportForm = ({ formId, rid, onCancel, exportOptions }: MailExportForm
setValue('messagesCount', messages.length);
}, [setValue, messages.length]);

const handleExport = async ({ type, toUsers, subject, additionalEmails }: MailExportFormValues) => {
const handleExport = async ({ toUsers, subject, additionalEmails }: MailExportFormValues) => {
roomExportMutation.mutateAsync({
rid,
type,
type: 'email',
toUsers,
toEmails: additionalEmails?.split(','),
subject,
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/server/lib/dataExport/exportRoomMessagesToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ const getAttachmentData = (attachment: MessageAttachment, message: IMessage) =>
};
};

type MessageData = Pick<IMessage, 'msg' | 'ts'> & {
export type MessageData = Pick<IMessage, 'msg' | 'ts'> & {
username?: IUser['username'] | IUser['name'];
attachments?: ReturnType<typeof getAttachmentData>[];
type?: IMessage['t'];
};

const getMessageData = (
export const getMessageData = (
msg: IMessage,
hideUsers: boolean,
userData: Pick<IUser, 'username'> | undefined,
Expand Down Expand Up @@ -160,7 +160,7 @@ const getMessageData = (
return messageObject;
};

const exportMessageObject = (type: 'json' | 'html', messageObject: MessageData, messageFile?: FileProp): string => {
export const exportMessageObject = (type: 'json' | 'html', messageObject: MessageData, messageFile?: FileProp): string => {
if (type === 'json') {
return JSON.stringify(messageObject);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ const exportMessageObject = (type: 'json' | 'html', messageObject: MessageData,
return file.join('\n');
};

const exportRoomMessages = async (
export const exportRoomMessages = async (
rid: IRoom['_id'],
exportType: 'json' | 'html',
skip: number,
Expand Down
Loading

0 comments on commit f83bd56

Please sign in to comment.