2019-04-08 12:35:28 +00:00
|
|
|
import EJSON from 'ejson';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
import { InteractionManager } from 'react-native';
|
2019-04-08 12:35:28 +00:00
|
|
|
|
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';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../database';
|
|
|
|
import reduxStore from '../../createStore';
|
|
|
|
import { addUserTyping, removeUserTyping, clearUserTyping } from '../../../actions/usersTyping';
|
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-09-16 20:26:32 +00:00
|
|
|
console.log(`[RCRN] Subscribed to room ${ 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-14 11:16:59 +00:00
|
|
|
const handleConnection = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
this.loadMissedMessages({ rid }).catch(e => console.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) {
|
2019-09-16 20:26:32 +00:00
|
|
|
reduxStore.dispatch(addUserTyping(username));
|
2019-04-08 12:35:28 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
reduxStore.dispatch(removeUserTyping(username));
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
} else if (ev === 'deleteMessage') {
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
2019-04-08 12:35:28 +00:00
|
|
|
if (ddpMessage && ddpMessage.fields && ddpMessage.fields.args.length > 0) {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const { _id } = ddpMessage.fields.args[0];
|
|
|
|
const db = database.active;
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
|
|
|
const threadMessagesCollection = db.collections.get('thread_messages');
|
|
|
|
let deleteMessage;
|
|
|
|
let deleteThread;
|
|
|
|
let deleteThreadMessage;
|
|
|
|
|
|
|
|
// Delete message
|
|
|
|
try {
|
|
|
|
const m = await msgCollection.find(_id);
|
|
|
|
deleteMessage = m.prepareDestroyPermanently();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete thread
|
|
|
|
try {
|
|
|
|
const m = await threadsCollection.find(_id);
|
|
|
|
deleteThread = m.prepareDestroyPermanently();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete thread message
|
|
|
|
try {
|
|
|
|
const m = await threadMessagesCollection.find(_id);
|
|
|
|
deleteThreadMessage = m.prepareDestroyPermanently();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
deleteMessage, deleteThread, deleteThreadMessage
|
|
|
|
);
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
const db = database.active;
|
|
|
|
const batch = [];
|
|
|
|
const subCollection = db.collections.get('subscriptions');
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
|
|
|
const threadMessagesCollection = db.collections.get('thread_messages');
|
|
|
|
|
|
|
|
// Create or update message
|
2019-04-08 12:35:28 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const messageRecord = await msgCollection.find(message._id);
|
|
|
|
batch.push(
|
|
|
|
messageRecord.prepareUpdate((m) => {
|
|
|
|
Object.assign(m, message);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
batch.push(
|
|
|
|
msgCollection.prepareCreate(protectedFunction((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
|
|
|
|
m.subscription.id = rid;
|
|
|
|
Object.assign(m, message);
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread
|
|
|
|
if (message.tlm) {
|
|
|
|
try {
|
|
|
|
const threadRecord = await threadsCollection.find(message._id);
|
|
|
|
batch.push(
|
|
|
|
threadRecord.prepareUpdate((t) => {
|
|
|
|
Object.assign(t, message);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
batch.push(
|
|
|
|
threadsCollection.prepareCreate(protectedFunction((t) => {
|
|
|
|
t._raw = sanitizedRaw({ id: message._id }, threadsCollection.schema);
|
|
|
|
t.subscription.id = rid;
|
|
|
|
Object.assign(t, message);
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread message
|
|
|
|
if (message.tmid) {
|
|
|
|
try {
|
|
|
|
const threadMessageRecord = await threadMessagesCollection.find(message._id);
|
|
|
|
batch.push(
|
|
|
|
threadMessageRecord.prepareUpdate((tm) => {
|
|
|
|
Object.assign(tm, message);
|
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
batch.push(
|
|
|
|
threadMessagesCollection.prepareCreate(protectedFunction((tm) => {
|
|
|
|
tm._raw = sanitizedRaw({ id: message._id }, threadMessagesCollection.schema);
|
|
|
|
Object.assign(tm, message);
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
await subCollection.find(rid);
|
|
|
|
this.readMessages(rid);
|
2019-04-08 12:35:28 +00:00
|
|
|
} catch (e) {
|
2019-09-16 20:26:32 +00:00
|
|
|
console.log('Subscription not found. We probably subscribed to a not joined channel. No need to mark as read.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...batch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-09-16 20:26:32 +00:00
|
|
|
reduxStore.dispatch(clearUserTyping());
|
2019-02-12 16:14:11 +00:00
|
|
|
};
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2019-08-14 11:16:59 +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-08-23 13:18:47 +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
|
|
|
};
|
|
|
|
}
|