2019-03-27 20:06:57 +00:00
|
|
|
|
import { AsyncStorage, InteractionManager } from 'react-native';
|
2018-12-21 10:55:35 +00:00
|
|
|
|
import semver from 'semver';
|
2020-03-18 21:26:17 +00:00
|
|
|
|
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';
|
2019-07-15 17:24:48 +00:00
|
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2019-10-07 20:56:30 +00:00
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2019-09-03 19:27:57 +00:00
|
|
|
|
import * as FileSystem from 'expo-file-system';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
|
import reduxStore from './createStore';
|
2018-06-18 13:30:36 +00:00
|
|
|
|
import defaultSettings from '../constants/settings';
|
2017-12-13 15:00:26 +00:00
|
|
|
|
import messagesStatus from '../constants/messagesStatus';
|
2019-10-07 20:56:30 +00:00
|
|
|
|
import database from './database';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
import log from '../utils/log';
|
2019-02-07 15:48:10 +00:00
|
|
|
|
import { isIOS, getBundleId } from '../utils/deviceInfo';
|
2019-09-03 19:27:57 +00:00
|
|
|
|
import { extractHostname } from '../utils/server';
|
2020-03-18 21:26:17 +00:00
|
|
|
|
import fetch, { BASIC_AUTH_KEY } from '../utils/fetch';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
|
import { setUser, setLoginServices, loginRequest } from '../actions/login';
|
2018-12-05 20:52:08 +00:00
|
|
|
|
import { disconnect, connectSuccess, connectRequest } from '../actions/connect';
|
2019-07-29 20:44:39 +00:00
|
|
|
|
import {
|
|
|
|
|
shareSelectServer, shareSetUser
|
|
|
|
|
} from '../actions/share';
|
2017-08-15 19:28:46 +00:00
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
|
import subscribeRooms from './methods/subscriptions/rooms';
|
2020-03-18 21:26:17 +00:00
|
|
|
|
import getUsersPresence, { getUserPresence, subscribeUsersPresence } from './methods/getUsersPresence';
|
2017-08-11 18:18:09 +00:00
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
|
import protectedFunction from './methods/helpers/protectedFunction';
|
|
|
|
|
import readMessages from './methods/readMessages';
|
2020-03-18 21:26:17 +00:00
|
|
|
|
import getSettings, { getLoginSettings, setSettings } from './methods/getSettings';
|
2018-02-19 21:15:31 +00:00
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
|
import getRooms from './methods/getRooms';
|
|
|
|
|
import getPermissions from './methods/getPermissions';
|
2019-10-07 20:56:30 +00:00
|
|
|
|
import { getCustomEmojis, setCustomEmojis } from './methods/getCustomEmojis';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
import getSlashCommands from './methods/getSlashCommands';
|
2019-04-26 21:15:25 +00:00
|
|
|
|
import getRoles from './methods/getRoles';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
import canOpenRoom from './methods/canOpenRoom';
|
2020-02-19 19:43:47 +00:00
|
|
|
|
import triggerBlockAction, { triggerSubmitView, triggerCancel } from './methods/actions';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
|
|
import loadMessagesForRoom from './methods/loadMessagesForRoom';
|
|
|
|
|
import loadMissedMessages from './methods/loadMissedMessages';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
import loadThreadMessages from './methods/loadThreadMessages';
|
2018-02-19 21:15:31 +00:00
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
|
import sendMessage, { sendMessageCall } from './methods/sendMessage';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
import { sendFileMessage, cancelUpload, isUploadActive } from './methods/sendFileMessage';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
import callJitsi from './methods/callJitsi';
|
|
|
|
|
|
2019-06-10 16:23:19 +00:00
|
|
|
|
import { getDeviceToken } from '../notifications/push';
|
2019-07-15 17:24:48 +00:00
|
|
|
|
import { SERVERS, SERVER_URL } from '../constants/userDefaults';
|
2019-10-07 20:56:30 +00:00
|
|
|
|
import { setActiveUsers } from '../actions/activeUsers';
|
2019-12-11 23:01:12 +00:00
|
|
|
|
import I18n from '../i18n';
|
2020-04-07 13:26:19 +00:00
|
|
|
|
import { twoFactor } from '../utils/twoFactor';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
2018-09-04 14:29:20 +00:00
|
|
|
|
const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY';
|
2019-12-11 23:01:12 +00:00
|
|
|
|
export const THEME_PREFERENCES_KEY = 'RC_THEME_PREFERENCES_KEY';
|
2019-09-03 19:27:57 +00:00
|
|
|
|
export const CRASH_REPORT_KEY = 'RC_CRASH_REPORT_KEY';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
const returnAnArray = obj => obj || [];
|
2019-04-26 21:15:25 +00:00
|
|
|
|
const MIN_ROCKETCHAT_VERSION = '0.70.0';
|
2018-02-19 21:15:31 +00:00
|
|
|
|
|
2019-05-28 16:52:26 +00:00
|
|
|
|
const STATUSES = ['offline', 'online', 'away', 'busy'];
|
|
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
|
const RocketChat = {
|
2017-11-13 13:53:45 +00:00
|
|
|
|
TOKEN_KEY,
|
2019-10-07 20:56:30 +00:00
|
|
|
|
callJitsi,
|
2019-09-03 19:27:57 +00:00
|
|
|
|
async subscribeRooms() {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
if (!this.roomsSub) {
|
|
|
|
|
try {
|
|
|
|
|
this.roomsSub = await subscribeRooms.call(this);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
|
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
|
}
|
2019-09-03 19:27:57 +00:00
|
|
|
|
},
|
2018-05-07 20:43:26 +00:00
|
|
|
|
canOpenRoom,
|
2018-05-24 20:17:45 +00:00
|
|
|
|
createChannel({
|
|
|
|
|
name, users, type, readOnly, broadcast
|
|
|
|
|
}) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall(type ? 'createPrivateGroup' : 'createChannel', name, users, readOnly, {}, { broadcast });
|
2017-08-10 16:25:50 +00:00
|
|
|
|
},
|
2017-08-13 01:35:09 +00:00
|
|
|
|
async getUserToken() {
|
|
|
|
|
try {
|
2019-07-15 17:24:48 +00:00
|
|
|
|
return await RNUserDefaults.get(TOKEN_KEY);
|
2017-08-13 01:35:09 +00:00
|
|
|
|
} catch (error) {
|
2019-07-15 17:24:48 +00:00
|
|
|
|
console.warn(`RNUserDefaults error: ${ error.message }`);
|
2017-08-13 01:35:09 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-12-11 23:01:12 +00:00
|
|
|
|
async getWebsocketInfo({ server }) {
|
|
|
|
|
// Use useSsl: false only if server url starts with http://
|
|
|
|
|
const useSsl = !/http:\/\//.test(server);
|
|
|
|
|
|
|
|
|
|
const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await sdk.connect();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.message && err.message.includes('400')) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Websocket_disabled',
|
|
|
|
|
messageOptions: {
|
|
|
|
|
contact: I18n.t('Contact_your_server_admin')
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sdk.disconnect();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true
|
|
|
|
|
};
|
|
|
|
|
},
|
2019-04-17 17:01:03 +00:00
|
|
|
|
async getServerInfo(server) {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
const notRCServer = {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Not_RC_Server',
|
|
|
|
|
messageOptions: {
|
|
|
|
|
contact: I18n.t('Contact_your_server_admin')
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-08-01 19:35:06 +00:00
|
|
|
|
try {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
const result = await fetch(`${ server }/api/info`).then(async(response) => {
|
|
|
|
|
let res = notRCServer;
|
|
|
|
|
try {
|
|
|
|
|
res = await response.json();
|
|
|
|
|
if (!(res && res.success)) {
|
|
|
|
|
return notRCServer;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
|
if (result.success) {
|
|
|
|
|
if (semver.lt(result.version, MIN_ROCKETCHAT_VERSION)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Invalid_server_version',
|
|
|
|
|
messageOptions: {
|
2019-04-17 17:01:03 +00:00
|
|
|
|
currentVersion: result.version,
|
2018-12-21 10:55:35 +00:00
|
|
|
|
minVersion: MIN_ROCKETCHAT_VERSION
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
2019-12-11 23:01:12 +00:00
|
|
|
|
return result;
|
2018-08-01 19:35:06 +00:00
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
|
log(e);
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
return {
|
|
|
|
|
success: false,
|
2019-12-11 23:01:12 +00:00
|
|
|
|
message: 'The_URL_is_invalid',
|
|
|
|
|
messageOptions: {
|
|
|
|
|
contact: I18n.t('Contact_your_server_admin')
|
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
stopListener(listener) {
|
|
|
|
|
return listener && listener.stop();
|
2017-12-08 19:13:21 +00:00
|
|
|
|
},
|
2019-12-11 23:01:12 +00:00
|
|
|
|
connect({ server, user, logoutOnError = false }) {
|
2019-06-05 19:11:29 +00:00
|
|
|
|
return new Promise((resolve) => {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
if (!this.sdk || this.sdk.client.host !== server) {
|
|
|
|
|
database.setActiveDB(server);
|
|
|
|
|
}
|
2019-06-05 19:11:29 +00:00
|
|
|
|
reduxStore.dispatch(connectRequest());
|
2017-11-19 02:44:55 +00:00
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
if (this.connectTimeout) {
|
|
|
|
|
clearTimeout(this.connectTimeout);
|
|
|
|
|
}
|
2019-02-12 16:14:11 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
if (this.connectedListener) {
|
|
|
|
|
this.connectedListener.then(this.stopListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.closeListener) {
|
|
|
|
|
this.closeListener.then(this.stopListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.usersListener) {
|
|
|
|
|
this.usersListener.then(this.stopListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.notifyLoggedListener) {
|
|
|
|
|
this.notifyLoggedListener.then(this.stopListener);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
|
if (this.roomsSub) {
|
|
|
|
|
this.roomsSub.stop();
|
2020-04-07 13:26:19 +00:00
|
|
|
|
this.roomsSub = null;
|
2019-06-17 13:57:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
if (this.sdk) {
|
|
|
|
|
this.sdk.disconnect();
|
|
|
|
|
this.sdk = null;
|
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
|
2020-04-07 13:26:19 +00:00
|
|
|
|
if (this.code) {
|
|
|
|
|
this.code = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
// Use useSsl: false only if server url starts with http://
|
|
|
|
|
const useSsl = !/http:\/\//.test(server);
|
|
|
|
|
|
|
|
|
|
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
|
|
|
|
|
this.getSettings();
|
|
|
|
|
|
2020-04-07 13:26:19 +00:00
|
|
|
|
const sdkConnect = () => this.sdk.connect()
|
2019-06-05 19:11:29 +00:00
|
|
|
|
.then(() => {
|
|
|
|
|
if (user && user.token) {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
|
2019-06-05 19:11:29 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.log('connect error', err);
|
|
|
|
|
|
|
|
|
|
// when `connect` raises an error, we try again in 10 seconds
|
|
|
|
|
this.connectTimeout = setTimeout(() => {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
sdkConnect();
|
2019-06-05 19:11:29 +00:00
|
|
|
|
}, 10000);
|
|
|
|
|
});
|
2019-02-07 15:48:10 +00:00
|
|
|
|
|
2020-04-07 13:26:19 +00:00
|
|
|
|
sdkConnect();
|
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
this.connectedListener = this.sdk.onStreamData('connected', () => {
|
2019-06-05 19:11:29 +00:00
|
|
|
|
reduxStore.dispatch(connectSuccess());
|
2019-02-07 15:48:10 +00:00
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
this.closeListener = this.sdk.onStreamData('close', () => {
|
2019-06-05 19:11:29 +00:00
|
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
|
});
|
2019-05-28 16:52:26 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
this.usersListener = this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
|
2019-06-05 19:11:29 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
this.notifyLoggedListener = this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
|
2019-06-05 19:11:29 +00:00
|
|
|
|
const { eventName } = ddpMessage.fields;
|
|
|
|
|
if (eventName === 'user-status') {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
this.activeUsers = this.activeUsers || {};
|
|
|
|
|
if (!this._setUserTimer) {
|
|
|
|
|
this._setUserTimer = setTimeout(() => {
|
|
|
|
|
const activeUsersBatch = this.activeUsers;
|
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsersBatch));
|
|
|
|
|
});
|
|
|
|
|
this._setUserTimer = null;
|
|
|
|
|
return this.activeUsers = {};
|
|
|
|
|
}, 10000);
|
|
|
|
|
}
|
2019-06-05 19:11:29 +00:00
|
|
|
|
const userStatus = ddpMessage.fields.args[0];
|
2020-04-07 13:26:19 +00:00
|
|
|
|
const [id,, status, statusText] = userStatus;
|
|
|
|
|
this.activeUsers[id] = { status: STATUSES[status], statusText };
|
2019-10-07 20:56:30 +00:00
|
|
|
|
|
|
|
|
|
const { user: loggedUser } = reduxStore.getState().login;
|
|
|
|
|
if (loggedUser && loggedUser.id === id) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
reduxStore.dispatch(setUser({ status: STATUSES[status], statusText }));
|
2019-06-05 19:11:29 +00:00
|
|
|
|
}
|
2019-05-28 16:52:26 +00:00
|
|
|
|
}
|
2019-06-05 19:11:29 +00:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
2018-11-16 11:06:29 +00:00
|
|
|
|
},
|
2017-11-24 20:44:52 +00:00
|
|
|
|
|
2019-07-29 20:44:39 +00:00
|
|
|
|
async shareExtensionInit(server) {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
database.setShareDB(server);
|
2019-07-29 20:44:39 +00:00
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
|
if (this.shareSDK) {
|
|
|
|
|
this.shareSDK.disconnect();
|
|
|
|
|
this.shareSDK = null;
|
2019-07-29 20:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use useSsl: false only if server url starts with http://
|
|
|
|
|
const useSsl = !/http:\/\//.test(server);
|
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
|
this.shareSDK = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
|
2019-07-29 20:44:39 +00:00
|
|
|
|
|
|
|
|
|
// set Server
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const serversDB = database.servers;
|
2019-07-29 20:44:39 +00:00
|
|
|
|
reduxStore.dispatch(shareSelectServer(server));
|
|
|
|
|
|
|
|
|
|
// set User info
|
2019-10-07 20:56:30 +00:00
|
|
|
|
try {
|
|
|
|
|
const userId = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
|
|
|
|
const userCollections = serversDB.collections.get('users');
|
|
|
|
|
let user = null;
|
|
|
|
|
if (userId) {
|
|
|
|
|
const userRecord = await userCollections.find(userId);
|
|
|
|
|
user = {
|
|
|
|
|
id: userRecord.id,
|
|
|
|
|
token: userRecord.token,
|
2020-04-13 19:34:05 +00:00
|
|
|
|
username: userRecord.username,
|
|
|
|
|
roles: userRecord.roles
|
2019-10-07 20:56:30 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2020-04-13 19:34:05 +00:00
|
|
|
|
reduxStore.dispatch(shareSetUser(user));
|
2019-10-07 20:56:30 +00:00
|
|
|
|
await RocketChat.login({ resume: user.token });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-12-11 23:01:12 +00:00
|
|
|
|
closeShareExtension() {
|
|
|
|
|
if (this.shareSDK) {
|
|
|
|
|
this.shareSDK.disconnect();
|
|
|
|
|
this.shareSDK = null;
|
|
|
|
|
}
|
|
|
|
|
database.share = null;
|
2020-04-13 19:34:05 +00:00
|
|
|
|
|
|
|
|
|
reduxStore.dispatch(shareSetUser(null));
|
2019-12-11 23:01:12 +00:00
|
|
|
|
},
|
2019-07-29 20:44:39 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
updateJitsiTimeout(rid) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('jitsi:updateTimeout', rid);
|
2019-07-29 20:44:39 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
register(credentials) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.50.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('users.register', credentials, false);
|
2017-11-07 16:28:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
setUsername(username) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('setUsername', username);
|
2017-11-07 16:28:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
|
forgotPassword(email) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.64.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('users.forgotPassword', { email }, false);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loginTOTP(params) {
|
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await this.login(params);
|
|
|
|
|
return resolve(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.data?.error && (e.data.error === 'totp-required' || e.data.error === 'totp-invalid')) {
|
|
|
|
|
const { details } = e.data;
|
|
|
|
|
try {
|
2020-04-13 19:34:05 +00:00
|
|
|
|
const code = await twoFactor({ method: details?.method || 'totp', invalid: e.data.error === 'totp-invalid' });
|
|
|
|
|
return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }));
|
2020-04-07 13:26:19 +00:00
|
|
|
|
} catch {
|
|
|
|
|
// twoFactor was canceled
|
|
|
|
|
return reject();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-10 13:42:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2020-04-07 13:26:19 +00:00
|
|
|
|
loginWithPassword({ user, password }) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
let params = { user, password };
|
2017-08-13 23:02:46 +00:00
|
|
|
|
const state = reduxStore.getState();
|
|
|
|
|
|
|
|
|
|
if (state.settings.LDAP_Enable) {
|
|
|
|
|
params = {
|
2019-01-31 11:48:28 +00:00
|
|
|
|
username: user,
|
|
|
|
|
ldapPass: password,
|
2017-08-13 23:02:46 +00:00
|
|
|
|
ldap: true,
|
|
|
|
|
ldapOptions: {}
|
|
|
|
|
};
|
|
|
|
|
} else if (state.settings.CROWD_Enable) {
|
|
|
|
|
params = {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
username: user,
|
|
|
|
|
crowdPassword: password,
|
2018-10-16 20:27:37 +00:00
|
|
|
|
crowd: true
|
2017-08-13 23:02:46 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
|
try {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.loginTOTP(params);
|
2018-10-15 20:22:42 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
|
async loginOAuthOrSso(params) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
try {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
const result = await this.login(params);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: result.token }));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
|
async login(params) {
|
|
|
|
|
try {
|
2019-12-11 23:01:12 +00:00
|
|
|
|
const sdk = this.shareSDK || this.sdk;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.64.0
|
2019-12-11 23:01:12 +00:00
|
|
|
|
await sdk.login(params);
|
|
|
|
|
const { result } = sdk.currentLogin;
|
2019-02-07 15:48:10 +00:00
|
|
|
|
const user = {
|
|
|
|
|
id: result.userId,
|
|
|
|
|
token: result.authToken,
|
|
|
|
|
username: result.me.username,
|
|
|
|
|
name: result.me.name,
|
|
|
|
|
language: result.me.language,
|
|
|
|
|
status: result.me.status,
|
2020-04-07 13:26:19 +00:00
|
|
|
|
statusText: result.me.statusText,
|
2019-02-07 15:48:10 +00:00
|
|
|
|
customFields: result.me.customFields,
|
2019-04-26 20:54:58 +00:00
|
|
|
|
emails: result.me.emails,
|
|
|
|
|
roles: result.me.roles
|
2019-02-07 15:48:10 +00:00
|
|
|
|
};
|
|
|
|
|
return user;
|
2018-10-15 20:22:42 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
async logout({ server }) {
|
2019-02-14 16:43:56 +00:00
|
|
|
|
if (this.roomsSub) {
|
|
|
|
|
this.roomsSub.stop();
|
2020-04-07 13:26:19 +00:00
|
|
|
|
this.roomsSub = null;
|
2019-02-14 16:43:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
|
if (this.activeUsersSubTimeout) {
|
|
|
|
|
clearTimeout(this.activeUsersSubTimeout);
|
|
|
|
|
this.activeUsersSubTimeout = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
await this.removePushToken();
|
2018-10-15 20:22:42 +00:00
|
|
|
|
} catch (error) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
console.log('logout -> removePushToken -> catch -> error', error);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
try {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.60.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
await this.sdk.logout();
|
2018-12-05 20:52:08 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('logout -> api logout -> catch -> error', error);
|
|
|
|
|
}
|
2019-02-07 15:48:10 +00:00
|
|
|
|
this.sdk = null;
|
2018-12-05 20:52:08 +00:00
|
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
|
try {
|
|
|
|
|
const servers = await RNUserDefaults.objectForKey(SERVERS);
|
|
|
|
|
await RNUserDefaults.setObjectForKey(SERVERS, servers && servers.filter(srv => srv[SERVER_URL] !== server));
|
2019-09-03 19:27:57 +00:00
|
|
|
|
// clear certificate for server - SSL Pinning
|
|
|
|
|
const certificate = await RNUserDefaults.objectForKey(extractHostname(server));
|
|
|
|
|
if (certificate && certificate.path) {
|
|
|
|
|
await RNUserDefaults.clear(extractHostname(server));
|
|
|
|
|
await FileSystem.deleteAsync(certificate.path);
|
|
|
|
|
}
|
2019-07-15 17:24:48 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('logout_rn_user_defaults', error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userId = await RNUserDefaults.get(`${ TOKEN_KEY }-${ server }`);
|
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
try {
|
|
|
|
|
const serversDB = database.servers;
|
|
|
|
|
await serversDB.action(async() => {
|
|
|
|
|
const usersCollection = serversDB.collections.get('users');
|
2019-12-11 23:01:12 +00:00
|
|
|
|
const userRecord = await usersCollection.find(userId);
|
|
|
|
|
const serverCollection = serversDB.collections.get('servers');
|
|
|
|
|
const serverRecord = await serverCollection.find(server);
|
|
|
|
|
await serversDB.batch(
|
|
|
|
|
userRecord.prepareDestroyPermanently(),
|
|
|
|
|
serverRecord.prepareDestroyPermanently()
|
|
|
|
|
);
|
2019-10-07 20:56:30 +00:00
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
2019-07-15 17:24:48 +00:00
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
await RNUserDefaults.clear('currentServer');
|
|
|
|
|
await RNUserDefaults.clear(TOKEN_KEY);
|
|
|
|
|
await RNUserDefaults.clear(`${ TOKEN_KEY }-${ server }`);
|
2020-03-18 21:26:17 +00:00
|
|
|
|
await RNUserDefaults.clear(`${ BASIC_AUTH_KEY }-${ server }`);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
|
2018-10-18 17:56:49 +00:00
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const db = database.active;
|
|
|
|
|
await db.action(() => db.unsafeResetDatabase());
|
2018-10-18 17:56:49 +00:00
|
|
|
|
} catch (error) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
console.log(error);
|
2018-10-18 17:56:49 +00:00
|
|
|
|
}
|
2018-10-15 20:22:42 +00:00
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
async clearCache({ server }) {
|
|
|
|
|
try {
|
|
|
|
|
const serversDB = database.servers;
|
|
|
|
|
await serversDB.action(async() => {
|
|
|
|
|
const serverCollection = serversDB.collections.get('servers');
|
|
|
|
|
const serverRecord = await serverCollection.find(server);
|
|
|
|
|
await serverRecord.update((s) => {
|
|
|
|
|
s.roomsUpdatedAt = null;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const db = database.active;
|
|
|
|
|
await db.action(() => db.unsafeResetDatabase());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
registerPushToken() {
|
2019-06-17 13:57:07 +00:00
|
|
|
|
return new Promise(async(resolve) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
const token = getDeviceToken();
|
|
|
|
|
if (token) {
|
2019-01-29 19:52:56 +00:00
|
|
|
|
const type = isIOS ? 'apn' : 'gcm';
|
2018-12-05 20:52:08 +00:00
|
|
|
|
const data = {
|
|
|
|
|
value: token,
|
|
|
|
|
type,
|
2019-02-07 15:48:10 +00:00
|
|
|
|
appName: getBundleId
|
2018-12-05 20:52:08 +00:00
|
|
|
|
};
|
2019-06-17 13:57:07 +00:00
|
|
|
|
try {
|
|
|
|
|
// RC 0.60.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
await this.post('push.token', data);
|
2019-06-17 13:57:07 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
}
|
|
|
|
|
return resolve();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
removePushToken() {
|
|
|
|
|
const token = getDeviceToken();
|
|
|
|
|
if (token) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.60.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.del('push.token', { token });
|
2018-07-10 13:40:32 +00:00
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return Promise.resolve();
|
2017-11-18 20:17:24 +00:00
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
|
loadMissedMessages,
|
|
|
|
|
loadMessagesForRoom,
|
2019-04-17 17:01:03 +00:00
|
|
|
|
loadThreadMessages,
|
2018-04-24 19:34:03 +00:00
|
|
|
|
sendMessage,
|
|
|
|
|
getRooms,
|
|
|
|
|
readMessages,
|
2019-12-11 23:01:12 +00:00
|
|
|
|
async resendMessage(message, tmid) {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const db = database.active;
|
2018-12-05 20:52:08 +00:00
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
await db.action(async() => {
|
|
|
|
|
await message.update((m) => {
|
|
|
|
|
m.status = messagesStatus.TEMP;
|
|
|
|
|
});
|
2018-12-05 20:52:08 +00:00
|
|
|
|
});
|
2019-12-11 23:01:12 +00:00
|
|
|
|
let m = {
|
|
|
|
|
id: message.id,
|
|
|
|
|
msg: message.msg,
|
|
|
|
|
subscription: { id: message.subscription.id }
|
|
|
|
|
};
|
|
|
|
|
if (tmid) {
|
|
|
|
|
m = {
|
|
|
|
|
...m,
|
|
|
|
|
tmid
|
|
|
|
|
};
|
2019-02-25 16:23:17 +00:00
|
|
|
|
}
|
2019-12-11 23:01:12 +00:00
|
|
|
|
await sendMessageCall.call(this, m);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
|
async search({ text, filterUsers = true, filterRooms = true }) {
|
|
|
|
|
const searchText = text.trim();
|
2018-09-28 20:17:49 +00:00
|
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
|
this.oldPromise('cancel');
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
|
if (searchText === '') {
|
|
|
|
|
delete this.oldPromise;
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const db = database.active;
|
|
|
|
|
let data = await db.collections.get('subscriptions').query(
|
|
|
|
|
Q.where('name', Q.like(`%${ Q.sanitizeLikeString(searchText) }%`))
|
|
|
|
|
).fetch();
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
|
|
if (filterUsers && !filterRooms) {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
data = data.filter(item => item.t === 'd');
|
2018-08-31 18:13:30 +00:00
|
|
|
|
} else if (!filterUsers && filterRooms) {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
data = data.filter(item => item.t !== 'd');
|
2018-08-31 18:13:30 +00:00
|
|
|
|
}
|
|
|
|
|
data = data.slice(0, 7);
|
|
|
|
|
|
2020-04-13 19:34:05 +00:00
|
|
|
|
data = data.map((sub) => {
|
|
|
|
|
if (sub.t !== 'd') {
|
|
|
|
|
return ({
|
|
|
|
|
rid: sub.rid,
|
|
|
|
|
name: sub.name,
|
|
|
|
|
fname: sub.fname,
|
|
|
|
|
t: sub.t,
|
|
|
|
|
search: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return sub;
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
|
const usernames = data.map(sub => sub.name);
|
|
|
|
|
try {
|
|
|
|
|
if (data.length < 7) {
|
|
|
|
|
const { users, rooms } = await Promise.race([
|
|
|
|
|
RocketChat.spotlight(searchText, usernames, { users: filterUsers, rooms: filterRooms }),
|
|
|
|
|
new Promise((resolve, reject) => this.oldPromise = reject)
|
|
|
|
|
]);
|
2020-02-19 19:43:47 +00:00
|
|
|
|
if (filterUsers) {
|
|
|
|
|
data = data.concat(users.map(user => ({
|
|
|
|
|
...user,
|
|
|
|
|
rid: user.username,
|
|
|
|
|
name: user.username,
|
|
|
|
|
t: 'd',
|
|
|
|
|
search: true
|
|
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
if (filterRooms) {
|
|
|
|
|
rooms.forEach((room) => {
|
|
|
|
|
// Check if it exists on local database
|
|
|
|
|
const index = data.findIndex(item => item.rid === room._id);
|
|
|
|
|
if (index === -1) {
|
|
|
|
|
data.push({
|
|
|
|
|
rid: room._id,
|
|
|
|
|
...room,
|
|
|
|
|
search: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
}
|
2018-09-28 20:17:49 +00:00
|
|
|
|
delete this.oldPromise;
|
2018-08-31 18:13:30 +00:00
|
|
|
|
return data;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn(e);
|
2018-09-28 20:17:49 +00:00
|
|
|
|
return data;
|
|
|
|
|
// return [];
|
2018-08-31 18:13:30 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
|
spotlight(search, usernames, type) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('spotlight', search, usernames, type);
|
2017-08-10 16:16:32 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
createDirectMessage(username) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('im.create', { username });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
createGroupChat() {
|
|
|
|
|
const { users } = reduxStore.getState().selectedUsers;
|
|
|
|
|
const usernames = users.map(u => u.name).join(',');
|
|
|
|
|
|
|
|
|
|
// RC 3.1.0
|
|
|
|
|
return this.post('im.create', { usernames });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
createDiscussion({
|
|
|
|
|
prid, pmid, t_name, reply, users
|
|
|
|
|
}) {
|
|
|
|
|
// RC 1.0.0
|
|
|
|
|
return this.post('rooms.createDiscussion', {
|
|
|
|
|
prid, pmid, t_name, reply, users
|
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
|
},
|
2020-04-07 13:26:19 +00:00
|
|
|
|
|
2019-06-20 19:02:50 +00:00
|
|
|
|
joinRoom(roomId, type) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
// TODO: join code
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2019-06-20 19:02:50 +00:00
|
|
|
|
if (type === 'p') {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('joinRoom', roomId);
|
2019-06-20 19:02:50 +00:00
|
|
|
|
}
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('channels.join', { roomId });
|
2017-08-10 20:09:54 +00:00
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
triggerBlockAction,
|
|
|
|
|
triggerSubmitView,
|
|
|
|
|
triggerCancel,
|
2018-07-17 19:10:27 +00:00
|
|
|
|
sendFileMessage,
|
|
|
|
|
cancelUpload,
|
|
|
|
|
isUploadActive,
|
2018-04-24 19:34:03 +00:00
|
|
|
|
getSettings,
|
2020-03-18 21:26:17 +00:00
|
|
|
|
getLoginSettings,
|
2019-12-11 23:01:12 +00:00
|
|
|
|
setSettings,
|
2018-04-24 19:34:03 +00:00
|
|
|
|
getPermissions,
|
2019-06-17 13:57:07 +00:00
|
|
|
|
getCustomEmojis,
|
2019-10-07 20:56:30 +00:00
|
|
|
|
setCustomEmojis,
|
2019-06-10 18:36:56 +00:00
|
|
|
|
getSlashCommands,
|
2019-04-26 21:15:25 +00:00
|
|
|
|
getRoles,
|
2017-11-19 02:44:55 +00:00
|
|
|
|
parseSettings: settings => settings.reduce((ret, item) => {
|
2020-03-18 21:26:17 +00:00
|
|
|
|
ret[item._id] = defaultSettings[item._id] && item[defaultSettings[item._id].type];
|
|
|
|
|
if (item._id === 'Hide_System_Messages') {
|
|
|
|
|
ret[item._id] = ret[item._id]
|
|
|
|
|
.reduce((array, value) => [...array, ...value === 'mute_unmute' ? ['user-muted', 'user-unmuted'] : [value]], []);
|
|
|
|
|
}
|
2017-11-19 02:44:55 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}, {}),
|
2017-11-19 14:57:50 +00:00
|
|
|
|
_prepareSettings(settings) {
|
|
|
|
|
return settings.map((setting) => {
|
2018-06-18 13:30:36 +00:00
|
|
|
|
setting[defaultSettings[setting._id].type] = setting.value;
|
2017-11-19 14:57:50 +00:00
|
|
|
|
return setting;
|
|
|
|
|
});
|
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
deleteMessage(messageId, rid) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.delete', { msgId: messageId, roomId: rid });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
|
|
|
|
editMessage(message) {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const { id, msg, rid } = message;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.update', { roomId: rid, msgId: id, text: msg });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2020-03-18 21:26:17 +00:00
|
|
|
|
markAsUnread({ messageId }) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('subscriptions.unread', { firstUnreadMessage: { _id: messageId } });
|
2020-03-18 21:26:17 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
toggleStarMessage(messageId, starred) {
|
|
|
|
|
if (starred) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.unStarMessage', { messageId });
|
2018-12-05 20:52:08 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.starMessage', { messageId });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
togglePinMessage(messageId, pinned) {
|
|
|
|
|
if (pinned) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.unPinMessage', { messageId });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.pinMessage', { messageId });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2019-05-16 13:40:50 +00:00
|
|
|
|
reportMessage(messageId) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.reportMessage', { messageId, description: 'Message reported by user' });
|
2019-05-16 13:40:50 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
async getRoom(rid) {
|
|
|
|
|
try {
|
|
|
|
|
const db = database.active;
|
|
|
|
|
const room = await db.collections.get('subscriptions').find(rid);
|
|
|
|
|
return Promise.resolve(room);
|
|
|
|
|
} catch (error) {
|
2017-11-21 17:05:14 +00:00
|
|
|
|
return Promise.reject(new Error('Room not found'));
|
|
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2019-05-29 21:19:12 +00:00
|
|
|
|
async getPermalinkMessage(message) {
|
2018-05-18 17:55:08 +00:00
|
|
|
|
let room;
|
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
room = await RocketChat.getRoom(message.subscription.id);
|
2018-05-18 17:55:08 +00:00
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
const { server } = reduxStore.getState().server;
|
2017-11-21 17:05:14 +00:00
|
|
|
|
const roomType = {
|
|
|
|
|
p: 'group',
|
|
|
|
|
c: 'channel',
|
|
|
|
|
d: 'direct'
|
|
|
|
|
}[room.t];
|
2019-10-07 20:56:30 +00:00
|
|
|
|
return `${ server }/${ roomType }/${ room.name }?msg=${ message.id }`;
|
2017-11-21 17:05:14 +00:00
|
|
|
|
},
|
2019-05-29 21:19:12 +00:00
|
|
|
|
getPermalinkChannel(channel) {
|
|
|
|
|
const { server } = reduxStore.getState().server;
|
|
|
|
|
const roomType = {
|
|
|
|
|
p: 'group',
|
|
|
|
|
c: 'channel',
|
|
|
|
|
d: 'direct'
|
|
|
|
|
}[channel.t];
|
|
|
|
|
return `${ server }/${ roomType }/${ channel.name }`;
|
|
|
|
|
},
|
2017-11-20 22:18:00 +00:00
|
|
|
|
subscribe(...args) {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.subscribe(...args);
|
2018-10-15 20:22:42 +00:00
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
subscribeRoom(...args) {
|
|
|
|
|
return this.sdk.subscribeRoom(...args);
|
|
|
|
|
},
|
2018-10-15 20:22:42 +00:00
|
|
|
|
unsubscribe(subscription) {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.unsubscribe(subscription);
|
2017-11-20 22:18:00 +00:00
|
|
|
|
},
|
2019-04-08 12:35:28 +00:00
|
|
|
|
onStreamData(...args) {
|
|
|
|
|
return this.sdk.onStreamData(...args);
|
|
|
|
|
},
|
2017-11-21 16:55:32 +00:00
|
|
|
|
emitTyping(room, t = true) {
|
|
|
|
|
const { login } = reduxStore.getState();
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('stream-notify-room', `${ room }/typing`, login.user.username, t);
|
2017-12-04 18:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceAway() {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('UserPresence:away');
|
2017-12-04 18:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceOnline() {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('UserPresence:online');
|
2017-12-05 19:57:44 +00:00
|
|
|
|
},
|
2020-04-07 13:26:19 +00:00
|
|
|
|
setUserStatus(status, message) {
|
|
|
|
|
// RC 1.2.0
|
|
|
|
|
return this.post('users.setStatus', { status, message });
|
2018-01-30 19:48:26 +00:00
|
|
|
|
},
|
|
|
|
|
setReaction(emoji, messageId) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.62.2
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.react', { emoji, messageId });
|
2018-02-19 21:19:39 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
toggleFavorite(roomId, favorite) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.64.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('rooms.favorite', { roomId, favorite });
|
2018-03-02 15:11:34 +00:00
|
|
|
|
},
|
2019-07-15 17:24:48 +00:00
|
|
|
|
toggleRead(read, roomId) {
|
|
|
|
|
if (read) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('subscriptions.unread', { roomId });
|
2019-07-15 17:24:48 +00:00
|
|
|
|
}
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('subscriptions.read', { rid: roomId });
|
2019-07-15 17:24:48 +00:00
|
|
|
|
},
|
2019-04-17 17:01:03 +00:00
|
|
|
|
getRoomMembers(rid, allUsers, skip = 0, limit = 10) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.42.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('getUsersOfRoom', rid, allUsers, { skip, limit });
|
2018-03-23 16:49:51 +00:00
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
getUserRoles() {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.27.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('getUserRoles');
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
getRoomCounters(roomId, t) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.65.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.get(`${ this.roomTypeToApiType(t) }.counters`, { roomId });
|
2018-12-05 20:52:08 +00:00
|
|
|
|
},
|
2019-04-08 12:35:28 +00:00
|
|
|
|
getChannelInfo(roomId) {
|
|
|
|
|
// RC 0.48.0
|
|
|
|
|
return this.sdk.get('channels.info', { roomId });
|
|
|
|
|
},
|
2019-04-29 16:03:52 +00:00
|
|
|
|
getUserInfo(userId) {
|
|
|
|
|
// RC 0.48.0
|
|
|
|
|
return this.sdk.get('users.info', { userId });
|
|
|
|
|
},
|
2019-09-03 19:27:57 +00:00
|
|
|
|
getRoomInfo(roomId) {
|
|
|
|
|
// RC 0.72.0
|
|
|
|
|
return this.sdk.get('rooms.info', { roomId });
|
|
|
|
|
},
|
2020-04-07 13:26:19 +00:00
|
|
|
|
|
2020-04-13 19:34:05 +00:00
|
|
|
|
getUidDirectMessage(room) {
|
|
|
|
|
const { id: userId } = reduxStore.getState().login.user;
|
|
|
|
|
|
2020-04-07 13:26:19 +00:00
|
|
|
|
// legacy method
|
|
|
|
|
if (!room.uids && room.rid && room.t === 'd') {
|
|
|
|
|
return room.rid.replace(userId, '').trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RocketChat.isGroupChat(room)) {
|
|
|
|
|
return false;
|
2018-03-29 17:55:37 +00:00
|
|
|
|
}
|
2020-04-07 13:26:19 +00:00
|
|
|
|
|
|
|
|
|
const me = room && room.uids && room.uids.find(uid => uid === userId);
|
|
|
|
|
const other = room && room.uids && room.uids.filter(uid => uid !== userId);
|
|
|
|
|
|
|
|
|
|
return other && other.length ? other[0] : me;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isGroupChat(room) {
|
|
|
|
|
return (room.uids && room.uids.length > 2) || (room.usernames && room.usernames.length > 2);
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2020-04-07 13:26:19 +00:00
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
|
toggleBlockUser(rid, blocked, block) {
|
|
|
|
|
if (block) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('blockUser', { rid, blocked });
|
2018-03-23 16:49:51 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('unblockUser', { rid, blocked });
|
2018-03-23 16:49:51 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
leaveRoom(roomId, t) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post(`${ this.roomTypeToApiType(t) }.leave`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2020-03-18 21:26:17 +00:00
|
|
|
|
deleteRoom(roomId, t) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post(`${ this.roomTypeToApiType(t) }.delete`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
|
toggleMuteUserInRoom(rid, username, mute) {
|
|
|
|
|
if (mute) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('muteUserInRoom', { rid, username });
|
2018-04-24 19:34:03 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('unmuteUserInRoom', { rid, username });
|
2018-04-24 19:34:03 +00:00
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
toggleArchiveRoom(roomId, t, archive) {
|
2018-03-29 17:55:37 +00:00
|
|
|
|
if (archive) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post(`${ this.roomTypeToApiType(t) }.archive`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post(`${ this.roomTypeToApiType(t) }.unarchive`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2019-07-15 17:24:48 +00:00
|
|
|
|
hideRoom(roomId, t) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post(`${ this.roomTypeToApiType(t) }.close`, { roomId });
|
2019-07-15 17:24:48 +00:00
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
saveRoomSettings(rid, params) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.55.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('saveRoomSettings', rid, params);
|
|
|
|
|
},
|
|
|
|
|
post(...args) {
|
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await this.sdk.post(...args);
|
|
|
|
|
return resolve(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.data && (e.data.errorType === 'totp-required' || e.data.errorType === 'totp-invalid')) {
|
|
|
|
|
const { details } = e.data;
|
|
|
|
|
try {
|
|
|
|
|
await twoFactor({ method: details?.method, invalid: e.data.errorType === 'totp-invalid' });
|
|
|
|
|
return resolve(this.post(...args));
|
|
|
|
|
} catch {
|
|
|
|
|
// twoFactor was canceled
|
|
|
|
|
return resolve({});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
methodCall(...args) {
|
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await this.sdk.methodCall(...args, this.code);
|
|
|
|
|
return resolve(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
sendEmailCode() {
|
|
|
|
|
const { username } = reduxStore.getState().login.user;
|
|
|
|
|
// RC 3.1.0
|
|
|
|
|
return this.post('users.2fa.sendEmailCode', { emailOrUsername: username });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2019-06-05 16:29:07 +00:00
|
|
|
|
saveUserProfile(data, customFields) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.62.2
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('users.updateOwnBasicInfo', { data, customFields });
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
saveUserPreferences(params) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('saveUserPreferences', params);
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
saveNotificationSettings(roomId, notifications) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.63.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('rooms.saveNotification', { roomId, notifications });
|
2018-04-24 19:34:03 +00:00
|
|
|
|
},
|
|
|
|
|
addUsersToRoom(rid) {
|
|
|
|
|
let { users } = reduxStore.getState().selectedUsers;
|
|
|
|
|
users = users.map(u => u.name);
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('addUsersToRoom', { rid, users });
|
2018-04-24 19:34:03 +00:00
|
|
|
|
},
|
2019-04-17 17:01:03 +00:00
|
|
|
|
getSingleMessage(msgId) {
|
|
|
|
|
// RC 0.57.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('getSingleMessage', msgId);
|
2019-04-17 17:01:03 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
async hasPermission(permissions, rid) {
|
|
|
|
|
const db = database.active;
|
|
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
|
|
|
|
const permissionsCollection = db.collections.get('permissions');
|
2019-04-26 20:54:58 +00:00
|
|
|
|
let roomRoles = [];
|
2018-12-21 10:55:35 +00:00
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
// get the room from database
|
|
|
|
|
const room = await subsCollection.find(rid);
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// get room roles
|
2020-04-13 19:34:05 +00:00
|
|
|
|
roomRoles = room.roles || [];
|
2018-12-21 10:55:35 +00:00
|
|
|
|
} catch (error) {
|
2019-10-07 20:56:30 +00:00
|
|
|
|
console.log('hasPermission -> Room not found');
|
|
|
|
|
return permissions.reduce((result, permission) => {
|
|
|
|
|
result[permission] = false;
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
// get permissions from database
|
|
|
|
|
try {
|
|
|
|
|
const permissionsFiltered = await permissionsCollection.query(Q.where('id', Q.oneOf(permissions))).fetch();
|
2020-04-13 19:34:05 +00:00
|
|
|
|
const shareUser = reduxStore.getState().share.user;
|
|
|
|
|
const loginUser = reduxStore.getState().login.user;
|
2019-10-07 20:56:30 +00:00
|
|
|
|
// get user roles on the server from redux
|
2020-04-13 19:34:05 +00:00
|
|
|
|
const userRoles = (shareUser.roles || loginUser.roles) || [];
|
2019-10-07 20:56:30 +00:00
|
|
|
|
// merge both roles
|
|
|
|
|
const mergedRoles = [...new Set([...roomRoles, ...userRoles])];
|
|
|
|
|
|
|
|
|
|
// return permissions in object format
|
|
|
|
|
// e.g. { 'edit-room': true, 'set-readonly': false }
|
|
|
|
|
return permissions.reduce((result, permission) => {
|
|
|
|
|
result[permission] = false;
|
|
|
|
|
const permissionFound = permissionsFiltered.find(p => p.id === permission);
|
|
|
|
|
if (permissionFound) {
|
|
|
|
|
result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
|
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
getAvatarSuggestion() {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('getAvatarSuggestion');
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
resetAvatar(userId) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.55.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('users.resetAvatar', { userId });
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
setAvatarFromService({ data, contentType = '', service = null }) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('setAvatarFromService', data, contentType, service);
|
2018-09-04 14:29:20 +00:00
|
|
|
|
},
|
2019-09-03 19:27:57 +00:00
|
|
|
|
async getAllowCrashReport() {
|
|
|
|
|
const allowCrashReport = await AsyncStorage.getItem(CRASH_REPORT_KEY);
|
|
|
|
|
if (allowCrashReport === null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return JSON.parse(allowCrashReport);
|
|
|
|
|
},
|
2018-09-04 14:29:20 +00:00
|
|
|
|
async getSortPreferences() {
|
2019-07-29 20:44:39 +00:00
|
|
|
|
const prefs = await RNUserDefaults.objectForKey(SORT_PREFS_KEY);
|
|
|
|
|
return prefs;
|
2018-09-04 14:29:20 +00:00
|
|
|
|
},
|
|
|
|
|
async saveSortPreference(param) {
|
|
|
|
|
try {
|
|
|
|
|
let prefs = await RocketChat.getSortPreferences();
|
|
|
|
|
prefs = { ...prefs, ...param };
|
2019-07-29 20:44:39 +00:00
|
|
|
|
return await RNUserDefaults.setObjectForKey(SORT_PREFS_KEY, prefs);
|
2018-09-04 14:29:20 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error);
|
|
|
|
|
}
|
2018-11-14 21:42:03 +00:00
|
|
|
|
},
|
|
|
|
|
async getLoginServices(server) {
|
|
|
|
|
try {
|
2019-08-20 19:24:22 +00:00
|
|
|
|
let loginServices = [];
|
2018-11-14 21:42:03 +00:00
|
|
|
|
const loginServicesResult = await fetch(`${ server }/api/v1/settings.oauth`).then(response => response.json());
|
2019-08-20 19:24:22 +00:00
|
|
|
|
|
2020-03-18 21:26:17 +00:00
|
|
|
|
if (loginServicesResult.success && loginServicesResult.services) {
|
2018-11-14 21:42:03 +00:00
|
|
|
|
const { services } = loginServicesResult;
|
2019-08-20 19:24:22 +00:00
|
|
|
|
loginServices = services;
|
|
|
|
|
|
|
|
|
|
const loginServicesReducer = loginServices.reduce((ret, item) => {
|
|
|
|
|
const name = item.name || item.buttonLabelText || item.service;
|
|
|
|
|
const authType = this._determineAuthType(item);
|
|
|
|
|
|
|
|
|
|
if (authType !== 'not_supported') {
|
|
|
|
|
ret[name] = { ...item, name, authType };
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}, {});
|
|
|
|
|
reduxStore.dispatch(setLoginServices(loginServicesReducer));
|
2020-04-07 13:26:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
reduxStore.dispatch(setLoginServices({}));
|
2018-11-14 21:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
console.log(error);
|
|
|
|
|
reduxStore.dispatch(setLoginServices({}));
|
2018-11-14 21:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-20 19:24:22 +00:00
|
|
|
|
_determineAuthType(services) {
|
2019-09-03 19:27:57 +00:00
|
|
|
|
const {
|
|
|
|
|
name, custom, showButton = true, service
|
|
|
|
|
} = services;
|
2019-08-20 19:24:22 +00:00
|
|
|
|
|
2020-01-29 20:20:36 +00:00
|
|
|
|
const authName = name || service;
|
|
|
|
|
|
2019-09-03 19:27:57 +00:00
|
|
|
|
if (custom && showButton) {
|
2019-08-20 19:24:22 +00:00
|
|
|
|
return 'oauth_custom';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (service === 'saml') {
|
|
|
|
|
return 'saml';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (service === 'cas') {
|
|
|
|
|
return 'cas';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove this after other oauth providers are implemented. e.g. Drupal, github_enterprise
|
2020-01-29 20:20:36 +00:00
|
|
|
|
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter', 'wordpress'];
|
|
|
|
|
return availableOAuth.includes(authName) ? 'oauth' : 'not_supported';
|
2019-08-20 19:24:22 +00:00
|
|
|
|
},
|
2018-11-14 21:42:03 +00:00
|
|
|
|
getUsernameSuggestion() {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.65.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.get('users.getUsernameSuggestion');
|
2018-12-05 20:52:08 +00:00
|
|
|
|
},
|
|
|
|
|
roomTypeToApiType(t) {
|
|
|
|
|
const types = {
|
2020-02-19 19:43:47 +00:00
|
|
|
|
c: 'channels', d: 'im', p: 'groups', l: 'channels'
|
2018-12-05 20:52:08 +00:00
|
|
|
|
};
|
|
|
|
|
return types[t];
|
2018-12-12 15:15:10 +00:00
|
|
|
|
},
|
|
|
|
|
getFiles(roomId, type, offset) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.get(`${ this.roomTypeToApiType(type) }.files`, {
|
2018-12-12 15:15:10 +00:00
|
|
|
|
roomId,
|
|
|
|
|
offset,
|
|
|
|
|
sort: { uploadedAt: -1 },
|
|
|
|
|
fields: {
|
|
|
|
|
name: 1, description: 1, size: 1, type: 1, uploadedAt: 1, url: 1, userId: 1
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getMessages(roomId, type, query, offset) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.get(`${ this.roomTypeToApiType(type) }.messages`, {
|
2018-12-12 15:15:10 +00:00
|
|
|
|
roomId,
|
|
|
|
|
query,
|
|
|
|
|
offset,
|
|
|
|
|
sort: { ts: -1 }
|
|
|
|
|
});
|
|
|
|
|
},
|
2019-06-10 18:36:31 +00:00
|
|
|
|
|
|
|
|
|
getReadReceipts(messageId) {
|
|
|
|
|
return this.sdk.get('chat.getMessageReadReceipts', {
|
|
|
|
|
messageId
|
|
|
|
|
});
|
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
searchMessages(roomId, searchText) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.60.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.get('chat.search', {
|
2018-12-12 15:15:10 +00:00
|
|
|
|
roomId,
|
|
|
|
|
searchText
|
|
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
|
},
|
|
|
|
|
toggleFollowMessage(mid, follow) {
|
|
|
|
|
// RC 1.0
|
|
|
|
|
if (follow) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.followMessage', { mid });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
}
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('chat.unfollowMessage', { mid });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
},
|
2019-04-24 18:36:29 +00:00
|
|
|
|
getThreadsList({ rid, count, offset }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
|
// RC 1.0
|
2019-04-24 18:36:29 +00:00
|
|
|
|
return this.sdk.get('chat.getThreadsList', {
|
|
|
|
|
rid, count, offset, sort: { ts: -1 }
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getSyncThreadsList({ rid, updatedSince }) {
|
|
|
|
|
// RC 1.0
|
|
|
|
|
return this.sdk.get('chat.syncThreadsList', {
|
|
|
|
|
rid, updatedSince
|
|
|
|
|
});
|
2019-05-28 16:52:26 +00:00
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
runSlashCommand(command, roomId, params, triggerId, tmid) {
|
2019-06-10 18:36:56 +00:00
|
|
|
|
// RC 0.60.2
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('commands.run', {
|
2020-02-19 19:43:47 +00:00
|
|
|
|
command, roomId, params, triggerId, tmid
|
2019-06-10 18:36:56 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getCommandPreview(command, roomId, params) {
|
|
|
|
|
// RC 0.65.0
|
|
|
|
|
return this.sdk.get('commands.preview', {
|
|
|
|
|
command, roomId, params
|
|
|
|
|
});
|
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
executeCommandPreview(command, params, roomId, previewItem, triggerId, tmid) {
|
2019-06-10 18:36:56 +00:00
|
|
|
|
// RC 0.65.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('commands.preview', {
|
2020-02-19 19:43:47 +00:00
|
|
|
|
command, params, roomId, previewItem, triggerId, tmid
|
2019-06-10 18:36:56 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
_setUser(ddpMessage) {
|
|
|
|
|
this.activeUsers = this.activeUsers || {};
|
|
|
|
|
const { user } = reduxStore.getState().login;
|
|
|
|
|
|
|
|
|
|
if (ddpMessage.fields && user && user.id === ddpMessage.id) {
|
|
|
|
|
reduxStore.dispatch(setUser(ddpMessage.fields));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ddpMessage.cleared && user && user.id === ddpMessage.id) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
reduxStore.dispatch(setUser({ status: { status: 'offline' } }));
|
2019-10-07 20:56:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this._setUserTimer) {
|
|
|
|
|
this._setUserTimer = setTimeout(() => {
|
|
|
|
|
const activeUsersBatch = this.activeUsers;
|
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsersBatch));
|
|
|
|
|
});
|
|
|
|
|
this._setUserTimer = null;
|
|
|
|
|
return this.activeUsers = {};
|
|
|
|
|
}, 10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ddpMessage.fields) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
this.activeUsers[ddpMessage.id] = { status: 'offline' };
|
2019-10-07 20:56:30 +00:00
|
|
|
|
} else if (ddpMessage.fields.status) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
this.activeUsers[ddpMessage.id] = { status: ddpMessage.fields.status };
|
2019-10-07 20:56:30 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-03-18 21:26:17 +00:00
|
|
|
|
getUsersPresence,
|
|
|
|
|
getUserPresence,
|
|
|
|
|
subscribeUsersPresence,
|
2019-06-10 16:22:35 +00:00
|
|
|
|
getDirectory({
|
|
|
|
|
query, count, offset, sort
|
|
|
|
|
}) {
|
|
|
|
|
// RC 1.0
|
|
|
|
|
return this.sdk.get('directory', {
|
|
|
|
|
query, count, offset, sort
|
|
|
|
|
});
|
2019-07-15 17:24:48 +00:00
|
|
|
|
},
|
2019-10-07 20:56:30 +00:00
|
|
|
|
async canAutoTranslate() {
|
|
|
|
|
const db = database.active;
|
2019-07-15 17:24:48 +00:00
|
|
|
|
try {
|
|
|
|
|
const AutoTranslate_Enabled = reduxStore.getState().settings && reduxStore.getState().settings.AutoTranslate_Enabled;
|
|
|
|
|
if (!AutoTranslate_Enabled) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
|
const permissionsCollection = db.collections.get('permissions');
|
|
|
|
|
const autoTranslatePermission = await permissionsCollection.find('auto-translate');
|
2019-07-15 17:24:48 +00:00
|
|
|
|
const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || [];
|
|
|
|
|
return autoTranslatePermission.roles.some(role => userRoles.includes(role));
|
2019-09-03 19:27:57 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
saveAutoTranslate({
|
|
|
|
|
rid, field, value, options
|
|
|
|
|
}) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('autoTranslate.saveSettings', rid, field, value, options);
|
2019-07-15 17:24:48 +00:00
|
|
|
|
},
|
|
|
|
|
getSupportedLanguagesAutoTranslate() {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('autoTranslate.getSupportedLanguages', 'en');
|
2019-07-15 17:24:48 +00:00
|
|
|
|
},
|
|
|
|
|
translateMessage(message, targetLanguage) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.methodCall('autoTranslate.translateMessage', message, targetLanguage);
|
2020-01-29 20:20:36 +00:00
|
|
|
|
},
|
|
|
|
|
getRoomTitle(room) {
|
|
|
|
|
const { UI_Use_Real_Name: useRealName } = reduxStore.getState().settings;
|
2020-04-07 13:26:19 +00:00
|
|
|
|
const { username } = reduxStore.getState().login.user;
|
|
|
|
|
if (RocketChat.isGroupChat(room) && !(room.name && room.name.length)) {
|
|
|
|
|
return room.usernames.filter(u => u !== username).sort((u1, u2) => u1.localeCompare(u2)).join(', ');
|
|
|
|
|
}
|
2020-01-29 20:20:36 +00:00
|
|
|
|
return ((room.prid || useRealName) && room.fname) || room.name;
|
|
|
|
|
},
|
2020-02-19 19:43:47 +00:00
|
|
|
|
getRoomAvatar(room) {
|
2020-04-07 13:26:19 +00:00
|
|
|
|
if (RocketChat.isGroupChat(room)) {
|
|
|
|
|
return room.uids.length + room.usernames.join();
|
|
|
|
|
}
|
2020-02-19 19:43:47 +00:00
|
|
|
|
return room.prid ? room.fname : room.name;
|
|
|
|
|
},
|
2020-01-29 20:20:36 +00:00
|
|
|
|
|
|
|
|
|
findOrCreateInvite({ rid, days, maxUses }) {
|
|
|
|
|
// RC 2.4.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('findOrCreateInvite', { rid, days, maxUses });
|
2020-01-29 20:20:36 +00:00
|
|
|
|
},
|
|
|
|
|
validateInviteToken(token) {
|
|
|
|
|
// RC 2.4.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('validateInviteToken', { token });
|
2020-01-29 20:20:36 +00:00
|
|
|
|
},
|
|
|
|
|
useInviteToken(token) {
|
|
|
|
|
// RC 2.4.0
|
2020-04-07 13:26:19 +00:00
|
|
|
|
return this.post('useInviteToken', { token });
|
2017-11-20 22:18:00 +00:00
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RocketChat;
|