2018-06-18 13:30:36 +00:00
|
|
|
import { AsyncStorage, Platform, InteractionManager } from 'react-native';
|
2017-08-13 23:02:46 +00:00
|
|
|
import { hashPassword } from 'react-native-meteor/lib/utils';
|
2018-04-24 19:34:03 +00:00
|
|
|
import foreach from 'lodash/forEach';
|
2017-08-15 19:28:46 +00:00
|
|
|
import RNFetchBlob from 'react-native-fetch-blob';
|
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';
|
|
|
|
|
|
|
|
import { setUser, setLoginServices, removeLoginServices, loginRequest, loginSuccess, loginFailure } from '../actions/login';
|
|
|
|
import { disconnect, connectSuccess, connectFailure } 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-12-20 19:20:06 +00:00
|
|
|
import Ddp from './ddp';
|
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';
|
|
|
|
|
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
|
|
|
const call = (method, ...params) => RocketChat.ddp.call(method, ...params); // eslint-disable-line
|
|
|
|
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 }`);
|
|
|
|
}
|
|
|
|
},
|
2017-09-01 19:42:50 +00:00
|
|
|
async testServer(url) {
|
2017-12-19 17:01:00 +00:00
|
|
|
if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
|
2017-09-01 19:42:50 +00:00
|
|
|
const response = await fetch(url, { method: 'HEAD' });
|
|
|
|
if (response.status === 200 && response.headers.get('x-instance-id') != null && response.headers.get('x-instance-id').length) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-06-18 13:30:36 +00:00
|
|
|
}, 5000);
|
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-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
if (!user) {
|
|
|
|
const { user: u } = reduxStore.getState().login;
|
|
|
|
user = Object.assign({}, u);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
// TODO: one api call
|
|
|
|
// call /me only one time
|
|
|
|
if (!user.username) {
|
|
|
|
const me = await this.me({ token: user.token, userId: user.id });
|
2018-06-13 01:33:00 +00:00
|
|
|
user = { ...user, ...me };
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
|
|
|
if (user.username) {
|
|
|
|
const userInfo = await this.userInfo({ token: user.token, userId: user.id });
|
2018-06-13 01:33:00 +00:00
|
|
|
user = { ...user, ...userInfo.user };
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
return reduxStore.dispatch(loginSuccess(user));
|
|
|
|
} catch (e) {
|
|
|
|
log('rocketchat.loginSuccess', e);
|
2017-12-20 19:20:06 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
connect(url, login) {
|
2017-08-18 21:30:16 +00:00
|
|
|
return new Promise((resolve) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (this.ddp) {
|
|
|
|
this.ddp.disconnect();
|
|
|
|
delete this.ddp;
|
|
|
|
}
|
2017-11-19 02:44:55 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.ddp = new Ddp(url, login);
|
|
|
|
if (login) {
|
|
|
|
protectedFunction(() => RocketChat.getRooms());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ddp.on('login', protectedFunction(() => reduxStore.dispatch(loginRequest())));
|
|
|
|
|
2018-06-13 01:29:18 +00:00
|
|
|
this.ddp.on('loginError', protectedFunction(err => reduxStore.dispatch(loginFailure(err))));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
this.ddp.on('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
this.ddp.on('background', () => this.getRooms().catch(e => log('background getRooms', e)));
|
2017-08-17 16:55:47 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('disconnected', () => console.log('disconnected'));
|
|
|
|
|
|
|
|
this.ddp.on('logged', protectedFunction((user) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
this.getRooms().catch(e => log('logged getRooms', e));
|
2018-04-24 19:34:03 +00:00
|
|
|
this.loginSuccess(user);
|
|
|
|
}));
|
2018-06-13 01:33:00 +00:00
|
|
|
this.ddp.once('logged', protectedFunction(({ id }) => {
|
|
|
|
this.subscribeRooms(id);
|
|
|
|
this.ddp.subscribe('stream-notify-logged', 'updateAvatar', false);
|
|
|
|
}));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
this.ddp.on('disconnected', protectedFunction(() => {
|
|
|
|
reduxStore.dispatch(disconnect());
|
2018-05-23 13:39:18 +00:00
|
|
|
console.warn(this.ddp);
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
2018-01-15 18:44:20 +00:00
|
|
|
this.ddp.on('stream-room-messages', (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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('stream-notify-room', protectedFunction((ddpMessage) => {
|
2017-12-20 19:20:06 +00:00
|
|
|
const [_rid, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
if (ev !== 'typing') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return reduxStore.dispatch(someoneTyping({ _rid, username: ddpMessage.fields.args[0], typing: ddpMessage.fields.args[1] }));
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2017-12-20 19:20:06 +00:00
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
this.ddp.on('stream-notify-logged', (ddpMessage) => {
|
|
|
|
// this entire logic needs a better solution
|
|
|
|
// we're using it only because our image cache lib doesn't support clear cache
|
|
|
|
if (ddpMessage.fields && ddpMessage.fields.eventName === 'updateAvatar') {
|
|
|
|
const { args } = ddpMessage.fields;
|
2018-06-18 13:30:36 +00:00
|
|
|
InteractionManager.runAfterInteractions(() =>
|
2018-06-13 01:33:00 +00:00
|
|
|
args.forEach((arg) => {
|
|
|
|
const user = database.objects('users').filtered('username = $0', arg.username);
|
2018-06-18 13:30:36 +00:00
|
|
|
if (user.length > 0) {
|
2018-06-13 01:33:00 +00:00
|
|
|
user[0].avatarVersion += 1;
|
|
|
|
}
|
2018-06-18 13:30:36 +00:00
|
|
|
}));
|
2018-06-13 01:33:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
// this.ddp.on('stream-notify-user', protectedFunction((ddpMessage) => {
|
|
|
|
// console.warn('rc.stream-notify-user')
|
|
|
|
// const [type, data] = ddpMessage.fields.args;
|
|
|
|
// const [, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
// if (/subscriptions/.test(ev)) {
|
|
|
|
// if (data.roles) {
|
|
|
|
// data.roles = data.roles.map(role => ({ value: role }));
|
|
|
|
// }
|
|
|
|
// if (data.blocker) {
|
|
|
|
// data.blocked = true;
|
|
|
|
// } else {
|
|
|
|
// data.blocked = false;
|
|
|
|
// }
|
|
|
|
// if (data.mobilePushNotifications === 'nothing') {
|
|
|
|
// data.notifications = true;
|
|
|
|
// } else {
|
|
|
|
// data.notifications = false;
|
|
|
|
// }
|
|
|
|
// database.write(() => {
|
|
|
|
// database.create('subscriptions', data, true);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// if (/rooms/.test(ev) && type === 'updated') {
|
|
|
|
// const sub = database.objects('subscriptions').filtered('rid == $0', data._id)[0];
|
|
|
|
|
|
|
|
// database.write(() => {
|
|
|
|
// sub.roomUpdatedAt = data._updatedAt;
|
|
|
|
// sub.lastMessage = normalizeMessage(data.lastMessage);
|
|
|
|
// sub.ro = data.ro;
|
|
|
|
// sub.description = data.description;
|
|
|
|
// sub.topic = data.topic;
|
|
|
|
// sub.announcement = data.announcement;
|
|
|
|
// sub.reactWhenReadOnly = data.reactWhenReadOnly;
|
|
|
|
// sub.archived = data.archived;
|
|
|
|
// sub.joinCodeRequired = data.joinCodeRequired;
|
|
|
|
// if (data.muted) {
|
|
|
|
// sub.muted = data.muted.map(m => ({ value: m }));
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// if (/message/.test(ev)) {
|
|
|
|
// const [args] = ddpMessage.fields.args;
|
|
|
|
// const _id = Random.id();
|
|
|
|
// const message = {
|
|
|
|
// _id,
|
|
|
|
// rid: args.rid,
|
|
|
|
// msg: args.msg,
|
|
|
|
// ts: new Date(),
|
|
|
|
// _updatedAt: new Date(),
|
|
|
|
// status: messagesStatus.SENT,
|
|
|
|
// u: {
|
|
|
|
// _id,
|
|
|
|
// username: 'rocket.cat'
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// requestAnimationFrame(() => database.write(() => {
|
|
|
|
// database.create('messages', message, true);
|
|
|
|
// }));
|
|
|
|
// }
|
|
|
|
// }));
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('rocketchat_starred_message', protectedFunction((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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('rocketchat_pinned_message', protectedFunction((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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('rocketchat_mentioned_message', protectedFunction((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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('rocketchat_snippeted_message', protectedFunction((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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('room_files', protectedFunction((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-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('meteor_accounts_loginServiceConfiguration', protectedFunction((ddpMessage) => {
|
2018-02-23 20:29:06 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
this.loginServices = this.loginServices || {};
|
|
|
|
if (this.loginServiceTimer) {
|
|
|
|
clearTimeout(this.loginServiceTimer);
|
|
|
|
this.loginServiceTimer = null;
|
|
|
|
}
|
|
|
|
this.loginServiceTimer = setTimeout(() => {
|
|
|
|
reduxStore.dispatch(setLoginServices(this.loginServices));
|
|
|
|
this.loginServiceTimer = null;
|
|
|
|
return this.loginServices = {};
|
|
|
|
}, 1000);
|
|
|
|
this.loginServices[ddpMessage.fields.service] = { ...ddpMessage.fields };
|
|
|
|
delete this.loginServices[ddpMessage.fields.service].service;
|
|
|
|
} else if (ddpMessage.msg === 'removed') {
|
|
|
|
if (this.loginServiceTimer) {
|
|
|
|
clearTimeout(this.loginServiceTimer);
|
|
|
|
}
|
|
|
|
this.loginServiceTimer = setTimeout(() => reduxStore.dispatch(removeLoginServices()), 1000);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}));
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.ddp.on('rocketchat_roles', protectedFunction((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-05-18 17:55:08 +00:00
|
|
|
this.ddp.on('error', (err) => {
|
|
|
|
log('rocketchat.onerror', err);
|
2018-04-24 19:34:03 +00:00
|
|
|
reduxStore.dispatch(connectFailure());
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2017-11-13 13:35:01 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// TODO: fix api (get emojis by date/version....)
|
|
|
|
|
|
|
|
this.ddp.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) => {
|
|
|
|
log('rocketchat.connect catch', e);
|
|
|
|
});
|
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
|
|
|
},
|
|
|
|
|
|
|
|
setUsername({ credentials }) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('setUsername', credentials.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
|
|
|
},
|
|
|
|
|
2017-08-14 14:15:37 +00:00
|
|
|
loginWithPassword({ username, password, code }, callback) {
|
2017-08-13 23:02:46 +00:00
|
|
|
let params = {};
|
|
|
|
const state = reduxStore.getState();
|
|
|
|
|
|
|
|
if (state.settings.LDAP_Enable) {
|
|
|
|
params = {
|
|
|
|
ldap: true,
|
|
|
|
username,
|
|
|
|
ldapPass: password,
|
|
|
|
ldapOptions: {}
|
|
|
|
};
|
|
|
|
} else if (state.settings.CROWD_Enable) {
|
|
|
|
params = {
|
|
|
|
crowd: true,
|
|
|
|
username,
|
|
|
|
crowdPassword: password
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
params = {
|
|
|
|
password: hashPassword(password),
|
|
|
|
user: {
|
|
|
|
username
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-17 01:09:44 +00:00
|
|
|
if (typeof username === 'string' && username.indexOf('@') !== -1) {
|
|
|
|
params.user = { email: username };
|
2017-08-13 23:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 00:31:22 +00:00
|
|
|
if (code) {
|
|
|
|
params = {
|
|
|
|
totp: {
|
|
|
|
login: params,
|
|
|
|
code
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:29:12 +00:00
|
|
|
return this.login(params, callback);
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
login(params) {
|
|
|
|
return this.ddp.login(params);
|
|
|
|
},
|
|
|
|
logout({ server }) {
|
|
|
|
if (this.ddp) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
this.ddp.logout();
|
|
|
|
} catch (e) {
|
|
|
|
log('rocketchat.logout', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
database.deleteAll();
|
|
|
|
AsyncStorage.removeItem(TOKEN_KEY);
|
|
|
|
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`);
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2017-11-18 20:17:24 +00:00
|
|
|
registerPushToken(id, token) {
|
|
|
|
const key = Platform.OS === 'ios' ? 'apn' : 'gcm';
|
|
|
|
const data = {
|
|
|
|
id: `RocketChatRN${ id }`,
|
|
|
|
token: { [key]: token },
|
2017-11-21 14:19:54 +00:00
|
|
|
appName: 'chat.rocket.reactnative', // TODO: try to get from config file
|
2017-11-18 20:17:24 +00:00
|
|
|
userId: id,
|
|
|
|
metadata: {}
|
|
|
|
};
|
|
|
|
return call('raix:push-update', data);
|
|
|
|
},
|
|
|
|
|
|
|
|
updatePushToken(pushId) {
|
|
|
|
return call('raix:push-setuser', pushId);
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
loadMissedMessages,
|
|
|
|
loadMessagesForRoom,
|
|
|
|
getMessage,
|
|
|
|
sendMessage,
|
|
|
|
getRooms,
|
|
|
|
readMessages,
|
|
|
|
me({ server = reduxStore.getState().server.server, token, userId }) {
|
|
|
|
return fetch(`${ server }/api/v1/me`, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
'X-User-Id': userId
|
2017-12-20 19:20:06 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}).then(response => response.json());
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
userInfo({ server = reduxStore.getState().server.server, token, userId }) {
|
|
|
|
return fetch(`${ server }/api/v1/users.info?userId=${ userId }`, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
'X-User-Id': userId
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}).then(response => response.json());
|
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-04-24 19:34:03 +00:00
|
|
|
return _sendMessageCall(JSON.parse(JSON.stringify(message)));
|
2017-08-10 16:16:32 +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
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
"name":"yXfExLErmNR5eNPx7.png"
|
|
|
|
"size":961
|
|
|
|
"type":"image/png"
|
|
|
|
"rid":"GENERAL"
|
|
|
|
"description":""
|
|
|
|
"store":"fileSystem"
|
|
|
|
*/
|
2017-08-15 19:28:46 +00:00
|
|
|
_ufsCreate(fileInfo) {
|
|
|
|
// return call('ufsCreate', fileInfo);
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('ufsCreate', fileInfo);
|
2017-08-10 20:09:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// ["ZTE8CKHJt7LATv7Me","fileSystem","e8E96b2819"
|
2017-08-15 19:28:46 +00:00
|
|
|
_ufsComplete(fileId, store, token) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('ufsComplete', fileId, store, token);
|
2017-08-10 20:09:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
- "GENERAL"
|
|
|
|
- {
|
|
|
|
"type":"image/png",
|
|
|
|
"size":961,
|
|
|
|
"name":"yXfExLErmNR5eNPx7.png",
|
|
|
|
"description":"",
|
|
|
|
"url":"/ufs/fileSystem/ZTE8CKHJt7LATv7Me/yXfExLErmNR5eNPx7.png"
|
|
|
|
}
|
|
|
|
*/
|
2017-08-15 19:28:46 +00:00
|
|
|
_sendFileMessage(rid, data, msg = {}) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call('sendFileMessage', rid, null, data, msg);
|
2017-08-14 14:25:17 +00:00
|
|
|
},
|
2017-08-15 19:28:46 +00:00
|
|
|
async sendFileMessage(rid, fileInfo, data) {
|
2018-05-29 17:10:40 +00:00
|
|
|
let placeholder;
|
2017-08-15 19:28:46 +00:00
|
|
|
try {
|
2018-03-07 00:17:20 +00:00
|
|
|
if (!data) {
|
|
|
|
data = await RNFetchBlob.wrap(fileInfo.path);
|
|
|
|
const fileStat = await RNFetchBlob.fs.stat(fileInfo.path);
|
|
|
|
fileInfo.size = fileStat.size;
|
|
|
|
fileInfo.name = fileStat.filename;
|
|
|
|
}
|
2017-08-15 19:28:46 +00:00
|
|
|
|
2018-05-29 17:10:40 +00:00
|
|
|
const { FileUpload_MaxFileSize } = reduxStore.getState().settings;
|
|
|
|
|
|
|
|
// -1 maxFileSize means there is no limit
|
|
|
|
if (FileUpload_MaxFileSize > -1 && fileInfo.size > FileUpload_MaxFileSize) {
|
|
|
|
return Promise.reject({ error: 'error-file-too-large' }); // eslint-disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
placeholder = RocketChat.getMessage(rid, 'Sending a file');
|
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
const result = await RocketChat._ufsCreate({ ...fileInfo, rid });
|
2017-08-15 19:28:46 +00:00
|
|
|
await RNFetchBlob.fetch('POST', result.url, {
|
|
|
|
'Content-Type': 'application/octet-stream'
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
const completeRresult = await RocketChat._ufsComplete(result.fileId, fileInfo.store, result.token);
|
|
|
|
|
|
|
|
return await RocketChat._sendFileMessage(completeRresult.rid, {
|
|
|
|
_id: completeRresult._id,
|
|
|
|
type: completeRresult.type,
|
|
|
|
size: completeRresult.size,
|
|
|
|
name: completeRresult.name,
|
|
|
|
url: completeRresult.path
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
} finally {
|
2018-04-24 19:34:03 +00:00
|
|
|
// TODO: fix that
|
2018-03-07 00:17:20 +00:00
|
|
|
try {
|
2018-05-29 17:10:40 +00:00
|
|
|
if (placeholder) {
|
|
|
|
database.write(() => {
|
|
|
|
const msg = database.objects('messages').filtered('_id = $0', placeholder._id);
|
|
|
|
database.delete(msg);
|
|
|
|
});
|
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
|
|
|
},
|
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-06-18 13:30:36 +00:00
|
|
|
ret[item._id] = item[defaultSettings[item._id].type] || item.valueAsString || item.valueAsNumber ||
|
2017-11-24 20:44:52 +00:00
|
|
|
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-06-18 13:30:36 +00:00
|
|
|
_filterSettings: settings => settings.filter(setting => defaultSettings[setting._id] && setting.value),
|
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) {
|
|
|
|
log('rocketchat.getPermalink', e);
|
|
|
|
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) {
|
2017-12-20 19:20:06 +00:00
|
|
|
return this.ddp.subscribe(...args);
|
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
|
|
|
|
const userRoles = reduxStore.getState().login.user.roles || [];
|
|
|
|
// 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);
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|