2018-06-20 13:40:33 +00:00
|
|
|
|
import { AsyncStorage, Platform } from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
import foreach from 'lodash/forEach';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
2018-10-15 20:22:42 +00:00
|
|
|
|
import * as SDK from '@rocket.chat/sdk';
|
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';
|
2017-12-27 15:22:06 +00:00
|
|
|
|
import database from './realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
import log from '../utils/log';
|
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';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
import { setActiveUser } from '../actions/activeUsers';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
import { snippetedMessagesReceived } from '../actions/snippetedMessages';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
import { someoneTyping, roomMessageReceived } from '../actions/room';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
import { setRoles } from '../actions/roles';
|
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';
|
|
|
|
|
import getCustomEmoji from './methods/getCustomEmojis';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
import canOpenRoom from './methods/canOpenRoom';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
|
|
import _buildMessage from './methods/helpers/buildMessage';
|
|
|
|
|
import loadMessagesForRoom from './methods/loadMessagesForRoom';
|
|
|
|
|
import loadMissedMessages from './methods/loadMissedMessages';
|
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
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
|
import { getDeviceToken } from '../push';
|
|
|
|
|
|
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';
|
2018-10-15 20:22:42 +00:00
|
|
|
|
const call = (method, ...params) => SDK.driver.asyncCall(method, ...params);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
const returnAnArray = obj => obj || [];
|
2018-02-19 21:15:31 +00:00
|
|
|
|
|
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
|
|
|
|
|
}) {
|
|
|
|
|
return call(type ? 'createPrivateGroup' : 'createChannel', name, users, readOnly, {}, { broadcast });
|
2017-08-10 16:25:50 +00:00
|
|
|
|
},
|
2018-03-02 21:31:44 +00:00
|
|
|
|
async createDirectMessageAndWait(username) {
|
|
|
|
|
const room = await RocketChat.createDirectMessage(username);
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const data = database.objects('subscriptions')
|
|
|
|
|
.filtered('rid = $1', room.rid);
|
|
|
|
|
|
|
|
|
|
if (data.length) {
|
|
|
|
|
return resolve(data[0]);
|
|
|
|
|
}
|
|
|
|
|
data.addListener(() => {
|
|
|
|
|
if (!data.length) { return; }
|
|
|
|
|
data.removeAllListeners();
|
|
|
|
|
resolve(data[0]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
2017-08-13 01:35:09 +00:00
|
|
|
|
|
|
|
|
|
async getUserToken() {
|
|
|
|
|
try {
|
|
|
|
|
return await AsyncStorage.getItem(TOKEN_KEY);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(`AsyncStorage error: ${ error.message }`);
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-07-10 13:41:26 +00:00
|
|
|
|
_hasInstanceId(headers) {
|
2018-11-28 12:10:22 +00:00
|
|
|
|
return !!headers[Object.keys(headers).find(item => item.toLowerCase() === 'x-instance-id')];
|
2018-07-10 13:41:26 +00:00
|
|
|
|
},
|
2017-09-01 19:42:50 +00:00
|
|
|
|
async testServer(url) {
|
2018-08-01 19:35:06 +00:00
|
|
|
|
try {
|
|
|
|
|
let response = await RNFetchBlob.fetch('HEAD', url);
|
|
|
|
|
response = response.respInfo;
|
|
|
|
|
if (response.status === 200 && RocketChat._hasInstanceId(response.headers)) {
|
|
|
|
|
return url;
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
2018-08-01 19:35:06 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
log('testServer', e);
|
2017-09-01 19:42:50 +00:00
|
|
|
|
}
|
|
|
|
|
throw new Error({ error: 'invalid server' });
|
|
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 22:57:46 +00:00
|
|
|
|
if (this._setUserTimer) {
|
|
|
|
|
clearTimeout(this._setUserTimer);
|
|
|
|
|
this._setUserTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._setUserTimer = setTimeout(() => {
|
2018-03-29 17:55:37 +00:00
|
|
|
|
reduxStore.dispatch(setActiveUser(this.activeUsers));
|
2018-02-16 22:57:46 +00:00
|
|
|
|
this._setUserTimer = null;
|
|
|
|
|
return this.activeUsers = {};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
}, 2000);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
|
const activeUser = reduxStore.getState().activeUsers[ddpMessage.id];
|
|
|
|
|
if (!ddpMessage.fields) {
|
|
|
|
|
this.activeUsers[ddpMessage.id] = {};
|
|
|
|
|
} else {
|
|
|
|
|
this.activeUsers[ddpMessage.id] = { ...this.activeUsers[ddpMessage.id], ...activeUser, ...ddpMessage.fields };
|
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
loginSuccess({ user }) {
|
|
|
|
|
SDK.driver.login({ resume: user.token });
|
|
|
|
|
reduxStore.dispatch(setUser(user));
|
|
|
|
|
this.getRooms().catch(e => console.log(e));
|
|
|
|
|
this.getPermissions();
|
|
|
|
|
this.getCustomEmoji();
|
|
|
|
|
this.registerPushToken().then(result => console.log(result)).catch(e => alert(e));
|
|
|
|
|
},
|
|
|
|
|
connect({ server, user }) {
|
|
|
|
|
database.setActiveDB(server);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
if (this.ddp) {
|
|
|
|
|
RocketChat.disconnect();
|
|
|
|
|
this.ddp = null;
|
2018-11-14 21:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.api.setBaseUrl(server);
|
|
|
|
|
this.getSettings();
|
2018-10-15 20:22:42 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
if (user && user.token) {
|
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: user.token }));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
}
|
2017-11-19 02:44:55 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
// Use useSsl: false only if server url starts with http://
|
|
|
|
|
const useSsl = !/http:\/\//.test(server);
|
2018-10-23 21:39:48 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
reduxStore.dispatch(connectRequest());
|
|
|
|
|
SDK.driver.connect({ host: server, useSsl }, (err, ddp) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return console.warn(err);
|
2018-09-11 16:32:52 +00:00
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
this.ddp = ddp;
|
|
|
|
|
if (user && user.token) {
|
|
|
|
|
SDK.driver.login({ resume: user.token });
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('connected', () => {
|
|
|
|
|
reduxStore.dispatch(connectSuccess());
|
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('disconnected', protectedFunction(() => {
|
|
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
|
}));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('logged', protectedFunction((error, u) => {
|
|
|
|
|
this.subscribeRooms(u.id);
|
|
|
|
|
SDK.driver.subscribe('activeUsers');
|
|
|
|
|
SDK.driver.subscribe('roles');
|
|
|
|
|
}));
|
2018-06-20 13:40:33 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('forbidden', protectedFunction(() => reduxStore.dispatch(logout())));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('users', protectedFunction((error, ddpMessage) => RocketChat._setUser(ddpMessage)));
|
2017-08-17 16:55:47 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('stream-room-messages', (error, ddpMessage) => {
|
|
|
|
|
// TODO: debounce
|
|
|
|
|
const message = _buildMessage(ddpMessage.fields.args[0]);
|
|
|
|
|
requestAnimationFrame(() => reduxStore.dispatch(roomMessageReceived(message)));
|
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('stream-notify-room', protectedFunction((error, ddpMessage) => {
|
|
|
|
|
const [_rid, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
|
if (ev === 'typing') {
|
|
|
|
|
reduxStore.dispatch(someoneTyping({ _rid, username: ddpMessage.fields.args[0], typing: ddpMessage.fields.args[1] }));
|
|
|
|
|
} else if (ev === 'deleteMessage') {
|
|
|
|
|
database.write(() => {
|
|
|
|
|
if (ddpMessage && ddpMessage.fields && ddpMessage.fields.args.length > 0) {
|
|
|
|
|
const { _id } = ddpMessage.fields.args[0];
|
|
|
|
|
const message = database.objects('messages').filtered('_id = $0', _id);
|
|
|
|
|
database.delete(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('rocketchat_snippeted_message', protectedFunction((error, ddpMessage) => {
|
|
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
|
this.snippetedMessages = this.snippetedMessages || [];
|
2018-03-02 15:11:34 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
if (this.snippetedMessagesTimer) {
|
|
|
|
|
clearTimeout(this.snippetedMessagesTimer);
|
|
|
|
|
this.snippetedMessagesTimer = null;
|
2018-02-19 21:19:39 +00:00
|
|
|
|
}
|
2018-02-23 20:29:06 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
this.snippetedMessagesTimer = setTimeout(() => {
|
|
|
|
|
reduxStore.dispatch(snippetedMessagesReceived(this.snippetedMessages));
|
|
|
|
|
this.snippetedMessagesTimer = null;
|
|
|
|
|
return this.snippetedMessages = [];
|
|
|
|
|
}, 1000);
|
|
|
|
|
const message = ddpMessage.fields;
|
|
|
|
|
message._id = ddpMessage.id;
|
|
|
|
|
const snippetedMessage = _buildMessage(message);
|
|
|
|
|
this.snippetedMessages = [...this.snippetedMessages, snippetedMessage];
|
|
|
|
|
}
|
|
|
|
|
}));
|
2018-03-23 16:49:51 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.driver.on('rocketchat_roles', protectedFunction((error, ddpMessage) => {
|
|
|
|
|
this.roles = this.roles || {};
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
if (this.roleTimer) {
|
|
|
|
|
clearTimeout(this.roleTimer);
|
|
|
|
|
this.roleTimer = null;
|
|
|
|
|
}
|
|
|
|
|
this.roleTimer = setTimeout(() => {
|
|
|
|
|
reduxStore.dispatch(setRoles(this.roles));
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
database.write(() => {
|
|
|
|
|
foreach(this.roles, (description, _id) => {
|
|
|
|
|
database.create('roles', { _id, description }, true);
|
2018-03-29 17:55:37 +00:00
|
|
|
|
});
|
2018-12-05 20:52:08 +00:00
|
|
|
|
});
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
this.roleTimer = null;
|
|
|
|
|
return this.roles = {};
|
|
|
|
|
}, 1000);
|
|
|
|
|
this.roles[ddpMessage.id] = (ddpMessage.fields && ddpMessage.fields.description) || undefined;
|
|
|
|
|
}));
|
2018-11-16 11:06:29 +00:00
|
|
|
|
},
|
2017-11-24 20:44:52 +00:00
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
register(credentials) {
|
|
|
|
|
return SDK.api.post('users.register', credentials, false);
|
2017-11-07 16:28:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
setUsername(username) {
|
2018-11-14 21:42:03 +00:00
|
|
|
|
return call('setUsername', username);
|
2017-11-07 16:28:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
|
forgotPassword(email) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.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 = {
|
2018-10-16 20:27:37 +00:00
|
|
|
|
...params,
|
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 = {
|
2018-10-16 20:27:37 +00:00
|
|
|
|
...params,
|
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
|
|
|
|
},
|
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
|
async loginOAuth(params) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await SDK.driver.login(params);
|
|
|
|
|
reduxStore.dispatch(loginRequest({ resume: result.token }));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
|
async login(params) {
|
|
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return await SDK.api.login(params);
|
2018-10-15 20:22:42 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
reduxStore.dispatch(loginFailure(e));
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
async logout({ server }) {
|
|
|
|
|
// this.removePushToken().catch(error => console.log(error));
|
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 {
|
|
|
|
|
await SDK.api.logout();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('logout -> api logout -> catch -> error', error);
|
|
|
|
|
}
|
|
|
|
|
SDK.driver.ddp.disconnect();
|
|
|
|
|
this.ddp = null;
|
|
|
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
|
AsyncStorage.removeItem('currentServer'),
|
|
|
|
|
AsyncStorage.removeItem(TOKEN_KEY),
|
|
|
|
|
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`)
|
|
|
|
|
]).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
|
|
|
|
},
|
|
|
|
|
disconnect() {
|
|
|
|
|
try {
|
|
|
|
|
SDK.driver.unsubscribeAll();
|
|
|
|
|
} catch (error) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
console.log(error);
|
2018-10-15 20:22:42 +00:00
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
RocketChat.setApiUser({ userId: null, authToken: null });
|
|
|
|
|
},
|
|
|
|
|
setApiUser({ userId, authToken }) {
|
|
|
|
|
SDK.api.setAuth({ userId, authToken });
|
2018-12-05 20:52:08 +00:00
|
|
|
|
SDK.api.currentLogin = null;
|
|
|
|
|
},
|
|
|
|
|
registerPushToken() {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const token = getDeviceToken();
|
|
|
|
|
if (token) {
|
|
|
|
|
const type = Platform.OS === 'ios' ? 'apn' : 'gcm';
|
|
|
|
|
const data = {
|
|
|
|
|
value: token,
|
|
|
|
|
type,
|
|
|
|
|
appName: 'chat.rocket.reactnative' // TODO: try to get from config file
|
|
|
|
|
};
|
|
|
|
|
return SDK.api.post('push.token', data);
|
|
|
|
|
}
|
|
|
|
|
return resolve();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
removePushToken() {
|
|
|
|
|
const token = getDeviceToken();
|
|
|
|
|
if (token) {
|
|
|
|
|
return SDK.api.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,
|
|
|
|
|
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) {
|
|
|
|
|
database.write(() => {
|
|
|
|
|
message.status = messagesStatus.ERROR;
|
|
|
|
|
database.create('messages', message, true);
|
|
|
|
|
});
|
|
|
|
|
}
|
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);
|
2018-10-23 21:39:48 +00:00
|
|
|
|
const array = Array.from(data);
|
|
|
|
|
data = JSON.parse(JSON.stringify(array));
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
|
|
const usernames = data.map(sub => sub.name);
|
|
|
|
|
try {
|
|
|
|
|
if (data.length < 7) {
|
|
|
|
|
const { users, rooms } = await Promise.race([
|
|
|
|
|
RocketChat.spotlight(searchText, usernames, { users: filterUsers, rooms: filterRooms }),
|
|
|
|
|
new Promise((resolve, reject) => this.oldPromise = reject)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
return call('spotlight', search, usernames, type);
|
2017-08-10 16:16:32 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
createDirectMessage(username) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.post('im.create', { username });
|
2017-08-10 16:16:32 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
joinRoom(roomId) {
|
|
|
|
|
// TODO: join code
|
|
|
|
|
return SDK.api.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,
|
|
|
|
|
getCustomEmoji,
|
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;
|
|
|
|
|
});
|
|
|
|
|
},
|
2018-01-16 18:48:05 +00:00
|
|
|
|
parseEmojis: emojis => emojis.reduce((ret, item) => {
|
|
|
|
|
ret[item.name] = item.extension;
|
|
|
|
|
item.aliases.forEach((alias) => {
|
|
|
|
|
ret[alias.value] = item.extension;
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
}, {}),
|
|
|
|
|
_prepareEmojis(emojis) {
|
|
|
|
|
emojis.forEach((emoji) => {
|
|
|
|
|
emoji.aliases = emoji.aliases.map(alias => ({ value: alias }));
|
|
|
|
|
});
|
|
|
|
|
return emojis;
|
|
|
|
|
},
|
2017-11-21 14:55:50 +00:00
|
|
|
|
deleteMessage(message) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
const { _id, rid } = message;
|
|
|
|
|
return SDK.api.post('chat.delete', { roomId: rid, msgId: _id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
|
|
|
|
editMessage(message) {
|
|
|
|
|
const { _id, msg, rid } = message;
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.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) {
|
|
|
|
|
return SDK.api.post('chat.unStarMessage', { messageId: message._id });
|
|
|
|
|
}
|
|
|
|
|
return SDK.api.post('chat.starMessage', { messageId: message._id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
},
|
|
|
|
|
togglePinMessage(message) {
|
|
|
|
|
if (message.pinned) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.post('chat.unPinMessage', { messageId: message._id });
|
2017-11-21 14:55:50 +00:00
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.post('chat.pinMessage', { messageId: message._id });
|
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
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
getRoomInfo(roomId) {
|
|
|
|
|
return SDK.api.get('rooms.info', { roomId });
|
|
|
|
|
},
|
2017-11-21 14:55:50 +00:00
|
|
|
|
async getPermalink(message) {
|
2018-05-18 17:55:08 +00:00
|
|
|
|
let room;
|
|
|
|
|
try {
|
|
|
|
|
room = await RocketChat.getRoom(message.rid);
|
|
|
|
|
} catch (e) {
|
2018-10-15 20:22:42 +00:00
|
|
|
|
log('SDK.getPermalink', 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
|
|
|
|
},
|
2017-11-20 22:18:00 +00:00
|
|
|
|
subscribe(...args) {
|
2018-10-15 20:22:42 +00:00
|
|
|
|
return SDK.driver.subscribe(...args);
|
|
|
|
|
},
|
|
|
|
|
unsubscribe(subscription) {
|
|
|
|
|
return SDK.driver.unsubscribe(subscription);
|
2017-11-20 22:18:00 +00:00
|
|
|
|
},
|
2017-11-21 16:55:32 +00:00
|
|
|
|
emitTyping(room, t = true) {
|
|
|
|
|
const { login } = reduxStore.getState();
|
|
|
|
|
return call('stream-notify-room', `${ room }/typing`, login.user.username, t);
|
2017-12-04 18:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceAway() {
|
|
|
|
|
return call('UserPresence:away');
|
|
|
|
|
},
|
|
|
|
|
setUserPresenceOnline() {
|
|
|
|
|
return call('UserPresence:online');
|
2017-12-05 19:57:44 +00:00
|
|
|
|
},
|
|
|
|
|
setUserPresenceDefaultStatus(status) {
|
|
|
|
|
return call('UserPresence:setDefaultStatus', status);
|
2018-01-30 19:48:26 +00:00
|
|
|
|
},
|
|
|
|
|
setReaction(emoji, messageId) {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.post('chat.react', { emoji, messageId });
|
2018-02-19 21:19:39 +00:00
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
toggleFavorite(roomId, favorite) {
|
|
|
|
|
return SDK.api.post('rooms.favorite', { roomId, favorite });
|
2018-03-02 15:11:34 +00:00
|
|
|
|
},
|
|
|
|
|
getRoomMembers(rid, allUsers) {
|
|
|
|
|
return call('getUsersOfRoom', rid, allUsers);
|
2018-03-23 16:49:51 +00:00
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
getUserRoles() {
|
|
|
|
|
return call('getUserRoles');
|
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
getRoomCounters(roomId, t) {
|
|
|
|
|
return SDK.api.get(`${ this.roomTypeToApiType(t) }.counters`, { roomId });
|
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
async getRoomMember(rid, currentUserId) {
|
|
|
|
|
try {
|
|
|
|
|
const membersResult = await RocketChat.getRoomMembers(rid, true);
|
2018-12-12 15:15:10 +00:00
|
|
|
|
return Promise.resolve(membersResult.records.find(m => m._id !== currentUserId));
|
2018-03-29 17:55:37 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-03-23 16:49:51 +00:00
|
|
|
|
toggleBlockUser(rid, blocked, block) {
|
|
|
|
|
if (block) {
|
|
|
|
|
return call('blockUser', { rid, blocked });
|
|
|
|
|
}
|
|
|
|
|
return call('unblockUser', { rid, blocked });
|
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
leaveRoom(roomId, t) {
|
|
|
|
|
return SDK.api.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) {
|
|
|
|
|
return SDK.api.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) {
|
|
|
|
|
return call('muteUserInRoom', { rid, username });
|
|
|
|
|
}
|
|
|
|
|
return call('unmuteUserInRoom', { rid, username });
|
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
toggleArchiveRoom(roomId, t, archive) {
|
2018-03-29 17:55:37 +00:00
|
|
|
|
if (archive) {
|
2018-12-12 15:15:10 +00:00
|
|
|
|
return SDK.api.post(`${ this.roomTypeToApiType(t) }.archive`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
|
return SDK.api.post(`${ this.roomTypeToApiType(t) }.unarchive`, { roomId });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
},
|
|
|
|
|
saveRoomSettings(rid, params) {
|
|
|
|
|
return call('saveRoomSettings', rid, params);
|
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
saveUserProfile(data) {
|
|
|
|
|
return SDK.api.post('users.updateOwnBasicInfo', { data });
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
saveUserPreferences(params) {
|
|
|
|
|
return call('saveUserPreferences', params);
|
|
|
|
|
},
|
2018-12-05 20:52:08 +00:00
|
|
|
|
saveNotificationSettings(roomId, notifications) {
|
|
|
|
|
return SDK.api.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);
|
|
|
|
|
return call('addUsersToRoom', { rid, users });
|
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
|
hasPermission(permissions, rid) {
|
|
|
|
|
// get the room from realm
|
|
|
|
|
const room = database.objects('subscriptions').filtered('rid = $0', rid)[0];
|
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 room roles
|
|
|
|
|
const { roles } = room;
|
|
|
|
|
// transform room roles to array
|
|
|
|
|
const roomRoles = Array.from(Object.keys(roles), i => roles[i].value);
|
|
|
|
|
// 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) {
|
|
|
|
|
result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r.value));
|
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
getAvatarSuggestion() {
|
|
|
|
|
return call('getAvatarSuggestion');
|
|
|
|
|
},
|
2018-12-12 15:15:10 +00:00
|
|
|
|
resetAvatar(userId) {
|
|
|
|
|
return SDK.api.post('users.resetAvatar', { userId });
|
2018-06-13 01:33:00 +00:00
|
|
|
|
},
|
|
|
|
|
setAvatarFromService({ data, contentType = '', service = null }) {
|
|
|
|
|
return call('setAvatarFromService', data, contentType, service);
|
2018-09-04 14:29:20 +00:00
|
|
|
|
},
|
|
|
|
|
async getSortPreferences() {
|
|
|
|
|
const prefs = await AsyncStorage.getItem(SORT_PREFS_KEY);
|
|
|
|
|
return JSON.parse(prefs);
|
|
|
|
|
},
|
|
|
|
|
async saveSortPreference(param) {
|
|
|
|
|
try {
|
|
|
|
|
let prefs = await RocketChat.getSortPreferences();
|
|
|
|
|
prefs = { ...prefs, ...param };
|
|
|
|
|
return await AsyncStorage.setItem(SORT_PREFS_KEY, JSON.stringify(prefs));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error);
|
|
|
|
|
}
|
2018-11-14 21:42:03 +00:00
|
|
|
|
},
|
|
|
|
|
async getLoginServices(server) {
|
|
|
|
|
try {
|
|
|
|
|
let loginServicesFilter = [];
|
|
|
|
|
const loginServicesResult = await fetch(`${ server }/api/v1/settings.oauth`).then(response => response.json());
|
|
|
|
|
// TODO: remove this after SAML and custom oauth
|
|
|
|
|
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter'];
|
|
|
|
|
if (loginServicesResult.success && loginServicesResult.services.length > 0) {
|
|
|
|
|
const { services } = loginServicesResult;
|
|
|
|
|
loginServicesFilter = services.filter(item => availableOAuth.includes(item.name));
|
|
|
|
|
const loginServicesReducer = loginServicesFilter.reduce((ret, item) => {
|
|
|
|
|
ret[item.name] = item;
|
|
|
|
|
return ret;
|
|
|
|
|
}, {});
|
|
|
|
|
reduxStore.dispatch(setLoginServices(loginServicesReducer));
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve(loginServicesFilter.length);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error);
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
getUsernameSuggestion() {
|
2018-12-05 20:52:08 +00:00
|
|
|
|
return SDK.api.get('users.getUsernameSuggestion');
|
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
|
return SDK.api.get(`${ this.roomTypeToApiType(type) }.files`, {
|
|
|
|
|
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) {
|
|
|
|
|
return SDK.api.get(`${ this.roomTypeToApiType(type) }.messages`, {
|
|
|
|
|
roomId,
|
|
|
|
|
query,
|
|
|
|
|
offset,
|
|
|
|
|
sort: { ts: -1 }
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
searchMessages(roomId, searchText) {
|
|
|
|
|
return SDK.api.get('chat.search', {
|
|
|
|
|
roomId,
|
|
|
|
|
searchText
|
|
|
|
|
});
|
2017-11-20 22:18:00 +00:00
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RocketChat;
|