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';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
export default async 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;
|
|
|
|
}
|
|
|
|
timer = setTimeout(async() => {
|
|
|
|
try {
|
2019-02-07 15:48:10 +00:00
|
|
|
clearTimeout(timer);
|
2018-04-24 19:34:03 +00:00
|
|
|
timer = false;
|
2019-02-07 15:48:10 +00:00
|
|
|
if (this.sdk.userId) {
|
|
|
|
await this.getRooms();
|
|
|
|
loop();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2019-02-07 15:48:10 +00:00
|
|
|
loop();
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
};
|
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
this.sdk.onStreamData('connected', () => {
|
|
|
|
if (this.sdk.userId) {
|
|
|
|
this.getRooms();
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
this.sdk.onStreamData('close', () => {
|
|
|
|
loop();
|
2018-12-05 20:52:08 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
this.sdk.onStreamData('stream-notify-user', protectedFunction((ddpMessage) => {
|
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
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
database.write(() => {
|
|
|
|
database.delete(messages);
|
|
|
|
database.delete(subscription);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const rooms = database.objects('rooms').filtered('_id == $0', data.rid);
|
|
|
|
const tpm = merge(data, rooms[0]);
|
|
|
|
database.write(() => {
|
|
|
|
database.create('subscriptions', tpm, true);
|
|
|
|
database.delete(rooms);
|
|
|
|
});
|
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);
|
|
|
|
database.write(() => {
|
2019-02-07 15:48:10 +00:00
|
|
|
const tmp = merge(sub, data);
|
|
|
|
database.create('subscriptions', tmp, true);
|
2018-12-05 20:52:08 +00:00
|
|
|
});
|
|
|
|
} else if (type === 'inserted') {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('rooms', data, true);
|
|
|
|
});
|
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'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
requestAnimationFrame(() => database.write(() => {
|
|
|
|
database.create('messages', message, true);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-02-07 15:48:10 +00:00
|
|
|
await this.sdk.subscribeNotifyUser();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('subscribeRooms', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|