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';
|
2019-02-07 15:48:10 +00:00
|
|
|
|
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';
|
2019-06-26 19:50:03 +00:00
|
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
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-06-10 16:22:35 +00:00
|
|
|
|
import database from './realm';
|
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';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
|
import {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
setUser, setLoginServices, loginRequest, loginFailure, logout
|
2018-09-25 19:28:42 +00:00
|
|
|
|
} from '../actions/login';
|
2018-12-05 20:52:08 +00:00
|
|
|
|
import { disconnect, connectSuccess, connectRequest } from '../actions/connect';
|
2019-07-29 16:33:28 +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';
|
|
|
|
|
import subscribeRoom from './methods/subscriptions/room';
|
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';
|
|
|
|
|
import getSettings 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-06-17 13:57:07 +00:00
|
|
|
|
import getCustomEmojis 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';
|
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
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
import sendMessage, { getMessage, 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-06-10 16:23:19 +00:00
|
|
|
|
import { getDeviceToken } from '../notifications/push';
|
2019-08-22 14:32:23 +00:00
|
|
|
|
import { SERVERS, SERVER_URL } from '../constants/native';
|
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-05-21 12:12:15 +00:00
|
|
|
|
export const MARKDOWN_KEY = 'RC_MARKDOWN_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,
|
2018-04-24 19:34:03 +00:00
|
|
|
|
subscribeRooms,
|
|
|
|
|
subscribeRoom,
|
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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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-06-26 19:50:03 +00:00
|
|
|
|
return await RNUserDefaults.get(TOKEN_KEY);
|
2017-08-13 01:35:09 +00:00
|
|
|
|
} catch (error) {
|
2019-06-26 19:50:03 +00:00
|
|
|
|
console.warn(`RNUserDefaults error: ${ error.message }`);
|
2017-08-13 01:35:09 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-04-17 17:01:03 +00:00
|
|
|
|
async getServerInfo(server) {
|
2018-08-01 19:35:06 +00:00
|
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
|
const result = await fetch(`${ server }/api/info`).then(response => response.json());
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
return result;
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
2018-08-01 19:35:06 +00:00
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
|
log('err_get_server_info', e);
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'The_URL_is_invalid'
|
|
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
|
},
|
2017-12-08 19:13:21 +00:00
|
|
|
|
_setUser(ddpMessage) {
|
2018-02-16 22:57:46 +00:00
|
|
|
|
this.activeUsers = this.activeUsers || {};
|
2017-12-08 19:13:21 +00:00
|
|
|
|
const { user } = reduxStore.getState().login;
|
2018-02-16 22:57:46 +00:00
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
|
if (ddpMessage.fields && user && user.id === ddpMessage.id) {
|
2018-06-13 01:29:18 +00:00
|
|
|
|
reduxStore.dispatch(setUser(ddpMessage.fields));
|
2017-12-08 19:13:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 19:01:23 +00:00
|
|
|
|
if (ddpMessage.cleared && user && user.id === ddpMessage.id) {
|
|
|
|
|
reduxStore.dispatch(setUser({ status: 'offline' }));
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
|
if (!this._setUserTimer) {
|
|
|
|
|
this._setUserTimer = setTimeout(() => {
|
|
|
|
|
const batchUsers = this.activeUsers;
|
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2019-04-29 16:03:52 +00:00
|
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
|
Object.keys(batchUsers).forEach((key) => {
|
|
|
|
|
if (batchUsers[key] && batchUsers[key].id) {
|
|
|
|
|
try {
|
|
|
|
|
const data = batchUsers[key];
|
|
|
|
|
if (data.removed) {
|
|
|
|
|
const userRecord = database.memoryDatabase.objectForPrimaryKey('activeUsers', data.id);
|
|
|
|
|
if (userRecord) {
|
|
|
|
|
userRecord.status = 'offline';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
database.memoryDatabase.create('activeUsers', data, true);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-03-27 20:06:57 +00:00
|
|
|
|
});
|
|
|
|
|
this._setUserTimer = null;
|
|
|
|
|
return this.activeUsers = {};
|
|
|
|
|
}, 10000);
|
2018-02-16 22:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
|
if (!ddpMessage.fields) {
|
2019-04-29 16:03:52 +00:00
|
|
|
|
this.activeUsers[ddpMessage.id] = {
|
|
|
|
|
id: ddpMessage.id,
|
|
|
|
|
removed: true
|
|
|
|
|
};
|
2018-06-13 01:33:00 +00:00
|
|
|
|
} else {
|
2019-04-29 16:03:52 +00:00
|
|
|
|
this.activeUsers[ddpMessage.id] = {
|
|
|
|
|
id: ddpMessage.id, ...this.activeUsers[ddpMessage.id], ...ddpMessage.fields
|
|
|
|
|
};
|
2018-06-13 01:33:00 +00:00
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
connect({ server, user }) {
|
2019-06-05 19:11:29 +00:00
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
database.setActiveDB(server);
|
|
|
|
|
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-06-17 13:57:07 +00:00
|
|
|
|
if (this.roomsSub) {
|
|
|
|
|
this.roomsSub.stop();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
this.sdk.connect()
|
|
|
|
|
.then(() => {
|
|
|
|
|
if (user && user.token) {
|
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: user.token }));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.log('connect error', err);
|
|
|
|
|
|
|
|
|
|
// when `connect` raises an error, we try again in 10 seconds
|
|
|
|
|
this.connectTimeout = setTimeout(() => {
|
|
|
|
|
this.connect({ server, user });
|
|
|
|
|
}, 10000);
|
|
|
|
|
});
|
2019-02-07 15:48:10 +00:00
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
this.sdk.onStreamData('connected', () => {
|
|
|
|
|
reduxStore.dispatch(connectSuccess());
|
2019-06-17 13:57:07 +00:00
|
|
|
|
// const { isAuthenticated } = reduxStore.getState().login;
|
|
|
|
|
// if (isAuthenticated) {
|
|
|
|
|
// this.getUserPresence();
|
|
|
|
|
// }
|
2019-02-07 15:48:10 +00:00
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
this.sdk.onStreamData('close', () => {
|
|
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
|
});
|
2019-05-28 16:52:26 +00:00
|
|
|
|
|
2019-06-05 19:11:29 +00:00
|
|
|
|
this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
|
|
|
|
|
|
|
|
|
|
this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
|
|
|
|
|
const { eventName } = ddpMessage.fields;
|
|
|
|
|
if (eventName === 'user-status') {
|
|
|
|
|
const userStatus = ddpMessage.fields.args[0];
|
|
|
|
|
const [id, username, status] = userStatus;
|
|
|
|
|
if (username) {
|
|
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
|
try {
|
|
|
|
|
database.memoryDatabase.create('activeUsers', {
|
|
|
|
|
id, username, status: STATUSES[status]
|
|
|
|
|
}, true);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
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 16:33:28 +00:00
|
|
|
|
async shareExtensionInit(server) {
|
|
|
|
|
database.setActiveDB(server);
|
|
|
|
|
|
|
|
|
|
if (this.sdk) {
|
|
|
|
|
this.sdk.disconnect();
|
|
|
|
|
this.sdk = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use useSsl: false only if server url starts with http://
|
|
|
|
|
const useSsl = !/http:\/\//.test(server);
|
|
|
|
|
|
|
|
|
|
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
|
|
|
|
|
|
|
|
|
|
// set Server
|
|
|
|
|
const { serversDB } = database.databases;
|
|
|
|
|
reduxStore.dispatch(shareSelectServer(server));
|
|
|
|
|
|
|
|
|
|
// set User info
|
|
|
|
|
const userId = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
|
|
|
|
const user = userId && serversDB.objectForPrimaryKey('user', userId);
|
|
|
|
|
reduxStore.dispatch(shareSetUser({
|
|
|
|
|
id: user.id,
|
|
|
|
|
token: user.token,
|
|
|
|
|
username: user.username
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await RocketChat.login({ resume: user.token });
|
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
register(credentials) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.50.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('users.forgotPassword', { email }, false);
|
2017-11-10 13:42:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
async loginWithPassword({ user, password, code }) {
|
|
|
|
|
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 = {
|
2018-10-16 20:27:37 +00:00
|
|
|
|
...params,
|
|
|
|
|
crowd: true
|
2017-08-13 23:02:46 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 00:31:22 +00:00
|
|
|
|
if (code) {
|
|
|
|
|
params = {
|
2019-02-07 20:27:37 +00:00
|
|
|
|
user,
|
|
|
|
|
password,
|
2018-12-05 20:52:08 +00:00
|
|
|
|
code
|
2017-08-14 00:31:22 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
|
try {
|
|
|
|
|
return await this.login(params);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-08-09 17:28:46 +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 {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.64.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
await this.sdk.login(params);
|
|
|
|
|
const { result } = this.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,
|
|
|
|
|
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) {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
if (e.data && e.data.message && /you've been logged out by the server/i.test(e.data.message)) {
|
|
|
|
|
reduxStore.dispatch(logout({ server: this.sdk.client.host }));
|
|
|
|
|
} else {
|
|
|
|
|
reduxStore.dispatch(loginFailure(e));
|
|
|
|
|
}
|
2018-10-15 20:22:42 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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-06-26 19:50:03 +00:00
|
|
|
|
try {
|
|
|
|
|
const servers = await RNUserDefaults.objectForKey(SERVERS);
|
|
|
|
|
await RNUserDefaults.setObjectForKey(SERVERS, servers && servers.filter(srv => srv[SERVER_URL] !== server));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('logout_rn_user_defaults', error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { serversDB } = database.databases;
|
|
|
|
|
|
|
|
|
|
const userId = await RNUserDefaults.get(`${ TOKEN_KEY }-${ server }`);
|
|
|
|
|
|
|
|
|
|
serversDB.write(() => {
|
|
|
|
|
const user = serversDB.objectForPrimaryKey('user', userId);
|
|
|
|
|
serversDB.delete(user);
|
|
|
|
|
});
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
Promise.all([
|
2019-06-26 19:50:03 +00:00
|
|
|
|
RNUserDefaults.clear('currentServer'),
|
|
|
|
|
RNUserDefaults.clear(TOKEN_KEY),
|
|
|
|
|
RNUserDefaults.clear(`${ TOKEN_KEY }-${ server }`)
|
2018-12-05 20:52:08 +00:00
|
|
|
|
]).catch(error => console.log(error));
|
|
|
|
|
|
2018-10-18 17:56:49 +00:00
|
|
|
|
try {
|
2018-09-19 14:18:32 +00:00
|
|
|
|
database.deleteAll();
|
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
|
|
|
|
},
|
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
|
|
|
|
|
await this.sdk.post('push.token', data);
|
|
|
|
|
} 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
|
|
|
|
getMessage,
|
|
|
|
|
sendMessage,
|
|
|
|
|
getRooms,
|
|
|
|
|
readMessages,
|
2017-12-13 15:00:26 +00:00
|
|
|
|
async resendMessage(messageId) {
|
2017-12-27 15:22:06 +00:00
|
|
|
|
const message = await database.objects('messages').filtered('_id = $0', messageId)[0];
|
2018-12-05 20:52:08 +00:00
|
|
|
|
try {
|
|
|
|
|
database.write(() => {
|
|
|
|
|
message.status = messagesStatus.TEMP;
|
|
|
|
|
database.create('messages', message, true);
|
|
|
|
|
});
|
|
|
|
|
await sendMessageCall.call(this, JSON.parse(JSON.stringify(message)));
|
|
|
|
|
} catch (error) {
|
2019-02-25 16:23:17 +00:00
|
|
|
|
try {
|
|
|
|
|
database.write(() => {
|
|
|
|
|
message.status = messagesStatus.ERROR;
|
|
|
|
|
database.create('messages', message, true);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
|
log('err_resend_message', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
|
}
|
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 [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let data = database.objects('subscriptions').filtered('name CONTAINS[c] $0', searchText);
|
|
|
|
|
|
|
|
|
|
if (filterUsers && !filterRooms) {
|
|
|
|
|
data = data.filtered('t = $0', 'd');
|
|
|
|
|
} else if (!filterUsers && filterRooms) {
|
|
|
|
|
data = data.filtered('t != $0', 'd');
|
|
|
|
|
}
|
|
|
|
|
data = data.slice(0, 7);
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
data = data.concat(users.map(user => ({
|
|
|
|
|
...user,
|
|
|
|
|
rid: user.username,
|
|
|
|
|
name: user.username,
|
|
|
|
|
t: 'd',
|
|
|
|
|
search: true
|
|
|
|
|
})), rooms.map(room => ({
|
|
|
|
|
rid: room._id,
|
|
|
|
|
...room,
|
|
|
|
|
search: true
|
|
|
|
|
})));
|
|
|
|
|
}
|
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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('im.create', { username });
|
2017-08-10 16:16:32 +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') {
|
|
|
|
|
return this.sdk.methodCall('joinRoom', roomId);
|
|
|
|
|
}
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('channels.join', { roomId });
|
2017-08-10 20:09:54 +00:00
|
|
|
|
},
|
2018-07-17 19:10:27 +00:00
|
|
|
|
sendFileMessage,
|
|
|
|
|
cancelUpload,
|
|
|
|
|
isUploadActive,
|
2018-04-24 19:34:03 +00:00
|
|
|
|
getSettings,
|
|
|
|
|
getPermissions,
|
2019-06-17 13:57:07 +00:00
|
|
|
|
getCustomEmojis,
|
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) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
ret[item._id] = item[defaultSettings[item._id].type];
|
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;
|
|
|
|
|
});
|
|
|
|
|
},
|
2017-11-21 14:55:50 +00:00
|
|
|
|
deleteMessage(message) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
const { _id, rid } = message;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.48.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.delete', { roomId: rid, msgId: _id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
|
|
|
|
editMessage(message) {
|
|
|
|
|
const { _id, msg, rid } = message;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.update', { roomId: rid, msgId: _id, text: msg });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2017-11-24 20:44:52 +00:00
|
|
|
|
toggleStarMessage(message) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
if (message.starred) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.unStarMessage', { messageId: message._id });
|
2018-12-05 20:52:08 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.starMessage', { messageId: message._id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
|
|
|
|
togglePinMessage(message) {
|
|
|
|
|
if (message.pinned) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.unPinMessage', { messageId: message._id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.59.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('chat.pinMessage', { messageId: message._id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
2019-05-16 13:40:50 +00:00
|
|
|
|
reportMessage(messageId) {
|
|
|
|
|
return this.sdk.post('chat.reportMessage', { messageId, description: 'Message reported by user' });
|
|
|
|
|
},
|
2017-11-21 14:55:50 +00:00
|
|
|
|
getRoom(rid) {
|
2018-04-24 19:34:03 +00:00
|
|
|
|
const [result] = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
|
if (!result) {
|
2017-11-21 17:05:14 +00:00
|
|
|
|
return Promise.reject(new Error('Room not found'));
|
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
return Promise.resolve(result);
|
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 {
|
|
|
|
|
room = await RocketChat.getRoom(message.rid);
|
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
|
log('err_get_permalink', 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];
|
2018-04-24 19:34:03 +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
|
|
|
|
},
|
|
|
|
|
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();
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('stream-notify-room', `${ room }/typing`, login.user.username, t);
|
2017-12-04 18:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceAway() {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('UserPresence:away');
|
2017-12-04 18:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceOnline() {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('UserPresence:online');
|
2017-12-05 19:57:44 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceDefaultStatus(status) {
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('UserPresence:setDefaultStatus', status);
|
2018-01-30 19:48:26 +00:00
|
|
|
|
},
|
|
|
|
|
setReaction(emoji, messageId) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.62.2
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post('rooms.favorite', { roomId, favorite });
|
2018-03-02 15:11:34 +00:00
|
|
|
|
},
|
2019-07-01 14:20:38 +00:00
|
|
|
|
toggleRead(read, roomId) {
|
|
|
|
|
if (read) {
|
|
|
|
|
return this.sdk.post('subscriptions.unread', { roomId });
|
|
|
|
|
}
|
|
|
|
|
return this.sdk.post('subscriptions.read', { rid: roomId });
|
|
|
|
|
},
|
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
|
2019-04-17 17:01:03 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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 });
|
|
|
|
|
},
|
|
|
|
|
getRoomMemberId(rid, currentUserId) {
|
|
|
|
|
if (rid === `${ currentUserId }${ currentUserId }`) {
|
|
|
|
|
return currentUserId;
|
2018-03-29 17:55:37 +00:00
|
|
|
|
}
|
2019-04-29 16:03:52 +00:00
|
|
|
|
return rid.replace(currentUserId, '').trim();
|
2018-03-29 17:55:37 +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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('blockUser', { rid, blocked });
|
2018-03-23 16:49:51 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post(`${ this.roomTypeToApiType(t) }.leave`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
eraseRoom(roomId, t) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.49.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('muteUserInRoom', { rid, username });
|
2018-04-24 19:34:03 +00:00
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.post(`${ this.roomTypeToApiType(t) }.unarchive`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
2019-07-01 14:20:38 +00:00
|
|
|
|
hideRoom(roomId, t) {
|
|
|
|
|
return this.sdk.post(`${ this.roomTypeToApiType(t) }.close`, { roomId });
|
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
saveRoomSettings(rid, params) {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.55.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('saveRoomSettings', rid, params);
|
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
|
2019-06-05 16:29:07 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
|
|
|
|
return this.sdk.methodCall('getSingleMessage', msgId);
|
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
hasPermission(permissions, rid) {
|
2019-04-26 20:54:58 +00:00
|
|
|
|
let roomRoles = [];
|
2018-12-21 10:55:35 +00:00
|
|
|
|
try {
|
|
|
|
|
// get the room from realm
|
2019-04-08 12:35:28 +00:00
|
|
|
|
const [room] = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
|
if (!room) {
|
|
|
|
|
return permissions.reduce((result, permission) => {
|
|
|
|
|
result[permission] = false;
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// get room roles
|
2019-04-26 20:54:58 +00:00
|
|
|
|
roomRoles = room.roles;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('hasPermission -> error', error);
|
|
|
|
|
}
|
2018-06-18 13:30:36 +00:00
|
|
|
|
// get permissions from realm
|
|
|
|
|
const permissionsFiltered = database.objects('permissions')
|
|
|
|
|
.filter(permission => permissions.includes(permission._id));
|
2018-03-29 17:55:37 +00:00
|
|
|
|
// get user roles on the server from redux
|
2018-09-19 14:18:32 +00:00
|
|
|
|
const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || [];
|
2018-03-29 17:55:37 +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) => {
|
2018-06-18 13:30:36 +00:00
|
|
|
|
result[permission] = false;
|
|
|
|
|
const permissionFound = permissionsFiltered.find(p => p._id === permission);
|
|
|
|
|
if (permissionFound) {
|
2019-04-26 20:54:58 +00:00
|
|
|
|
result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r));
|
2018-06-18 13:30:36 +00:00
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
getAvatarSuggestion() {
|
2018-12-21 10:55:35 +00:00
|
|
|
|
// RC 0.51.0
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.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
|
2019-02-07 15:48:10 +00:00
|
|
|
|
return this.sdk.methodCall('setAvatarFromService', data, contentType, service);
|
2018-09-04 14:29:20 +00:00
|
|
|
|
},
|
2019-05-21 12:12:15 +00:00
|
|
|
|
async getUseMarkdown() {
|
|
|
|
|
const useMarkdown = await AsyncStorage.getItem(MARKDOWN_KEY);
|
|
|
|
|
if (useMarkdown === null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return JSON.parse(useMarkdown);
|
|
|
|
|
},
|
2018-09-04 14:29:20 +00:00
|
|
|
|
async getSortPreferences() {
|
2019-07-18 17:44:02 +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-18 17:44:02 +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-09 17:28:46 +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-07 14:36:54 +00:00
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
|
if (loginServicesResult.success && loginServicesResult.services.length > 0) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
2019-08-09 17:28:46 +00:00
|
|
|
|
return Promise.resolve(loginServices.length);
|
2018-11-14 21:42:03 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error);
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-09 17:28:46 +00:00
|
|
|
|
_determineAuthType(services) {
|
|
|
|
|
const { name, custom, service } = services;
|
2019-08-07 14:36:54 +00:00
|
|
|
|
|
|
|
|
|
if (custom) {
|
|
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 17:28:46 +00:00
|
|
|
|
// TODO: remove this after other oauth providers are implemented. e.g. Drupal, github_enterprise
|
|
|
|
|
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter'];
|
2019-08-07 14:36:54 +00:00
|
|
|
|
return availableOAuth.includes(name) ? 'oauth' : 'not_supported';
|
|
|
|
|
},
|
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 = {
|
|
|
|
|
c: 'channels', d: 'im', p: 'groups'
|
|
|
|
|
};
|
|
|
|
|
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) {
|
2019-05-15 19:33:30 +00:00
|
|
|
|
return this.sdk.post('chat.followMessage', { mid });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
}
|
2019-05-15 19:33:30 +00:00
|
|
|
|
return this.sdk.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
|
|
|
|
},
|
2019-06-10 18:36:56 +00:00
|
|
|
|
runSlashCommand(command, roomId, params) {
|
|
|
|
|
// RC 0.60.2
|
|
|
|
|
return this.sdk.post('commands.run', {
|
|
|
|
|
command, roomId, params
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getCommandPreview(command, roomId, params) {
|
|
|
|
|
// RC 0.65.0
|
|
|
|
|
return this.sdk.get('commands.preview', {
|
|
|
|
|
command, roomId, params
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
executeCommandPreview(command, params, roomId, previewItem) {
|
|
|
|
|
// RC 0.65.0
|
|
|
|
|
return this.sdk.post('commands.preview', {
|
|
|
|
|
command, params, roomId, previewItem
|
|
|
|
|
});
|
|
|
|
|
},
|
2019-06-17 13:57:07 +00:00
|
|
|
|
getUserPresence() {
|
|
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2019-05-28 16:52:26 +00:00
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
|
// if server is lower than 1.1.0
|
|
|
|
|
if (semver.lt(semver.coerce(serverVersion), '1.1.0')) {
|
|
|
|
|
if (this.activeUsersSubTimeout) {
|
|
|
|
|
clearTimeout(this.activeUsersSubTimeout);
|
|
|
|
|
this.activeUsersSubTimeout = false;
|
|
|
|
|
}
|
|
|
|
|
this.activeUsersSubTimeout = setTimeout(() => {
|
|
|
|
|
this.sdk.subscribe('activeUsers');
|
|
|
|
|
}, 5000);
|
|
|
|
|
return resolve();
|
|
|
|
|
} else {
|
|
|
|
|
const params = {};
|
|
|
|
|
// if (this.lastUserPresenceFetch) {
|
|
|
|
|
// params.from = this.lastUserPresenceFetch.toISOString();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// RC 1.1.0
|
|
|
|
|
const result = await this.sdk.get('users.presence', params);
|
|
|
|
|
if (result.success) {
|
|
|
|
|
// this.lastUserPresenceFetch = new Date();
|
|
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
|
result.users.forEach((item) => {
|
|
|
|
|
try {
|
|
|
|
|
item.id = item._id;
|
|
|
|
|
database.memoryDatabase.create('activeUsers', item, true);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-05-28 16:52:26 +00:00
|
|
|
|
});
|
2019-06-17 13:57:07 +00:00
|
|
|
|
this.sdk.subscribe('stream-notify-logged', 'user-status');
|
|
|
|
|
return resolve();
|
|
|
|
|
}
|
2019-05-28 16:52:26 +00:00
|
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
|
});
|
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-06-28 17:02:30 +00:00
|
|
|
|
},
|
|
|
|
|
canAutoTranslate() {
|
|
|
|
|
try {
|
|
|
|
|
const AutoTranslate_Enabled = reduxStore.getState().settings && reduxStore.getState().settings.AutoTranslate_Enabled;
|
|
|
|
|
if (!AutoTranslate_Enabled) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const autoTranslatePermission = database.objectForPrimaryKey('permissions', 'auto-translate');
|
|
|
|
|
const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || [];
|
|
|
|
|
return autoTranslatePermission.roles.some(role => userRoles.includes(role));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log('err_can_auto_translate', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
saveAutoTranslate({
|
|
|
|
|
rid, field, value, options
|
|
|
|
|
}) {
|
|
|
|
|
return this.sdk.methodCall('autoTranslate.saveSettings', rid, field, value, options);
|
|
|
|
|
},
|
|
|
|
|
getSupportedLanguagesAutoTranslate() {
|
|
|
|
|
return this.sdk.methodCall('autoTranslate.getSupportedLanguages', 'en');
|
|
|
|
|
},
|
|
|
|
|
translateMessage(message, targetLanguage) {
|
|
|
|
|
return this.sdk.methodCall('autoTranslate.translateMessage', message, targetLanguage);
|
2017-11-20 22:18:00 +00:00
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RocketChat;
|