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';
|
2022-02-15 18:46:17 +00:00
|
|
|
import EJSON from 'ejson';
|
2022-03-04 00:23:11 +00:00
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
import database from '../../database';
|
2018-05-18 17:55:08 +00:00
|
|
|
import protectedFunction from '../helpers/protectedFunction';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../helpers/log';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { store } from '../../store/auxStore';
|
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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../helpers/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';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../../encryption';
|
2022-02-15 18:46:17 +00:00
|
|
|
import updateMessages from '../updateMessages';
|
2022-03-04 00:23:11 +00:00
|
|
|
import {
|
|
|
|
IMessage,
|
2022-03-08 16:25:27 +00:00
|
|
|
IServerRoom,
|
2022-03-04 00:23:11 +00:00
|
|
|
IRoom,
|
|
|
|
ISubscription,
|
|
|
|
TMessageModel,
|
|
|
|
TRoomModel,
|
|
|
|
TThreadMessageModel,
|
2022-04-20 20:53:11 +00:00
|
|
|
TThreadModel,
|
|
|
|
SubscriptionType
|
2022-03-04 00:23:11 +00:00
|
|
|
} from '../../../definitions';
|
2022-04-07 16:53:07 +00:00
|
|
|
import sdk from '../../services/sdk';
|
2022-03-04 00:23:11 +00:00
|
|
|
import { IDDPMessage } from '../../../definitions/IDDPMessage';
|
|
|
|
import { getSubscriptionByRoomId } from '../../database/services/Subscription';
|
|
|
|
import { getMessageById } from '../../database/services/Message';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { E2E_MESSAGE_TYPE } from '../../constants';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { getRoom } from '../getRoom';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { merge } from '../helpers/mergeSubscriptionsRooms';
|
|
|
|
import { getRoomAvatar, getRoomTitle, getSenderName, random } from '../helpers';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
const removeListener = (listener: { stop: () => void }) => listener.stop();
|
2019-02-14 16:43:56 +00:00
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
let streamListener: Promise<any> | false;
|
|
|
|
let subServer: string;
|
2022-03-08 16:25:27 +00:00
|
|
|
let queue: { [key: string]: ISubscription | IRoom } = {};
|
2022-06-06 13:23:49 +00:00
|
|
|
let subTimer: ReturnType<typeof setTimeout> | null | false = null;
|
2020-02-17 19:06:18 +00:00
|
|
|
const WINDOW_TIME = 500;
|
2019-02-14 16:43:56 +00:00
|
|
|
|
2022-05-13 14:52:37 +00:00
|
|
|
export let roomsSubscription: { stop: () => void } | null = null;
|
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
const createOrUpdateSubscription = async (subscription: ISubscription, room: IServerRoom | IRoom) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
|
|
|
const roomsCollection = db.get('rooms');
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
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,
|
2020-11-30 20:00:31 +00:00
|
|
|
ignored: s.ignored,
|
2019-09-16 20:26:32 +00:00
|
|
|
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,
|
2020-09-11 14:31:38 +00:00
|
|
|
tags: s.tags,
|
|
|
|
encrypted: s.encrypted,
|
|
|
|
e2eKeyId: s.e2eKeyId,
|
2020-10-30 13:51:04 +00:00
|
|
|
E2EKey: s.E2EKey,
|
2023-01-12 13:32:33 +00:00
|
|
|
E2ESuggestedKey: s.E2ESuggestedKey,
|
2022-04-20 20:53:11 +00:00
|
|
|
avatarETag: s.avatarETag,
|
2022-06-13 13:24:54 +00:00
|
|
|
onHold: s.onHold,
|
|
|
|
hideMentionStatus: s.hideMentionStatus
|
2022-03-04 00:23:11 +00:00
|
|
|
} as ISubscription;
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (error) {
|
|
|
|
try {
|
2022-03-04 00:23:11 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await roomsCollection.create(
|
2022-03-04 00:23:11 +00:00
|
|
|
protectedFunction((r: TRoomModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
r._raw = sanitizedRaw({ id: room._id }, roomsCollection.schema);
|
|
|
|
Object.assign(r, room);
|
|
|
|
})
|
|
|
|
);
|
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-09-11 14:31:38 +00:00
|
|
|
e2eKeyId: r.e2eKeyId,
|
2020-05-08 17:36:10 +00:00
|
|
|
broadcast: r.broadcast,
|
|
|
|
customFields: r.customFields,
|
|
|
|
departmentId: r.departmentId,
|
2020-10-30 13:51:04 +00:00
|
|
|
livechatData: r.livechatData,
|
|
|
|
avatarETag: r.avatarETag
|
2022-03-04 00:23:11 +00:00
|
|
|
} as IRoom;
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
let tmp = merge(subscription, room);
|
|
|
|
tmp = (await Encryption.decryptSubscription(tmp)) as ISubscription;
|
|
|
|
const sub = await getSubscriptionByRoomId(tmp.rid);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// If we're receiving a E2EKey of a room
|
|
|
|
if (sub && !sub.E2EKey && subscription?.E2EKey) {
|
|
|
|
// Assing info from database subscription to tmp
|
|
|
|
// It should be a plain object
|
|
|
|
tmp = Object.assign(tmp, {
|
|
|
|
rid: sub.rid,
|
|
|
|
encrypted: sub.encrypted,
|
|
|
|
lastMessage: sub.lastMessage,
|
|
|
|
E2EKey: subscription.E2EKey,
|
|
|
|
e2eKeyId: sub.e2eKeyId
|
|
|
|
});
|
|
|
|
// Decrypt lastMessage using the received E2EKey
|
2022-03-04 00:23:11 +00:00
|
|
|
tmp = (await Encryption.decryptSubscription(tmp)) as ISubscription;
|
2020-09-11 14:31:38 +00:00
|
|
|
// Decrypt all pending messages of this room in parallel
|
|
|
|
Encryption.decryptPendingMessages(tmp.rid);
|
2023-01-12 13:32:33 +00:00
|
|
|
} else if (sub && subscription.E2ESuggestedKey) {
|
|
|
|
await Encryption.evaluateSuggestedKey(sub.rid, subscription.E2ESuggestedKey);
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
const batch: Model[] = [];
|
2020-09-11 14:31:38 +00:00
|
|
|
if (sub) {
|
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const update = sub.prepareUpdate(s => {
|
2020-09-11 14:31:38 +00:00
|
|
|
Object.assign(s, tmp);
|
|
|
|
if (subscription.announcement) {
|
|
|
|
if (subscription.announcement !== sub.announcement) {
|
|
|
|
s.bannerClosed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
batch.push(update);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const create = subCollection.prepareCreate(s => {
|
2020-09-11 14:31:38 +00:00
|
|
|
s._raw = sanitizedRaw({ id: tmp.rid }, subCollection.schema);
|
|
|
|
Object.assign(s, tmp);
|
|
|
|
if (s.roomUpdatedAt) {
|
|
|
|
s.roomUpdatedAt = new Date();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
batch.push(create);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
const { subscribedRoom } = store.getState().room;
|
|
|
|
if (tmp.lastMessage && subscribedRoom !== tmp.rid) {
|
2020-09-11 14:31:38 +00:00
|
|
|
const lastMessage = buildMessage(tmp.lastMessage);
|
2021-02-26 16:25:51 +00:00
|
|
|
const messagesCollection = db.get('messages');
|
2022-03-08 02:54:34 +00:00
|
|
|
let messageRecord = {} as TMessageModel | null;
|
|
|
|
if (lastMessage) {
|
|
|
|
messageRecord = await getMessageById(lastMessage._id);
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
if (messageRecord) {
|
|
|
|
batch.push(
|
|
|
|
messageRecord.prepareUpdate(() => {
|
|
|
|
Object.assign(messageRecord, lastMessage);
|
|
|
|
})
|
|
|
|
);
|
2019-10-08 12:36:15 +00:00
|
|
|
} else {
|
2020-09-11 14:31:38 +00:00
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
messagesCollection.prepareCreate(m => {
|
2022-03-08 02:54:34 +00:00
|
|
|
if (lastMessage) {
|
|
|
|
m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema);
|
|
|
|
if (m.subscription) {
|
|
|
|
m.subscription.id = lastMessage.rid;
|
|
|
|
}
|
2022-03-04 00:23:11 +00:00
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
return Object.assign(m, lastMessage);
|
|
|
|
})
|
|
|
|
);
|
2020-02-05 15:22:29 +00:00
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
2019-10-30 18:31:26 +00:00
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
await db.write(async () => {
|
2019-10-30 18:31:26 +00:00
|
|
|
await db.batch(...batch);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
const getSubQueueId = (rid: string) => `SUB-${rid}`;
|
2020-08-24 19:59:55 +00:00
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
const getRoomQueueId = (rid: string) => `ROOM-${rid}`;
|
2020-08-24 19:59:55 +00:00
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
const debouncedUpdate = (subscription: ISubscription) => {
|
2020-02-05 15:22:29 +00:00
|
|
|
if (!subTimer) {
|
|
|
|
subTimer = setTimeout(() => {
|
2020-08-24 19:59:55 +00:00
|
|
|
const batch = queue;
|
|
|
|
queue = {};
|
2020-02-05 15:22:29 +00:00
|
|
|
subTimer = null;
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.keys(batch).forEach(key => {
|
2020-02-17 19:06:18 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2020-08-24 19:59:55 +00:00
|
|
|
if (batch[key]) {
|
|
|
|
if (/SUB/.test(key)) {
|
2022-03-08 16:25:27 +00:00
|
|
|
const sub = batch[key] as ISubscription;
|
2020-08-24 19:59:55 +00:00
|
|
|
const roomQueueId = getRoomQueueId(sub.rid);
|
2022-03-08 16:25:27 +00:00
|
|
|
const room = batch[roomQueueId] as IRoom;
|
2020-08-24 19:59:55 +00:00
|
|
|
delete batch[roomQueueId];
|
|
|
|
createOrUpdateSubscription(sub, room);
|
|
|
|
} else {
|
2022-03-08 16:25:27 +00:00
|
|
|
const room = batch[key] as IRoom;
|
2022-04-20 20:53:11 +00:00
|
|
|
// If the omnichannel's chat is onHold and waitingResponse we shouldn't create or update the chat,
|
|
|
|
// because it should go to Queue
|
|
|
|
if (room.t === SubscriptionType.OMNICHANNEL && room.onHold && room.waitingResponse) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-08-24 19:59:55 +00:00
|
|
|
const subQueueId = getSubQueueId(room._id);
|
2022-03-08 16:25:27 +00:00
|
|
|
const sub = batch[subQueueId] as ISubscription;
|
2020-08-24 19:59:55 +00:00
|
|
|
delete batch[subQueueId];
|
|
|
|
createOrUpdateSubscription(sub, room);
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 19:06:18 +00:00
|
|
|
});
|
2020-02-05 15:22:29 +00:00
|
|
|
});
|
|
|
|
}, WINDOW_TIME);
|
|
|
|
}
|
2020-08-24 19:59:55 +00:00
|
|
|
queue[subscription.rid ? getSubQueueId(subscription.rid) : getRoomQueueId(subscription._id)] = subscription;
|
2020-02-05 15:22:29 +00:00
|
|
|
};
|
|
|
|
|
2019-06-26 12:33:56 +00:00
|
|
|
export default function subscribeRooms() {
|
2022-03-04 00:23:11 +00:00
|
|
|
const handleStreamMessageReceived = protectedFunction(async (ddpMessage: IDDPMessage) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
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
|
2022-03-04 00:23:11 +00:00
|
|
|
if (sdk && sdk.current.client && sdk.current.client.host !== subServer) {
|
2019-06-17 13:57:07 +00:00
|
|
|
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)) {
|
2023-02-07 16:47:06 +00:00
|
|
|
const [{ diff, unset }] = ddpMessage.fields.args;
|
2020-08-21 13:38:50 +00:00
|
|
|
if (diff?.statusLivechat) {
|
|
|
|
store.dispatch(setUser({ statusLivechat: diff.statusLivechat }));
|
|
|
|
}
|
2022-03-04 00:23:11 +00:00
|
|
|
if ((['settings.preferences.showMessageInMainThread'] as any) in diff) {
|
2020-10-30 17:35:07 +00:00
|
|
|
store.dispatch(setUser({ showMessageInMainThread: diff['settings.preferences.showMessageInMainThread'] }));
|
|
|
|
}
|
2022-08-25 19:53:19 +00:00
|
|
|
if ((['settings.preferences.alsoSendThreadToChannel'] as any) in diff) {
|
|
|
|
store.dispatch(setUser({ alsoSendThreadToChannel: diff['settings.preferences.alsoSendThreadToChannel'] }));
|
|
|
|
}
|
2023-02-07 16:47:06 +00:00
|
|
|
if (diff?.avatarETag) {
|
|
|
|
store.dispatch(setUser({ avatarETag: diff.avatarETag }));
|
|
|
|
}
|
|
|
|
if (unset?.avatarETag) {
|
|
|
|
store.dispatch(setUser({ avatarETag: '' }));
|
|
|
|
}
|
2020-06-26 20:45:21 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
if (/subscriptions/.test(ev)) {
|
|
|
|
if (type === 'removed') {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2019-09-16 20:26:32 +00:00
|
|
|
const sub = await subCollection.find(data.rid);
|
2022-03-04 00:23:11 +00:00
|
|
|
// TODO - today the Relation type from watermelon just support one to one relations
|
|
|
|
// @ts-ignore
|
|
|
|
const messages = (await sub.messages.fetch()) as TMessageModel[];
|
|
|
|
// @ts-ignore
|
|
|
|
const threads = (await sub.threads.fetch()) as TThreadModel[];
|
|
|
|
// @ts-ignore
|
|
|
|
const threadMessages = (await sub.threadMessages.fetch()) as TThreadMessageModel[];
|
|
|
|
|
|
|
|
const messagesToDelete = messages?.map((m: TMessageModel) => m.prepareDestroyPermanently());
|
|
|
|
const threadsToDelete = threads?.map((m: TThreadModel) => m.prepareDestroyPermanently());
|
|
|
|
const threadMessagesToDelete = threadMessages?.map((m: TThreadMessageModel) => m.prepareDestroyPermanently());
|
|
|
|
|
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await db.batch(sub.prepareDestroyPermanently(), ...messagesToDelete, ...threadsToDelete, ...threadMessagesToDelete);
|
2019-02-25 16:23:17 +00:00
|
|
|
});
|
2020-03-06 13:13:24 +00:00
|
|
|
|
2023-01-12 13:32:33 +00:00
|
|
|
Encryption.stopRoom(data.rid);
|
|
|
|
|
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-08-24 19:59:55 +00:00
|
|
|
debouncedUpdate(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-08-24 19:59:55 +00:00
|
|
|
debouncedUpdate(data);
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
if (/message/.test(ev)) {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2022-02-15 18:46:17 +00:00
|
|
|
const [args] = ddpMessage.fields.args;
|
|
|
|
const _id = random(17);
|
|
|
|
const message = {
|
2022-03-04 00:23:11 +00:00
|
|
|
// @ts-ignore
|
2022-02-15 18:46:17 +00:00
|
|
|
u: {
|
|
|
|
_id,
|
|
|
|
username: 'rocket.cat',
|
|
|
|
name: 'Rocket Cat'
|
|
|
|
},
|
|
|
|
...buildMessage(EJSON.fromJSONValue(args))
|
2022-03-04 00:23:11 +00:00
|
|
|
} as IMessage;
|
2022-02-15 18:46:17 +00:00
|
|
|
await updateMessages({ rid: args.rid, update: [message] });
|
2019-09-16 20:26:32 +00:00
|
|
|
} 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 {
|
2021-09-13 20:41:05 +00:00
|
|
|
const {
|
|
|
|
payload: { rid, message, sender }
|
|
|
|
} = notification;
|
2022-04-28 20:37:25 +00:00
|
|
|
const room = await getRoom(rid);
|
|
|
|
notification.title = getRoomTitle(room);
|
|
|
|
notification.avatar = getRoomAvatar(room);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// If it's from a encrypted room
|
2020-11-12 14:07:36 +00:00
|
|
|
if (message?.t === E2E_MESSAGE_TYPE) {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Decrypt this message content
|
|
|
|
const { msg } = await Encryption.decryptMessage({ ...message, rid });
|
|
|
|
// If it's a direct the content is the message decrypted
|
|
|
|
if (room.t === 'd') {
|
|
|
|
notification.text = msg;
|
2021-09-13 20:41:05 +00:00
|
|
|
// If it's a private group we should add the sender name
|
2020-09-11 14:31:38 +00:00
|
|
|
} else {
|
2022-04-28 20:37:25 +00:00
|
|
|
notification.text = `${getSenderName(sender)}: ${msg}`;
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 17:21:11 +00:00
|
|
|
} catch (e) {
|
2020-09-11 14:31:38 +00:00
|
|
|
log(e);
|
2020-02-12 17:21:11 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
if (/e2ekeyRequest/.test(ev)) {
|
|
|
|
const [roomId, keyId] = ddpMessage.fields.args;
|
|
|
|
try {
|
|
|
|
await Encryption.provideRoomKeyToUser(keyId, roomId);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 16:43:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const stop = () => {
|
|
|
|
if (streamListener) {
|
|
|
|
streamListener.then(removeListener);
|
|
|
|
streamListener = false;
|
|
|
|
}
|
2020-08-24 19:59:55 +00:00
|
|
|
queue = {};
|
2020-02-05 15:22:29 +00:00
|
|
|
if (subTimer) {
|
|
|
|
clearTimeout(subTimer);
|
|
|
|
subTimer = false;
|
|
|
|
}
|
2022-05-13 14:52:37 +00:00
|
|
|
roomsSubscription = null;
|
2019-02-14 16:43:56 +00:00
|
|
|
};
|
|
|
|
|
2022-03-04 00:23:11 +00:00
|
|
|
streamListener = 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
|
2022-03-04 00:23:11 +00:00
|
|
|
subServer = sdk.current.client.host;
|
|
|
|
sdk.current.subscribeNotifyUser().catch((e: unknown) => console.log(e));
|
2022-05-13 14:52:37 +00:00
|
|
|
roomsSubscription = { stop: () => stop() };
|
|
|
|
return null;
|
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
|
|
|
}
|