Skip to content
Closed
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
171 changes: 108 additions & 63 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ export async function findRoomByIdOrName({
checkedArchived = true,
}: {
params:
| {
roomId?: string;
}
| {
roomName?: string;
};
| {
roomId?: string;
}
| {
roomName?: string;
};
checkedArchived?: boolean;
}): Promise<IRoom> {
if (
Expand Down Expand Up @@ -372,54 +372,98 @@ const roomsSaveNotificationEndpoint = API.v1.post(
},
);

API.v1.addRoute(
'rooms.cleanHistory',
{ authRequired: true, validateParams: isRoomsCleanHistoryProps },
const roomsCleanHistoryEndpoint = API.v1.post('rooms.cleanHistory',
{
async post() {
const room = await findRoomByIdOrName({ params: this.bodyParams });
const { _id } = room;

if (!room || !(await canAccessRoomAsync(room, { _id: this.userId }))) {
return API.v1.failure('User does not have access to the room [error-not-allowed]', 'error-not-allowed');
}

const {
latest,
oldest,
inclusive = false,
limit,
excludePinned,
filesOnly,
ignoreThreads,
ignoreDiscussion,
users,
} = this.bodyParams;

if (!latest) {
return API.v1.failure('Body parameter "latest" is required.');
}

if (!oldest) {
return API.v1.failure('Body parameter "oldest" is required.');
}

const count = await cleanRoomHistoryMethod(this.userId, {
roomId: _id,
latest: new Date(latest),
oldest: new Date(oldest),
inclusive,
limit,
excludePinned: [true, 'true', 1, '1'].includes(excludePinned ?? false),
filesOnly: [true, 'true', 1, '1'].includes(filesOnly ?? false),
ignoreThreads: [true, 'true', 1, '1'].includes(ignoreThreads ?? false),
ignoreDiscussion: [true, 'true', 1, '1'].includes(ignoreDiscussion ?? false),
fromUsers: users?.filter(isTruthy) || [],
});

return API.v1.success({ _id, count });
authRequired: true,
body: ajv.compile<{
roomId?: string;
roomName?: string;
latest: string;
oldest: string;
inclusive?: boolean;
limit?: number;
excludePinned?: boolean | string | number;
filesOnly?: boolean | string | number;
ignoreThreads?: boolean | string | number;
ignoreDiscussion?: boolean | string | number;
users?: string[];

}>({
type: 'object',
properties: {
roomId: { type: 'string' },
roomName: { type: 'string' },
latest: { type: 'string', description: 'The end date of the range' },
oldest: { type: 'string', description: 'The start date of the range' },
inclusive: { type: 'boolean' },
limit: { type: 'number' },
excludePinned: { type: ['boolean', 'string', 'number'] },
filesOnly: { type: ['boolean', 'string', 'number'] },
ignoreThreads: { type: ['boolean', 'string', 'number'] },
ignoreDiscussion: { type: ['boolean', 'string', 'number'] },
users: { type: 'array', items: { type: 'string' } }
},
required: ['latest', 'oldest'],
anyOf: [
{ required: ['roomId'] },
{ required: ['roomName'] }
],
additionalProperties: false
}),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
response: {
200: ajv.compile<{
_id: string;
count: number;
success: true;
}>({
type: 'object',
properties: {
_id: { type: 'string' },
count: { type: 'number' },
success: { type: 'boolean', enum: [true] },
},
required: ['_id', 'count', 'success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const room = await findRoomByIdOrName({ params: this.bodyParams });

if (!room || !(await canAccessRoomAsync(room, { _id: this.userId }))) {
return API.v1.failure('User does not have access to the room [error-not-allowed]', 'error-not-allowed');
}
const { _id } = room;

const {
latest,
oldest,
inclusive = false,
limit,
excludePinned,
filesOnly,
ignoreThreads,
ignoreDiscussion,
users,
} = this.bodyParams;

const count = await cleanRoomHistoryMethod(this.userId, {
roomId: _id,
latest: new Date(latest),
oldest: new Date(oldest),
inclusive,
limit,
excludePinned: [true, 'true', 1, '1'].includes(excludePinned ?? false),
filesOnly: [true, 'true', 1, '1'].includes(filesOnly ?? false),
ignoreThreads: [true, 'true', 1, '1'].includes(ignoreThreads ?? false),
ignoreDiscussion: [true, 'true', 1, '1'].includes(ignoreDiscussion ?? false),
fromUsers: users?.filter(isTruthy) || [],
});

return API.v1.success({ _id, count });
}
);

API.v1.addRoute(
Expand Down Expand Up @@ -970,21 +1014,21 @@ API.v1.addRoute(

type RoomsFavorite =
| {
roomId: string;
favorite: boolean;
}
roomId: string;
favorite: boolean;
}
| {
roomName: string;
favorite: boolean;
};
roomName: string;
favorite: boolean;
};

type RoomsLeave =
| {
roomId: string;
}
roomId: string;
}
| {
roomName: string;
};
roomName: string;
};

const isRoomGetRolesPropsSchema = {
type: 'object',
Expand Down Expand Up @@ -1377,9 +1421,10 @@ export const roomEndpoints = API.v1
);
type RoomEndpoints = ExtractRoutesFromAPI<typeof roomEndpoints> &
ExtractRoutesFromAPI<typeof roomDeleteEndpoint> &
ExtractRoutesFromAPI<typeof roomsSaveNotificationEndpoint>;
ExtractRoutesFromAPI<typeof roomsSaveNotificationEndpoint> &
ExtractRoutesFromAPI<typeof roomsCleanHistoryEndpoint>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends RoomEndpoints {}
interface Endpoints extends RoomEndpoints { }
}
3 changes: 0 additions & 3 deletions packages/rest-typings/src/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,6 @@ export type RoomsEndpoints = {
};
};

'/v1/rooms.cleanHistory': {
POST: (params: RoomsCleanHistoryProps) => { _id: IRoom['_id']; count: number; success: boolean };
};

'/v1/rooms.createDiscussion': {
POST: (params: RoomsCreateDiscussionProps) => {
Expand Down