From 8bed4acf064891a393449c23485d3bc0abf1a2c2 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Fri, 25 Feb 2022 14:14:00 -0300 Subject: [PATCH 1/2] chore: implementing type for test api - user.setPreferences --- app/definitions/IUser.ts | 16 ++++++++++++++++ app/definitions/rest/v1/user.ts | 8 +++++++- app/lib/rocketchat/services/restApi.ts | 5 ++--- .../UserNotificationPreferencesView/index.tsx | 10 ++++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/definitions/IUser.ts b/app/definitions/IUser.ts index 461b52fb48c..df821ac384e 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -93,6 +93,22 @@ export interface IUserSettings { [key: string]: any; }; } +type TNotifications = 'default' | 'all' | 'mentions' | 'nothing'; + +export interface INotificationPreferences { + id: string; + enableMessageParserEarlyAdoption: boolean; + desktopNotifications: TNotifications; + pushNotifications: TNotifications; + emailNotificationMode?: 'mentions' | 'nothing'; +} + +export interface IUserPreferences { + user: { _id: string }; + settings: { + preferences: INotificationPreferences; + }; +} export interface IUser extends IRocketChatRecord, Omit { _id: string; diff --git a/app/definitions/rest/v1/user.ts b/app/definitions/rest/v1/user.ts index 7de12aabf0f..f391cf301ba 100644 --- a/app/definitions/rest/v1/user.ts +++ b/app/definitions/rest/v1/user.ts @@ -1,4 +1,4 @@ -import { IUser } from '../../IUser'; +import { INotificationPreferences, IUser, IUserPreferences } from '../../IUser'; export type UserEndpoints = { 'users.info': { @@ -11,4 +11,10 @@ export type UserEndpoints = { success: boolean; }; }; + 'users.setPreferences': { + POST: (params: { userId: IUser['_id']; data: Partial }) => { + user: IUserPreferences; + success: boolean; + }; + }; }; diff --git a/app/lib/rocketchat/services/restApi.ts b/app/lib/rocketchat/services/restApi.ts index 0ccf22034e5..d71ebbefb6b 100644 --- a/app/lib/rocketchat/services/restApi.ts +++ b/app/lib/rocketchat/services/restApi.ts @@ -1,6 +1,7 @@ import sdk from './sdk'; import { TEAM_TYPE } from '../../../definitions/ITeam'; import roomTypeToApiType, { RoomTypes } from '../methods/roomTypeToApiType'; +import { INotificationPreferences } from '../../../definitions'; export const createChannel = ({ name, @@ -301,10 +302,8 @@ export const reportMessage = (messageId: string): any => // @ts-ignore sdk.post('chat.reportMessage', { messageId, description: 'Message reported by user' }); -export const setUserPreferences = (userId: string, data: any): any => +export const setUserPreferences = (userId: string, data: Partial) => // RC 0.62.0 - // TODO: missing definitions from server - // @ts-ignore sdk.post('users.setPreferences', { userId, data }); export const setUserStatus = (status?: string, message?: string): any => diff --git a/app/views/UserNotificationPreferencesView/index.tsx b/app/views/UserNotificationPreferencesView/index.tsx index 8961cb38f79..79a68173234 100644 --- a/app/views/UserNotificationPreferencesView/index.tsx +++ b/app/views/UserNotificationPreferencesView/index.tsx @@ -106,10 +106,12 @@ class UserNotificationPreferencesView extends React.Component< const { user } = this.props; const { id } = user; const result = await RocketChat.setUserPreferences(id, params); - const { - user: { settings } - } = result; - this.setState({ preferences: settings.preferences }); + if (result.success) { + const { + user: { settings } + } = result; + this.setState({ preferences: settings.preferences }); + } }; render() { From 2bf746a23c1c5d3d9240baa84ed7b7c04adacdb0 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Fri, 25 Feb 2022 16:55:29 -0300 Subject: [PATCH 2/2] chore: minor tweak --- app/lib/rocketchat/services/restApi.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/lib/rocketchat/services/restApi.ts b/app/lib/rocketchat/services/restApi.ts index af09d4c4a48..585fb04f441 100644 --- a/app/lib/rocketchat/services/restApi.ts +++ b/app/lib/rocketchat/services/restApi.ts @@ -1,8 +1,7 @@ import sdk from './sdk'; import { TEAM_TYPE } from '../../../definitions/ITeam'; import roomTypeToApiType, { RoomTypes } from '../methods/roomTypeToApiType'; -import { SubscriptionType } from '../../../definitions'; -import { INotificationPreferences } from '../../../definitions'; +import { SubscriptionType, INotificationPreferences } from '../../../definitions'; export const createChannel = ({ name,