2022-02-10 12:10:42 +00:00
|
|
|
import { Rocketchat } from '@rocket.chat/sdk';
|
|
|
|
import EJSON from 'ejson';
|
2022-02-14 16:20:29 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2022-02-10 12:10:42 +00:00
|
|
|
|
|
|
|
import { twoFactor } from '../../../utils/twoFactor';
|
2022-02-14 16:20:29 +00:00
|
|
|
import { useSsl } from '../../../utils/url';
|
|
|
|
import reduxStore from '../../createStore';
|
2022-02-15 18:50:55 +00:00
|
|
|
import { Serialized, MatchPathPattern, OperationParams, PathFor, ResultFor } from '../../../definitions/rest/helpers';
|
2022-02-10 12:10:42 +00:00
|
|
|
|
|
|
|
class Sdk {
|
|
|
|
private sdk: typeof Rocketchat;
|
|
|
|
private code: any;
|
|
|
|
|
|
|
|
// TODO: We need to stop returning the SDK after all methods are dehydrated
|
|
|
|
initialize(server: string) {
|
|
|
|
this.code = null;
|
|
|
|
|
|
|
|
// The app can't reconnect if reopen interval is 5s while in development
|
|
|
|
this.sdk = new Rocketchat({ host: server, protocol: 'ddp', useSsl: useSsl(server), reopen: __DEV__ ? 20000 : 5000 });
|
|
|
|
return this.sdk;
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:23:13 +00:00
|
|
|
get current() {
|
|
|
|
return this.sdk;
|
|
|
|
}
|
|
|
|
|
2022-02-10 12:10:42 +00:00
|
|
|
/**
|
|
|
|
* TODO: evaluate the need for assigning "null" to this.sdk
|
|
|
|
* I'm returning "null" because we need to remove both instances of this.sdk here and on rocketchat.js
|
|
|
|
*/
|
|
|
|
disconnect() {
|
|
|
|
if (this.sdk) {
|
|
|
|
this.sdk.disconnect();
|
|
|
|
this.sdk = null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-14 16:20:29 +00:00
|
|
|
get<TPath extends PathFor<'GET'>>(
|
|
|
|
endpoint: TPath,
|
|
|
|
params: void extends OperationParams<'GET', MatchPathPattern<TPath>>
|
|
|
|
? void
|
|
|
|
: Serialized<OperationParams<'GET', MatchPathPattern<TPath>>> = undefined as void extends OperationParams<
|
|
|
|
'GET',
|
|
|
|
MatchPathPattern<TPath>
|
|
|
|
>
|
|
|
|
? void
|
|
|
|
: Serialized<OperationParams<'GET', MatchPathPattern<TPath>>>
|
2022-02-15 18:50:55 +00:00
|
|
|
): Promise<Serialized<ResultFor<'GET', MatchPathPattern<TPath>>>> {
|
2022-02-14 16:20:29 +00:00
|
|
|
return this.sdk.get(endpoint, params);
|
2022-02-10 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 16:20:29 +00:00
|
|
|
post<TPath extends PathFor<'POST'>>(
|
|
|
|
endpoint: TPath,
|
|
|
|
params: void extends OperationParams<'POST', MatchPathPattern<TPath>>
|
|
|
|
? void
|
|
|
|
: Serialized<OperationParams<'POST', MatchPathPattern<TPath>>> = undefined as void extends OperationParams<
|
|
|
|
'POST',
|
|
|
|
MatchPathPattern<TPath>
|
|
|
|
>
|
|
|
|
? void
|
|
|
|
: Serialized<OperationParams<'POST', MatchPathPattern<TPath>>>
|
|
|
|
): Promise<ResultFor<'POST', MatchPathPattern<TPath>>> {
|
2022-02-10 12:10:42 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-02-14 16:20:29 +00:00
|
|
|
const isMethodCall = endpoint?.startsWith('method.call/');
|
2022-02-10 12:10:42 +00:00
|
|
|
try {
|
2022-02-14 16:20:29 +00:00
|
|
|
const result = await this.sdk.post(endpoint, params);
|
2022-02-10 12:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* if API_Use_REST_For_DDP_Calls is enabled and it's a method call,
|
|
|
|
* responses have a different object structure
|
|
|
|
*/
|
|
|
|
if (isMethodCall) {
|
|
|
|
const response = JSON.parse(result.message);
|
|
|
|
if (response?.error) {
|
|
|
|
throw response.error;
|
|
|
|
}
|
|
|
|
return resolve(response.result);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
} catch (e: any) {
|
|
|
|
const errorType = isMethodCall ? e?.error : e?.data?.errorType;
|
|
|
|
const totpInvalid = 'totp-invalid';
|
|
|
|
const totpRequired = 'totp-required';
|
|
|
|
if ([totpInvalid, totpRequired].includes(errorType)) {
|
|
|
|
const { details } = isMethodCall ? e : e?.data;
|
|
|
|
try {
|
|
|
|
await twoFactor({ method: details?.method, invalid: errorType === totpInvalid });
|
2022-02-14 16:20:29 +00:00
|
|
|
return resolve(this.post(endpoint, params));
|
2022-02-10 12:10:42 +00:00
|
|
|
} catch {
|
|
|
|
// twoFactor was canceled
|
2022-02-14 16:20:29 +00:00
|
|
|
return resolve({} as any);
|
2022-02-10 12:10:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
methodCall(...args: any[]): Promise<any> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const result = await this.sdk?.methodCall(...args, this.code || '');
|
|
|
|
return resolve(result);
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.error && (e.error === 'totp-required' || e.error === 'totp-invalid')) {
|
|
|
|
const { details } = e;
|
|
|
|
try {
|
|
|
|
this.code = await twoFactor({ method: details?.method, invalid: e.error === 'totp-invalid' });
|
|
|
|
return resolve(this.methodCall(...args));
|
|
|
|
} catch {
|
|
|
|
// twoFactor was canceled
|
|
|
|
return resolve({});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
methodCallWrapper(method: string, ...params: any[]): Promise<any> {
|
|
|
|
const { API_Use_REST_For_DDP_Calls } = reduxStore.getState().settings;
|
|
|
|
const { user } = reduxStore.getState().login;
|
|
|
|
if (API_Use_REST_For_DDP_Calls) {
|
|
|
|
const url = isEmpty(user) ? 'method.callAnon' : 'method.call';
|
2022-02-14 16:20:29 +00:00
|
|
|
// @ts-ignore
|
2022-02-10 12:10:42 +00:00
|
|
|
return this.post(`${url}/${method}`, {
|
|
|
|
message: EJSON.stringify({ method, params })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const parsedParams = params.map(param => {
|
|
|
|
if (param instanceof Date) {
|
|
|
|
return { $date: new Date(param).getTime() };
|
|
|
|
}
|
|
|
|
return param;
|
|
|
|
});
|
|
|
|
return this.methodCall(method, ...parsedParams);
|
|
|
|
}
|
2022-02-14 19:23:13 +00:00
|
|
|
|
|
|
|
subscribe(...args: any[]) {
|
|
|
|
return this.sdk.subscribe(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeRaw(...args: any[]) {
|
|
|
|
return this.sdk.subscribeRaw(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeRoom(...args: any[]) {
|
|
|
|
return this.sdk.subscribeRoom(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe(subscription: any[]) {
|
|
|
|
return this.sdk.unsubscribe(subscription);
|
|
|
|
}
|
|
|
|
|
|
|
|
onStreamData(...args: any[]) {
|
|
|
|
return this.sdk.onStreamData(...args);
|
|
|
|
}
|
2022-02-10 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sdk = new Sdk();
|
|
|
|
|
|
|
|
export default sdk;
|