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';
|
2022-02-10 20:16:10 +00:00
|
|
|
import { getMessageById } from '../../database/services/Message';
|
|
|
|
import { getThreadById } from '../../database/services/Thread';
|
|
|
|
import { getThreadMessageById } from '../../database/services/ThreadMessage';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { store as reduxStore } from '../../store/auxStore';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { addUserTyping, clearUserTyping, removeUserTyping } from '../../../actions/usersTyping';
|
2019-10-08 12:36:15 +00:00
|
|
|
import debounce from '../../../utils/debounce';
|
2020-04-30 17:53:35 +00:00
|
|
|
import { subscribeRoom, unsubscribeRoom } from '../../../actions/room';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../../encryption';
|
2022-03-04 00:49:20 +00:00
|
|
|
import { IMessage, TMessageModel, TSubscriptionModel, TThreadMessageModel, TThreadModel } from '../../../definitions';
|
|
|
|
import { IDDPMessage } from '../../../definitions/IDDPMessage';
|
2022-04-07 16:53:07 +00:00
|
|
|
import sdk from '../../services/sdk';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { readMessages } from '../readMessages';
|
|
|
|
import { loadMissedMessages } from '../loadMissedMessages';
|
2020-02-05 13:34:53 +00:00
|
|
|
|
2020-03-02 20:12:41 +00:00
|
|
|
const WINDOW_TIME = 1000;
|
|
|
|
|
2020-02-05 13:34:53 +00:00
|
|
|
export default class RoomSubscription {
|
2022-03-04 00:49:20 +00:00
|
|
|
private rid: string;
|
|
|
|
private isAlive: boolean;
|
2022-06-06 13:23:49 +00:00
|
|
|
private timer: ReturnType<typeof setTimeout> | null;
|
2022-03-04 00:49:20 +00:00
|
|
|
private queue: { [key: string]: IMessage };
|
|
|
|
private messagesBatch: {};
|
|
|
|
private _messagesBatch: { [key: string]: TMessageModel };
|
|
|
|
private threadsBatch: {};
|
|
|
|
private _threadsBatch: { [key: string]: TThreadModel };
|
|
|
|
private threadMessagesBatch: {};
|
|
|
|
private _threadMessagesBatch: { [key: string]: TThreadMessageModel };
|
|
|
|
private promises?: Promise<TSubscriptionModel[]>;
|
|
|
|
private connectedListener?: Promise<any>;
|
|
|
|
private disconnectedListener?: Promise<any>;
|
|
|
|
private notifyRoomListener?: Promise<any>;
|
|
|
|
private messageReceivedListener?: Promise<any>;
|
|
|
|
private lastOpen?: Date;
|
|
|
|
|
|
|
|
constructor(rid: string) {
|
2020-02-05 13:34:53 +00:00
|
|
|
this.rid = rid;
|
|
|
|
this.isAlive = true;
|
2020-03-02 20:12:41 +00:00
|
|
|
this.timer = null;
|
|
|
|
this.queue = {};
|
|
|
|
this.messagesBatch = {};
|
|
|
|
this.threadsBatch = {};
|
|
|
|
this.threadMessagesBatch = {};
|
2022-03-04 00:49:20 +00:00
|
|
|
|
|
|
|
this._messagesBatch = {};
|
|
|
|
this._threadsBatch = {};
|
|
|
|
this._threadMessagesBatch = {};
|
2020-02-05 13:34:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
subscribe = async () => {
|
|
|
|
console.log(`[RCRN] Subscribing to room ${this.rid}`);
|
2020-02-05 13:34:53 +00:00
|
|
|
if (this.promises) {
|
|
|
|
await this.unsubscribe();
|
|
|
|
}
|
2022-04-04 19:15:29 +00:00
|
|
|
this.promises = sdk.subscribeRoom(this.rid);
|
2020-02-05 13:34:53 +00:00
|
|
|
|
2022-04-04 19:15:29 +00:00
|
|
|
this.connectedListener = sdk.onStreamData('connected', this.handleConnection);
|
|
|
|
this.disconnectedListener = sdk.onStreamData('close', this.handleConnection);
|
|
|
|
this.notifyRoomListener = sdk.onStreamData('stream-notify-room', this.handleNotifyRoomReceived);
|
|
|
|
this.messageReceivedListener = sdk.onStreamData('stream-room-messages', this.handleMessageReceived);
|
2020-02-05 13:34:53 +00:00
|
|
|
if (!this.isAlive) {
|
2022-03-04 00:49:20 +00:00
|
|
|
await this.unsubscribe();
|
2020-02-05 13:34:53 +00:00
|
|
|
}
|
2020-04-30 17:53:35 +00:00
|
|
|
|
|
|
|
reduxStore.dispatch(subscribeRoom(this.rid));
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
unsubscribe = async () => {
|
|
|
|
console.log(`[RCRN] Unsubscribing from room ${this.rid}`);
|
2020-02-05 13:34:53 +00:00
|
|
|
this.isAlive = false;
|
2020-08-25 17:44:16 +00:00
|
|
|
reduxStore.dispatch(unsubscribeRoom(this.rid));
|
2020-02-05 13:34:53 +00:00
|
|
|
if (this.promises) {
|
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const subscriptions = (await this.promises) || [];
|
2020-02-17 16:07:09 +00:00
|
|
|
subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom')));
|
2020-02-05 13:34:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 18:03:53 +00:00
|
|
|
reduxStore.dispatch(clearUserTyping());
|
2020-02-05 13:34:53 +00:00
|
|
|
this.removeListener(this.connectedListener);
|
|
|
|
this.removeListener(this.disconnectedListener);
|
|
|
|
this.removeListener(this.notifyRoomListener);
|
|
|
|
this.removeListener(this.messageReceivedListener);
|
2020-03-02 20:12:41 +00:00
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-13 16:23:20 +00:00
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
removeListener = async (promise?: Promise<any>): Promise<void> => {
|
2020-02-05 13:34:53 +00:00
|
|
|
if (promise) {
|
|
|
|
try {
|
|
|
|
const listener = await promise;
|
|
|
|
listener.stop();
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-11 14:12:25 +00:00
|
|
|
handleConnection = async () => {
|
|
|
|
try {
|
|
|
|
reduxStore.dispatch(clearUserTyping());
|
2022-04-28 20:37:25 +00:00
|
|
|
await loadMissedMessages({ rid: this.rid });
|
2022-03-11 14:12:25 +00:00
|
|
|
const _lastOpen = new Date();
|
|
|
|
this.read(_lastOpen);
|
|
|
|
this.lastOpen = _lastOpen;
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
};
|
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
handleNotifyRoomReceived = protectedFunction((ddpMessage: IDDPMessage) => {
|
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') {
|
2020-03-26 16:59:11 +00:00
|
|
|
const { user } = reduxStore.getState().login;
|
2020-06-26 20:48:40 +00:00
|
|
|
const { UI_Use_Real_Name } = reduxStore.getState().settings;
|
2021-02-23 16:57:18 +00:00
|
|
|
const { rooms } = reduxStore.getState().room;
|
|
|
|
if (rooms[0] !== _rid) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-26 20:48:40 +00:00
|
|
|
const [name, typing] = ddpMessage.fields.args;
|
|
|
|
const key = UI_Use_Real_Name ? 'name' : 'username';
|
|
|
|
if (name !== user[key]) {
|
2020-03-26 16:59:11 +00:00
|
|
|
if (typing) {
|
2020-06-26 20:48:40 +00:00
|
|
|
reduxStore.dispatch(addUserTyping(name));
|
2020-03-26 16:59:11 +00:00
|
|
|
} else {
|
2020-06-26 20:48:40 +00:00
|
|
|
reduxStore.dispatch(removeUserTyping(name));
|
2020-03-26 16:59:11 +00:00
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
} else if (ev === 'deleteMessage') {
|
2021-09-13 20:41:05 +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;
|
2021-02-26 16:25:51 +00:00
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadsCollection = db.get('threads');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2022-03-04 00:49:20 +00:00
|
|
|
let deleteMessage: TMessageModel;
|
|
|
|
let deleteThread: TThreadModel;
|
|
|
|
let deleteThreadMessage: TThreadMessageModel;
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-03-04 00:49:20 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
read = debounce((lastOpen: Date) => {
|
2022-04-28 20:37:25 +00:00
|
|
|
readMessages(this.rid, lastOpen);
|
2019-10-08 12:36:15 +00:00
|
|
|
}, 300);
|
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
updateMessage = (message: IMessage): Promise<void> =>
|
2021-09-13 20:41:05 +00:00
|
|
|
new Promise(async resolve => {
|
2020-03-02 20:12:41 +00:00
|
|
|
if (this.rid !== message.rid) {
|
2021-05-27 17:23:17 +00:00
|
|
|
return resolve();
|
2020-03-02 20:12:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadsCollection = db.get('threads');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
// Decrypt the message if necessary
|
|
|
|
message = await Encryption.decryptMessage(message);
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// Create or update message
|
2019-04-08 12:35:28 +00:00
|
|
|
try {
|
2022-02-10 20:16:10 +00:00
|
|
|
let operation = null;
|
|
|
|
const messageRecord = await getMessageById(message._id);
|
|
|
|
if (messageRecord) {
|
|
|
|
operation = messageRecord.prepareUpdate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((m: TMessageModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(m, message);
|
|
|
|
})
|
|
|
|
);
|
2022-02-10 20:16:10 +00:00
|
|
|
} else {
|
|
|
|
operation = msgCollection.prepareCreate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((m: TMessageModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
|
2022-03-04 00:49:20 +00:00
|
|
|
if (m.subscription) m.subscription.id = this.rid;
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(m, message);
|
|
|
|
})
|
|
|
|
);
|
2020-04-30 18:04:39 +00:00
|
|
|
}
|
2022-02-10 20:16:10 +00:00
|
|
|
this._messagesBatch[message._id] = operation;
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread
|
|
|
|
if (message.tlm) {
|
|
|
|
try {
|
2022-02-10 20:16:10 +00:00
|
|
|
let operation = null;
|
|
|
|
const threadRecord = await getThreadById(message._id);
|
|
|
|
if (threadRecord) {
|
|
|
|
operation = threadRecord.prepareUpdate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((t: TThreadModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(t, message);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
operation = threadsCollection.prepareCreate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((t: TThreadModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
t._raw = sanitizedRaw({ id: message._id }, threadsCollection.schema);
|
2022-03-04 00:49:20 +00:00
|
|
|
if (t.subscription) t.subscription.id = this.rid;
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(t, message);
|
|
|
|
})
|
|
|
|
);
|
2020-04-30 18:04:39 +00:00
|
|
|
}
|
2022-02-10 20:16:10 +00:00
|
|
|
this._threadsBatch[message._id] = operation;
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update thread message
|
|
|
|
if (message.tmid) {
|
|
|
|
try {
|
2022-02-10 20:16:10 +00:00
|
|
|
let operation = null;
|
|
|
|
const threadMessageRecord = await getThreadMessageById(message._id);
|
|
|
|
if (threadMessageRecord) {
|
|
|
|
operation = threadMessageRecord.prepareUpdate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((tm: TThreadMessageModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(tm, message);
|
2022-03-04 00:49:20 +00:00
|
|
|
if (message.tmid) {
|
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
|
|
|
}
|
2022-02-10 20:16:10 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
operation = threadMessagesCollection.prepareCreate(
|
2022-03-04 00:49:20 +00:00
|
|
|
protectedFunction((tm: TThreadMessageModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
tm._raw = sanitizedRaw({ id: message._id }, threadMessagesCollection.schema);
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(tm, message);
|
2022-03-04 00:49:20 +00:00
|
|
|
if (tm.subscription) {
|
|
|
|
tm.subscription.id = this.rid;
|
|
|
|
}
|
|
|
|
if (message.tmid) {
|
|
|
|
tm.rid = message.tmid;
|
|
|
|
delete tm.tmid;
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
})
|
|
|
|
);
|
2020-04-30 18:04:39 +00:00
|
|
|
}
|
2022-02-10 20:16:10 +00:00
|
|
|
this._threadMessagesBatch[message._id] = operation;
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
|
2020-03-02 20:12:41 +00:00
|
|
|
return resolve();
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
handleMessageReceived = (ddpMessage: IDDPMessage) => {
|
2020-03-02 20:12:41 +00:00
|
|
|
if (!this.timer) {
|
2021-09-13 20:41:05 +00:00
|
|
|
this.timer = setTimeout(async () => {
|
2020-03-02 20:12:41 +00:00
|
|
|
// copy variables values to local and clean them
|
|
|
|
const _lastOpen = this.lastOpen;
|
|
|
|
const _queue = Object.keys(this.queue).map(key => this.queue[key]);
|
|
|
|
this._messagesBatch = this.messagesBatch;
|
|
|
|
this._threadsBatch = this.threadsBatch;
|
|
|
|
this._threadMessagesBatch = this.threadMessagesBatch;
|
|
|
|
this.queue = {};
|
|
|
|
this.messagesBatch = {};
|
|
|
|
this.threadsBatch = {};
|
|
|
|
this.threadMessagesBatch = {};
|
|
|
|
this.timer = null;
|
|
|
|
|
|
|
|
for (let i = 0; i < _queue.length; i += 1) {
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await this.updateMessage(_queue[i]);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2022-03-04 00:49:20 +00:00
|
|
|
await db.write(async () => {
|
2020-03-02 20:12:41 +00:00
|
|
|
await db.batch(
|
|
|
|
...Object.values(this._messagesBatch),
|
|
|
|
...Object.values(this._threadsBatch),
|
|
|
|
...Object.values(this._threadMessagesBatch)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.read(_lastOpen);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean local variables
|
|
|
|
this._messagesBatch = {};
|
|
|
|
this._threadsBatch = {};
|
|
|
|
this._threadMessagesBatch = {};
|
|
|
|
}, WINDOW_TIME);
|
|
|
|
}
|
|
|
|
this.lastOpen = new Date();
|
2022-03-04 00:49:20 +00:00
|
|
|
const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0])) as IMessage;
|
2020-03-02 20:12:41 +00:00
|
|
|
this.queue[message._id] = message;
|
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|