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';
|
2019-10-08 12:36:15 +00:00
|
|
|
import debounce from '../../../utils/debounce';
|
2020-02-05 13:34:53 +00:00
|
|
|
import RocketChat from '../../rocketchat';
|
|
|
|
|
|
|
|
export default class RoomSubscription {
|
|
|
|
constructor(rid) {
|
|
|
|
this.rid = rid;
|
|
|
|
this.isAlive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe = async() => {
|
|
|
|
console.log(`[RCRN] Subscribing to room ${ this.rid }`);
|
|
|
|
if (this.promises) {
|
|
|
|
await this.unsubscribe();
|
|
|
|
}
|
|
|
|
this.promises = RocketChat.subscribeRoom(this.rid);
|
|
|
|
|
|
|
|
this.connectedListener = RocketChat.onStreamData('connected', this.handleConnection);
|
|
|
|
this.disconnectedListener = RocketChat.onStreamData('close', this.handleConnection);
|
|
|
|
this.notifyRoomListener = RocketChat.onStreamData('stream-notify-room', this.handleNotifyRoomReceived);
|
|
|
|
this.messageReceivedListener = RocketChat.onStreamData('stream-room-messages', this.handleMessageReceived);
|
|
|
|
if (!this.isAlive) {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
unsubscribe = async() => {
|
|
|
|
console.log(`[RCRN] Unsubscribing from room ${ this.rid }`);
|
|
|
|
this.isAlive = false;
|
|
|
|
if (this.promises) {
|
|
|
|
try {
|
|
|
|
const subscriptions = await this.promises || [];
|
|
|
|
subscriptions.map(sub => sub.unsubscribe());
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.removeListener(this.connectedListener);
|
|
|
|
this.removeListener(this.disconnectedListener);
|
|
|
|
this.removeListener(this.notifyRoomListener);
|
|
|
|
this.removeListener(this.messageReceivedListener);
|
|
|
|
reduxStore.dispatch(clearUserTyping());
|
|
|
|
}
|
2019-12-13 16:23:20 +00:00
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
removeListener = async(promise) => {
|
|
|
|
if (promise) {
|
|
|
|
try {
|
|
|
|
const listener = await promise;
|
|
|
|
listener.stop();
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
handleConnection = () => {
|
|
|
|
RocketChat.loadMissedMessages({ rid: this.rid }).catch(e => console.log(e));
|
2019-04-08 12:35:28 +00:00
|
|
|
};
|
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
handleNotifyRoomReceived = protectedFunction((ddpMessage) => {
|
2019-04-08 12:35:28 +00:00
|
|
|
const [_rid, ev] = ddpMessage.fields.eventName.split('/');
|
2020-02-05 13:34:53 +00:00
|
|
|
if (this.rid !== _rid) {
|
2019-04-08 12:35:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
read = debounce((lastOpen) => {
|
2020-02-05 17:31:36 +00:00
|
|
|
RocketChat.readMessages(this.rid, lastOpen);
|
2019-10-08 12:36:15 +00:00
|
|
|
}, 300);
|
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
handleMessageReceived = protectedFunction((ddpMessage) => {
|
2019-04-17 17:01:03 +00:00
|
|
|
const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0]));
|
2019-09-26 16:54:24 +00:00
|
|
|
const lastOpen = new Date();
|
2020-02-05 13:34:53 +00:00
|
|
|
if (this.rid !== message.rid) {
|
2019-04-08 12:35:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
const db = database.active;
|
|
|
|
const batch = [];
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
|
|
|
const threadMessagesCollection = db.collections.get('thread_messages');
|
2019-10-08 12:36:15 +00:00
|
|
|
let messageRecord;
|
|
|
|
let threadRecord;
|
|
|
|
let threadMessageRecord;
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
// Create or update message
|
2019-04-08 12:35:28 +00:00
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
messageRecord = await msgCollection.find(message._id);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
if (messageRecord) {
|
2019-11-07 19:53:39 +00:00
|
|
|
try {
|
|
|
|
const update = messageRecord.prepareUpdate((m) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(m, message);
|
2019-11-07 19:53:39 +00:00
|
|
|
});
|
|
|
|
batch.push(update);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
batch.push(
|
|
|
|
msgCollection.prepareCreate(protectedFunction((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
|
2020-02-05 13:34:53 +00:00
|
|
|
m.subscription.id = this.rid;
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(m, message);
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread
|
|
|
|
if (message.tlm) {
|
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
threadRecord = await threadsCollection.find(message._id);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threadRecord) {
|
2019-09-16 20:26:32 +00:00
|
|
|
batch.push(
|
2019-10-08 12:36:15 +00:00
|
|
|
threadRecord.prepareUpdate(protectedFunction((t) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(t, message);
|
2019-10-08 12:36:15 +00:00
|
|
|
}))
|
2019-09-16 20:26:32 +00:00
|
|
|
);
|
2019-10-08 12:36:15 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
batch.push(
|
|
|
|
threadsCollection.prepareCreate(protectedFunction((t) => {
|
|
|
|
t._raw = sanitizedRaw({ id: message._id }, threadsCollection.schema);
|
2020-02-05 13:34:53 +00:00
|
|
|
t.subscription.id = this.rid;
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(t, message);
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread message
|
|
|
|
if (message.tmid) {
|
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
threadMessageRecord = await threadMessagesCollection.find(message._id);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threadMessageRecord) {
|
2019-09-16 20:26:32 +00:00
|
|
|
batch.push(
|
2019-10-08 12:36:15 +00:00
|
|
|
threadMessageRecord.prepareUpdate(protectedFunction((tm) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(tm, message);
|
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
2019-10-08 12:36:15 +00:00
|
|
|
}))
|
2019-09-16 20:26:32 +00:00
|
|
|
);
|
2019-10-08 12:36:15 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
batch.push(
|
|
|
|
threadMessagesCollection.prepareCreate(protectedFunction((tm) => {
|
|
|
|
tm._raw = sanitizedRaw({ id: message._id }, threadMessagesCollection.schema);
|
|
|
|
Object.assign(tm, message);
|
2020-02-05 13:34:53 +00:00
|
|
|
tm.subscription.id = this.rid;
|
2019-09-16 20:26:32 +00:00
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
this.read(lastOpen);
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...batch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|