2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
|
|
|
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';
|
2019-06-10 16:23:19 +00:00
|
|
|
import { notificationReceived } from '../../../actions/notification';
|
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;
|
2019-02-14 16:43:56 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// TODO: batch execution
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
lastMessage: s.lastMessage
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await roomsCollection.create((r) => {
|
|
|
|
r._raw = sanitizedRaw({ id: room._id }, roomsCollection.schema);
|
|
|
|
Object.assign(r, room);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} 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 = {
|
|
|
|
customFields: r.customFields,
|
|
|
|
broadcast: r.broadcast,
|
|
|
|
encrypted: r.encrypted,
|
|
|
|
ro: r.ro
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmp = merge(subscription, room);
|
|
|
|
await db.action(async() => {
|
|
|
|
try {
|
|
|
|
const sub = await subCollection.find(tmp.rid);
|
|
|
|
await sub.update((s) => {
|
|
|
|
Object.assign(s, tmp);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
await subCollection.create((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: tmp.rid }, subCollection.schema);
|
|
|
|
Object.assign(s, tmp);
|
|
|
|
if (s.roomUpdatedAt) {
|
|
|
|
s.roomUpdatedAt = new Date();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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('/');
|
|
|
|
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,
|
|
|
|
...threadMessagesToDelete,
|
|
|
|
);
|
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 {
|
2019-09-16 20:26:32 +00:00
|
|
|
await createOrUpdateSubscription(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') {
|
|
|
|
await createOrUpdateSubscription(null, 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,
|
|
|
|
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;
|
|
|
|
store.dispatch(notificationReceived(notification));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|