2019-02-07 15:48:10 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../../encryption';
|
2022-02-09 21:16:20 +00:00
|
|
|
import { store as reduxStore } from '../../auxStore';
|
2022-02-07 18:44:04 +00:00
|
|
|
import { compareServerVersion } from '../../utils';
|
2021-09-13 20:41:05 +00:00
|
|
|
import findSubscriptionsRooms from './findSubscriptionsRooms';
|
|
|
|
import normalizeMessage from './normalizeMessage';
|
2022-02-21 19:41:49 +00:00
|
|
|
import { ISubscription, IServerRoom, IServerSubscription, IServerSubscriptionItem, IServerRoomItem } from '../../../definitions';
|
2018-04-24 19:34:03 +00:00
|
|
|
// TODO: delete and update
|
|
|
|
|
2022-02-21 19:41:49 +00:00
|
|
|
export const merge = (
|
|
|
|
subscription: ISubscription | IServerSubscriptionItem,
|
|
|
|
room?: ISubscription | IServerRoomItem
|
|
|
|
): ISubscription => {
|
|
|
|
const serverVersion = reduxStore.getState().server.version as string;
|
|
|
|
subscription = EJSON.fromJSONValue(subscription) as ISubscription;
|
2019-02-07 15:48:10 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (room) {
|
2022-02-21 19:41:49 +00:00
|
|
|
room = EJSON.fromJSONValue(room) as ISubscription;
|
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
|
|
|
}
|
2022-02-21 19:41:49 +00:00
|
|
|
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'lowerThan', '3.7.0')) {
|
2021-02-26 18:31:35 +00:00
|
|
|
const updatedAt = room?._updatedAt ? new Date(room._updatedAt) : null;
|
|
|
|
const lastMessageTs = subscription?.lastMessage?.ts ? new Date(subscription.lastMessage.ts) : null;
|
2022-02-21 19:41:49 +00:00
|
|
|
// @ts-ignore
|
|
|
|
// If all parameters are null it will return zero, if only one is null it will return its timestamp only.
|
|
|
|
// "It works", but it's not the best solution. It does not accept "Date" as a parameter, but it works.
|
2021-02-26 18:31:35 +00:00
|
|
|
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;
|
2022-02-21 19:41:49 +00:00
|
|
|
// @ts-ignore Same as above scenario
|
2021-09-13 20:41:05 +00:00
|
|
|
subscription.roomUpdatedAt = subscription.lr
|
2022-02-21 19:41:49 +00:00
|
|
|
? // @ts-ignore Same as above scenario
|
|
|
|
Math.max(new Date(subscription.lr), new Date(lastRoomUpdate))
|
2021-09-13 20:41:05 +00:00
|
|
|
: lastRoomUpdate;
|
2021-02-26 18:31:35 +00:00
|
|
|
}
|
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;
|
2021-04-07 18:31:25 +00:00
|
|
|
subscription.teamId = room.teamId;
|
|
|
|
subscription.teamMain = room.teamMain;
|
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) {
|
2022-02-21 19:41:49 +00:00
|
|
|
subscription.name = subscription.fname as string;
|
2018-07-20 19:54:20 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-02-21 19:41:49 +00:00
|
|
|
export default async (serverSubscriptions: IServerSubscription, serverRooms: IServerRoom): Promise<ISubscription[]> => {
|
|
|
|
const subscriptions = serverSubscriptions.update;
|
|
|
|
const rooms = serverRooms.update;
|
2020-04-06 20:23:13 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
// Find missing rooms/subscriptions on local database
|
2022-02-21 19:41:49 +00:00
|
|
|
const findData = await findSubscriptionsRooms(subscriptions, rooms);
|
2020-09-11 14:31:38 +00:00
|
|
|
// Merge each subscription into a room
|
2022-02-21 19:41:49 +00:00
|
|
|
const mergedSubscriptions = findData.subscriptions.map(subscription => {
|
2022-02-23 18:55:38 +00:00
|
|
|
const index = rooms.findIndex(({ _id }) => _id === subscription.rid);
|
2020-09-11 14:31:38 +00:00
|
|
|
// Room not found
|
|
|
|
if (index < 0) {
|
2022-02-21 19:41:49 +00:00
|
|
|
return merge(subscription);
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
const [room] = rooms.splice(index, 1);
|
2022-02-21 19:41:49 +00:00
|
|
|
return merge(subscription, room);
|
2020-09-11 14:31:38 +00:00
|
|
|
});
|
|
|
|
// Decrypt all subscriptions missing decryption
|
2022-02-21 19:41:49 +00:00
|
|
|
const decryptedSubscriptions = (await Encryption.decryptSubscriptions(mergedSubscriptions)) as ISubscription[];
|
2020-04-06 20:23:13 +00:00
|
|
|
|
2022-02-21 19:41:49 +00:00
|
|
|
return decryptedSubscriptions;
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|