2019-04-08 12:35:28 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../../utils/log';
|
2019-04-08 12:35:28 +00:00
|
|
|
import protectedFunction from '../helpers/protectedFunction';
|
|
|
|
import buildMessage from '../helpers/buildMessage';
|
|
|
|
import database from '../../realm';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
const unsubscribe = subscriptions => subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom')));
|
2019-02-12 16:14:11 +00:00
|
|
|
const removeListener = listener => listener.stop();
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
export default function subscribeRoom({ rid }) {
|
2019-04-08 12:35:28 +00:00
|
|
|
let promises;
|
|
|
|
let timer = null;
|
|
|
|
let connectedListener;
|
|
|
|
let disconnectedListener;
|
|
|
|
let notifyRoomListener;
|
|
|
|
let messageReceivedListener;
|
|
|
|
const typingTimeouts = {};
|
2019-02-07 15:48:10 +00:00
|
|
|
const loop = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (timer) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-12 16:14:11 +00:00
|
|
|
timer = setTimeout(() => {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-02-07 15:48:10 +00:00
|
|
|
clearTimeout(timer);
|
2018-04-24 19:34:03 +00:00
|
|
|
timer = false;
|
2019-02-12 16:14:11 +00:00
|
|
|
this.loadMissedMessages({ rid });
|
|
|
|
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-12 16:14:11 +00:00
|
|
|
const handleConnected = () => {
|
|
|
|
this.loadMissedMessages({ rid });
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDisconnected = () => {
|
2019-02-07 15:48:10 +00:00
|
|
|
if (this.sdk.userId) {
|
2019-02-12 16:14:11 +00:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
const getUserTyping = username => (
|
|
|
|
database
|
|
|
|
.memoryDatabase.objects('usersTyping')
|
|
|
|
.filtered('rid = $0 AND username = $1', rid, username)
|
|
|
|
);
|
|
|
|
|
|
|
|
const removeUserTyping = (username) => {
|
|
|
|
const userTyping = getUserTyping(username);
|
|
|
|
try {
|
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
database.memoryDatabase.delete(userTyping);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (typingTimeouts[username]) {
|
|
|
|
clearTimeout(typingTimeouts[username]);
|
|
|
|
typingTimeouts[username] = null;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('TCL: removeUserTyping -> error', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const addUserTyping = (username) => {
|
|
|
|
const userTyping = getUserTyping(username);
|
|
|
|
// prevent duplicated
|
|
|
|
if (userTyping.length === 0) {
|
|
|
|
try {
|
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
database.memoryDatabase.create('usersTyping', { rid, username });
|
|
|
|
});
|
|
|
|
|
|
|
|
if (typingTimeouts[username]) {
|
|
|
|
clearTimeout(typingTimeouts[username]);
|
|
|
|
typingTimeouts[username] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
typingTimeouts[username] = setTimeout(() => {
|
|
|
|
removeUserTyping(username);
|
|
|
|
}, 10000);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('TCL: addUserTyping -> error', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleNotifyRoomReceived = protectedFunction((ddpMessage) => {
|
|
|
|
const [_rid, ev] = ddpMessage.fields.eventName.split('/');
|
|
|
|
if (rid !== _rid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ev === 'typing') {
|
|
|
|
const [username, typing] = ddpMessage.fields.args;
|
|
|
|
if (typing) {
|
|
|
|
addUserTyping(username);
|
|
|
|
} else {
|
|
|
|
removeUserTyping(username);
|
|
|
|
}
|
|
|
|
} else if (ev === 'deleteMessage') {
|
|
|
|
database.write(() => {
|
|
|
|
if (ddpMessage && ddpMessage.fields && ddpMessage.fields.args.length > 0) {
|
|
|
|
const { _id } = ddpMessage.fields.args[0];
|
|
|
|
const message = database.objects('messages').filtered('_id = $0', _id);
|
|
|
|
database.delete(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleMessageReceived = protectedFunction((ddpMessage) => {
|
|
|
|
const message = buildMessage(ddpMessage.fields.args[0]);
|
|
|
|
if (rid !== message.rid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('messages', EJSON.fromJSONValue(message), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
const [room] = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
|
|
|
|
if (room._id) {
|
|
|
|
this.readMessages(rid);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('handleMessageReceived', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-12 16:14:11 +00:00
|
|
|
const stop = () => {
|
|
|
|
if (promises) {
|
|
|
|
promises.then(unsubscribe);
|
|
|
|
promises = false;
|
|
|
|
}
|
|
|
|
if (connectedListener) {
|
|
|
|
connectedListener.then(removeListener);
|
|
|
|
connectedListener = false;
|
|
|
|
}
|
|
|
|
if (disconnectedListener) {
|
|
|
|
disconnectedListener.then(removeListener);
|
|
|
|
disconnectedListener = false;
|
2019-02-07 15:48:10 +00:00
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
if (notifyRoomListener) {
|
|
|
|
notifyRoomListener.then(removeListener);
|
|
|
|
notifyRoomListener = false;
|
|
|
|
}
|
|
|
|
if (messageReceivedListener) {
|
|
|
|
messageReceivedListener.then(removeListener);
|
|
|
|
messageReceivedListener = false;
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
clearTimeout(timer);
|
2019-02-14 16:43:56 +00:00
|
|
|
timer = false;
|
2019-04-08 12:35:28 +00:00
|
|
|
Object.keys(typingTimeouts).forEach((key) => {
|
|
|
|
if (typingTimeouts[key]) {
|
|
|
|
clearTimeout(typingTimeouts[key]);
|
|
|
|
typingTimeouts[key] = null;
|
|
|
|
}
|
|
|
|
});
|
2019-02-12 16:14:11 +00:00
|
|
|
};
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2019-02-12 16:14:11 +00:00
|
|
|
connectedListener = this.sdk.onStreamData('connected', handleConnected);
|
|
|
|
disconnectedListener = this.sdk.onStreamData('close', handleDisconnected);
|
2019-04-08 12:35:28 +00:00
|
|
|
notifyRoomListener = this.sdk.onStreamData('stream-notify-room', handleNotifyRoomReceived);
|
|
|
|
messageReceivedListener = this.sdk.onStreamData('stream-room-messages', handleMessageReceived);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
|
|
|
try {
|
2019-02-07 15:48:10 +00:00
|
|
|
promises = this.sdk.subscribeRoom(rid);
|
2018-12-05 20:52:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('subscribeRoom', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-10-15 20:22:42 +00:00
|
|
|
stop: () => stop()
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
}
|