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
|
|
|
// import * as actions from '../actions';
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-11-14 21:42:03 +00:00
|
|
|
setUser, setLoginServices, loginRequest, loginSuccess, loginFailure, logout
|
2018-09-25 19:28:42 +00:00
|
|
|
} from '../actions/login';
|
2018-10-15 20:22:42 +00:00
|
|
|
import { disconnect, connectSuccess } from '../actions/connect';
|
2018-03-29 17:55:37 +00:00
|
|
|
import { setActiveUser } from '../actions/activeUsers';
|
2018-03-02 15:11:34 +00:00
|
|
|
import { starredMessagesReceived, starredMessageUnstarred } from '../actions/starredMessages';
|
|
|
|
import { pinnedMessagesReceived, pinnedMessageUnpinned } from '../actions/pinnedMessages';
|
|
|
|
import { mentionedMessagesReceived } from '../actions/mentionedMessages';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { snippetedMessagesReceived } from '../actions/snippetedMessages';
|
|
|
|
import { roomFilesReceived } from '../actions/roomFiles';
|
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-04-24 19:34:03 +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) {
|
|
|
|
return (headers['x-instance-id'] != null && headers['x-instance-id'].length > 0) || (headers['X-Instance-ID'] != null && headers['X-Instance-ID'].length > 0);
|
|
|
|
},
|
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-04-24 19:34:03 +00:00
|
|
|
async loginSuccess(user) {
|
2018-11-14 21:42:03 +00:00
|
|
|
if (!user) {
|
|
|
|
const { user: u } = reduxStore.getState().login;
|
|
|
|
user = Object.assign({}, u);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
// TODO: one api call
|
|
|
|
// call /me only one time
|
|
|
|
try {
|
2018-05-18 17:55:08 +00:00
|
|
|
if (!user.username) {
|
2018-11-14 21:42:03 +00:00
|
|
|
// get me from api
|
|
|
|
let me = await SDK.api.get('me');
|
|
|
|
// if server didn't found username
|
|
|
|
if (!me.username) {
|
|
|
|
// search username from credentials (sent during registerSubmit)
|
|
|
|
const { username } = reduxStore.getState().login.credentials;
|
|
|
|
if (username) {
|
|
|
|
// set username
|
|
|
|
await RocketChat.setUsername({ username });
|
|
|
|
me = { ...me, username };
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
user = { ...user, ...me };
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2018-11-14 21:42:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('SDK.loginSuccess set username', e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-05-18 17:55:08 +00:00
|
|
|
if (user.username) {
|
2018-10-15 20:22:42 +00:00
|
|
|
const userInfo = await SDK.api.get('users.info', { userId: user.id });
|
2018-06-13 01:33:00 +00:00
|
|
|
user = { ...user, ...userInfo.user };
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-10-15 20:22:42 +00:00
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
RocketChat.registerPushToken(user.id);
|
2018-10-15 20:22:42 +00:00
|
|
|
reduxStore.dispatch(setUser(user));
|
2018-08-31 16:46:33 +00:00
|
|
|
reduxStore.dispatch(loginSuccess(user));
|
|
|
|
this.ddp.subscribe('userData');
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-10-15 20:22:42 +00:00
|
|
|
log('SDK.loginSuccess', e);
|
2017-12-20 19:20:06 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
connect(url, login) {
|
2018-10-23 21:39:48 +00:00
|
|
|
return new Promise(() => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (this.ddp) {
|
2018-10-15 20:22:42 +00:00
|
|
|
RocketChat.disconnect();
|
|
|
|
this.ddp = null;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-11-19 02:44:55 +00:00
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
SDK.api.setBaseUrl(url);
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
if (login) {
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.api.setAuth({ authToken: login.token, userId: login.id });
|
2018-10-23 21:39:48 +00:00
|
|
|
RocketChat.setApiUser({ userId: login.id, authToken: login.token });
|
2018-09-11 16:32:52 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.connect({ host: url, useSsl: true }, (err, ddp) => {
|
|
|
|
if (err) {
|
|
|
|
return console.warn(err);
|
|
|
|
}
|
|
|
|
this.ddp = ddp;
|
|
|
|
if (login) {
|
2018-10-16 20:27:37 +00:00
|
|
|
SDK.driver.login({ resume: login.resume });
|
2018-10-15 20:22:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
SDK.driver.on('connected', () => {
|
|
|
|
reduxStore.dispatch(connectSuccess());
|
|
|
|
SDK.driver.subscribe('meteor.loginServiceConfiguration');
|
|
|
|
SDK.driver.subscribe('activeUsers');
|
|
|
|
SDK.driver.subscribe('roles');
|
|
|
|
RocketChat.getSettings();
|
|
|
|
RocketChat.getPermissions();
|
|
|
|
RocketChat.getCustomEmoji();
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('login', protectedFunction(() => reduxStore.dispatch(loginRequest())));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('forbidden', protectedFunction(() => reduxStore.dispatch(logout())));
|
2018-06-20 13:40:33 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('users', protectedFunction((error, ddpMessage) => RocketChat._setUser(ddpMessage)));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
// SDK.driver.on('background', () => this.getRooms().catch(e => log('background getRooms', e)));
|
2017-08-17 16:55:47 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('logged', protectedFunction((error, user) => {
|
2018-10-23 21:39:48 +00:00
|
|
|
RocketChat.setApiUser({ userId: user.id, authToken: user.token });
|
2018-04-24 19:34:03 +00:00
|
|
|
this.loginSuccess(user);
|
2018-07-10 13:40:32 +00:00
|
|
|
this.getRooms().catch(e => log('logged getRooms', e));
|
2018-10-15 20:22:42 +00:00
|
|
|
this.subscribeRooms(user.id);
|
2018-06-13 01:33:00 +00:00
|
|
|
}));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('disconnected', protectedFunction(() => {
|
2018-04-24 19:34:03 +00:00
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
}));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('stream-room-messages', (error, ddpMessage) => {
|
2018-05-23 13:39:18 +00:00
|
|
|
// TODO: debounce
|
2018-04-24 19:34:03 +00:00
|
|
|
const message = _buildMessage(ddpMessage.fields.args[0]);
|
|
|
|
requestAnimationFrame(() => reduxStore.dispatch(roomMessageReceived(message)));
|
2018-01-15 18:44:20 +00:00
|
|
|
});
|
2017-12-20 19:20:06 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('stream-notify-room', protectedFunction((error, ddpMessage) => {
|
2017-12-20 19:20:06 +00:00
|
|
|
const [_rid, ev] = ddpMessage.fields.eventName.split('/');
|
2018-09-28 18:57:29 +00:00
|
|
|
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-04-24 19:34:03 +00:00
|
|
|
}));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('rocketchat_starred_message', protectedFunction((error, ddpMessage) => {
|
2018-02-19 21:19:39 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
2018-03-02 15:11:34 +00:00
|
|
|
this.starredMessages = this.starredMessages || [];
|
|
|
|
|
|
|
|
if (this.starredMessagesTimer) {
|
|
|
|
clearTimeout(this.starredMessagesTimer);
|
|
|
|
this.starredMessagesTimer = null;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.starredMessagesTimer = setTimeout(protectedFunction(() => {
|
2018-03-02 15:11:34 +00:00
|
|
|
reduxStore.dispatch(starredMessagesReceived(this.starredMessages));
|
|
|
|
this.starredMessagesTimer = null;
|
|
|
|
return this.starredMessages = [];
|
2018-04-24 19:34:03 +00:00
|
|
|
}), 1000);
|
2018-02-19 21:19:39 +00:00
|
|
|
const message = ddpMessage.fields;
|
|
|
|
message._id = ddpMessage.id;
|
2018-04-24 19:34:03 +00:00
|
|
|
const starredMessage = _buildMessage(message);
|
2018-03-02 15:11:34 +00:00
|
|
|
this.starredMessages = [...this.starredMessages, starredMessage];
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
if (ddpMessage.msg === 'removed') {
|
2018-03-02 15:11:34 +00:00
|
|
|
if (reduxStore.getState().starredMessages.isOpen) {
|
|
|
|
return reduxStore.dispatch(starredMessageUnstarred(ddpMessage.id));
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('rocketchat_pinned_message', protectedFunction((error, ddpMessage) => {
|
2018-02-19 21:19:39 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
2018-03-02 15:11:34 +00:00
|
|
|
this.pinnedMessages = this.pinnedMessages || [];
|
|
|
|
|
|
|
|
if (this.pinnedMessagesTimer) {
|
|
|
|
clearTimeout(this.pinnedMessagesTimer);
|
|
|
|
this.pinnedMessagesTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pinnedMessagesTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(pinnedMessagesReceived(this.pinnedMessages));
|
|
|
|
this.pinnedMessagesTimer = null;
|
|
|
|
return this.pinnedMessages = [];
|
|
|
|
}, 1000);
|
2018-02-19 21:19:39 +00:00
|
|
|
const message = ddpMessage.fields;
|
|
|
|
message._id = ddpMessage.id;
|
2018-04-24 19:34:03 +00:00
|
|
|
const pinnedMessage = _buildMessage(message);
|
2018-03-02 15:11:34 +00:00
|
|
|
this.pinnedMessages = [...this.pinnedMessages, pinnedMessage];
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
if (ddpMessage.msg === 'removed') {
|
2018-03-02 15:11:34 +00:00
|
|
|
if (reduxStore.getState().pinnedMessages.isOpen) {
|
|
|
|
return reduxStore.dispatch(pinnedMessageUnpinned(ddpMessage.id));
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('rocketchat_mentioned_message', protectedFunction((error, ddpMessage) => {
|
2018-03-02 15:11:34 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
this.mentionedMessages = this.mentionedMessages || [];
|
|
|
|
|
|
|
|
if (this.mentionedMessagesTimer) {
|
|
|
|
clearTimeout(this.mentionedMessagesTimer);
|
|
|
|
this.mentionedMessagesTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mentionedMessagesTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(mentionedMessagesReceived(this.mentionedMessages));
|
|
|
|
this.mentionedMessagesTimer = null;
|
|
|
|
return this.mentionedMessages = [];
|
|
|
|
}, 1000);
|
|
|
|
const message = ddpMessage.fields;
|
|
|
|
message._id = ddpMessage.id;
|
2018-04-24 19:34:03 +00:00
|
|
|
const mentionedMessage = _buildMessage(message);
|
2018-03-02 15:11:34 +00:00
|
|
|
this.mentionedMessages = [...this.mentionedMessages, mentionedMessage];
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-02-23 20:29:06 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('rocketchat_snippeted_message', protectedFunction((error, ddpMessage) => {
|
2018-03-23 16:49:51 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
this.snippetedMessages = this.snippetedMessages || [];
|
|
|
|
|
|
|
|
if (this.snippetedMessagesTimer) {
|
|
|
|
clearTimeout(this.snippetedMessagesTimer);
|
|
|
|
this.snippetedMessagesTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.snippetedMessagesTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(snippetedMessagesReceived(this.snippetedMessages));
|
|
|
|
this.snippetedMessagesTimer = null;
|
|
|
|
return this.snippetedMessages = [];
|
|
|
|
}, 1000);
|
|
|
|
const message = ddpMessage.fields;
|
|
|
|
message._id = ddpMessage.id;
|
2018-04-24 19:34:03 +00:00
|
|
|
const snippetedMessage = _buildMessage(message);
|
2018-03-23 16:49:51 +00:00
|
|
|
this.snippetedMessages = [...this.snippetedMessages, snippetedMessage];
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('room_files', protectedFunction((error, ddpMessage) => {
|
2018-03-23 16:49:51 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
this.roomFiles = this.roomFiles || [];
|
|
|
|
|
|
|
|
if (this.roomFilesTimer) {
|
|
|
|
clearTimeout(this.roomFilesTimer);
|
|
|
|
this.roomFilesTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.roomFilesTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(roomFilesReceived(this.roomFiles));
|
|
|
|
this.roomFilesTimer = null;
|
|
|
|
return this.roomFiles = [];
|
|
|
|
}, 1000);
|
|
|
|
const { fields } = ddpMessage;
|
|
|
|
const message = {
|
|
|
|
_id: ddpMessage.id,
|
|
|
|
ts: fields.uploadedAt,
|
|
|
|
msg: fields.description,
|
|
|
|
status: 0,
|
|
|
|
attachments: [{
|
|
|
|
title: fields.name
|
|
|
|
}],
|
|
|
|
urls: [],
|
|
|
|
reactions: [],
|
|
|
|
u: {
|
|
|
|
username: fields.user.username
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const fileUrl = `/file-upload/${ ddpMessage.id }/${ fields.name }`;
|
|
|
|
if (/image/.test(fields.type)) {
|
|
|
|
message.attachments[0].image_type = fields.type;
|
|
|
|
message.attachments[0].image_url = fileUrl;
|
|
|
|
} else if (/audio/.test(fields.type)) {
|
|
|
|
message.attachments[0].audio_type = fields.type;
|
|
|
|
message.attachments[0].audio_url = fileUrl;
|
|
|
|
} else if (/video/.test(fields.type)) {
|
|
|
|
message.attachments[0].video_type = fields.type;
|
|
|
|
message.attachments[0].video_url = fileUrl;
|
|
|
|
}
|
|
|
|
this.roomFiles = [...this.roomFiles, message];
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
SDK.driver.on('rocketchat_roles', protectedFunction((error, ddpMessage) => {
|
2018-03-29 17:55:37 +00:00
|
|
|
this.roles = this.roles || {};
|
|
|
|
|
|
|
|
if (this.roleTimer) {
|
|
|
|
clearTimeout(this.roleTimer);
|
|
|
|
this.roleTimer = null;
|
|
|
|
}
|
|
|
|
this.roleTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(setRoles(this.roles));
|
|
|
|
|
|
|
|
database.write(() => {
|
2018-04-24 19:34:03 +00:00
|
|
|
foreach(this.roles, (description, _id) => {
|
2018-03-29 17:55:37 +00:00
|
|
|
database.create('roles', { _id, description }, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.roleTimer = null;
|
|
|
|
return this.roles = {};
|
2018-04-24 19:34:03 +00:00
|
|
|
}, 1000);
|
|
|
|
this.roles[ddpMessage.id] = (ddpMessage.fields && ddpMessage.fields.description) || undefined;
|
|
|
|
}));
|
2017-08-13 23:02:46 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
// SDK.driver.on('error', (err) => {
|
|
|
|
// log('SDK.onerror', err);
|
|
|
|
// reduxStore.dispatch(connectFailure());
|
|
|
|
// });
|
|
|
|
|
|
|
|
// SDK.driver.on('open', protectedFunction(() => {
|
|
|
|
// RocketChat.getSettings();
|
|
|
|
// RocketChat.getPermissions();
|
|
|
|
// reduxStore.dispatch(connectSuccess());
|
|
|
|
// resolve();
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// this.ddp.once('open', protectedFunction(() => {
|
|
|
|
// this.ddp.subscribe('activeUsers');
|
|
|
|
// this.ddp.subscribe('roles');
|
|
|
|
// RocketChat.getCustomEmoji();
|
|
|
|
// }));
|
2018-05-18 17:55:08 +00:00
|
|
|
}).catch((e) => {
|
2018-10-15 20:22:42 +00:00
|
|
|
log('SDK.connect catch', e);
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2017-11-24 20:44:52 +00:00
|
|
|
},
|
2018-11-16 11:06:29 +00:00
|
|
|
connected() {
|
|
|
|
return SDK.driver.ddp && SDK.driver.ddp._logged;
|
|
|
|
},
|
2017-11-24 20:44:52 +00:00
|
|
|
|
2017-11-07 16:28:02 +00:00
|
|
|
register({ credentials }) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('registerUser', credentials);
|
2017-11-07 16:28:02 +00:00
|
|
|
},
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
setUsername({ username }) {
|
|
|
|
return call('setUsername', username);
|
2017-11-07 16:28:02 +00:00
|
|
|
},
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
forgotPassword(email) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('sendForgotPasswordEmail', email);
|
2017-11-10 13:42:02 +00:00
|
|
|
},
|
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
async loginWithPassword({ username, password, code }) {
|
2018-10-16 20:27:37 +00:00
|
|
|
let params = { username, 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
|
|
|
};
|
2018-10-16 20:27:37 +00:00
|
|
|
} else if (typeof username === 'string' && username.indexOf('@') !== -1) {
|
|
|
|
params.email = username;
|
|
|
|
delete params.username;
|
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,
|
|
|
|
code,
|
|
|
|
totp: true
|
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-10-15 20:22:42 +00:00
|
|
|
async login(params) {
|
|
|
|
try {
|
|
|
|
await SDK.driver.login(params);
|
|
|
|
} catch (e) {
|
|
|
|
reduxStore.dispatch(loginFailure(e));
|
|
|
|
throw e;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
logout({ server }) {
|
2018-10-15 20:22:42 +00:00
|
|
|
try {
|
|
|
|
RocketChat.disconnect();
|
|
|
|
SDK.driver.logout();
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
AsyncStorage.removeItem(TOKEN_KEY);
|
|
|
|
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`);
|
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) {
|
|
|
|
console.warn(error);
|
|
|
|
}
|
2018-10-15 20:22:42 +00:00
|
|
|
},
|
|
|
|
disconnect() {
|
|
|
|
try {
|
|
|
|
SDK.driver.unsubscribeAll();
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error);
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
RocketChat.setApiUser({ userId: null, authToken: null });
|
|
|
|
},
|
|
|
|
setApiUser({ userId, authToken }) {
|
|
|
|
SDK.api.setAuth({ userId, authToken });
|
|
|
|
SDK.api.currentLogin = { userId, authToken };
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
2018-07-10 13:40:32 +00:00
|
|
|
registerPushToken(userId) {
|
|
|
|
const deviceToken = getDeviceToken();
|
|
|
|
if (deviceToken) {
|
|
|
|
const key = Platform.OS === 'ios' ? 'apn' : 'gcm';
|
|
|
|
const data = {
|
|
|
|
id: `RocketChatRN${ userId }`,
|
|
|
|
token: { [key]: deviceToken },
|
|
|
|
appName: 'chat.rocket.reactnative', // TODO: try to get from config file
|
|
|
|
userId,
|
|
|
|
metadata: {}
|
|
|
|
};
|
|
|
|
return call('raix:push-update', data);
|
|
|
|
}
|
2017-11-18 20:17:24 +00:00
|
|
|
},
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
// updatePushToken(pushId) {
|
|
|
|
// return call('raix:push-setuser', pushId);
|
|
|
|
// },
|
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];
|
|
|
|
database.write(() => {
|
2017-12-13 15:00:26 +00:00
|
|
|
message.status = messagesStatus.TEMP;
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('messages', message, true);
|
2017-12-13 15:00:26 +00:00
|
|
|
});
|
2018-11-16 11:06:29 +00:00
|
|
|
return _sendMessageCall.call(this, JSON.parse(JSON.stringify(message)));
|
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) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('createDirectMessage', username);
|
2017-08-10 16:16:32 +00:00
|
|
|
},
|
|
|
|
joinRoom(rid) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('joinRoom', rid);
|
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-09-25 19:28:42 +00:00
|
|
|
ret[item._id] = item[defaultSettings[item._id].type] || item.valueAsString || item.valueAsNumber
|
|
|
|
|| item.valueAsBoolean || item.value;
|
2017-11-19 02:44:55 +00:00
|
|
|
return ret;
|
|
|
|
}, {}),
|
2017-11-19 14:57:50 +00:00
|
|
|
_prepareSettings(settings) {
|
|
|
|
return settings.map((setting) => {
|
2018-06-18 13:30:36 +00:00
|
|
|
setting[defaultSettings[setting._id].type] = setting.value;
|
2017-11-19 14:57:50 +00:00
|
|
|
return setting;
|
|
|
|
});
|
|
|
|
},
|
2018-11-14 21:42:03 +00:00
|
|
|
_filterSettings: settings => settings.filter(setting => defaultSettings[setting._id] && (setting.value || setting.valueAsString || setting.valueAsNumber || setting.valueAsBoolean)),
|
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) {
|
|
|
|
return call('deleteMessage', { _id: message._id });
|
|
|
|
},
|
|
|
|
editMessage(message) {
|
|
|
|
const { _id, msg, rid } = message;
|
|
|
|
return call('updateMessage', { _id, msg, rid });
|
|
|
|
},
|
2017-11-24 20:44:52 +00:00
|
|
|
toggleStarMessage(message) {
|
2017-11-21 14:55:50 +00:00
|
|
|
return call('starMessage', { _id: message._id, rid: message.rid, starred: !message.starred });
|
|
|
|
},
|
|
|
|
togglePinMessage(message) {
|
|
|
|
if (message.pinned) {
|
|
|
|
return call('unpinMessage', message);
|
|
|
|
}
|
|
|
|
return call('pinMessage', message);
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
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) {
|
|
|
|
return call('setReaction', emoji, messageId);
|
2018-02-19 21:19:39 +00:00
|
|
|
},
|
|
|
|
toggleFavorite(rid, f) {
|
|
|
|
return call('toggleFavorite', rid, !f);
|
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');
|
|
|
|
},
|
|
|
|
async getRoomMember(rid, currentUserId) {
|
|
|
|
try {
|
|
|
|
const membersResult = await RocketChat.getRoomMembers(rid, true);
|
|
|
|
return Promise.resolve(membersResult.records.find(m => m.id !== currentUserId));
|
|
|
|
} 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 });
|
|
|
|
},
|
|
|
|
leaveRoom(rid) {
|
|
|
|
return call('leaveRoom', rid);
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
|
|
|
eraseRoom(rid) {
|
|
|
|
return call('eraseRoom', rid);
|
|
|
|
},
|
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-03-29 17:55:37 +00:00
|
|
|
toggleArchiveRoom(rid, archive) {
|
|
|
|
if (archive) {
|
|
|
|
return call('archiveRoom', rid);
|
|
|
|
}
|
|
|
|
return call('unarchiveRoom', rid);
|
|
|
|
},
|
|
|
|
saveRoomSettings(rid, params) {
|
|
|
|
return call('saveRoomSettings', rid, params);
|
|
|
|
},
|
2018-06-13 01:33:00 +00:00
|
|
|
saveUserProfile(params, customFields) {
|
|
|
|
return call('saveUserProfile', params, customFields);
|
|
|
|
},
|
|
|
|
saveUserPreferences(params) {
|
|
|
|
return call('saveUserPreferences', params);
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
saveNotificationSettings(rid, param, value) {
|
|
|
|
return call('saveNotificationSettings', rid, param, value);
|
|
|
|
},
|
|
|
|
messageSearch(text, rid, limit) {
|
|
|
|
return call('messageSearch', text, rid, limit);
|
|
|
|
},
|
|
|
|
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');
|
|
|
|
},
|
|
|
|
resetAvatar() {
|
|
|
|
return call('resetAvatar');
|
|
|
|
},
|
|
|
|
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() {
|
|
|
|
return SDK.driver.asyncCall('getUsernameSuggestion');
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|