Chore: Migrate REST API - getRoomMembers to Typescript (#3899)

This commit is contained in:
Alex Junior 2022-03-15 22:37:49 -03:00 committed by GitHub
parent 4cfadbc97b
commit f98f2a46d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 33 deletions

View File

@ -4,6 +4,7 @@ import type { IServerRoom } from '../../IRoom';
import type { IUser } from '../../IUser';
import { IGetRoomRoles } from '../../IRole';
import { IServerAttachment } from '../../IAttachment';
import { PaginatedRequest } from '../helpers/PaginatedRequest';
export type ChannelsEndpoints = {
'channels.files': {
@ -15,12 +16,15 @@ export type ChannelsEndpoints = {
};
};
'channels.members': {
GET: (params: { roomId: IServerRoom['_id']; offset?: number; count?: number; filter?: string; status?: string[] }) => {
count: number;
offset: number;
GET: (params: {
roomId: IServerRoom['_id'];
offset?: number;
count?: number;
filter?: boolean;
status?: string[];
}) => PaginatedRequest<{
members: IUser[];
total: number;
};
}>;
};
'channels.history': {
GET: (params: { roomId: string; count: number; latest?: string }) => {

View File

@ -4,6 +4,7 @@ import type { IServerRoom } from '../../IRoom';
import type { IUser } from '../../IUser';
import { IGetRoomRoles } from '../../IRole';
import { IServerAttachment } from '../../IAttachment';
import { PaginatedRequest } from '../helpers/PaginatedRequest';
export type GroupsEndpoints = {
'groups.files': {
@ -15,12 +16,15 @@ export type GroupsEndpoints = {
};
};
'groups.members': {
GET: (params: { roomId: IServerRoom['_id']; offset?: number; count?: number; filter?: string; status?: string[] }) => {
count: number;
offset: number;
GET: (params: {
roomId: IServerRoom['_id'];
offset?: number;
count?: number;
filter?: string;
status?: string[];
}) => PaginatedRequest<{
members: IUser[];
total: number;
};
}>;
};
'groups.history': {
GET: (params: { roomId: string; count: number; latest?: string }) => {

View File

@ -2,6 +2,7 @@ import type { IMessageFromServer } from '../../IMessage';
import type { IServerRoom, RoomID, RoomType } from '../../IRoom';
import type { IUser } from '../../IUser';
import { IServerAttachment } from '../../IAttachment';
import { PaginatedRequest } from '../helpers/PaginatedRequest';
export type ImEndpoints = {
'im.create': {
@ -34,12 +35,15 @@ export type ImEndpoints = {
};
};
'im.members': {
GET: (params: { roomId: IServerRoom['_id']; offset?: number; count?: number; filter?: string; status?: string[] }) => {
count: number;
offset: number;
GET: (params: {
roomId: IServerRoom['_id'];
offset?: number;
count?: number;
filter?: string;
status?: string[];
}) => PaginatedRequest<{
members: IUser[];
total: number;
};
}>;
};
'im.history': {
GET: (params: { roomId: string; count: number; latest?: string }) => {

View File

@ -309,24 +309,6 @@ const RocketChat = {
return sdk.onStreamData(...args);
},
toggleFavorite,
async getRoomMembers({ rid, allUsers, roomType, type, filter, skip = 0, limit = 10 }) {
const serverVersion = reduxStore.getState().server.version;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.16.0')) {
const params = {
roomId: rid,
offset: skip,
count: limit,
...(type !== 'all' && { 'status[]': type }),
...(filter && { filter })
};
// RC 3.16.0
const result = await sdk.get(`${this.roomTypeToApiType(roomType)}.members`, params);
return result?.members;
}
// RC 0.42.0
const result = await this.methodCallWrapper('getUsersOfRoom', rid, allUsers, { skip, limit });
return result?.records;
},
methodCallWrapper(method, ...params) {
return sdk.methodCallWrapper(method, ...params);
},

View File

@ -817,3 +817,43 @@ export const sendEmailCode = () => {
// RC 3.1.0
return sdk.post('users.2fa.sendEmailCode', { emailOrUsername: username });
};
export const getRoomMembers = async ({
rid,
allUsers,
roomType,
type,
filter,
skip = 0,
limit = 10
}: {
rid: string;
allUsers: boolean;
type: 'all' | 'online';
roomType: SubscriptionType;
filter: boolean;
skip: number;
limit: number;
}) => {
const t = roomType as SubscriptionType.CHANNEL | SubscriptionType.GROUP | SubscriptionType.DIRECT;
const serverVersion = reduxStore.getState().server.version;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.16.0')) {
const params = {
roomId: rid,
offset: skip,
count: limit,
...(type !== 'all' && { 'status[]': type }),
...(filter && { filter })
};
// RC 3.16.0
const result = await sdk.get(`${roomTypeToApiType(t)}.members`, params);
if (result.success) {
return result?.members;
}
}
// RC 0.42.0
const result = await sdk.methodCallWrapper('getUsersOfRoom', rid, allUsers, { skip, limit });
if (result.success) {
return result?.records;
}
};