2019-02-07 15:48:10 +00:00
|
|
|
import EJSON from 'ejson';
|
2021-02-26 18:31:35 +00:00
|
|
|
import { lt, coerce } from 'semver';
|
2019-02-07 15:48:10 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import normalizeMessage from './normalizeMessage';
|
2020-04-06 20:23:13 +00:00
|
|
|
import findSubscriptionsRooms from './findSubscriptionsRooms';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../../encryption';
|
2021-02-26 18:31:35 +00:00
|
|
|
import reduxStore from '../../createStore';
|
2018-04-24 19:34:03 +00:00
|
|
|
// TODO: delete and update
|
|
|
|
|
|
|
|
export const merge = (subscription, room) => {
|
2021-02-26 18:31:35 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2019-02-07 15:48:10 +00:00
|
|
|
subscription = EJSON.fromJSONValue(subscription);
|
|
|
|
room = EJSON.fromJSONValue(room);
|
|
|
|
|
2018-08-10 17:26:36 +00:00
|
|
|
if (!subscription) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
if (room) {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (room._updatedAt) {
|
|
|
|
subscription.lastMessage = normalizeMessage(room.lastMessage);
|
|
|
|
subscription.description = room.description;
|
|
|
|
subscription.topic = room.topic;
|
|
|
|
subscription.announcement = room.announcement;
|
|
|
|
subscription.reactWhenReadOnly = room.reactWhenReadOnly;
|
|
|
|
subscription.archived = room.archived || false;
|
|
|
|
subscription.joinCodeRequired = room.joinCodeRequired;
|
2019-09-18 17:32:12 +00:00
|
|
|
subscription.jitsiTimeout = room.jitsiTimeout;
|
2020-04-01 12:28:54 +00:00
|
|
|
subscription.usernames = room.usernames;
|
|
|
|
subscription.uids = room.uids;
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
2021-02-26 18:31:35 +00:00
|
|
|
if (serverVersion && lt(coerce(serverVersion), '3.7.0')) {
|
|
|
|
const updatedAt = room?._updatedAt ? new Date(room._updatedAt) : null;
|
|
|
|
const lastMessageTs = subscription?.lastMessage?.ts ? new Date(subscription.lastMessage.ts) : null;
|
|
|
|
subscription.roomUpdatedAt = Math.max(updatedAt, lastMessageTs);
|
|
|
|
} else {
|
|
|
|
// https://github.com/RocketChat/Rocket.Chat/blob/develop/app/ui-sidenav/client/roomList.js#L180
|
|
|
|
const lastRoomUpdate = room.lm || subscription.ts || subscription._updatedAt;
|
|
|
|
subscription.roomUpdatedAt = subscription.lr ? Math.max(new Date(subscription.lr), new Date(lastRoomUpdate)) : lastRoomUpdate;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
subscription.ro = room.ro;
|
2018-05-24 20:17:45 +00:00
|
|
|
subscription.broadcast = room.broadcast;
|
2020-09-11 14:31:38 +00:00
|
|
|
subscription.encrypted = room.encrypted;
|
|
|
|
subscription.e2eKeyId = room.e2eKeyId;
|
2020-10-30 13:51:04 +00:00
|
|
|
subscription.avatarETag = room.avatarETag;
|
2019-04-26 21:13:07 +00:00
|
|
|
if (!subscription.roles || !subscription.roles.length) {
|
|
|
|
subscription.roles = [];
|
|
|
|
}
|
2020-11-30 20:00:31 +00:00
|
|
|
if (!subscription.ignored?.length) {
|
|
|
|
subscription.ignored = [];
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
if (room.muted && room.muted.length) {
|
2019-05-20 20:43:50 +00:00
|
|
|
subscription.muted = room.muted.filter(muted => !!muted);
|
2018-06-01 17:38:13 +00:00
|
|
|
} else {
|
|
|
|
subscription.muted = [];
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
if (room.v) {
|
|
|
|
subscription.visitor = room.v;
|
|
|
|
}
|
|
|
|
if (room.departmentId) {
|
|
|
|
subscription.departmentId = room.departmentId;
|
|
|
|
}
|
|
|
|
if (room.servedBy) {
|
|
|
|
subscription.servedBy = room.servedBy;
|
|
|
|
}
|
|
|
|
if (room.livechatData) {
|
|
|
|
subscription.livechatData = room.livechatData;
|
|
|
|
}
|
|
|
|
if (room.tags) {
|
|
|
|
subscription.tags = room.tags;
|
|
|
|
}
|
2020-03-06 14:19:03 +00:00
|
|
|
subscription.sysMes = room.sysMes;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 19:54:20 +00:00
|
|
|
if (!subscription.name) {
|
|
|
|
subscription.name = subscription.fname;
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:22:22 +00:00
|
|
|
if (!subscription.autoTranslate) {
|
|
|
|
subscription.autoTranslate = false;
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
subscription.blocker = !!subscription.blocker;
|
|
|
|
subscription.blocked = !!subscription.blocked;
|
2018-04-24 19:34:03 +00:00
|
|
|
return subscription;
|
|
|
|
};
|
|
|
|
|
2020-04-06 20:23:13 +00:00
|
|
|
export default async(subscriptions = [], rooms = []) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (subscriptions.update) {
|
|
|
|
subscriptions = subscriptions.update;
|
|
|
|
rooms = rooms.update;
|
|
|
|
}
|
2020-04-06 20:23:13 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
// Find missing rooms/subscriptions on local database
|
2020-04-06 20:23:13 +00:00
|
|
|
({ subscriptions, rooms } = await findSubscriptionsRooms(subscriptions, rooms));
|
2020-09-11 14:31:38 +00:00
|
|
|
// Merge each subscription into a room
|
|
|
|
subscriptions = subscriptions.map((s) => {
|
|
|
|
const index = rooms.findIndex(({ _id }) => _id === s.rid);
|
|
|
|
// Room not found
|
|
|
|
if (index < 0) {
|
|
|
|
return merge(s);
|
|
|
|
}
|
|
|
|
const [room] = rooms.splice(index, 1);
|
|
|
|
return merge(s, room);
|
|
|
|
});
|
|
|
|
// Decrypt all subscriptions missing decryption
|
|
|
|
subscriptions = await Encryption.decryptSubscriptions(subscriptions);
|
2020-04-06 20:23:13 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
return {
|
2020-09-11 14:31:38 +00:00
|
|
|
subscriptions,
|
2018-04-24 19:34:03 +00:00
|
|
|
rooms
|
|
|
|
};
|
|
|
|
};
|