2017-08-09 20:18:00 +00:00
|
|
|
import Random from 'react-native-meteor/lib/Random';
|
2017-11-18 20:17:24 +00:00
|
|
|
import { AsyncStorage, Platform } from 'react-native';
|
2017-08-13 23:02:46 +00:00
|
|
|
import { hashPassword } from 'react-native-meteor/lib/utils';
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
import RNFetchBlob from 'react-native-fetch-blob';
|
2017-09-21 17:08:00 +00:00
|
|
|
import reduxStore from './createStore';
|
2017-08-13 23:02:46 +00:00
|
|
|
import settingsType 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';
|
2017-08-13 23:02:46 +00:00
|
|
|
import * as actions from '../actions';
|
2018-01-15 18:44:20 +00:00
|
|
|
import { someoneTyping, roomMessageReceived } from '../actions/room';
|
2017-12-05 19:57:44 +00:00
|
|
|
import { setUser } from '../actions/login';
|
2017-12-20 19:20:06 +00:00
|
|
|
import { disconnect, disconnect_by_user, connectSuccess, connectFailure } from '../actions/connect';
|
2017-12-08 19:13:21 +00:00
|
|
|
import { requestActiveUser } from '../actions/activeUsers';
|
2017-12-20 19:20:06 +00:00
|
|
|
import Ddp from './ddp';
|
2017-08-15 19:28:46 +00:00
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
export { Accounts } from 'react-native-meteor';
|
|
|
|
|
2017-12-20 19:20:06 +00:00
|
|
|
const call = (method, ...params) => RocketChat.ddp.call(method, ...params); // eslint-disable-line
|
2017-11-07 20:25:04 +00:00
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
2017-12-13 15:00:26 +00:00
|
|
|
const SERVER_TIMEOUT = 30000;
|
2017-08-11 18:18:09 +00:00
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
const RocketChat = {
|
2017-11-13 13:53:45 +00:00
|
|
|
TOKEN_KEY,
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
createChannel({ name, users, type }) {
|
2017-11-19 02:31:34 +00:00
|
|
|
return call(type ? 'createChannel' : 'createPrivateGroup', name, users, type);
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
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) {
|
|
|
|
let status;
|
|
|
|
if (!ddpMessage.fields) {
|
|
|
|
status = 'offline';
|
|
|
|
} else {
|
|
|
|
status = ddpMessage.fields.status || 'offline';
|
|
|
|
}
|
|
|
|
|
|
|
|
const { user } = reduxStore.getState().login;
|
|
|
|
if (user && user.id === ddpMessage.id) {
|
|
|
|
return reduxStore.dispatch(setUser({ status }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const activeUser = {};
|
|
|
|
activeUser[ddpMessage.id] = status;
|
|
|
|
return reduxStore.dispatch(requestActiveUser(activeUser));
|
|
|
|
},
|
2017-12-20 19:20:06 +00:00
|
|
|
reconnect() {
|
|
|
|
if (this.ddp) {
|
|
|
|
this.ddp.reconnect();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
connect(url) {
|
|
|
|
if (this.ddp) {
|
|
|
|
this.ddp.disconnect();
|
|
|
|
}
|
|
|
|
this.ddp = new Ddp(url);
|
2017-08-18 21:30:16 +00:00
|
|
|
return new Promise((resolve) => {
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.on('disconnected_by_user', () => {
|
|
|
|
reduxStore.dispatch(disconnect_by_user());
|
|
|
|
});
|
|
|
|
this.ddp.on('disconnected', () => {
|
2017-08-17 16:55:47 +00:00
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
});
|
2017-12-27 15:22:06 +00:00
|
|
|
this.ddp.on('open', async() => {
|
|
|
|
resolve(reduxStore.dispatch(connectSuccess()));
|
|
|
|
});
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.on('connected', () => {
|
|
|
|
RocketChat.getSettings();
|
|
|
|
RocketChat.getPermissions();
|
|
|
|
});
|
2017-11-19 02:44:55 +00:00
|
|
|
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.on('error', (err) => {
|
|
|
|
alert(JSON.stringify(err));
|
|
|
|
reduxStore.dispatch(connectFailure());
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-17 16:55:47 +00:00
|
|
|
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.on('connected', () => this.ddp.subscribe('activeUsers', null, false));
|
|
|
|
|
|
|
|
this.ddp.on('users', (ddpMessage) => {
|
|
|
|
if (ddpMessage.collection === 'users') {
|
|
|
|
return RocketChat._setUser(ddpMessage);
|
|
|
|
}
|
2017-08-09 20:18:00 +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) => {
|
2017-12-20 19:20:06 +00:00
|
|
|
const message = this._buildMessage(ddpMessage.fields.args[0]);
|
2018-01-15 18:44:20 +00:00
|
|
|
return reduxStore.dispatch(roomMessageReceived(message));
|
|
|
|
});
|
2017-12-20 19:20:06 +00:00
|
|
|
|
|
|
|
this.ddp.on('stream-notify-room', (ddpMessage) => {
|
|
|
|
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] }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.ddp.on('stream-notify-user', (ddpMessage) => {
|
|
|
|
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 }));
|
2017-11-20 17:27:34 +00:00
|
|
|
}
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
|
|
|
database.create('subscriptions', data, true);
|
2017-12-20 19:20:06 +00:00
|
|
|
});
|
2017-08-18 21:30:16 +00:00
|
|
|
}
|
2017-12-20 19:20:06 +00:00
|
|
|
if (/rooms/.test(ev) && type === 'updated') {
|
2017-12-27 15:22:06 +00:00
|
|
|
const sub = database.objects('subscriptions').filtered('rid == $0', data._id)[0];
|
|
|
|
database.write(() => {
|
2017-12-20 19:20:06 +00:00
|
|
|
sub.roomUpdatedAt = data._updatedAt;
|
|
|
|
});
|
2017-08-16 23:29:12 +00:00
|
|
|
}
|
|
|
|
});
|
2017-08-13 23:02:46 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-13 13:35:01 +00:00
|
|
|
me({ server, token, userId }) {
|
|
|
|
return fetch(`${ server }/api/v1/me`, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
'X-User-Id': userId
|
|
|
|
}
|
|
|
|
}).then(response => response.json());
|
|
|
|
},
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
userInfo({ 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
|
|
|
|
}
|
|
|
|
}).then(response => response.json());
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
loadSubscriptions(cb) {
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.call('subscriptions/get').then((data) => {
|
2017-08-11 18:18:09 +00:00
|
|
|
if (data.length) {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
2017-08-11 18:18:09 +00:00
|
|
|
data.forEach((subscription) => {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('subscriptions', subscription, true);
|
2017-08-11 18:18:09 +00:00
|
|
|
});
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
|
|
|
return cb && cb();
|
|
|
|
});
|
|
|
|
},
|
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);
|
|
|
|
},
|
2017-08-09 20:18:00 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
_parseUrls(urls) {
|
|
|
|
return urls.filter(url => url.meta && !url.ignoreParse).map((url, index) => {
|
|
|
|
const tmp = {};
|
|
|
|
const { meta } = url;
|
|
|
|
tmp._id = index;
|
|
|
|
tmp.title = meta.ogTitle || meta.twitterTitle || meta.title || meta.pageTitle || meta.oembedTitle;
|
|
|
|
tmp.description = meta.ogDescription || meta.twitterDescription || meta.description || meta.oembedAuthorName;
|
|
|
|
let decodedOgImage;
|
|
|
|
if (meta.ogImage) {
|
|
|
|
decodedOgImage = meta.ogImage.replace(/&/g, '&');
|
|
|
|
}
|
|
|
|
tmp.image = decodedOgImage || meta.twitterImage || meta.oembedThumbnailUrl;
|
|
|
|
tmp.url = url.url;
|
|
|
|
return tmp;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
_buildMessage(message) {
|
2017-12-13 15:00:26 +00:00
|
|
|
message.status = messagesStatus.SENT;
|
2017-12-02 13:19:58 +00:00
|
|
|
message.attachments = message.attachments || [];
|
|
|
|
if (message.urls) {
|
|
|
|
message.urls = RocketChat._parseUrls(message.urls);
|
|
|
|
}
|
|
|
|
// loadHistory returns message.starred as object
|
|
|
|
// stream-room-messages returns message.starred as an array
|
|
|
|
message.starred = message.starred && (Array.isArray(message.starred) ? message.starred.length > 0 : !!message.starred);
|
|
|
|
return message;
|
|
|
|
},
|
2017-08-10 23:21:46 +00:00
|
|
|
loadMessagesForRoom(rid, end, cb) {
|
2017-12-20 19:20:06 +00:00
|
|
|
return this.ddp.call('loadHistory', rid, end, 20).then((data) => {
|
|
|
|
if (data && data.messages.length) {
|
|
|
|
const messages = data.messages.map(message => this._buildMessage(message));
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
2017-12-20 19:20:06 +00:00
|
|
|
messages.forEach((message) => {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('messages', message, true);
|
2017-08-11 18:18:09 +00:00
|
|
|
});
|
2017-12-20 19:20:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (cb) {
|
|
|
|
cb({ end: data && data.messages.length < 20 });
|
|
|
|
}
|
|
|
|
return data.message;
|
|
|
|
}, (err) => {
|
|
|
|
if (err) {
|
2017-08-17 06:28:41 +00:00
|
|
|
if (cb) {
|
2017-12-20 19:20:06 +00:00
|
|
|
cb({ end: true });
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2017-12-20 19:20:06 +00:00
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
getMessage(rid, msg = {}) {
|
2017-08-09 20:18:00 +00:00
|
|
|
const _id = Random.id();
|
2017-08-15 19:28:46 +00:00
|
|
|
const message = {
|
|
|
|
_id,
|
|
|
|
rid,
|
|
|
|
msg,
|
|
|
|
ts: new Date(),
|
|
|
|
_updatedAt: new Date(),
|
2017-12-13 15:00:26 +00:00
|
|
|
status: messagesStatus.TEMP,
|
2017-08-15 19:28:46 +00:00
|
|
|
u: {
|
2017-08-21 00:11:46 +00:00
|
|
|
_id: reduxStore.getState().login.user.id || '1',
|
2017-11-13 22:49:52 +00:00
|
|
|
username: reduxStore.getState().login.user.username
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
|
|
|
};
|
2017-08-09 20:18:00 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
|
|
|
database.create('messages', message, true);
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
return message;
|
|
|
|
},
|
2017-12-13 15:00:26 +00:00
|
|
|
async _sendMessageCall(message) {
|
|
|
|
const { _id, rid, msg } = message;
|
|
|
|
const sendMessageCall = call('sendMessage', { _id, rid, msg });
|
|
|
|
const timeoutCall = new Promise(resolve => setTimeout(resolve, SERVER_TIMEOUT, 'timeout'));
|
|
|
|
const result = await Promise.race([sendMessageCall, timeoutCall]);
|
|
|
|
if (result === 'timeout') {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
2017-12-13 15:00:26 +00:00
|
|
|
message.status = messagesStatus.ERROR;
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('messages', message, true);
|
2017-12-13 15:00:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async sendMessage(rid, msg) {
|
2017-08-15 19:28:46 +00:00
|
|
|
const tempMessage = this.getMessage(rid, msg);
|
2017-12-13 15:00:26 +00:00
|
|
|
return RocketChat._sendMessageCall(tempMessage);
|
|
|
|
},
|
|
|
|
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
|
|
|
});
|
|
|
|
return RocketChat._sendMessageCall(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
|
|
|
},
|
2017-08-11 18:18:09 +00:00
|
|
|
readMessages(rid) {
|
|
|
|
return call('readMessages', rid);
|
|
|
|
},
|
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) {
|
|
|
|
const placeholder = RocketChat.getMessage(rid, 'Sending an image');
|
|
|
|
try {
|
|
|
|
const result = await RocketChat._ufsCreate({ ...fileInfo, rid });
|
|
|
|
|
|
|
|
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 {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
|
|
|
const msg = database.objects('messages').filtered('_id = $0', placeholder._id);
|
|
|
|
database.delete(msg);
|
2017-08-15 19:28:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2017-11-19 04:09:56 +00:00
|
|
|
async getRooms() {
|
2017-12-27 15:22:06 +00:00
|
|
|
const { login } = reduxStore.getState();
|
|
|
|
let lastMessage = database
|
2017-11-19 04:09:56 +00:00
|
|
|
.objects('subscriptions')
|
2017-11-24 17:21:21 +00:00
|
|
|
.sorted('roomUpdatedAt', true)[0];
|
|
|
|
lastMessage = lastMessage && new Date(lastMessage.roomUpdatedAt);
|
2017-11-19 04:09:56 +00:00
|
|
|
let [subscriptions, rooms] = await Promise.all([call('subscriptions/get', lastMessage), call('rooms/get', lastMessage)]);
|
|
|
|
|
|
|
|
if (lastMessage) {
|
|
|
|
subscriptions = subscriptions.update;
|
|
|
|
rooms = rooms.update;
|
|
|
|
}
|
2017-11-24 17:21:21 +00:00
|
|
|
|
2017-11-19 04:09:56 +00:00
|
|
|
const data = subscriptions.map((subscription) => {
|
2017-11-21 01:02:00 +00:00
|
|
|
const room = rooms.find(({ _id }) => _id === subscription.rid);
|
|
|
|
if (room) {
|
2017-11-24 17:21:21 +00:00
|
|
|
subscription.roomUpdatedAt = room._updatedAt;
|
2017-11-21 01:02:00 +00:00
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
if (subscription.roles) {
|
|
|
|
subscription.roles = subscription.roles.map(role => ({ value: role }));
|
|
|
|
}
|
2017-11-19 04:09:56 +00:00
|
|
|
return subscription;
|
|
|
|
});
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
2017-11-19 04:09:56 +00:00
|
|
|
data.forEach(subscription =>
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('subscriptions', subscription, true));
|
2017-09-21 17:08:00 +00:00
|
|
|
});
|
2017-12-20 19:20:06 +00:00
|
|
|
this.ddp.subscribe('stream-notify-user', `${ login.user.id }/subscriptions-changed`, false);
|
|
|
|
this.ddp.subscribe('stream-notify-user', `${ login.user.id }/rooms-changed`, false);
|
2017-11-19 04:09:56 +00:00
|
|
|
return data;
|
2017-08-17 02:06:22 +00:00
|
|
|
},
|
2017-12-20 19:20:06 +00:00
|
|
|
disconnect() {
|
|
|
|
if (!this.ddp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reduxStore.dispatch(disconnect_by_user());
|
|
|
|
delete this.ddp;
|
|
|
|
return this.ddp.disconnect();
|
|
|
|
},
|
|
|
|
login(params, callback) {
|
|
|
|
return this.ddp.call('login', params).then((result) => {
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(null, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, (err) => {
|
|
|
|
if (/user not found/i.test(err.reason)) {
|
|
|
|
err.error = 1;
|
|
|
|
err.reason = 'User or Password incorrect';
|
|
|
|
err.message = 'User or Password incorrect';
|
|
|
|
}
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(err, null);
|
|
|
|
}
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
|
|
|
},
|
2017-11-07 20:25:04 +00:00
|
|
|
logout({ server }) {
|
2017-12-20 19:20:06 +00:00
|
|
|
if (this.ddp) {
|
|
|
|
this.ddp.logout();
|
|
|
|
}
|
2017-12-27 15:22:06 +00:00
|
|
|
database.deleteAll();
|
2017-11-07 20:25:04 +00:00
|
|
|
AsyncStorage.removeItem(TOKEN_KEY);
|
|
|
|
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`);
|
2017-11-19 02:44:55 +00:00
|
|
|
},
|
|
|
|
async getSettings() {
|
2017-12-27 15:22:06 +00:00
|
|
|
const temp = database.objects('settings').sorted('_updatedAt', true)[0];
|
2017-11-19 02:44:55 +00:00
|
|
|
const result = await (!temp ? call('public-settings/get') : call('public-settings/get', new Date(temp._updatedAt)));
|
|
|
|
const settings = temp ? result.update : result;
|
2017-11-19 14:57:50 +00:00
|
|
|
const filteredSettings = RocketChat._prepareSettings(RocketChat._filterSettings(settings));
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
|
|
|
filteredSettings.forEach(setting => database.create('settings', setting, true));
|
2017-11-19 02:44:55 +00:00
|
|
|
});
|
2017-12-27 15:22:06 +00:00
|
|
|
reduxStore.dispatch(actions.addSettings(RocketChat.parseSettings(filteredSettings)));
|
2017-11-19 02:44:55 +00:00
|
|
|
},
|
|
|
|
parseSettings: settings => settings.reduce((ret, item) => {
|
2017-11-24 20:44:52 +00:00
|
|
|
ret[item._id] = item[settingsType[item.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) => {
|
|
|
|
setting[settingsType[setting.type]] = setting.value;
|
|
|
|
return setting;
|
|
|
|
});
|
|
|
|
},
|
2017-11-20 22:18:00 +00:00
|
|
|
_filterSettings: settings => settings.filter(setting => settingsType[setting.type] && setting.value),
|
2017-11-24 20:44:52 +00:00
|
|
|
async getPermissions() {
|
2017-12-27 15:22:06 +00:00
|
|
|
const temp = database.objects('permissions').sorted('_updatedAt', true)[0];
|
2017-11-24 20:44:52 +00:00
|
|
|
const result = await (!temp ? call('permissions/get') : call('permissions/get', new Date(temp._updatedAt)));
|
|
|
|
let permissions = temp ? result.update : result;
|
|
|
|
permissions = RocketChat._preparePermissions(permissions);
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
|
|
|
permissions.forEach(permission => database.create('permissions', permission, true));
|
2017-11-24 20:44:52 +00:00
|
|
|
});
|
|
|
|
reduxStore.dispatch(actions.setAllPermissions(RocketChat.parsePermissions(permissions)));
|
|
|
|
},
|
|
|
|
parsePermissions: permissions => permissions.reduce((ret, item) => {
|
|
|
|
ret[item._id] = item.roles.reduce((roleRet, role) => [...roleRet, role.value], []);
|
|
|
|
return ret;
|
|
|
|
}, {}),
|
|
|
|
_preparePermissions(permissions) {
|
|
|
|
permissions.forEach((permission) => {
|
|
|
|
permission.roles = permission.roles.map(role => ({ value: role }));
|
|
|
|
});
|
|
|
|
return permissions;
|
|
|
|
},
|
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) {
|
2017-12-27 15:22:06 +00:00
|
|
|
const result = database.objects('subscriptions').filtered('rid = $0', rid);
|
2017-11-21 17:05:14 +00:00
|
|
|
if (result.length === 0) {
|
|
|
|
return Promise.reject(new Error('Room not found'));
|
|
|
|
}
|
|
|
|
return Promise.resolve(result[0]);
|
2017-11-21 14:55:50 +00:00
|
|
|
},
|
|
|
|
async getPermalink(message) {
|
2017-11-21 17:05:14 +00:00
|
|
|
const room = await RocketChat.getRoom(message.rid);
|
|
|
|
const roomType = {
|
|
|
|
p: 'group',
|
|
|
|
c: 'channel',
|
|
|
|
d: 'direct'
|
|
|
|
}[room.t];
|
|
|
|
return `${ room._server.id }/${ roomType }/${ room.name }?msg=${ message._id }`;
|
|
|
|
},
|
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);
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|