Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-insects-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use minor instead of patch anymore for this project

---

Migrated users.getPreferences to OpenAPI pattern with AJV validation
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"tmid",
"tshow"
]
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.vscode settings is out of the scoop for this PR could you fix this

76 changes: 50 additions & 26 deletions apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { MeteorError, Team, api, Calendar } from '@rocket.chat/core-services';

import type { IExportOperation, ILoginToken, IPersonalAccessToken, IUser, UserStatus } from '@rocket.chat/core-typings';
import { Users, Subscriptions, Sessions } from '@rocket.chat/models';
import {


isUserCreateParamsPOST,
isUserSetActiveStatusParamsPOST,
isUserDeactivateIdleParamsPOST,
Expand Down Expand Up @@ -171,9 +174,9 @@ API.v1.addRoute(
const twoFactorOptions = !userData.typedPassword
? null
: {
twoFactorCode: userData.typedPassword,
twoFactorMethod: 'password',
};
twoFactorCode: userData.typedPassword,
twoFactorMethod: 'password',
};

await executeSaveUserProfile.call(this, this.user, userData, this.bodyParams.customFields, twoFactorOptions);

Expand Down Expand Up @@ -536,10 +539,10 @@ API.v1.addRoute(
const limit =
count !== 0
? [
{
$limit: count,
},
]
{
$limit: count,
},
]
: [];

const result = await Users.col
Expand Down Expand Up @@ -752,6 +755,21 @@ API.v1.addRoute(
},
);

const UserPreferencesResponseSchema = {
type: 'object',
properties: {
preferences: {
type: 'object',
additionalProperties: true,
},
success: { type: 'boolean', enum: [true] },
},
required: ['preferences', 'success'],
additionalProperties: false,
};

const isUserPreferencesResponse = ajv.compile(UserPreferencesResponseSchema);

const usersEndpoints = API.v1
.post(
'users.createToken',
Expand Down Expand Up @@ -877,26 +895,34 @@ const usersEndpoints = API.v1

return API.v1.success({ suggestions });
},
);

API.v1.addRoute(
'users.getPreferences',
{ authRequired: true },
{
async get() {
)
.get(
'users.getPreferences',
{
authRequired: true,
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
200: isUserPreferencesResponse,
},
},
async function action() {
const user = await Users.findOneById(this.userId);
if (user?.settings) {
const { preferences = {} } = user?.settings;
preferences.language = user?.language;

return API.v1.success({
preferences,
});
if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user');
}
return API.v1.failure(i18n.t('Accounts_Default_User_Preferences_not_available').toUpperCase());

const preferences = {
...(user.settings?.preferences ?? {}),
language: user.language,
};

return API.v1.success({ preferences });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't change the action function code

},
},
);
);

export type UsersEndpoints = ExtractRoutesFromAPI<typeof usersEndpoints>;

API.v1.addRoute(
'users.forgotPassword',
Expand Down Expand Up @@ -1554,9 +1580,7 @@ settings.watch<number>('Rate_Limiter_Limit_RegisterUser', (value) => {
API.v1.updateRateLimiterDictionaryForRoute(userRegisterRoute, value);
});

type UsersEndpoints = ExtractRoutesFromAPI<typeof usersEndpoints>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends UsersEndpoints {}
Comment on lines -1558 to -1562
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this's was right, you could return it as it's

interface Endpoints extends UsersEndpoints { }
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to remove the users.getPreferences from the rest-typings

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its my mistake I removed it on my machine but totally forgot to git add it before committing :)