Skip to content

Commit

Permalink
chore!: remove query field on online groups listing (#33647)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogarim authored Oct 18, 2024
1 parent 019a69a commit 15582b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
18 changes: 12 additions & 6 deletions apps/meteor/app/api/server/v1/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Team, isMeteorError } from '@rocket.chat/core-services';
import type { IIntegration, IUser, IRoom, RoomType } from '@rocket.chat/core-typings';
import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models';
import { isGroupsOnlineProps } from '@rocket.chat/rest-typings';
import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import type { Filter } from 'mongodb';
Expand Down Expand Up @@ -779,23 +780,28 @@ API.v1.addRoute(
// TODO: CACHE: same as channels.online
API.v1.addRoute(
'groups.online',
{ authRequired: true },
{ authRequired: true, validateParams: isGroupsOnlineProps },
{
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: 'p' });

const room = await Rooms.findOne(ourQuery as Record<string, any>);
const filter = {
...query,
...(_id ? { _id } : {}),
t: 'p',
};

const room = await Rooms.findOne(filter as Record<string, any>);
if (!room) {
return API.v1.failure('Group does not exists');
}
const user = await getLoggedInUser(this.request);

const user = await getLoggedInUser(this.request);
if (!user) {
return API.v1.failure('User does not exists');
}
Expand Down
12 changes: 9 additions & 3 deletions apps/meteor/tests/end-to-end/api/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,9 @@ describe('[Groups]', () => {
it('should return an array with online members', async () => {
const { testUser, testUserCredentials, room } = await createUserAndChannel();

const response = await request.get(api('groups.online')).set(testUserCredentials).query(`query={"_id": "${room._id}"}`);
const response = await request.get(api('groups.online')).set(testUserCredentials).query({
_id: room._id,
});

const { body } = response;

Expand All @@ -1134,7 +1136,9 @@ describe('[Groups]', () => {
it('should return an empty array if members are offline', async () => {
const { testUserCredentials, room } = await createUserAndChannel(false);

const response = await request.get(api('groups.online')).set(testUserCredentials).query(`query={"_id": "${room._id}"}`);
const response = await request.get(api('groups.online')).set(testUserCredentials).query({
_id: room._id,
});

const { body } = response;

Expand All @@ -1150,7 +1154,9 @@ describe('[Groups]', () => {
await request
.get(api('groups.online'))
.set(outsiderCredentials)
.query(`query={"_id": "${room._id}"}`)
.query({
_id: room._id,
})
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
Expand Down
12 changes: 9 additions & 3 deletions packages/rest-typings/src/v1/groups/GroupsOnlineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ const ajv = new Ajv({
coerceTypes: true,
});

export type GroupsOnlineProps = { query?: Record<string, any> };
const groupsOnlyPropsSchema = {
export type GroupsOnlineProps = { _id?: string; query?: Record<string, any> };

const groupsOnlinePropsSchema = {
type: 'object',
properties: {
_id: {
type: 'string',
nullable: true,
},
query: {
type: 'string',
nullable: true,
Expand All @@ -16,4 +21,5 @@ const groupsOnlyPropsSchema = {
required: [],
additionalProperties: false,
};
export const isGroupsOnlineProps = ajv.compile<GroupsOnlineProps>(groupsOnlyPropsSchema);

export const isGroupsOnlineProps = ajv.compile<GroupsOnlineProps>(groupsOnlinePropsSchema);

0 comments on commit 15582b1

Please sign in to comment.