2018-04-24 19:34:03 +00:00
|
|
|
import database from '../../realm';
|
|
|
|
import { merge } from '../helpers/mergeSubscriptionsRooms';
|
2018-05-18 17:55:08 +00:00
|
|
|
import protectedFunction from '../helpers/protectedFunction';
|
2018-05-23 13:39:18 +00:00
|
|
|
import messagesStatus from '../../../constants/messagesStatus';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../../utils/log';
|
2018-12-05 20:52:08 +00:00
|
|
|
import random from '../../../utils/random';
|
2019-02-07 16:13:21 +00:00
|
|
|
import store from '../../createStore';
|
|
|
|
import { roomsRequest } from '../../../actions/rooms';
|
2019-06-10 16:23:19 +00:00
|
|
|
import { notificationReceived } from '../../../actions/notification';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-14 16:43:56 +00:00
|
|
|
const removeListener = listener => listener.stop();
|
|
|
|
|
|
|
|
let connectedListener;
|
|
|
|
let disconnectedListener;
|
|
|
|
let streamListener;
|
2019-06-17 13:57:07 +00:00
|
|
|
let subServer;
|
2019-02-14 16:43:56 +00:00
|
|
|
|
2019-06-26 12:33:56 +00:00
|
|
|
export default function subscribeRooms() {
|
2018-04-24 19:34:03 +00:00
|
|
|
let timer = null;
|
2019-02-07 15:48:10 +00:00
|
|
|
const loop = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (timer) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-07 16:13:21 +00:00
|
|
|
timer = setTimeout(() => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
2019-02-07 20:28:39 +00:00
|
|
|
store.dispatch(roomsRequest());
|
|
|
|
loop();
|
2018-04-24 19:34:03 +00:00
|
|
|
}, 5000);
|
|
|
|
};
|
|
|
|
|
2019-02-14 16:43:56 +00:00
|
|
|
const handleConnected = () => {
|
2019-02-07 20:28:39 +00:00
|
|
|
store.dispatch(roomsRequest());
|
2018-12-05 20:52:08 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
2019-02-14 16:43:56 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-14 16:43:56 +00:00
|
|
|
const handleDisconnected = () => {
|
2019-02-07 20:28:39 +00:00
|
|
|
if (this.sdk.userId) {
|
|
|
|
loop();
|
|
|
|
}
|
2019-02-14 16:43:56 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-14 16:43:56 +00:00
|
|
|
const handleStreamMessageReceived = protectedFunction((ddpMessage) => {
|
2019-06-17 13:57:07 +00:00
|
|
|
// check if the server from variable is the same as the js sdk client
|
|
|
|
if (this.sdk && this.sdk.client && this.sdk.client.host !== subServer) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [type, data] = ddpMessage.fields.args;
|
|
|
|
const [, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
if (/subscriptions/.test(ev)) {
|
|
|
|
if (type === 'removed') {
|
|
|
|
let messages = [];
|
|
|
|
const [subscription] = database.objects('subscriptions').filtered('_id == $0', data._id);
|
2018-09-19 14:18:32 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (subscription) {
|
|
|
|
messages = database.objects('messages').filtered('rid == $0', subscription.rid);
|
2018-09-19 14:18:32 +00:00
|
|
|
}
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
database.delete(messages);
|
|
|
|
database.delete(subscription);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_stream_msg_received_sub_removed', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
} else {
|
|
|
|
const rooms = database.objects('rooms').filtered('_id == $0', data.rid);
|
|
|
|
const tpm = merge(data, rooms[0]);
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('subscriptions', tpm, true);
|
|
|
|
database.delete(rooms);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_stream_msg_received_sub_updated', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
if (/rooms/.test(ev)) {
|
|
|
|
if (type === 'updated') {
|
|
|
|
const [sub] = database.objects('subscriptions').filtered('rid == $0', data._id);
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
const tmp = merge(sub, data);
|
|
|
|
database.create('subscriptions', tmp, true);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_stream_msg_received_room_updated', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
} else if (type === 'inserted') {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('rooms', data, true);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_stream_msg_received_room_inserted', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
if (/message/.test(ev)) {
|
|
|
|
const [args] = ddpMessage.fields.args;
|
|
|
|
const _id = random(17);
|
|
|
|
const message = {
|
|
|
|
_id,
|
|
|
|
rid: args.rid,
|
|
|
|
msg: args.msg,
|
|
|
|
ts: new Date(),
|
|
|
|
_updatedAt: new Date(),
|
|
|
|
status: messagesStatus.SENT,
|
|
|
|
u: {
|
2018-05-23 13:39:18 +00:00
|
|
|
_id,
|
2018-12-05 20:52:08 +00:00
|
|
|
username: 'rocket.cat'
|
|
|
|
}
|
|
|
|
};
|
2019-02-25 16:23:17 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('messages', message, true);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_stream_msg_received_message', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
|
|
|
});
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
2019-06-10 16:23:19 +00:00
|
|
|
if (/notification/.test(ev)) {
|
|
|
|
const [notification] = ddpMessage.fields.args;
|
|
|
|
store.dispatch(notificationReceived(notification));
|
|
|
|
}
|
2019-02-14 16:43:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const stop = () => {
|
|
|
|
if (connectedListener) {
|
|
|
|
connectedListener.then(removeListener);
|
|
|
|
connectedListener = false;
|
|
|
|
}
|
|
|
|
if (disconnectedListener) {
|
|
|
|
disconnectedListener.then(removeListener);
|
|
|
|
disconnectedListener = false;
|
|
|
|
}
|
|
|
|
if (streamListener) {
|
|
|
|
streamListener.then(removeListener);
|
|
|
|
streamListener = false;
|
|
|
|
}
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
connectedListener = this.sdk.onStreamData('connected', handleConnected);
|
|
|
|
disconnectedListener = this.sdk.onStreamData('close', handleDisconnected);
|
|
|
|
streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-06-17 13:57:07 +00:00
|
|
|
// set the server that started this task
|
|
|
|
subServer = this.sdk.client.host;
|
2019-06-26 12:33:56 +00:00
|
|
|
this.sdk.subscribeNotifyUser().catch(e => console.log(e));
|
|
|
|
|
|
|
|
return {
|
|
|
|
stop: () => stop()
|
|
|
|
};
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_subscribe_rooms', e);
|
2019-06-26 12:33:56 +00:00
|
|
|
return Promise.reject();
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|