Skip to content

Commit

Permalink
Chore: Convert users endpoints (#25635)
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>
Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
  • Loading branch information
3 people committed Jun 17, 2022
1 parent 2c05ffd commit 6b6f9dc
Show file tree
Hide file tree
Showing 23 changed files with 1,370 additions and 669 deletions.
11 changes: 10 additions & 1 deletion apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} from '@rocket.chat/rest-typings';
import type { IUser, IMethodConnection, IRoom } from '@rocket.chat/core-typings';
import type { ValidateFunction } from 'ajv';
import type { Request } from 'express';
import type { Request, Response } from 'express';

import { ITwoFactorOptions } from '../../2fa/server/code';

Expand Down Expand Up @@ -73,11 +73,13 @@ type Options = (

type PartialThis = {
readonly request: Request & { query: Record<string, string> };
readonly response: Response;
};

type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
readonly requestIp: string;
urlParams: UrlParams<TPathPattern>;
readonly response: Response;
// TODO make it unsafe
readonly queryParams: TMethod extends 'GET'
? TOptions extends { validateParams: ValidateFunction<infer T> }
Expand All @@ -91,6 +93,9 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
? T
: Partial<OperationParams<TMethod, TPathPattern>>;
readonly request: Request;

readonly queryOperations: TOptions extends { queryOperations: infer T } ? T : never;

/* @deprecated */
requestParams(): OperationParams<TMethod, TPathPattern>;
getLoggedInUser(): TOptions extends { authRequired: true } ? IUser : IUser | undefined;
Expand All @@ -106,6 +111,8 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
/* @deprecated */
getUserFromParams(): IUser;
/* @deprecated */
isUserFromParams(): boolean;
/* @deprecated */
getUserInfo(me: IUser): TOptions extends { authRequired: true }
? IUser & {
email?: string;
Expand Down Expand Up @@ -153,6 +160,8 @@ type Operations<TPathPattern extends PathPattern, TOptions extends Options = {}>
declare class APIClass<TBasePath extends string = '/'> {
fieldSeparator: string;

updateRateLimiterDictionaryForRoute(route: string, rateLimiterDictionary: number): void;

limitedUserFieldsToExclude(fields: { [x: string]: unknown }, limitedUserFieldsToExclude: unknown): { [x: string]: unknown };

limitedUserFieldsToExcludeIfIsPrivilegedUser(
Expand Down
15 changes: 3 additions & 12 deletions apps/meteor/app/api/server/lib/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function findUsersToAutocomplete({
term: string;
};
}): Promise<{
items: IUser[];
items: Required<Pick<IUser, '_id' | 'name' | 'username' | 'nickname' | 'status' | 'avatarETag'>>[];
}> {
if (!(await hasPermissionAsync(uid, 'view-outside-room'))) {
return { items: [] };
Expand Down Expand Up @@ -69,16 +69,7 @@ export function getInclusiveFields(query: { [k: string]: 1 }): {} {
* get the default fields if **fields** are empty (`{}`) or `undefined`/`null`
* @param {Object|null|undefined} fields the fields from parsed jsonQuery
*/
export function getNonEmptyFields(fields: {}): {
name: number;
username: number;
emails: number;
roles: number;
status: number;
active: number;
avatarETag: number;
lastLogin: number;
} {
export function getNonEmptyFields(fields: { [k: string]: 1 | 0 }): { [k: string]: 1 } {
const defaultFields = {
name: 1,
username: 1,
Expand All @@ -88,7 +79,7 @@ export function getNonEmptyFields(fields: {}): {
active: 1,
avatarETag: 1,
lastLogin: 1,
};
} as const;

if (!fields || Object.keys(fields).length === 0) {
return defaultFields;
Expand Down
Loading

0 comments on commit 6b6f9dc

Please sign in to comment.