diff --git a/app/definitions/ILoggedUser.ts b/app/definitions/ILoggedUser.ts index d83f87fa5..b9864b2d4 100644 --- a/app/definitions/ILoggedUser.ts +++ b/app/definitions/ILoggedUser.ts @@ -1,6 +1,6 @@ import Model from '@nozbe/watermelondb/Model'; -import { IUserEmail } from './IUser'; +import { IUserEmail, IUserSettings } from './IUser'; import { UserStatus } from './UserStatus'; export interface ILoggedUser { @@ -18,16 +18,33 @@ export interface ILoggedUser { emails?: IUserEmail[]; roles?: string[]; avatarETag?: string; - isFromWebView: boolean; - showMessageInMainThread: boolean; - enableMessageParserEarlyAdoption: boolean; + showMessageInMainThread?: boolean; + isFromWebView?: boolean; + enableMessageParserEarlyAdoption?: boolean; } export interface ILoginResultFromServer { status: string; authToken: string; userId: string; - me: ILoggedUser; + me: { + id: string; + token: string; + username: string; + name: string; + language?: string; + status: UserStatus; + statusText?: string; + customFields?: { + [key: string]: any; + }; + statusLivechat?: string; + emails?: IUserEmail[]; + roles?: string[]; + avatarETag?: string; + isFromWebView?: boolean; + settings: IUserSettings; + }; } export type TLoggedUserModel = ILoggedUser & Model; diff --git a/app/lib/rocketchat/rocketchat.js b/app/lib/rocketchat/rocketchat.js index 2a068b1dd..5d39f760b 100644 --- a/app/lib/rocketchat/rocketchat.js +++ b/app/lib/rocketchat/rocketchat.js @@ -5,7 +5,7 @@ import { Rocketchat as RocketchatClient, settings as RocketChatSettings } from ' import { InteractionManager } from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; import { setActiveUsers } from '../../actions/activeUsers'; -import { connectRequest, connectSuccess, disconnect } from '../../actions/connect'; +import { connectRequest, connectSuccess, disconnect as disconnectAction } from '../../actions/connect'; import { encryptionInit } from '../../actions/encryption'; import { loginRequest, setUser } from '../../actions/login'; import { updatePermission } from '../../actions/permissions'; @@ -63,7 +63,17 @@ import getUserInfo from './services/getUserInfo'; // Services import sdk from './services/sdk'; import toggleFavorite from './services/toggleFavorite'; -import { login, loginTOTP, loginWithPassword, loginOAuthOrSso, getLoginServices, _determineAuthType } from './services/connect'; +import { + login, + loginTOTP, + loginWithPassword, + loginOAuthOrSso, + getLoginServices, + determineAuthType, + disconnect, + checkAndReopen, + abort +} from './services/connect'; import * as restAPis from './services/restApi'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; @@ -167,21 +177,9 @@ const RocketChat = { return listener && listener.stop(); }, // Abort all requests and create a new AbortController - abort() { - if (this.controller) { - this.controller.abort(); - if (this.sdk) { - this.sdk.abort(); - } - } - this.controller = new AbortController(); - }, - checkAndReopen() { - return this?.sdk?.checkAndReopen(); - }, - disconnect() { - this.sdk = sdk.disconnect(); - }, + abort, + checkAndReopen, + disconnect, connect({ server, user, logoutOnError = false }) { return new Promise(resolve => { if (this?.sdk?.client?.host === server) { @@ -257,7 +255,7 @@ const RocketChat = { }); this.closeListener = this.sdk.onStreamData('close', () => { - reduxStore.dispatch(disconnect()); + reduxStore.dispatch(disconnectAction()); }); this.usersListener = this.sdk.onStreamData( @@ -835,7 +833,7 @@ const RocketChat = { return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs); }, getLoginServices, - _determineAuthType, + determineAuthType, roomTypeToApiType, readThreads(tmid) { const serverVersion = reduxStore.getState().server.version; diff --git a/app/lib/rocketchat/services/connect.ts b/app/lib/rocketchat/services/connect.ts index b7058c2fd..0b0d84898 100644 --- a/app/lib/rocketchat/services/connect.ts +++ b/app/lib/rocketchat/services/connect.ts @@ -10,7 +10,7 @@ import { loginRequest, setLoginServices, setUser } from '../../../actions/login' import sdk from './sdk'; import I18n from '../../../i18n'; import { MIN_ROCKETCHAT_VERSION } from '../rocketchat'; -import { ICredentials, ILoggedUser, IRocketChat } from '../../../definitions'; +import { ICredentials, ILoggedUser } from '../../../definitions'; import { isIOS } from '../../../utils/deviceInfo'; interface IServices { @@ -22,10 +22,9 @@ interface IServices { service: string; } -async function login(this: IRocketChat, credentials: ICredentials, isFromWebView = false): Promise { - const sdk = this.shareSDK || this.sdk; +async function login(credentials: ICredentials, isFromWebView = false): Promise { // RC 0.64.0 - await sdk.login(credentials); + await sdk.current.login(credentials); const result = sdk.currentLogin?.result; if (result) { const user: ILoggedUser = { @@ -49,15 +48,10 @@ async function login(this: IRocketChat, credentials: ICredentials, isFromWebView } } -function loginTOTP( - this: IRocketChat, - params: ICredentials, - loginEmailPassword?: boolean, - isFromWebView = false -): Promise { +function loginTOTP(params: ICredentials, loginEmailPassword?: boolean, isFromWebView = false): Promise { return new Promise(async (resolve, reject) => { try { - const result = await this.login(params, isFromWebView); + const result = await login(params, isFromWebView); if (result) { return resolve(result); } @@ -78,11 +72,11 @@ function loginTOTP( params = { user, password }; } - return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword)); + return resolve(loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword)); } return resolve( - this.loginTOTP({ + loginTOTP({ totp: { login: { ...params @@ -102,7 +96,7 @@ function loginTOTP( }); } -function loginWithPassword(this: IRocketChat, { user, password }: { user: string; password: string }) { +function loginWithPassword({ user, password }: { user: string; password: string }) { let params: ICredentials = { user, password }; const state = store.getState(); @@ -121,11 +115,11 @@ function loginWithPassword(this: IRocketChat, { user, password }: { user: string }; } - return this.loginTOTP(params, true); + return loginTOTP(params, true); } -async function loginOAuthOrSso(this: IRocketChat, params: ICredentials, isFromWebView = true) { - const result = await this.loginTOTP(params, false, isFromWebView); +async function loginOAuthOrSso(params: ICredentials, isFromWebView = true) { + const result = await loginTOTP(params, false, isFromWebView); store.dispatch(loginRequest({ resume: result.token }, false, isFromWebView)); } @@ -209,7 +203,7 @@ async function getWebsocketInfo({ server }: { server: string }) { }; } -async function getLoginServices(this: IRocketChat, server: string) { +async function getLoginServices(server: string) { try { let loginServices = []; const loginServicesResult = await fetch(`${server}/api/v1/settings.oauth`).then(response => response.json()); @@ -220,7 +214,7 @@ async function getLoginServices(this: IRocketChat, server: string) { const loginServicesReducer = loginServices.reduce((ret: IServices[], item: IServices) => { const name = item.name || item.buttonLabelText || item.service; - const authType = this._determineAuthType(item); + const authType = determineAuthType(item); if (authType !== 'not_supported') { ret[name as unknown as number] = { ...item, name, authType }; @@ -238,7 +232,7 @@ async function getLoginServices(this: IRocketChat, server: string) { } } -function _determineAuthType(services: IServices) { +function determineAuthType(services: IServices) { const { name, custom, showButton = true, service } = services; const authName = name || service; @@ -275,5 +269,5 @@ export { getServerInfo, getWebsocketInfo, getLoginServices, - _determineAuthType + determineAuthType };