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';
|
2019-04-17 17:01:03 +00:00
|
|
|
import debounce from '../../../utils/debounce';
|
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 connectedListener;
|
|
|
|
let disconnectedListener;
|
|
|
|
let notifyRoomListener;
|
|
|
|
let messageReceivedListener;
|
|
|
|
const typingTimeouts = {};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
const handleConnection = () => {
|
2019-02-12 16:14:11 +00:00
|
|
|
this.loadMissedMessages({ rid });
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2019-04-17 17:01:03 +00:00
|
|
|
const thread = database.objects('threads').filtered('_id = $0', _id);
|
|
|
|
database.delete(thread);
|
|
|
|
const threadMessage = database.objects('threadMessages').filtered('_id = $0', _id);
|
|
|
|
database.delete(threadMessage);
|
|
|
|
const cleanTmids = database.objects('messages').filtered('tmid = $0', _id).snapshot();
|
|
|
|
if (cleanTmids && cleanTmids.length) {
|
|
|
|
cleanTmids.forEach((m) => {
|
|
|
|
m.tmid = null;
|
|
|
|
});
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
const read = debounce(() => {
|
|
|
|
const [room] = database.objects('subscriptions').filtered('rid = $0', rid);
|
2019-05-20 20:43:50 +00:00
|
|
|
if (room && room._id) {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.readMessages(rid);
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
const handleMessageReceived = protectedFunction((ddpMessage) => {
|
2019-04-17 17:01:03 +00:00
|
|
|
const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0]));
|
2019-04-08 12:35:28 +00:00
|
|
|
if (rid !== message.rid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
try {
|
|
|
|
database.write(() => {
|
2019-04-17 17:01:03 +00:00
|
|
|
database.create('messages', message, true);
|
|
|
|
// if it's a thread "header"
|
|
|
|
if (message.tlm) {
|
|
|
|
database.create('threads', message, true);
|
|
|
|
} else if (message.tmid) {
|
|
|
|
message.rid = message.tmid;
|
|
|
|
database.create('threadMessages', message, true);
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
});
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
read();
|
2019-04-08 12:35:28 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
Object.keys(typingTimeouts).forEach((key) => {
|
|
|
|
if (typingTimeouts[key]) {
|
|
|
|
clearTimeout(typingTimeouts[key]);
|
|
|
|
typingTimeouts[key] = null;
|
|
|
|
}
|
|
|
|
});
|
2019-05-03 17:17:29 +00:00
|
|
|
database.memoryDatabase.write(() => {
|
|
|
|
const usersTyping = database.memoryDatabase.objects('usersTyping').filtered('rid == $0', rid);
|
|
|
|
database.memoryDatabase.delete(usersTyping);
|
|
|
|
});
|
2019-02-12 16:14:11 +00:00
|
|
|
};
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
connectedListener = this.sdk.onStreamData('connected', handleConnection);
|
|
|
|
disconnectedListener = this.sdk.onStreamData('close', handleConnection);
|
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) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(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
|
|
|
};
|
|
|
|
}
|