2018-05-23 13:39:18 +00:00
|
|
|
import Random from 'react-native-meteor/lib/Random';
|
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-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
export default async function subscribeRooms(id) {
|
|
|
|
const subscriptions = Promise.all([
|
|
|
|
this.ddp.subscribe('stream-notify-user', `${ id }/subscriptions-changed`, false),
|
|
|
|
this.ddp.subscribe('stream-notify-user', `${ id }/rooms-changed`, false),
|
|
|
|
this.ddp.subscribe('stream-notify-user', `${ id }/message`, false)
|
|
|
|
]);
|
|
|
|
|
|
|
|
let timer = null;
|
|
|
|
const loop = (time = new Date()) => {
|
|
|
|
if (timer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer = setTimeout(async() => {
|
|
|
|
try {
|
|
|
|
await this.getRooms(time);
|
|
|
|
timer = false;
|
|
|
|
loop();
|
|
|
|
} catch (e) {
|
|
|
|
loop(time);
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
};
|
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
if (!this.ddp && this._login) {
|
|
|
|
loop();
|
|
|
|
} else {
|
2018-05-07 20:43:26 +00:00
|
|
|
this.ddp.on('logged', () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
this.ddp.on('logout', () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = true;
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
this.ddp.on('disconnected', () => {
|
|
|
|
if (this._login) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
this.ddp.on('stream-notify-user', protectedFunction((ddpMessage) => {
|
2018-05-07 20:43:26 +00:00
|
|
|
const [type, data] = ddpMessage.fields.args;
|
|
|
|
const [, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
if (/subscriptions/.test(ev)) {
|
|
|
|
const tpm = merge(data);
|
2018-05-23 13:39:18 +00:00
|
|
|
database.write(() => {
|
2018-05-07 20:43:26 +00:00
|
|
|
database.create('subscriptions', tpm, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (/rooms/.test(ev) && type === 'updated') {
|
|
|
|
const [sub] = database.objects('subscriptions').filtered('rid == $0', data._id);
|
|
|
|
database.write(() => {
|
|
|
|
merge(sub, data);
|
|
|
|
});
|
|
|
|
}
|
2018-05-23 13:39:18 +00:00
|
|
|
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-05-18 17:55:08 +00:00
|
|
|
}));
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
await subscriptions;
|
|
|
|
} catch (e) {
|
|
|
|
log('subscribeRooms', e);
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
// console.log(this.ddp.subscriptions);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|