2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2022-02-10 12:10:42 +00:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
|
|
|
import { Rocketchat as RocketchatClient, settings as RocketChatSettings } from '@rocket.chat/sdk';
|
|
|
|
import { InteractionManager } from 'react-native';
|
2020-09-11 16:30:28 +00:00
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
2022-02-10 12:10:42 +00:00
|
|
|
import { setActiveUsers } from '../../actions/activeUsers';
|
|
|
|
import { connectRequest, connectSuccess, disconnect } from '../../actions/connect';
|
|
|
|
import { encryptionInit } from '../../actions/encryption';
|
|
|
|
import { loginRequest, setLoginServices, setUser } from '../../actions/login';
|
|
|
|
import { updatePermission } from '../../actions/permissions';
|
|
|
|
import { selectServerFailure } from '../../actions/server';
|
|
|
|
import { updateSettings } from '../../actions/settings';
|
|
|
|
import { shareSelectServer, shareSetSettings, shareSetUser } from '../../actions/share';
|
|
|
|
import defaultSettings from '../../constants/settings';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { getDeviceToken } from '../../notifications/push';
|
|
|
|
import { getBundleId, isIOS } from '../../utils/deviceInfo';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import fetch from '../../utils/fetch';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import SSLPinning from '../../utils/sslPinning';
|
|
|
|
import { twoFactor } from '../../utils/twoFactor';
|
|
|
|
import { useSsl } from '../../utils/url';
|
|
|
|
import database from '../database';
|
|
|
|
import { sanitizeLikeString } from '../database/utils';
|
|
|
|
import { Encryption } from '../encryption';
|
|
|
|
import triggerBlockAction, { triggerCancel, triggerSubmitView } from '../methods/actions';
|
|
|
|
import callJitsi, { callJitsiWithoutServer } from '../methods/callJitsi';
|
|
|
|
import canOpenRoom from '../methods/canOpenRoom';
|
2020-08-21 13:38:50 +00:00
|
|
|
import {
|
2021-09-13 20:41:05 +00:00
|
|
|
getEnterpriseModules,
|
|
|
|
hasLicense,
|
|
|
|
isOmnichannelModuleAvailable,
|
|
|
|
setEnterpriseModules
|
2022-02-10 12:10:42 +00:00
|
|
|
} from '../methods/enterpriseModules';
|
|
|
|
import { getCustomEmojis, setCustomEmojis } from '../methods/getCustomEmojis';
|
|
|
|
import { getPermissions, setPermissions } from '../methods/getPermissions';
|
|
|
|
import { getRoles, onRolesChanged, setRoles } from '../methods/getRoles';
|
|
|
|
import getRooms from '../methods/getRooms';
|
|
|
|
import getSettings, { getLoginSettings, setSettings, subscribeSettings } from '../methods/getSettings';
|
|
|
|
import getSlashCommands from '../methods/getSlashCommands';
|
|
|
|
import protectedFunction from '../methods/helpers/protectedFunction';
|
|
|
|
import loadMessagesForRoom from '../methods/loadMessagesForRoom';
|
|
|
|
import loadMissedMessages from '../methods/loadMissedMessages';
|
|
|
|
import loadNextMessages from '../methods/loadNextMessages';
|
|
|
|
import loadSurroundingMessages from '../methods/loadSurroundingMessages';
|
|
|
|
import loadThreadMessages from '../methods/loadThreadMessages';
|
|
|
|
import logout, { removeServer } from '../methods/logout';
|
|
|
|
import readMessages from '../methods/readMessages';
|
|
|
|
import { cancelUpload, isUploadActive, sendFileMessage } from '../methods/sendFileMessage';
|
|
|
|
import sendMessage, { resendMessage } from '../methods/sendMessage';
|
|
|
|
import subscribeRooms from '../methods/subscriptions/rooms';
|
|
|
|
import UserPreferences from '../userPreferences';
|
|
|
|
import { compareServerVersion } from '../utils';
|
|
|
|
import { getUserPresence, subscribeUsersPresence } from '../methods/getUsersPresence';
|
|
|
|
import { store as reduxStore } from '../auxStore';
|
|
|
|
// Methods
|
|
|
|
import clearCache from './methods/clearCache';
|
|
|
|
import getPermalinkMessage from './methods/getPermalinkMessage';
|
|
|
|
import getRoom from './methods/getRoom';
|
|
|
|
import isGroupChat from './methods/isGroupChat';
|
2022-02-15 18:50:55 +00:00
|
|
|
import roomTypeToApiType from './methods/roomTypeToApiType';
|
2022-02-10 12:10:42 +00:00
|
|
|
import getUserInfo from './services/getUserInfo';
|
|
|
|
// Services
|
|
|
|
import sdk from './services/sdk';
|
|
|
|
import toggleFavorite from './services/toggleFavorite';
|
2022-02-15 18:50:55 +00:00
|
|
|
import * as restAPis from './services/restApi';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
2020-08-19 17:14:22 +00:00
|
|
|
const CURRENT_SERVER = 'currentServer';
|
2018-09-04 14:29:20 +00:00
|
|
|
const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY';
|
2020-11-30 17:00:06 +00:00
|
|
|
const CERTIFICATE_KEY = 'RC_CERTIFICATE_KEY';
|
2019-12-04 16:39:53 +00:00
|
|
|
export const THEME_PREFERENCES_KEY = 'RC_THEME_PREFERENCES_KEY';
|
2019-08-23 13:18:47 +00:00
|
|
|
export const CRASH_REPORT_KEY = 'RC_CRASH_REPORT_KEY';
|
2020-09-11 17:34:11 +00:00
|
|
|
export const ANALYTICS_EVENTS_KEY = 'RC_ANALYTICS_EVENTS_KEY';
|
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,
|
2020-08-19 17:14:22 +00:00
|
|
|
CURRENT_SERVER,
|
2020-11-30 17:00:06 +00:00
|
|
|
CERTIFICATE_KEY,
|
2022-02-15 18:50:55 +00:00
|
|
|
...restAPis,
|
2019-09-18 17:32:12 +00:00
|
|
|
callJitsi,
|
2021-11-16 16:04:33 +00:00
|
|
|
callJitsiWithoutServer,
|
2019-08-27 17:41:07 +00:00
|
|
|
async subscribeRooms() {
|
2020-04-03 18:03:53 +00:00
|
|
|
if (!this.roomsSub) {
|
|
|
|
try {
|
|
|
|
this.roomsSub = await subscribeRooms.call(this);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2019-08-27 17:41:07 +00:00
|
|
|
},
|
2020-10-30 18:31:04 +00:00
|
|
|
unsubscribeRooms() {
|
|
|
|
if (this.roomsSub) {
|
|
|
|
this.roomsSub.stop();
|
|
|
|
this.roomsSub = null;
|
|
|
|
}
|
|
|
|
},
|
2018-05-07 20:43:26 +00:00
|
|
|
canOpenRoom,
|
2019-11-13 19:52:00 +00:00
|
|
|
async getWebsocketInfo({ server }) {
|
2020-05-04 20:20:45 +00:00
|
|
|
const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: useSsl(server) });
|
2019-11-13 19:52:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await sdk.connect();
|
|
|
|
} catch (err) {
|
|
|
|
if (err.message && err.message.includes('400')) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2020-09-11 16:30:28 +00:00
|
|
|
message: I18n.t('Websocket_disabled', { contact: I18n.t('Contact_your_server_admin') })
|
2019-11-13 19:52:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sdk.disconnect();
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true
|
|
|
|
};
|
|
|
|
},
|
2019-04-17 17:01:03 +00:00
|
|
|
async getServerInfo(server) {
|
2018-08-01 19:35:06 +00:00
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const response = await RNFetchBlob.fetch('GET', `${server}/api/info`, { ...RocketChatSettings.customHeaders });
|
2020-09-11 16:30:28 +00:00
|
|
|
try {
|
|
|
|
// Try to resolve as json
|
|
|
|
const jsonRes = response.json();
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!jsonRes?.success) {
|
2020-09-11 16:30:28 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') })
|
|
|
|
};
|
2019-11-13 19:52:00 +00:00
|
|
|
}
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(jsonRes.version, 'lowerThan', MIN_ROCKETCHAT_VERSION)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
2020-09-11 16:30:28 +00:00
|
|
|
message: I18n.t('Invalid_server_version', {
|
|
|
|
currentVersion: jsonRes.version,
|
2018-12-21 10:55:35 +00:00
|
|
|
minVersion: MIN_ROCKETCHAT_VERSION
|
2020-09-11 16:30:28 +00:00
|
|
|
})
|
2018-12-21 10:55:35 +00:00
|
|
|
};
|
|
|
|
}
|
2020-09-11 16:30:28 +00:00
|
|
|
return jsonRes;
|
|
|
|
} catch (error) {
|
|
|
|
// Request is successful, but response isn't a json
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2018-08-01 19:35:06 +00:00
|
|
|
} catch (e) {
|
2020-09-11 16:30:28 +00:00
|
|
|
if (e?.message) {
|
|
|
|
if (e.message === 'Aborted') {
|
|
|
|
reduxStore.dispatch(selectServerFailure());
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: e.message
|
|
|
|
};
|
2020-05-05 13:11:28 +00:00
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2020-09-11 16:30:28 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
2020-09-11 16:30:28 +00:00
|
|
|
message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') })
|
2018-12-21 10:55:35 +00:00
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
},
|
2019-09-16 20:26:32 +00:00
|
|
|
stopListener(listener) {
|
|
|
|
return listener && listener.stop();
|
2017-12-08 19:13:21 +00:00
|
|
|
},
|
2020-05-05 13:11:28 +00:00
|
|
|
// 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();
|
|
|
|
},
|
2021-03-05 16:10:21 +00:00
|
|
|
checkAndReopen() {
|
|
|
|
return this?.sdk?.checkAndReopen();
|
|
|
|
},
|
2021-04-01 12:58:20 +00:00
|
|
|
disconnect() {
|
2022-02-10 12:10:42 +00:00
|
|
|
this.sdk = sdk.disconnect();
|
2021-04-01 12:58:20 +00:00
|
|
|
},
|
2019-11-27 20:52:49 +00:00
|
|
|
connect({ server, user, logoutOnError = false }) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(resolve => {
|
2021-03-05 16:10:21 +00:00
|
|
|
if (this?.sdk?.client?.host === server) {
|
|
|
|
return resolve();
|
|
|
|
} else {
|
2021-04-01 12:58:20 +00:00
|
|
|
this.disconnect();
|
2019-09-16 20:26:32 +00:00
|
|
|
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
|
|
|
|
2021-05-05 16:02:26 +00:00
|
|
|
if (this.connectingListener) {
|
|
|
|
this.connectingListener.then(this.stopListener);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +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);
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:30:55 +00:00
|
|
|
if (this.notifyAllListener) {
|
|
|
|
this.notifyAllListener.then(this.stopListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.rolesListener) {
|
|
|
|
this.rolesListener.then(this.stopListener);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.notifyLoggedListener) {
|
|
|
|
this.notifyLoggedListener.then(this.stopListener);
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:31:04 +00:00
|
|
|
this.unsubscribeRooms();
|
2019-06-17 13:57:07 +00:00
|
|
|
|
2020-08-28 19:41:08 +00:00
|
|
|
EventEmitter.emit('INQUIRY_UNSUBSCRIBE');
|
2020-07-31 18:22:30 +00:00
|
|
|
|
2022-02-10 12:10:42 +00:00
|
|
|
this.sdk = sdk.initialize(server);
|
2019-06-05 19:11:29 +00:00
|
|
|
this.getSettings();
|
|
|
|
|
2021-12-07 20:05:30 +00:00
|
|
|
this.sdk
|
|
|
|
.connect()
|
|
|
|
.then(() => {
|
|
|
|
console.log('connected');
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log('connect error', err);
|
|
|
|
});
|
2020-04-03 18:03:53 +00:00
|
|
|
|
2021-05-05 16:02:26 +00:00
|
|
|
this.connectingListener = this.sdk.onStreamData('connecting', () => {
|
2021-03-05 16:10:21 +00:00
|
|
|
reduxStore.dispatch(connectRequest());
|
|
|
|
});
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
this.connectedListener = this.sdk.onStreamData('connected', () => {
|
2021-12-07 20:05:30 +00:00
|
|
|
const { connected } = reduxStore.getState().meteor;
|
|
|
|
if (connected) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-05 19:11:29 +00:00
|
|
|
reduxStore.dispatch(connectSuccess());
|
2021-12-07 20:05:30 +00:00
|
|
|
const { server: currentServer } = reduxStore.getState().server;
|
|
|
|
if (user?.token && server === currentServer) {
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
|
|
|
|
}
|
2019-02-07 15:48:10 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
this.usersListener = this.sdk.onStreamData(
|
|
|
|
'users',
|
|
|
|
protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage))
|
|
|
|
);
|
|
|
|
|
|
|
|
this.notifyAllListener = this.sdk.onStreamData(
|
|
|
|
'stream-notify-all',
|
|
|
|
protectedFunction(async ddpMessage => {
|
|
|
|
const { eventName } = ddpMessage.fields;
|
|
|
|
if (/public-settings-changed/.test(eventName)) {
|
|
|
|
const { _id, value } = ddpMessage.fields.args[1];
|
|
|
|
const db = database.active;
|
|
|
|
const settingsCollection = db.get('settings');
|
|
|
|
try {
|
|
|
|
const settingsRecord = await settingsCollection.find(_id);
|
|
|
|
const { type } = defaultSettings[_id];
|
|
|
|
if (type) {
|
|
|
|
await db.action(async () => {
|
|
|
|
await settingsRecord.update(u => {
|
|
|
|
u[type] = value;
|
|
|
|
});
|
2021-07-01 19:30:55 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
reduxStore.dispatch(updateSettings(_id, value));
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2021-07-01 19:30:55 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.rolesListener = this.sdk.onStreamData(
|
|
|
|
'stream-roles',
|
|
|
|
protectedFunction(ddpMessage => onRolesChanged(ddpMessage))
|
|
|
|
);
|
|
|
|
|
2022-02-09 20:44:53 +00:00
|
|
|
// RC 4.1
|
|
|
|
this.sdk.onStreamData('stream-user-presence', ddpMessage => {
|
|
|
|
const userStatus = ddpMessage.fields.args[0];
|
|
|
|
const { uid } = ddpMessage.fields;
|
|
|
|
const [, status, statusText] = userStatus;
|
|
|
|
const newStatus = { status: STATUSES[status], statusText };
|
|
|
|
reduxStore.dispatch(setActiveUsers({ [uid]: newStatus }));
|
|
|
|
|
|
|
|
const { user: loggedUser } = reduxStore.getState().login;
|
|
|
|
if (loggedUser && loggedUser.id === uid) {
|
|
|
|
reduxStore.dispatch(setUser(newStatus));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
this.notifyLoggedListener = this.sdk.onStreamData(
|
|
|
|
'stream-notify-logged',
|
|
|
|
protectedFunction(async ddpMessage => {
|
|
|
|
const { eventName } = ddpMessage.fields;
|
2022-02-09 20:44:53 +00:00
|
|
|
|
|
|
|
// `user-status` event is deprecated after RC 4.1 in favor of `stream-user-presence/${uid}`
|
2021-09-13 20:41:05 +00:00
|
|
|
if (/user-status/.test(eventName)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
const userStatus = ddpMessage.fields.args[0];
|
|
|
|
const [id, , status, statusText] = userStatus;
|
|
|
|
this.activeUsers[id] = { status: STATUSES[status], statusText };
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const { user: loggedUser } = reduxStore.getState().login;
|
|
|
|
if (loggedUser && loggedUser.id === id) {
|
|
|
|
reduxStore.dispatch(setUser({ status: STATUSES[status], statusText }));
|
|
|
|
}
|
|
|
|
} else if (/updateAvatar/.test(eventName)) {
|
|
|
|
const { username, etag } = ddpMessage.fields.args[0];
|
|
|
|
const db = database.active;
|
|
|
|
const userCollection = db.get('users');
|
|
|
|
try {
|
|
|
|
const [userRecord] = await userCollection.query(Q.where('username', Q.eq(username))).fetch();
|
|
|
|
await db.action(async () => {
|
|
|
|
await userRecord.update(u => {
|
|
|
|
u.avatarETag = etag;
|
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
} catch {
|
|
|
|
// We can't create a new record since we don't receive the user._id
|
|
|
|
}
|
|
|
|
} else if (/permissions-changed/.test(eventName)) {
|
|
|
|
const { _id, roles } = ddpMessage.fields.args[1];
|
|
|
|
const db = database.active;
|
|
|
|
const permissionsCollection = db.get('permissions');
|
|
|
|
try {
|
|
|
|
const permissionsRecord = await permissionsCollection.find(_id);
|
|
|
|
await db.action(async () => {
|
|
|
|
await permissionsRecord.update(u => {
|
|
|
|
u.roles = roles;
|
|
|
|
});
|
2021-07-01 13:57:23 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
reduxStore.dispatch(updatePermission(_id, roles));
|
|
|
|
} catch (err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
} else if (/Users:NameChanged/.test(eventName)) {
|
|
|
|
const userNameChanged = ddpMessage.fields.args[0];
|
|
|
|
const db = database.active;
|
|
|
|
const userCollection = db.get('users');
|
|
|
|
try {
|
|
|
|
const userRecord = await userCollection.find(userNameChanged._id);
|
|
|
|
await db.action(async () => {
|
|
|
|
await userRecord.update(u => {
|
|
|
|
Object.assign(u, userNameChanged);
|
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
} catch {
|
|
|
|
// User not found
|
|
|
|
await db.action(async () => {
|
|
|
|
await userCollection.create(u => {
|
|
|
|
u._raw = sanitizedRaw({ id: userNameChanged._id }, userCollection.schema);
|
|
|
|
Object.assign(u, userNameChanged);
|
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +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 16:33:28 +00:00
|
|
|
async shareExtensionInit(server) {
|
2019-12-04 16:41:37 +00:00
|
|
|
database.setShareDB(server);
|
2019-07-29 16:33:28 +00:00
|
|
|
|
2020-11-30 17:00:06 +00:00
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const certificate = await UserPreferences.getStringAsync(`${RocketChat.CERTIFICATE_KEY}-${server}`);
|
2020-11-30 17:00:06 +00:00
|
|
|
await SSLPinning.setCertificate(certificate, server);
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2022-02-10 12:10:42 +00:00
|
|
|
this.shareSDK = sdk.disconnect();
|
|
|
|
this.shareSDK = sdk.initialize(server);
|
2019-07-29 16:33:28 +00:00
|
|
|
|
|
|
|
// set Server
|
2020-11-04 16:53:44 +00:00
|
|
|
const currentServer = { server };
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2020-11-04 16:53:44 +00:00
|
|
|
try {
|
|
|
|
const serverRecord = await serversCollection.find(server);
|
|
|
|
currentServer.version = serverRecord.version;
|
|
|
|
} catch {
|
|
|
|
// Record not found
|
|
|
|
}
|
|
|
|
reduxStore.dispatch(shareSelectServer(currentServer));
|
2019-07-29 16:33:28 +00:00
|
|
|
|
2020-07-08 16:19:41 +00:00
|
|
|
RocketChat.setCustomEmojis();
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2020-10-30 15:54:02 +00:00
|
|
|
// set Settings
|
|
|
|
const settings = ['Accounts_AvatarBlockUnauthenticatedAccess'];
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const settingsCollection = db.get('settings');
|
2020-10-30 15:54:02 +00:00
|
|
|
const settingsRecords = await settingsCollection.query(Q.where('id', Q.oneOf(settings))).fetch();
|
|
|
|
const parsed = Object.values(settingsRecords).map(item => ({
|
|
|
|
_id: item.id,
|
|
|
|
valueAsString: item.valueAsString,
|
|
|
|
valueAsBoolean: item.valueAsBoolean,
|
|
|
|
valueAsNumber: item.valueAsNumber,
|
|
|
|
valueAsArray: item.valueAsArray,
|
|
|
|
_updatedAt: item._updatedAt
|
|
|
|
}));
|
|
|
|
reduxStore.dispatch(shareSetSettings(this.parseSettings(parsed)));
|
|
|
|
|
|
|
|
// set User info
|
2021-09-13 20:41:05 +00:00
|
|
|
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
|
2021-02-26 16:25:51 +00:00
|
|
|
const userCollections = serversDB.get('users');
|
2019-09-16 20:26:32 +00:00
|
|
|
let user = null;
|
|
|
|
if (userId) {
|
2019-10-02 12:55:25 +00:00
|
|
|
const userRecord = await userCollections.find(userId);
|
2019-09-16 20:26:32 +00:00
|
|
|
user = {
|
2019-10-02 12:55:25 +00:00
|
|
|
id: userRecord.id,
|
|
|
|
token: userRecord.token,
|
2020-04-13 12:51:16 +00:00
|
|
|
username: userRecord.username,
|
|
|
|
roles: userRecord.roles
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
|
|
|
}
|
2020-04-13 12:51:16 +00:00
|
|
|
reduxStore.dispatch(shareSetUser(user));
|
2019-09-16 20:26:32 +00:00
|
|
|
await RocketChat.login({ resume: user.token });
|
2020-09-11 14:31:38 +00:00
|
|
|
reduxStore.dispatch(encryptionInit());
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-07-29 16:33:28 +00:00
|
|
|
},
|
2019-12-04 16:41:37 +00:00
|
|
|
closeShareExtension() {
|
2022-02-10 12:10:42 +00:00
|
|
|
this.shareSDK = sdk.disconnect();
|
2019-12-04 16:41:37 +00:00
|
|
|
database.share = null;
|
2020-04-13 12:51:16 +00:00
|
|
|
|
2020-11-04 16:53:44 +00:00
|
|
|
reduxStore.dispatch(shareSelectServer({}));
|
2020-05-13 19:04:50 +00:00
|
|
|
reduxStore.dispatch(shareSetUser({}));
|
2020-10-30 15:54:02 +00:00
|
|
|
reduxStore.dispatch(shareSetSettings({}));
|
2019-12-04 16:41:37 +00:00
|
|
|
},
|
2019-07-29 16:33:28 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
async e2eFetchMyKeys() {
|
|
|
|
// RC 0.70.0
|
|
|
|
const sdk = this.shareSDK || this.sdk;
|
|
|
|
const result = await sdk.get('e2e.fetchMyKeys');
|
|
|
|
// snake_case -> camelCase
|
|
|
|
if (result.success) {
|
|
|
|
return {
|
|
|
|
success: result.success,
|
|
|
|
publicKey: result.public_key,
|
|
|
|
privateKey: result.private_key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2020-10-30 18:31:04 +00:00
|
|
|
e2eResetOwnKey() {
|
|
|
|
this.unsubscribeRooms();
|
|
|
|
|
|
|
|
// RC 0.72.0
|
|
|
|
return this.methodCallWrapper('e2e.resetOwnE2EKey');
|
|
|
|
},
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2021-07-05 18:21:15 +00:00
|
|
|
loginTOTP(params, loginEmailPassword, isFromWebView = false) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2020-04-01 20:32:24 +00:00
|
|
|
try {
|
2021-07-05 18:21:15 +00:00
|
|
|
const result = await this.login(params, isFromWebView);
|
2020-04-01 20:32:24 +00:00
|
|
|
return resolve(result);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.data?.error && (e.data.error === 'totp-required' || e.data.error === 'totp-invalid')) {
|
|
|
|
const { details } = e.data;
|
|
|
|
try {
|
2021-03-22 17:26:44 +00:00
|
|
|
const code = await twoFactor({ method: details?.method || 'totp', invalid: details?.error === 'totp-invalid' });
|
|
|
|
|
|
|
|
if (loginEmailPassword) {
|
|
|
|
reduxStore.dispatch(setUser({ username: params.user || params.username }));
|
|
|
|
|
|
|
|
// Force normalized params for 2FA starting RC 3.9.0.
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.9.0')) {
|
2021-03-22 17:26:44 +00:00
|
|
|
const user = params.user ?? params.username;
|
|
|
|
const password = params.password ?? params.ldapPass ?? params.crowdPassword;
|
|
|
|
params = { user, password };
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
2020-12-01 12:54:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return resolve(
|
|
|
|
this.loginTOTP({
|
|
|
|
totp: {
|
|
|
|
login: {
|
|
|
|
...params
|
|
|
|
},
|
|
|
|
code: code?.twoFactorCode
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-04-01 20:32:24 +00:00
|
|
|
} catch {
|
|
|
|
// twoFactor was canceled
|
|
|
|
return reject();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-10 13:42:02 +00:00
|
|
|
},
|
|
|
|
|
2020-04-01 20:32:24 +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-11-13 16:02:36 +00:00
|
|
|
username: user,
|
|
|
|
crowdPassword: password,
|
2018-10-16 20:27:37 +00:00
|
|
|
crowd: true
|
2017-08-13 23:02:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:54:15 +00:00
|
|
|
return this.loginTOTP(params, true);
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
|
|
|
|
2021-07-05 18:21:15 +00:00
|
|
|
async loginOAuthOrSso(params, isFromWebView = true) {
|
|
|
|
const result = await this.loginTOTP(params, false, isFromWebView);
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: result.token }, false, isFromWebView));
|
2018-12-05 20:52:08 +00:00
|
|
|
},
|
|
|
|
|
2021-07-05 18:21:15 +00:00
|
|
|
async login(credentials, isFromWebView = false) {
|
2020-05-08 16:37:49 +00:00
|
|
|
const sdk = this.shareSDK || this.sdk;
|
|
|
|
// RC 0.64.0
|
2021-07-05 18:21:15 +00:00
|
|
|
await sdk.login(credentials);
|
2020-05-08 16:37:49 +00:00
|
|
|
const { result } = sdk.currentLogin;
|
|
|
|
const user = {
|
|
|
|
id: result.userId,
|
|
|
|
token: result.authToken,
|
|
|
|
username: result.me.username,
|
|
|
|
name: result.me.name,
|
|
|
|
language: result.me.language,
|
|
|
|
status: result.me.status,
|
|
|
|
statusText: result.me.statusText,
|
|
|
|
customFields: result.me.customFields,
|
2020-06-26 20:45:21 +00:00
|
|
|
statusLivechat: result.me.statusLivechat,
|
2020-05-08 16:37:49 +00:00
|
|
|
emails: result.me.emails,
|
2020-08-25 20:04:18 +00:00
|
|
|
roles: result.me.roles,
|
2020-10-30 13:12:02 +00:00
|
|
|
avatarETag: result.me.avatarETag,
|
2021-07-05 18:21:15 +00:00
|
|
|
isFromWebView,
|
2021-10-20 16:32:58 +00:00
|
|
|
showMessageInMainThread: result.me.settings?.preferences?.showMessageInMainThread ?? true,
|
|
|
|
enableMessageParserEarlyAdoption: result.me.settings?.preferences?.enableMessageParserEarlyAdoption ?? true
|
2020-05-08 16:37:49 +00:00
|
|
|
};
|
|
|
|
return user;
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
2020-05-04 20:20:45 +00:00
|
|
|
logout,
|
2020-08-25 16:51:49 +00:00
|
|
|
logoutOtherLocations() {
|
|
|
|
const { id: userId } = reduxStore.getState().login.user;
|
|
|
|
return this.sdk.post('users.removeOtherTokens', { userId });
|
|
|
|
},
|
2020-05-04 20:20:45 +00:00
|
|
|
removeServer,
|
2022-02-10 12:10:42 +00:00
|
|
|
clearCache,
|
2018-12-05 20:52:08 +00:00
|
|
|
registerPushToken() {
|
2021-09-13 20:41:05 +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-01 20:32:24 +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,
|
2021-05-26 17:24:54 +00:00
|
|
|
loadSurroundingMessages,
|
|
|
|
loadNextMessages,
|
2019-04-17 17:01:03 +00:00
|
|
|
loadThreadMessages,
|
2018-04-24 19:34:03 +00:00
|
|
|
sendMessage,
|
|
|
|
getRooms,
|
|
|
|
readMessages,
|
2020-09-11 14:31:38 +00:00
|
|
|
resendMessage,
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2021-01-20 17:34:01 +00:00
|
|
|
async localSearch({ text, filterUsers = true, filterRooms = true }) {
|
2018-08-31 18:13:30 +00:00
|
|
|
const searchText = text.trim();
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(searchText);
|
2021-09-13 20:41:05 +00:00
|
|
|
let data = await db
|
|
|
|
.get('subscriptions')
|
|
|
|
.query(
|
|
|
|
Q.or(Q.where('name', Q.like(`%${likeString}%`)), Q.where('fname', Q.like(`%${likeString}%`))),
|
|
|
|
Q.experimentalSortBy('room_updated_at', Q.desc)
|
|
|
|
)
|
|
|
|
.fetch();
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
if (filterUsers && !filterRooms) {
|
2020-04-30 18:10:19 +00:00
|
|
|
data = data.filter(item => item.t === 'd' && !RocketChat.isGroupChat(item));
|
2018-08-31 18:13:30 +00:00
|
|
|
} else if (!filterUsers && filterRooms) {
|
2020-04-30 18:10:19 +00:00
|
|
|
data = data.filter(item => item.t !== 'd' || RocketChat.isGroupChat(item));
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2020-08-21 13:38:50 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
data = data.slice(0, 7);
|
|
|
|
|
2021-02-26 16:01:45 +00:00
|
|
|
data = data.map(sub => ({
|
|
|
|
rid: sub.rid,
|
|
|
|
name: sub.name,
|
|
|
|
fname: sub.fname,
|
|
|
|
avatarETag: sub.avatarETag,
|
|
|
|
t: sub.t,
|
|
|
|
encrypted: sub.encrypted,
|
[NEW] Add/Create/Remove channel on a team (#3090)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* One way to refactor :P
* Next level refactor :)
* Fix create group dm
* Refactor renderItem
* Minor bug fixes
* Fix stories
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-19 21:14:42 +00:00
|
|
|
lastMessage: sub.lastMessage,
|
|
|
|
...(sub.teamId && { teamId: sub.teamId })
|
2021-02-26 16:01:45 +00:00
|
|
|
}));
|
2020-04-13 12:51:16 +00:00
|
|
|
|
2021-01-20 17:34:01 +00:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
|
|
|
async search({ text, filterUsers = true, filterRooms = true }) {
|
|
|
|
const searchText = text.trim();
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise('cancel');
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:53:12 +00:00
|
|
|
const data = await this.localSearch({ text, filterUsers, filterRooms });
|
2021-01-20 17:34:01 +00:00
|
|
|
|
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 }),
|
2021-09-13 20:41:05 +00:00
|
|
|
new Promise((resolve, reject) => (this.oldPromise = reject))
|
2018-08-31 18:13:30 +00:00
|
|
|
]);
|
2020-02-13 19:24:39 +00:00
|
|
|
if (filterUsers) {
|
2021-01-20 19:53:12 +00:00
|
|
|
users
|
|
|
|
.filter((item1, index) => users.findIndex(item2 => item2._id === item1._id) === index) // Remove duplicated data from response
|
|
|
|
.filter(user => !data.some(sub => user.username === sub.name)) // Make sure to remove users already on local database
|
2021-09-13 20:41:05 +00:00
|
|
|
.forEach(user => {
|
2021-01-20 19:53:12 +00:00
|
|
|
data.push({
|
|
|
|
...user,
|
|
|
|
rid: user.username,
|
|
|
|
name: user.username,
|
|
|
|
t: 'd',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
});
|
2020-02-13 19:24:39 +00:00
|
|
|
}
|
|
|
|
if (filterRooms) {
|
2021-09-13 20:41:05 +00:00
|
|
|
rooms.forEach(room => {
|
2020-02-13 19:24:39 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
createGroupChat() {
|
2020-04-03 18:02:10 +00:00
|
|
|
const { users } = reduxStore.getState().selectedUsers;
|
|
|
|
const usernames = users.map(u => u.name).join(',');
|
2020-04-01 12:28:54 +00:00
|
|
|
|
|
|
|
// RC 3.1.0
|
2020-04-03 18:02:10 +00:00
|
|
|
return this.post('im.create', { usernames });
|
2020-04-01 12:28:54 +00:00
|
|
|
},
|
2020-02-11 14:01:35 +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-03 20:53:48 +00:00
|
|
|
getLoginSettings,
|
2019-10-28 13:37:13 +00:00
|
|
|
setSettings,
|
2021-07-06 16:22:38 +00:00
|
|
|
subscribeSettings,
|
2018-04-24 19:34:03 +00:00
|
|
|
getPermissions,
|
2021-02-25 16:41:44 +00:00
|
|
|
setPermissions,
|
2019-06-17 13:57:07 +00:00
|
|
|
getCustomEmojis,
|
2019-09-16 20:26:32 +00:00
|
|
|
setCustomEmojis,
|
2020-08-21 13:38:50 +00:00
|
|
|
getEnterpriseModules,
|
|
|
|
setEnterpriseModules,
|
|
|
|
hasLicense,
|
|
|
|
isOmnichannelModuleAvailable,
|
2019-06-10 18:36:56 +00:00
|
|
|
getSlashCommands,
|
2019-04-26 21:15:25 +00:00
|
|
|
getRoles,
|
2021-07-01 16:51:19 +00:00
|
|
|
setRoles,
|
2021-09-13 20:41:05 +00:00
|
|
|
parseSettings: settings =>
|
|
|
|
settings.reduce((ret, item) => {
|
|
|
|
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])],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}, {}),
|
2017-11-19 14:57:50 +00:00
|
|
|
_prepareSettings(settings) {
|
2021-09-13 20:41:05 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
2020-09-11 14:31:38 +00:00
|
|
|
async editMessage(message) {
|
|
|
|
const { rid, msg } = await Encryption.encryptMessage(message);
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.49.0
|
2020-09-11 14:31:38 +00:00
|
|
|
return this.post('chat.update', { roomId: rid, msgId: message.id, text: msg });
|
2017-11-21 14:55:50 +00:00
|
|
|
},
|
2022-02-10 12:10:42 +00:00
|
|
|
getRoom,
|
|
|
|
getPermalinkMessage,
|
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];
|
2021-09-13 20:41:05 +00:00
|
|
|
return `${server}/${roomType}/${channel.name}`;
|
2019-05-29 21:19:12 +00:00
|
|
|
},
|
2017-11-20 22:18:00 +00:00
|
|
|
subscribe(...args) {
|
2022-02-14 19:23:13 +00:00
|
|
|
return sdk.subscribe(...args);
|
|
|
|
},
|
|
|
|
subscribeRaw(...args) {
|
|
|
|
return sdk.subscribeRaw(...args);
|
2018-10-15 20:22:42 +00:00
|
|
|
},
|
2020-02-05 13:34:53 +00:00
|
|
|
subscribeRoom(...args) {
|
2022-02-14 19:23:13 +00:00
|
|
|
return sdk.subscribeRoom(...args);
|
2020-02-05 13:34:53 +00:00
|
|
|
},
|
2018-10-15 20:22:42 +00:00
|
|
|
unsubscribe(subscription) {
|
2022-02-14 19:23:13 +00:00
|
|
|
return sdk.unsubscribe(subscription);
|
2017-11-20 22:18:00 +00:00
|
|
|
},
|
2019-04-08 12:35:28 +00:00
|
|
|
onStreamData(...args) {
|
2022-02-14 19:23:13 +00:00
|
|
|
return sdk.onStreamData(...args);
|
2019-04-08 12:35:28 +00:00
|
|
|
},
|
2020-06-26 20:48:40 +00:00
|
|
|
emitTyping(room, typing = true) {
|
|
|
|
const { login, settings } = reduxStore.getState();
|
|
|
|
const { UI_Use_Real_Name } = settings;
|
|
|
|
const { user } = login;
|
|
|
|
const name = UI_Use_Real_Name ? user.name : user.username;
|
2021-09-13 20:41:05 +00:00
|
|
|
return this.methodCall('stream-notify-room', `${room}/typing`, name, typing);
|
2017-12-04 18:24:21 +00:00
|
|
|
},
|
2022-02-10 12:10:42 +00:00
|
|
|
toggleFavorite,
|
2021-09-28 19:34:14 +00:00
|
|
|
async getRoomMembers({ rid, allUsers, roomType, type, filter, skip = 0, limit = 10 }) {
|
2021-09-16 16:26:05 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.16.0')) {
|
2021-09-16 16:26:05 +00:00
|
|
|
const params = {
|
|
|
|
roomId: rid,
|
|
|
|
offset: skip,
|
|
|
|
count: limit,
|
|
|
|
...(type !== 'all' && { 'status[]': type }),
|
|
|
|
...(filter && { filter })
|
|
|
|
};
|
|
|
|
// RC 3.16.0
|
2021-09-28 19:34:14 +00:00
|
|
|
const result = await this.sdk.get(`${this.roomTypeToApiType(roomType)}.members`, params);
|
|
|
|
return result?.members;
|
2021-09-16 16:26:05 +00:00
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.42.0
|
2021-09-28 19:34:14 +00:00
|
|
|
const result = await this.methodCallWrapper('getUsersOfRoom', rid, allUsers, { skip, limit });
|
|
|
|
return result?.records;
|
2018-03-23 16:49:51 +00:00
|
|
|
},
|
2020-08-19 17:13:02 +00:00
|
|
|
methodCallWrapper(method, ...params) {
|
2022-02-10 12:10:42 +00:00
|
|
|
return sdk.methodCallWrapper(method, ...params);
|
2020-06-29 17:57:39 +00:00
|
|
|
},
|
2022-02-10 12:10:42 +00:00
|
|
|
getUserInfo,
|
2020-04-09 05:20:57 +00:00
|
|
|
getUidDirectMessage(room) {
|
|
|
|
const { id: userId } = reduxStore.getState().login.user;
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
if (!room) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
// legacy method
|
2020-10-30 17:35:07 +00:00
|
|
|
if (!room?.uids && room.rid && room.t === 'd') {
|
2020-04-01 12:28:54 +00:00
|
|
|
return room.rid.replace(userId, '').trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RocketChat.isGroupChat(room)) {
|
|
|
|
return false;
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
const me = room.uids?.find(uid => uid === userId);
|
|
|
|
const other = room.uids?.filter(uid => uid !== userId);
|
2020-04-01 12:28:54 +00:00
|
|
|
|
|
|
|
return other && other.length ? other[0] : me;
|
|
|
|
},
|
|
|
|
|
2020-07-20 16:44:54 +00:00
|
|
|
isRead(item) {
|
|
|
|
let isUnread = item.archived !== true && item.open === true; // item is not archived and not opened
|
|
|
|
isUnread = isUnread && (item.unread > 0 || item.alert === true); // either its unread count > 0 or its alert
|
|
|
|
return !isUnread;
|
|
|
|
},
|
2022-02-10 12:10:42 +00:00
|
|
|
isGroupChat,
|
2020-04-01 20:32:24 +00:00
|
|
|
post(...args) {
|
2022-02-10 12:10:42 +00:00
|
|
|
return sdk.post(...args);
|
2020-04-01 20:32:24 +00:00
|
|
|
},
|
|
|
|
methodCall(...args) {
|
2022-02-10 12:10:42 +00:00
|
|
|
return sdk.methodCall(...args);
|
2020-04-01 20:32:24 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
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-06-29 17:57:39 +00:00
|
|
|
return this.methodCallWrapper('addUsersToRoom', { rid, users });
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
2020-07-31 18:22:30 +00:00
|
|
|
hasRole(role) {
|
|
|
|
const shareUser = reduxStore.getState().share.user;
|
|
|
|
const loginUser = reduxStore.getState().login.user;
|
|
|
|
// get user roles on the server from redux
|
2021-09-13 20:41:05 +00:00
|
|
|
const userRoles = shareUser?.roles || loginUser?.roles || [];
|
2020-07-31 18:22:30 +00:00
|
|
|
|
|
|
|
return userRoles.indexOf(r => r === role) > -1;
|
|
|
|
},
|
2021-02-25 16:41:44 +00:00
|
|
|
/**
|
|
|
|
* Permissions: array of permissions' roles from redux. Example: [['owner', 'admin'], ['leader']]
|
|
|
|
* Returns an array of boolean for each permission from permissions arg
|
|
|
|
*/
|
2019-09-16 20:26:32 +00:00
|
|
|
async hasPermission(permissions, rid) {
|
2019-04-26 20:54:58 +00:00
|
|
|
let roomRoles = [];
|
2021-10-05 13:59:40 +00:00
|
|
|
if (rid) {
|
|
|
|
const db = database.active;
|
|
|
|
const subsCollection = db.get('subscriptions');
|
|
|
|
try {
|
|
|
|
// get the room from database
|
|
|
|
const room = await subsCollection.find(rid);
|
|
|
|
// get room roles
|
|
|
|
roomRoles = room.roles || [];
|
|
|
|
} catch (error) {
|
|
|
|
console.log('hasPermission -> Room not found');
|
|
|
|
return permissions.map(() => false);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2021-02-25 16:41:44 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2020-04-13 12:51:16 +00:00
|
|
|
const shareUser = reduxStore.getState().share.user;
|
|
|
|
const loginUser = reduxStore.getState().login.user;
|
2019-09-16 20:26:32 +00:00
|
|
|
// get user roles on the server from redux
|
2021-09-13 20:41:05 +00:00
|
|
|
const userRoles = shareUser?.roles || loginUser?.roles || [];
|
2019-09-16 20:26:32 +00:00
|
|
|
const mergedRoles = [...new Set([...roomRoles, ...userRoles])];
|
2021-03-05 13:44:46 +00:00
|
|
|
return permissions.map(permission => permission?.some(r => mergedRoles.includes(r) ?? false));
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
},
|
2019-08-23 13:18:47 +00:00
|
|
|
async getAllowCrashReport() {
|
|
|
|
const allowCrashReport = await AsyncStorage.getItem(CRASH_REPORT_KEY);
|
|
|
|
if (allowCrashReport === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return JSON.parse(allowCrashReport);
|
|
|
|
},
|
2020-09-11 17:34:11 +00:00
|
|
|
async getAllowAnalyticsEvents() {
|
|
|
|
const allowAnalyticsEvents = await AsyncStorage.getItem(ANALYTICS_EVENTS_KEY);
|
|
|
|
if (allowAnalyticsEvents === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return JSON.parse(allowAnalyticsEvents);
|
|
|
|
},
|
2018-09-04 14:29:20 +00:00
|
|
|
async getSortPreferences() {
|
2020-08-19 17:14:22 +00:00
|
|
|
const prefs = await UserPreferences.getMapAsync(SORT_PREFS_KEY);
|
2019-07-18 17:44:02 +00:00
|
|
|
return prefs;
|
2018-09-04 14:29:20 +00:00
|
|
|
},
|
|
|
|
async saveSortPreference(param) {
|
2020-08-19 17:14:22 +00:00
|
|
|
let prefs = await RocketChat.getSortPreferences();
|
|
|
|
prefs = { ...prefs, ...param };
|
|
|
|
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
|
2018-11-14 21:42:03 +00:00
|
|
|
},
|
|
|
|
async getLoginServices(server) {
|
|
|
|
try {
|
2019-08-09 17:28:46 +00:00
|
|
|
let loginServices = [];
|
2021-09-13 20:41:05 +00:00
|
|
|
const loginServicesResult = await fetch(`${server}/api/v1/settings.oauth`).then(response => response.json());
|
2019-08-07 14:36:54 +00:00
|
|
|
|
2020-02-20 20:08:20 +00:00
|
|
|
if (loginServicesResult.success && loginServicesResult.services) {
|
2018-11-14 21:42:03 +00:00
|
|
|
const { services } = loginServicesResult;
|
2019-08-09 17:28:46 +00:00
|
|
|
loginServices = services;
|
2019-08-07 14:36:54 +00:00
|
|
|
|
2019-08-09 17:28:46 +00:00
|
|
|
const loginServicesReducer = loginServices.reduce((ret, item) => {
|
|
|
|
const name = item.name || item.buttonLabelText || item.service;
|
2019-08-07 14:36:54 +00:00
|
|
|
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-01 15:56:08 +00:00
|
|
|
} else {
|
|
|
|
reduxStore.dispatch(setLoginServices({}));
|
2018-11-14 21:42:03 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-04-01 15:56:08 +00:00
|
|
|
console.log(error);
|
|
|
|
reduxStore.dispatch(setLoginServices({}));
|
2018-11-14 21:42:03 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:28:46 +00:00
|
|
|
_determineAuthType(services) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { name, custom, showButton = true, service } = services;
|
2019-08-07 14:36:54 +00:00
|
|
|
|
2020-01-29 12:30:59 +00:00
|
|
|
const authName = name || service;
|
|
|
|
|
2019-08-22 19:24:27 +00:00
|
|
|
if (custom && showButton) {
|
2019-08-07 14:36:54 +00:00
|
|
|
return 'oauth_custom';
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:28:46 +00:00
|
|
|
if (service === 'saml') {
|
|
|
|
return 'saml';
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:37:42 +00:00
|
|
|
if (service === 'cas') {
|
|
|
|
return 'cas';
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:07:45 +00:00
|
|
|
if (authName === 'apple' && isIOS) {
|
|
|
|
return 'apple';
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:28:46 +00:00
|
|
|
// TODO: remove this after other oauth providers are implemented. e.g. Drupal, github_enterprise
|
2020-01-29 12:30:59 +00:00
|
|
|
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter', 'wordpress'];
|
|
|
|
return availableOAuth.includes(authName) ? 'oauth' : 'not_supported';
|
2019-08-07 14:36:54 +00:00
|
|
|
},
|
2022-02-15 18:50:55 +00:00
|
|
|
roomTypeToApiType,
|
2020-06-29 18:47:48 +00:00
|
|
|
readThreads(tmid) {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.4.0')) {
|
2020-06-29 18:47:48 +00:00
|
|
|
// RC 3.4.0
|
|
|
|
return this.methodCallWrapper('readThreads', tmid);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
2019-09-16 20:26:32 +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-03-30 20:19:01 +00:00
|
|
|
reduxStore.dispatch(setUser({ status: { status: 'offline' } }));
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 20:44:53 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
|
|
|
if (compareServerVersion(serverVersion, 'lowerThan', '4.1.0')) {
|
|
|
|
if (!this._setUserTimer) {
|
|
|
|
this._setUserTimer = setTimeout(() => {
|
|
|
|
const activeUsersBatch = this.activeUsers;
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsersBatch));
|
|
|
|
});
|
|
|
|
this._setUserTimer = null;
|
|
|
|
return (this.activeUsers = {});
|
|
|
|
}, 10000);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ddpMessage.fields) {
|
2020-03-30 20:19:01 +00:00
|
|
|
this.activeUsers[ddpMessage.id] = { status: 'offline' };
|
2019-09-16 20:26:32 +00:00
|
|
|
} else if (ddpMessage.fields.status) {
|
2020-03-30 20:19:01 +00:00
|
|
|
this.activeUsers[ddpMessage.id] = { status: ddpMessage.fields.status };
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
},
|
2020-03-03 21:10:39 +00:00
|
|
|
getUserPresence,
|
|
|
|
subscribeUsersPresence,
|
2021-02-25 16:41:44 +00:00
|
|
|
canAutoTranslate() {
|
2019-06-28 17:02:30 +00:00
|
|
|
try {
|
2021-02-25 16:41:44 +00:00
|
|
|
const { AutoTranslate_Enabled } = reduxStore.getState().settings;
|
2019-06-28 17:02:30 +00:00
|
|
|
if (!AutoTranslate_Enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-25 16:41:44 +00:00
|
|
|
const autoTranslatePermission = reduxStore.getState().permissions['auto-translate'];
|
2021-09-13 20:41:05 +00:00
|
|
|
const userRoles = reduxStore.getState().login?.user?.roles ?? [];
|
2022-03-02 14:18:01 +00:00
|
|
|
return autoTranslatePermission?.some(role => userRoles.includes(role)) ?? false;
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-28 17:02:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2020-09-11 14:31:38 +00:00
|
|
|
getSenderName(sender) {
|
|
|
|
const { UI_Use_Real_Name: useRealName } = reduxStore.getState().settings;
|
|
|
|
return useRealName ? sender.name : sender.username;
|
|
|
|
},
|
2020-01-28 13:22:35 +00:00
|
|
|
getRoomTitle(room) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { UI_Use_Real_Name: useRealName, UI_Allow_room_names_with_special_chars: allowSpecialChars } =
|
|
|
|
reduxStore.getState().settings;
|
2020-04-01 12:28:54 +00:00
|
|
|
const { username } = reduxStore.getState().login.user;
|
|
|
|
if (RocketChat.isGroupChat(room) && !(room.name && room.name.length)) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return room.usernames
|
|
|
|
.filter(u => u !== username)
|
|
|
|
.sort((u1, u2) => u1.localeCompare(u2))
|
|
|
|
.join(', ');
|
2020-04-01 12:28:54 +00:00
|
|
|
}
|
2020-04-30 16:24:49 +00:00
|
|
|
if (allowSpecialChars && room.t !== 'd') {
|
|
|
|
return room.fname || room.name;
|
|
|
|
}
|
2020-01-28 13:22:35 +00:00
|
|
|
return ((room.prid || useRealName) && room.fname) || room.name;
|
|
|
|
},
|
2020-02-12 17:21:11 +00:00
|
|
|
getRoomAvatar(room) {
|
2020-04-01 12:28:54 +00:00
|
|
|
if (RocketChat.isGroupChat(room)) {
|
2020-04-30 18:07:07 +00:00
|
|
|
return room.uids?.length + room.usernames?.join();
|
2020-04-01 12:28:54 +00:00
|
|
|
}
|
2020-02-12 17:21:11 +00:00
|
|
|
return room.prid ? room.fname : room.name;
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|