dehydrate abort, checkAndReopen and disconnect
This commit is contained in:
parent
5a4bfc8f54
commit
ab8ff58fba
|
@ -1,6 +1,6 @@
|
||||||
import Model from '@nozbe/watermelondb/Model';
|
import Model from '@nozbe/watermelondb/Model';
|
||||||
|
|
||||||
import { IUserEmail } from './IUser';
|
import { IUserEmail, IUserSettings } from './IUser';
|
||||||
import { UserStatus } from './UserStatus';
|
import { UserStatus } from './UserStatus';
|
||||||
|
|
||||||
export interface ILoggedUser {
|
export interface ILoggedUser {
|
||||||
|
@ -18,16 +18,33 @@ export interface ILoggedUser {
|
||||||
emails?: IUserEmail[];
|
emails?: IUserEmail[];
|
||||||
roles?: string[];
|
roles?: string[];
|
||||||
avatarETag?: string;
|
avatarETag?: string;
|
||||||
isFromWebView: boolean;
|
showMessageInMainThread?: boolean;
|
||||||
showMessageInMainThread: boolean;
|
isFromWebView?: boolean;
|
||||||
enableMessageParserEarlyAdoption: boolean;
|
enableMessageParserEarlyAdoption?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ILoginResultFromServer {
|
export interface ILoginResultFromServer {
|
||||||
status: string;
|
status: string;
|
||||||
authToken: string;
|
authToken: string;
|
||||||
userId: 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;
|
export type TLoggedUserModel = ILoggedUser & Model;
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Rocketchat as RocketchatClient, settings as RocketChatSettings } from '
|
||||||
import { InteractionManager } from 'react-native';
|
import { InteractionManager } from 'react-native';
|
||||||
import RNFetchBlob from 'rn-fetch-blob';
|
import RNFetchBlob from 'rn-fetch-blob';
|
||||||
import { setActiveUsers } from '../../actions/activeUsers';
|
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 { encryptionInit } from '../../actions/encryption';
|
||||||
import { loginRequest, setUser } from '../../actions/login';
|
import { loginRequest, setUser } from '../../actions/login';
|
||||||
import { updatePermission } from '../../actions/permissions';
|
import { updatePermission } from '../../actions/permissions';
|
||||||
|
@ -63,7 +63,17 @@ import getUserInfo from './services/getUserInfo';
|
||||||
// Services
|
// Services
|
||||||
import sdk from './services/sdk';
|
import sdk from './services/sdk';
|
||||||
import toggleFavorite from './services/toggleFavorite';
|
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';
|
import * as restAPis from './services/restApi';
|
||||||
|
|
||||||
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
||||||
|
@ -167,21 +177,9 @@ const RocketChat = {
|
||||||
return listener && listener.stop();
|
return listener && listener.stop();
|
||||||
},
|
},
|
||||||
// Abort all requests and create a new AbortController
|
// Abort all requests and create a new AbortController
|
||||||
abort() {
|
abort,
|
||||||
if (this.controller) {
|
checkAndReopen,
|
||||||
this.controller.abort();
|
disconnect,
|
||||||
if (this.sdk) {
|
|
||||||
this.sdk.abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.controller = new AbortController();
|
|
||||||
},
|
|
||||||
checkAndReopen() {
|
|
||||||
return this?.sdk?.checkAndReopen();
|
|
||||||
},
|
|
||||||
disconnect() {
|
|
||||||
this.sdk = sdk.disconnect();
|
|
||||||
},
|
|
||||||
connect({ server, user, logoutOnError = false }) {
|
connect({ server, user, logoutOnError = false }) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
if (this?.sdk?.client?.host === server) {
|
if (this?.sdk?.client?.host === server) {
|
||||||
|
@ -257,7 +255,7 @@ const RocketChat = {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.closeListener = this.sdk.onStreamData('close', () => {
|
this.closeListener = this.sdk.onStreamData('close', () => {
|
||||||
reduxStore.dispatch(disconnect());
|
reduxStore.dispatch(disconnectAction());
|
||||||
});
|
});
|
||||||
|
|
||||||
this.usersListener = this.sdk.onStreamData(
|
this.usersListener = this.sdk.onStreamData(
|
||||||
|
@ -835,7 +833,7 @@ const RocketChat = {
|
||||||
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
|
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
|
||||||
},
|
},
|
||||||
getLoginServices,
|
getLoginServices,
|
||||||
_determineAuthType,
|
determineAuthType,
|
||||||
roomTypeToApiType,
|
roomTypeToApiType,
|
||||||
readThreads(tmid) {
|
readThreads(tmid) {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
const serverVersion = reduxStore.getState().server.version;
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { loginRequest, setLoginServices, setUser } from '../../../actions/login'
|
||||||
import sdk from './sdk';
|
import sdk from './sdk';
|
||||||
import I18n from '../../../i18n';
|
import I18n from '../../../i18n';
|
||||||
import { MIN_ROCKETCHAT_VERSION } from '../rocketchat';
|
import { MIN_ROCKETCHAT_VERSION } from '../rocketchat';
|
||||||
import { ICredentials, ILoggedUser, IRocketChat } from '../../../definitions';
|
import { ICredentials, ILoggedUser } from '../../../definitions';
|
||||||
import { isIOS } from '../../../utils/deviceInfo';
|
import { isIOS } from '../../../utils/deviceInfo';
|
||||||
|
|
||||||
interface IServices {
|
interface IServices {
|
||||||
|
@ -22,10 +22,9 @@ interface IServices {
|
||||||
service: string;
|
service: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(this: IRocketChat, credentials: ICredentials, isFromWebView = false): Promise<ILoggedUser | undefined> {
|
async function login(credentials: ICredentials, isFromWebView = false): Promise<ILoggedUser | undefined> {
|
||||||
const sdk = this.shareSDK || this.sdk;
|
|
||||||
// RC 0.64.0
|
// RC 0.64.0
|
||||||
await sdk.login(credentials);
|
await sdk.current.login(credentials);
|
||||||
const result = sdk.currentLogin?.result;
|
const result = sdk.currentLogin?.result;
|
||||||
if (result) {
|
if (result) {
|
||||||
const user: ILoggedUser = {
|
const user: ILoggedUser = {
|
||||||
|
@ -49,15 +48,10 @@ async function login(this: IRocketChat, credentials: ICredentials, isFromWebView
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loginTOTP(
|
function loginTOTP(params: ICredentials, loginEmailPassword?: boolean, isFromWebView = false): Promise<ILoggedUser> {
|
||||||
this: IRocketChat,
|
|
||||||
params: ICredentials,
|
|
||||||
loginEmailPassword?: boolean,
|
|
||||||
isFromWebView = false
|
|
||||||
): Promise<ILoggedUser> {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const result = await this.login(params, isFromWebView);
|
const result = await login(params, isFromWebView);
|
||||||
if (result) {
|
if (result) {
|
||||||
return resolve(result);
|
return resolve(result);
|
||||||
}
|
}
|
||||||
|
@ -78,11 +72,11 @@ function loginTOTP(
|
||||||
params = { user, password };
|
params = { user, password };
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
return resolve(loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(
|
return resolve(
|
||||||
this.loginTOTP({
|
loginTOTP({
|
||||||
totp: {
|
totp: {
|
||||||
login: {
|
login: {
|
||||||
...params
|
...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 };
|
let params: ICredentials = { user, password };
|
||||||
const state = store.getState();
|
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) {
|
async function loginOAuthOrSso(params: ICredentials, isFromWebView = true) {
|
||||||
const result = await this.loginTOTP(params, false, isFromWebView);
|
const result = await loginTOTP(params, false, isFromWebView);
|
||||||
store.dispatch(loginRequest({ resume: result.token }, 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 {
|
try {
|
||||||
let loginServices = [];
|
let loginServices = [];
|
||||||
const loginServicesResult = await fetch(`${server}/api/v1/settings.oauth`).then(response => response.json());
|
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 loginServicesReducer = loginServices.reduce((ret: IServices[], item: IServices) => {
|
||||||
const name = item.name || item.buttonLabelText || item.service;
|
const name = item.name || item.buttonLabelText || item.service;
|
||||||
const authType = this._determineAuthType(item);
|
const authType = determineAuthType(item);
|
||||||
|
|
||||||
if (authType !== 'not_supported') {
|
if (authType !== 'not_supported') {
|
||||||
ret[name as unknown as number] = { ...item, name, authType };
|
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 { name, custom, showButton = true, service } = services;
|
||||||
|
|
||||||
const authName = name || service;
|
const authName = name || service;
|
||||||
|
@ -275,5 +269,5 @@ export {
|
||||||
getServerInfo,
|
getServerInfo,
|
||||||
getWebsocketInfo,
|
getWebsocketInfo,
|
||||||
getLoginServices,
|
getLoginServices,
|
||||||
_determineAuthType
|
determineAuthType
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue