-
Notifications
You must be signed in to change notification settings - Fork 13.5k
refactor(groups): migrate groups.members to typed REST endpoint #39865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@rocket.chat/rest-typings': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Migration of groups.memebers added ajv validation for request and response, schema changes for request. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| import { Team, isMeteorError } from '@rocket.chat/core-services'; | ||
| import type { IIntegration, IUser, IRoom, RoomType, UserStatus } from '@rocket.chat/core-typings'; | ||
| import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models'; | ||
| import { isGroupsOnlineProps, isGroupsMessagesProps, isGroupsFilesProps } from '@rocket.chat/rest-typings'; | ||
| import { isGroupsOnlineProps | ||
| , isGroupsMessagesProps, | ||
| isGroupsFilesProps, | ||
| ajv, | ||
| validateBadRequestErrorResponse, | ||
| validateUnauthorizedErrorResponse, | ||
| validateForbiddenErrorResponse, | ||
| withGroupBaseProperties, } from '@rocket.chat/rest-typings'; | ||
| import { isTruthy } from '@rocket.chat/tools'; | ||
| import { check, Match } from 'meteor/check'; | ||
| import { Meteor } from 'meteor/meteor'; | ||
| import type { Filter } from 'mongodb'; | ||
|
|
||
|
|
||
| import type { ExtractRoutesFromAPI } from '../ApiClass'; | ||
| import { eraseRoom } from '../../../../server/lib/eraseRoom'; | ||
| import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom'; | ||
| import { openRoom } from '../../../../server/lib/openRoom'; | ||
|
|
@@ -36,6 +45,111 @@ import { addUserToFileObj } from '../helpers/addUserToFileObj'; | |
| import { composeRoomWithLastMessage } from '../helpers/composeRoomWithLastMessage'; | ||
| import { getPaginationItems } from '../helpers/getPaginationItems'; | ||
| import { getUserFromParams, getUserListFromParams } from '../helpers/getUserFromParams'; | ||
| import type { PaginatedRequest, GroupsBaseProps } from '@rocket.chat/rest-typings'; | ||
| import { nullable } from 'zod'; | ||
|
|
||
| type GroupsMembersProps = PaginatedRequest<GroupsBaseProps & { filter?: string; status?: string[] }>; | ||
|
|
||
| const GroupsMembersPropsSchema = withGroupBaseProperties({ | ||
| offset: { | ||
| type: 'string', | ||
| }, | ||
| count: { | ||
| type: 'string', | ||
| }, | ||
| filter: { | ||
| type: 'string', | ||
| }, | ||
| query: { | ||
| type: 'string', | ||
| nullable: true | ||
| }, | ||
| sort: { | ||
| type: 'string', | ||
| nullable: true | ||
| }, | ||
| status: { | ||
| type: 'array', | ||
| items: { type: 'string' }, | ||
| }, | ||
| }); | ||
|
|
||
| const isGroupsMembersProps = ajv.compile<GroupsMembersProps>(GroupsMembersPropsSchema); | ||
|
|
||
|
|
||
| const isGroupsMembersResponse = ajv.compile({ | ||
| type: 'object', | ||
| properties: { | ||
| members: { | ||
| type: 'array', | ||
| items: { $ref: '#/components/schemas/IUser' }, | ||
| }, | ||
| count: { type: 'integer' }, | ||
| offset: { type: 'integer' }, | ||
| total: { type: 'integer' }, | ||
| success: { type: 'boolean', enum: [true] }, | ||
| }, | ||
| required: ['members', 'success'], | ||
| additionalProperties: false, | ||
| }); | ||
|
Comment on lines
+79
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Examine the groups.ts file to see the current state and patterns
cat -n apps/meteor/app/api/server/v1/groups.ts | head -150Repository: RocketChat/Rocket.Chat Length of output: 6668 🏁 Script executed: # Look for similar response schema patterns in rooms.ts to confirm the convention
rg "ajv\.compile" apps/meteor/app/api/server/v1/rooms.ts -A 10 | head -100Repository: RocketChat/Rocket.Chat Length of output: 2266 🏁 Script executed: # Check if there's a changeset describing this as an OpenAPI migration
fd -e md apps/meteor/app/api/server/v1/ | head -20Repository: RocketChat/Rocket.Chat Length of output: 491 🏁 Script executed: # Search for pagination-field patterns in other migrated endpoints
rg "count.*offset.*total" apps/meteor/app/api/server/v1/ --type ts -B 2 | head -80Repository: RocketChat/Rocket.Chat Length of output: 3920 🏁 Script executed: # Check if there's a changeset or PR context for this groups.ts file
find apps/meteor/app/api/server/v1 -name "*.md" -o -name "changeset*" | head -10Repository: RocketChat/Rocket.Chat Length of output: 48 🏁 Script executed: # Verify by examining rooms.ts more closely to see the pattern distinction
rg "rooms\.delete|rooms\.leave|rooms\.favorite" apps/meteor/app/api/server/v1/rooms.ts -B 5 -A 15 | head -80Repository: RocketChat/Rocket.Chat Length of output: 1381 🏁 Script executed: # Check the context around groups.ts to verify it's an OpenAPI migration
rg "GroupsHistoryEndpoints|GroupsMembersEndpoints" apps/meteor/app/api/server/v1/groups.ts -A 3 -B 3Repository: RocketChat/Rocket.Chat Length of output: 364 🏁 Script executed: # Confirm the pattern: compare groups.members (paginated) vs a simple groups endpoint
rg "groups\.(delete|leave|archive)" apps/meteor/app/api/server/v1/groups.ts -B 5 -A 15 | head -100Repository: RocketChat/Rocket.Chat Length of output: 1159 Explicitly type the 200 response schema and require all pagination fields. The Suggested fix+type GroupsMembersResponse = {
+ members: IUser[];
+ count: number;
+ offset: number;
+ total: number;
+ success: true;
+};
+
-const isGroupsMembersResponse = ajv.compile({
+const isGroupsMembersResponse = ajv.compile<GroupsMembersResponse>({
type: 'object',
properties: {
members: {
type: 'array',
items: { $ref: '#/components/schemas/IUser' },
},
count: { type: 'integer' },
offset: { type: 'integer' },
total: { type: 'integer' },
success: { type: 'boolean', enum: [true] },
},
- required: ['members', 'success'],
+ required: ['members', 'count', 'offset', 'total', 'success'],
additionalProperties: false,
});🤖 Prompt for AI Agents |
||
|
|
||
| const groupsEndPoints = API.v1 | ||
| .get( | ||
| 'groups.members', | ||
| { | ||
| authRequired: true, | ||
| query: isGroupsMembersProps, | ||
| response: { | ||
| 200: isGroupsMembersResponse, | ||
| 400: validateBadRequestErrorResponse, | ||
| 401: validateUnauthorizedErrorResponse, | ||
| 403: validateForbiddenErrorResponse | ||
| } | ||
| }, | ||
| async function action(){ | ||
| const findResult = await findPrivateGroupByIdOrName({ | ||
| params: this.queryParams, | ||
| userId: this.userId, | ||
| }); | ||
|
|
||
| if (findResult.broadcast && !(await hasPermissionAsync(this.userId, 'view-broadcast-member-list', findResult.rid))) { | ||
| return API.v1.forbidden('User does not have the permissions required for this action'); | ||
| } | ||
|
|
||
| const { offset: skip, count: limit } = await getPaginationItems(this.queryParams); | ||
| const { sort = {} } = await this.parseJsonQuery(); | ||
|
|
||
| check( | ||
| this.queryParams, | ||
| Match.ObjectIncluding({ | ||
| status: Match.Maybe([String]), | ||
| filter: Match.Maybe(String), | ||
| }), | ||
| ); | ||
|
|
||
| const { status, filter } = this.queryParams; | ||
|
|
||
| const { cursor, totalCount } = await findUsersOfRoom({ | ||
| rid: findResult.rid, | ||
| ...(status && { status: { $in: status as UserStatus[] } }), | ||
| skip, | ||
| limit, | ||
| filter, | ||
| ...(sort?.username && { sort: { username: sort.username } }), | ||
| }); | ||
|
|
||
| const [members, total] = await Promise.all([cursor.toArray(), totalCount]); | ||
|
|
||
| return API.v1.success({ | ||
| members, | ||
| count: members.length, | ||
| offset: skip, | ||
| total, | ||
| }); | ||
| } | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| async function getRoomFromParams(params: { roomId?: string } | { roomName?: string }): Promise<IRoom> { | ||
| if ( | ||
|
|
@@ -718,54 +832,6 @@ API.v1.addRoute( | |
| }, | ||
| ); | ||
|
|
||
| API.v1.addRoute( | ||
| 'groups.members', | ||
| { authRequired: true }, | ||
| { | ||
| async get() { | ||
| const findResult = await findPrivateGroupByIdOrName({ | ||
| params: this.queryParams, | ||
| userId: this.userId, | ||
| }); | ||
|
|
||
| if (findResult.broadcast && !(await hasPermissionAsync(this.userId, 'view-broadcast-member-list', findResult.rid))) { | ||
| return API.v1.forbidden(); | ||
| } | ||
|
|
||
| const { offset: skip, count: limit } = await getPaginationItems(this.queryParams); | ||
| const { sort = {} } = await this.parseJsonQuery(); | ||
|
|
||
| check( | ||
| this.queryParams, | ||
| Match.ObjectIncluding({ | ||
| status: Match.Maybe([String]), | ||
| filter: Match.Maybe(String), | ||
| }), | ||
| ); | ||
|
|
||
| const { status, filter } = this.queryParams; | ||
|
|
||
| const { cursor, totalCount } = await findUsersOfRoom({ | ||
| rid: findResult.rid, | ||
| ...(status && { status: { $in: status as UserStatus[] } }), | ||
| skip, | ||
| limit, | ||
| filter, | ||
| ...(sort?.username && { sort: { username: sort.username } }), | ||
| }); | ||
|
|
||
| const [members, total] = await Promise.all([cursor.toArray(), totalCount]); | ||
|
|
||
| return API.v1.success({ | ||
| members, | ||
| count: members.length, | ||
| offset: skip, | ||
| total, | ||
| }); | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| API.v1.addRoute( | ||
| 'groups.messages', | ||
| { authRequired: true, validateParams: isGroupsMessagesProps }, | ||
|
|
@@ -1300,3 +1366,11 @@ API.v1.addRoute( | |
| }, | ||
| }, | ||
| ); | ||
|
|
||
|
|
||
| export type GroupsHistoryEndpoints = ExtractRoutesFromAPI<typeof groupsEndPoints>; | ||
|
|
||
| declare module '@rocket.chat/rest-typings' { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface | ||
| interface Endpoints extends GroupsHistoryEndpoints {} | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.