2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-02-17 19:06:18 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
import database from '../../database';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { merge } from '../helpers/mergeSubscriptionsRooms';
|
2018-05-18 17:55:08 +00:00
|
|
|
import protectedFunction from '../helpers/protectedFunction';
|
2018-05-23 13:39:18 +00:00
|
|
|
import messagesStatus from '../../../constants/messagesStatus';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../../utils/log';
|
2018-12-05 20:52:08 +00:00
|
|
|
import random from '../../../utils/random';
|
2019-02-07 16:13:21 +00:00
|
|
|
import store from '../../createStore';
|
|
|
|
import { roomsRequest } from '../../../actions/rooms';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { handlePayloadUserInteraction } from '../actions';
|
2020-02-05 15:22:29 +00:00
|
|
|
import buildMessage from '../helpers/buildMessage';
|
2020-02-12 17:21:11 +00:00
|
|
|
import RocketChat from '../../rocketchat';
|
2020-06-16 20:32:30 +00:00
|
|
|
import EventEmitter from '../../../utils/events';
|
2020-03-20 16:38:01 +00:00
|
|
|
import { removedRoom } from '../../../actions/room';
|
2020-06-26 20:45:21 +00:00
|
|
|
import { setUser } from '../../../actions/login';
|
2020-06-16 20:32:30 +00:00
|
|
|
import { INAPP_NOTIFICATION_EMITTER } from '../../../containers/InAppNotification';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-02-14 16:43:56 +00:00
|
|
|
const removeListener = listener => listener.stop();
|
|
|
|
|
|
|
|
let connectedListener;
|
|
|
|
let disconnectedListener;
|
|
|
|
let streamListener;
|
2019-06-17 13:57:07 +00:00
|
|
|
let subServer;
|
2020-02-05 15:22:29 +00:00
|
|
|
let subQueue = {};
|
|
|
|
let subTimer = null;
|
|
|
|
let roomQueue = {};
|
|
|
|
let roomTimer = null;
|
2020-02-17 19:06:18 +00:00
|
|
|
const WINDOW_TIME = 500;
|
2019-02-14 16:43:56 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const createOrUpdateSubscription = async(subscription, room) => {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const subCollection = db.collections.get('subscriptions');
|
|
|
|
const roomsCollection = db.collections.get('rooms');
|
|
|
|
|
|
|
|
if (!subscription) {
|
|
|
|
try {
|
|
|
|
const s = await subCollection.find(room._id);
|
|
|
|
// We have to create a plain obj so we can manipulate it on `merge`
|
|
|
|
// Can we do it in a better way?
|
|
|
|
subscription = {
|
|
|
|
_id: s._id,
|
|
|
|
f: s.f,
|
|
|
|
t: s.t,
|
|
|
|
ts: s.ts,
|
|
|
|
ls: s.ls,
|
|
|
|
name: s.name,
|
|
|
|
fname: s.fname,
|
|
|
|
rid: s.rid,
|
|
|
|
open: s.open,
|
|
|
|
alert: s.alert,
|
|
|
|
unread: s.unread,
|
|
|
|
userMentions: s.userMentions,
|
|
|
|
roomUpdatedAt: s.roomUpdatedAt,
|
|
|
|
ro: s.ro,
|
|
|
|
lastOpen: s.lastOpen,
|
|
|
|
description: s.description,
|
|
|
|
announcement: s.announcement,
|
2020-05-08 12:57:04 +00:00
|
|
|
bannerClosed: s.bannerClosed,
|
2019-09-16 20:26:32 +00:00
|
|
|
topic: s.topic,
|
|
|
|
blocked: s.blocked,
|
|
|
|
blocker: s.blocker,
|
|
|
|
reactWhenReadOnly: s.reactWhenReadOnly,
|
|
|
|
archived: s.archived,
|
|
|
|
joinCodeRequired: s.joinCodeRequired,
|
|
|
|
muted: s.muted,
|
|
|
|
broadcast: s.broadcast,
|
|
|
|
prid: s.prid,
|
|
|
|
draftMessage: s.draftMessage,
|
|
|
|
lastThreadSync: s.lastThreadSync,
|
2019-09-18 17:32:12 +00:00
|
|
|
jitsiTimeout: s.jitsiTimeout,
|
2019-09-16 20:26:32 +00:00
|
|
|
autoTranslate: s.autoTranslate,
|
|
|
|
autoTranslateLanguage: s.autoTranslateLanguage,
|
2020-04-01 12:28:54 +00:00
|
|
|
lastMessage: s.lastMessage,
|
2020-04-13 12:51:16 +00:00
|
|
|
roles: s.roles,
|
2020-04-01 12:28:54 +00:00
|
|
|
usernames: s.usernames,
|
2020-05-08 17:36:10 +00:00
|
|
|
uids: s.uids,
|
|
|
|
visitor: s.visitor,
|
|
|
|
departmentId: s.departmentId,
|
|
|
|
servedBy: s.servedBy,
|
|
|
|
livechatData: s.livechatData,
|
|
|
|
tags: s.tags
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
2019-10-08 12:36:15 +00:00
|
|
|
await roomsCollection.create(protectedFunction((r) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
r._raw = sanitizedRaw({ id: room._id }, roomsCollection.schema);
|
|
|
|
Object.assign(r, room);
|
2019-10-08 12:36:15 +00:00
|
|
|
}));
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!room && subscription) {
|
|
|
|
try {
|
|
|
|
const r = await roomsCollection.find(subscription.rid);
|
|
|
|
// We have to create a plain obj so we can manipulate it on `merge`
|
|
|
|
// Can we do it in a better way?
|
|
|
|
room = {
|
2020-05-08 17:36:10 +00:00
|
|
|
v: r.v,
|
|
|
|
ro: r.ro,
|
|
|
|
tags: r.tags,
|
|
|
|
servedBy: r.servedBy,
|
2019-09-16 20:26:32 +00:00
|
|
|
encrypted: r.encrypted,
|
2020-05-08 17:36:10 +00:00
|
|
|
broadcast: r.broadcast,
|
|
|
|
customFields: r.customFields,
|
|
|
|
departmentId: r.departmentId,
|
|
|
|
livechatData: r.livechatData
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmp = merge(subscription, room);
|
|
|
|
await db.action(async() => {
|
2019-10-08 12:36:15 +00:00
|
|
|
let sub;
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
sub = await subCollection.find(tmp.rid);
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (error) {
|
2019-10-08 12:36:15 +00:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2019-10-30 18:31:26 +00:00
|
|
|
const batch = [];
|
2019-10-08 12:36:15 +00:00
|
|
|
if (sub) {
|
2019-11-07 19:53:39 +00:00
|
|
|
try {
|
|
|
|
const update = sub.prepareUpdate((s) => {
|
2019-10-30 18:31:26 +00:00
|
|
|
Object.assign(s, tmp);
|
2020-05-08 12:57:04 +00:00
|
|
|
if (subscription.announcement) {
|
|
|
|
if (subscription.announcement !== sub.announcement) {
|
|
|
|
s.bannerClosed = false;
|
|
|
|
}
|
|
|
|
}
|
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-11-07 19:53:39 +00:00
|
|
|
try {
|
|
|
|
const create = subCollection.prepareCreate((s) => {
|
2019-10-30 18:31:26 +00:00
|
|
|
s._raw = sanitizedRaw({ id: tmp.rid }, subCollection.schema);
|
|
|
|
Object.assign(s, tmp);
|
|
|
|
if (s.roomUpdatedAt) {
|
|
|
|
s.roomUpdatedAt = new Date();
|
|
|
|
}
|
2019-11-07 19:53:39 +00:00
|
|
|
});
|
|
|
|
batch.push(create);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2019-10-30 18:31:26 +00:00
|
|
|
|
2020-04-30 17:53:35 +00:00
|
|
|
const { rooms } = store.getState().room;
|
|
|
|
if (tmp.lastMessage && !rooms.includes(tmp.rid)) {
|
2020-02-05 15:22:29 +00:00
|
|
|
const lastMessage = buildMessage(tmp.lastMessage);
|
|
|
|
const messagesCollection = db.collections.get('messages');
|
|
|
|
let messageRecord;
|
|
|
|
try {
|
|
|
|
messageRecord = await messagesCollection.find(lastMessage._id);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2019-10-30 18:31:26 +00:00
|
|
|
|
2020-02-05 15:22:29 +00:00
|
|
|
if (messageRecord) {
|
|
|
|
batch.push(
|
|
|
|
messageRecord.prepareUpdate(() => {
|
|
|
|
Object.assign(messageRecord, lastMessage);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
batch.push(
|
|
|
|
messagesCollection.prepareCreate((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema);
|
|
|
|
m.subscription.id = lastMessage.rid;
|
|
|
|
return Object.assign(m, lastMessage);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 18:31:26 +00:00
|
|
|
|
|
|
|
await db.batch(...batch);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-05 15:22:29 +00:00
|
|
|
const debouncedUpdateSub = (subscription) => {
|
|
|
|
if (!subTimer) {
|
|
|
|
subTimer = setTimeout(() => {
|
|
|
|
const subBatch = subQueue;
|
|
|
|
subQueue = {};
|
|
|
|
subTimer = null;
|
|
|
|
Object.keys(subBatch).forEach((key) => {
|
2020-02-17 19:06:18 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
createOrUpdateSubscription(subBatch[key]);
|
|
|
|
});
|
2020-02-05 15:22:29 +00:00
|
|
|
});
|
|
|
|
}, WINDOW_TIME);
|
|
|
|
}
|
|
|
|
subQueue[subscription.rid] = subscription;
|
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedUpdateRoom = (room) => {
|
|
|
|
if (!roomTimer) {
|
|
|
|
roomTimer = setTimeout(() => {
|
|
|
|
const roomBatch = roomQueue;
|
|
|
|
roomQueue = {};
|
|
|
|
roomTimer = null;
|
|
|
|
Object.keys(roomBatch).forEach((key) => {
|
2020-02-17 19:06:18 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
createOrUpdateSubscription(null, roomBatch[key]);
|
|
|
|
});
|
2020-02-05 15:22:29 +00:00
|
|
|
});
|
|
|
|
}, WINDOW_TIME);
|
|
|
|
}
|
|
|
|
roomQueue[room._id] = room;
|
|
|
|
};
|
|
|
|
|
2019-06-26 12:33:56 +00:00
|
|
|
export default function subscribeRooms() {
|
2019-08-14 11:16:59 +00:00
|
|
|
const handleConnection = () => {
|
2019-02-07 20:28:39 +00:00
|
|
|
store.dispatch(roomsRequest());
|
2019-02-14 16:43:56 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const handleStreamMessageReceived = protectedFunction(async(ddpMessage) => {
|
|
|
|
const db = database.active;
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
// check if the server from variable is the same as the js sdk client
|
|
|
|
if (this.sdk && this.sdk.client && this.sdk.client.host !== subServer) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
if (ddpMessage.msg === 'added') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [type, data] = ddpMessage.fields.args;
|
|
|
|
const [, ev] = ddpMessage.fields.eventName.split('/');
|
2020-06-26 20:45:21 +00:00
|
|
|
if (/userData/.test(ev)) {
|
|
|
|
const [{ diff }] = ddpMessage.fields.args;
|
|
|
|
store.dispatch(setUser({ statusLivechat: diff?.statusLivechat }));
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
if (/subscriptions/.test(ev)) {
|
|
|
|
if (type === 'removed') {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const subCollection = db.collections.get('subscriptions');
|
|
|
|
const sub = await subCollection.find(data.rid);
|
|
|
|
const messages = await sub.messages.fetch();
|
|
|
|
const threads = await sub.threads.fetch();
|
|
|
|
const threadMessages = await sub.threadMessages.fetch();
|
|
|
|
const messagesToDelete = messages.map(m => m.prepareDestroyPermanently());
|
|
|
|
const threadsToDelete = threads.map(m => m.prepareDestroyPermanently());
|
|
|
|
const threadMessagesToDelete = threadMessages.map(m => m.prepareDestroyPermanently());
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
sub.prepareDestroyPermanently(),
|
|
|
|
...messagesToDelete,
|
|
|
|
...threadsToDelete,
|
2019-10-31 16:21:59 +00:00
|
|
|
...threadMessagesToDelete
|
2019-09-16 20:26:32 +00:00
|
|
|
);
|
2019-02-25 16:23:17 +00:00
|
|
|
});
|
2020-03-06 13:13:24 +00:00
|
|
|
|
|
|
|
const roomState = store.getState().room;
|
|
|
|
// Delete and remove events come from this stream
|
|
|
|
// Here we identify which one was triggered
|
|
|
|
if (data.rid === roomState.rid && roomState.isDeleting) {
|
2020-03-20 16:38:01 +00:00
|
|
|
store.dispatch(removedRoom());
|
2020-03-06 13:13:24 +00:00
|
|
|
} else {
|
2020-06-16 20:32:30 +00:00
|
|
|
EventEmitter.emit('ROOM_REMOVED', { rid: data.rid });
|
2020-03-06 13:13:24 +00:00
|
|
|
}
|
2019-02-25 16:23:17 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
} else {
|
2020-02-05 15:22:29 +00:00
|
|
|
debouncedUpdateSub(data);
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
if (/rooms/.test(ev)) {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (type === 'updated' || type === 'inserted') {
|
2020-02-05 15:22:29 +00:00
|
|
|
debouncedUpdateRoom(data);
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
if (/message/.test(ev)) {
|
|
|
|
const [args] = ddpMessage.fields.args;
|
|
|
|
const _id = random(17);
|
|
|
|
const message = {
|
|
|
|
_id,
|
|
|
|
rid: args.rid,
|
|
|
|
msg: args.msg,
|
2020-02-11 14:01:35 +00:00
|
|
|
blocks: args.blocks,
|
2018-12-05 20:52:08 +00:00
|
|
|
ts: new Date(),
|
|
|
|
_updatedAt: new Date(),
|
|
|
|
status: messagesStatus.SENT,
|
|
|
|
u: {
|
2018-05-23 13:39:18 +00:00
|
|
|
_id,
|
2018-12-05 20:52:08 +00:00
|
|
|
username: 'rocket.cat'
|
|
|
|
}
|
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
await db.action(async() => {
|
|
|
|
await msgCollection.create(protectedFunction((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
|
|
|
|
m.subscription.id = args.rid;
|
|
|
|
Object.assign(m, message);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
2019-06-10 16:23:19 +00:00
|
|
|
if (/notification/.test(ev)) {
|
|
|
|
const [notification] = ddpMessage.fields.args;
|
2020-02-12 17:21:11 +00:00
|
|
|
try {
|
|
|
|
const { payload: { rid } } = notification;
|
2020-02-21 16:13:05 +00:00
|
|
|
const room = await RocketChat.getRoom(rid);
|
|
|
|
notification.title = RocketChat.getRoomTitle(room);
|
|
|
|
notification.avatar = RocketChat.getRoomAvatar(room);
|
2020-02-12 17:21:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2020-06-16 20:32:30 +00:00
|
|
|
EventEmitter.emit(INAPP_NOTIFICATION_EMITTER, notification);
|
2019-06-10 16:23:19 +00:00
|
|
|
}
|
2020-02-11 14:01:35 +00:00
|
|
|
if (/uiInteraction/.test(ev)) {
|
|
|
|
const { type: eventType, ...args } = type;
|
|
|
|
handlePayloadUserInteraction(eventType, args);
|
|
|
|
}
|
2019-02-14 16:43:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const stop = () => {
|
|
|
|
if (connectedListener) {
|
|
|
|
connectedListener.then(removeListener);
|
|
|
|
connectedListener = false;
|
|
|
|
}
|
|
|
|
if (disconnectedListener) {
|
|
|
|
disconnectedListener.then(removeListener);
|
|
|
|
disconnectedListener = false;
|
|
|
|
}
|
|
|
|
if (streamListener) {
|
|
|
|
streamListener.then(removeListener);
|
|
|
|
streamListener = false;
|
|
|
|
}
|
2020-02-05 15:22:29 +00:00
|
|
|
subQueue = {};
|
|
|
|
roomQueue = {};
|
|
|
|
if (subTimer) {
|
|
|
|
clearTimeout(subTimer);
|
|
|
|
subTimer = false;
|
|
|
|
}
|
|
|
|
if (roomTimer) {
|
|
|
|
clearTimeout(roomTimer);
|
|
|
|
roomTimer = false;
|
|
|
|
}
|
2019-02-14 16:43:56 +00:00
|
|
|
};
|
|
|
|
|
2019-08-14 11:16:59 +00:00
|
|
|
connectedListener = this.sdk.onStreamData('connected', handleConnection);
|
|
|
|
disconnectedListener = this.sdk.onStreamData('close', handleConnection);
|
2019-02-14 16:43:56 +00:00
|
|
|
streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-06-17 13:57:07 +00:00
|
|
|
// set the server that started this task
|
|
|
|
subServer = this.sdk.client.host;
|
2019-06-26 12:33:56 +00:00
|
|
|
this.sdk.subscribeNotifyUser().catch(e => console.log(e));
|
|
|
|
|
|
|
|
return {
|
|
|
|
stop: () => stop()
|
|
|
|
};
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-06-26 12:33:56 +00:00
|
|
|
return Promise.reject();
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|