2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-02-10 12:10:42 +00:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
|
|
|
import { InteractionManager } from 'react-native';
|
|
|
|
import { setActiveUsers } from '../../actions/activeUsers';
|
|
|
|
import { encryptionInit } from '../../actions/encryption';
|
2022-03-08 18:54:26 +00:00
|
|
|
import { setUser } from '../../actions/login';
|
2022-02-10 12:10:42 +00:00
|
|
|
import { shareSelectServer, shareSetSettings, shareSetUser } from '../../actions/share';
|
|
|
|
import defaultSettings from '../../constants/settings';
|
|
|
|
import { getDeviceToken } from '../../notifications/push';
|
|
|
|
import { getBundleId, isIOS } from '../../utils/deviceInfo';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import SSLPinning from '../../utils/sslPinning';
|
|
|
|
import database from '../database';
|
|
|
|
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';
|
2022-03-08 18:54:26 +00:00
|
|
|
import { getRoles, setRoles } from '../methods/getRoles';
|
2022-02-10 12:10:42 +00:00
|
|
|
import getRooms from '../methods/getRooms';
|
|
|
|
import getSettings, { getLoginSettings, setSettings, subscribeSettings } from '../methods/getSettings';
|
|
|
|
import getSlashCommands from '../methods/getSlashCommands';
|
|
|
|
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';
|
2022-03-08 14:24:21 +00:00
|
|
|
import * as search from './methods/search';
|
2022-02-10 12:10:42 +00:00
|
|
|
// Services
|
|
|
|
import sdk from './services/sdk';
|
|
|
|
import toggleFavorite from './services/toggleFavorite';
|
2022-03-08 18:54:26 +00:00
|
|
|
import {
|
|
|
|
login,
|
|
|
|
loginTOTP,
|
|
|
|
loginWithPassword,
|
|
|
|
loginOAuthOrSso,
|
|
|
|
getLoginServices,
|
|
|
|
determineAuthType,
|
|
|
|
disconnect,
|
|
|
|
checkAndReopen,
|
|
|
|
abort,
|
|
|
|
getServerInfo,
|
|
|
|
getWebsocketInfo,
|
|
|
|
stopListener,
|
|
|
|
connect
|
|
|
|
} from './services/connect';
|
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';
|
2022-03-08 18:54:26 +00:00
|
|
|
export const MIN_ROCKETCHAT_VERSION = '0.70.0';
|
|
|
|
export const STATUSES = ['offline', 'online', 'away', 'busy'];
|
2019-05-28 16:52:26 +00:00
|
|
|
|
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,
|
2022-03-08 14:24:21 +00:00
|
|
|
...search,
|
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,
|
2022-03-08 18:54:26 +00:00
|
|
|
getWebsocketInfo,
|
|
|
|
getServerInfo,
|
|
|
|
stopListener,
|
2020-05-05 13:11:28 +00:00
|
|
|
// Abort all requests and create a new AbortController
|
2022-03-08 18:54:26 +00:00
|
|
|
abort,
|
|
|
|
checkAndReopen,
|
|
|
|
disconnect,
|
|
|
|
connect,
|
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 {
|
2022-03-09 19:41:26 +00:00
|
|
|
const certificate = UserPreferences.getString(`${RocketChat.CERTIFICATE_KEY}-${server}`);
|
|
|
|
SSLPinning.setCertificate(certificate, server);
|
2020-11-30 17:00:06 +00:00
|
|
|
} 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
|
2022-03-09 19:41:26 +00:00
|
|
|
const userId = UserPreferences.getString(`${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;
|
|
|
|
},
|
2022-03-08 18:54:26 +00:00
|
|
|
loginTOTP,
|
|
|
|
loginWithPassword,
|
|
|
|
loginOAuthOrSso,
|
|
|
|
login,
|
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,
|
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;
|
|
|
|
});
|
|
|
|
},
|
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
|
|
|
},
|
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);
|
|
|
|
},
|
2022-03-09 19:41:26 +00:00
|
|
|
getSortPreferences() {
|
|
|
|
return UserPreferences.getMap(SORT_PREFS_KEY);
|
2018-09-04 14:29:20 +00:00
|
|
|
},
|
2022-03-09 19:41:26 +00:00
|
|
|
saveSortPreference(param) {
|
|
|
|
let prefs = RocketChat.getSortPreferences();
|
2020-08-19 17:14:22 +00:00
|
|
|
prefs = { ...prefs, ...param };
|
2022-03-09 19:41:26 +00:00
|
|
|
return UserPreferences.setMap(SORT_PREFS_KEY, prefs);
|
2018-11-14 21:42:03 +00:00
|
|
|
},
|
2022-03-08 18:54:26 +00:00
|
|
|
getLoginServices,
|
|
|
|
determineAuthType,
|
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;
|