From 7bcb791a14da5d07bed2f6a48160c70212fc0f6c Mon Sep 17 00:00:00 2001 From: Ricardo Garim Date: Fri, 18 Oct 2024 09:35:57 -0300 Subject: [PATCH] chore!: remove query field on online channels listing (#33646) --- .../app/api/server/helpers/parseJsonQuery.ts | 1 + apps/meteor/app/api/server/v1/channels.ts | 15 +++++++++++---- apps/meteor/tests/end-to-end/api/channels.ts | 4 ++-- .../src/v1/channels/ChannelsOnlineProps.ts | 6 +++++- packages/rest-typings/src/v1/channels/channels.ts | 1 + 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/meteor/app/api/server/helpers/parseJsonQuery.ts b/apps/meteor/app/api/server/helpers/parseJsonQuery.ts index 631506e625a06..bbd58e1185560 100644 --- a/apps/meteor/app/api/server/helpers/parseJsonQuery.ts +++ b/apps/meteor/app/api/server/helpers/parseJsonQuery.ts @@ -61,6 +61,7 @@ export async function parseJsonQuery(api: PartialThis): Promise<{ '/api/v1/custom-user-status.list', '/api/v1/custom-sounds.list', '/api/v1/channels.list', + '/api/v1/channels.online', ].includes(route); const isUnsafeQueryParamsAllowed = process.env.ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS?.toUpperCase() === 'TRUE'; diff --git a/apps/meteor/app/api/server/v1/channels.ts b/apps/meteor/app/api/server/v1/channels.ts index 874ca5af4910e..2d82b04ad6f3e 100644 --- a/apps/meteor/app/api/server/v1/channels.ts +++ b/apps/meteor/app/api/server/v1/channels.ts @@ -20,6 +20,7 @@ import { isChannelsDeleteProps, isChannelsListProps, isChannelsFilesListProps, + isChannelsOnlineProps, } from '@rocket.chat/rest-typings'; import { Meteor } from 'meteor/meteor'; @@ -1072,17 +1073,23 @@ API.v1.addRoute( API.v1.addRoute( 'channels.online', - { authRequired: true }, + { authRequired: true, validateParams: isChannelsOnlineProps }, { async get() { const { query } = await this.parseJsonQuery(); - if (!query || Object.keys(query).length === 0) { + const { _id } = this.queryParams; + + if ((!query || Object.keys(query).length === 0) && !_id) { return API.v1.failure('Invalid query'); } - const ourQuery = Object.assign({}, query, { t: 'c' }); + const filter = { + ...query, + ...(_id ? { _id } : {}), + t: 'c', + }; - const room = await Rooms.findOne(ourQuery as Record); + const room = await Rooms.findOne(filter as Record); if (!room) { return API.v1.failure('Channel does not exists'); } diff --git a/apps/meteor/tests/end-to-end/api/channels.ts b/apps/meteor/tests/end-to-end/api/channels.ts index 6137ae6b946d5..e11c421b87078 100644 --- a/apps/meteor/tests/end-to-end/api/channels.ts +++ b/apps/meteor/tests/end-to-end/api/channels.ts @@ -950,7 +950,7 @@ describe('[Channels]', () => { return request .get(api('channels.online')) .set(testUserCredentials) - .query(`query={"_id": "${room._id}"}`) + .query({ _id: room._id }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { @@ -974,7 +974,7 @@ describe('[Channels]', () => { return request .get(api('channels.online')) .set(outsiderCredentials) - .query(`query={"_id": "${room._id}"}`) + .query({ _id: room._id }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { diff --git a/packages/rest-typings/src/v1/channels/ChannelsOnlineProps.ts b/packages/rest-typings/src/v1/channels/ChannelsOnlineProps.ts index 80bba717904f6..0a2b6a622e18b 100644 --- a/packages/rest-typings/src/v1/channels/ChannelsOnlineProps.ts +++ b/packages/rest-typings/src/v1/channels/ChannelsOnlineProps.ts @@ -4,10 +4,14 @@ const ajv = new Ajv({ coerceTypes: true, }); -export type ChannelsOnlineProps = { query?: Record }; +export type ChannelsOnlineProps = { _id?: string; query?: Record }; const channelsOnlyPropsSchema = { type: 'object', properties: { + _id: { + type: 'string', + nullable: true, + }, query: { type: 'string', nullable: true, diff --git a/packages/rest-typings/src/v1/channels/channels.ts b/packages/rest-typings/src/v1/channels/channels.ts index 5ee5480ee6a8d..c3138db174364 100644 --- a/packages/rest-typings/src/v1/channels/channels.ts +++ b/packages/rest-typings/src/v1/channels/channels.ts @@ -35,6 +35,7 @@ import type { ChannelsUnarchiveProps } from './ChannelsUnarchiveProps'; export * from './ChannelsFilesListProps'; export * from './ChannelsListProps'; +export * from './ChannelsOnlineProps'; export type ChannelsEndpoints = { '/v1/channels.files': {