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';
|
2021-09-13 20:41:05 +00:00
|
|
|
import findSubscriptionsRooms from './findSubscriptionsRooms';
|
|
|
|
import normalizeMessage from './normalizeMessage';
|
2022-03-08 16:25:27 +00:00
|
|
|
import { ISubscription, IServerSubscription, IServerRoom, IRoom } from '../../../definitions';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { compareServerVersion } from './compareServerVersion';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
export const merge = (subscription: ISubscription | IServerSubscription, room?: IRoom | IServerRoom): ISubscription => {
|
2022-02-21 19:41:49 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version as string;
|
2022-03-08 16:25:27 +00:00
|
|
|
const mergedSubscription: ISubscription = EJSON.fromJSONValue(subscription);
|
2019-02-07 15:48:10 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (room) {
|
2022-03-08 16:25:27 +00:00
|
|
|
room = EJSON.fromJSONValue(room);
|
|
|
|
if (room?._updatedAt) {
|
|
|
|
mergedSubscription.lastMessage = normalizeMessage(room.lastMessage);
|
|
|
|
mergedSubscription.description = room.description;
|
|
|
|
mergedSubscription.topic = room.topic;
|
|
|
|
mergedSubscription.announcement = room.announcement;
|
|
|
|
mergedSubscription.reactWhenReadOnly = room.reactWhenReadOnly;
|
|
|
|
mergedSubscription.archived = room.archived || false;
|
|
|
|
mergedSubscription.joinCodeRequired = room.joinCodeRequired;
|
|
|
|
mergedSubscription.jitsiTimeout = room.jitsiTimeout;
|
|
|
|
mergedSubscription.usernames = room.usernames;
|
|
|
|
mergedSubscription.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;
|
2022-03-08 16:25:27 +00:00
|
|
|
// @ts-ignore
|
2022-03-08 21:17:05 +00:00
|
|
|
const lastMessageTs = mergedSubscription?.lastMessage?.ts ? new Date(mergedSubscription.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.
|
2022-03-08 16:25:27 +00:00
|
|
|
mergedSubscription.roomUpdatedAt = Math.max(updatedAt, lastMessageTs);
|
2021-02-26 18:31:35 +00:00
|
|
|
} else {
|
|
|
|
// https://github.com/RocketChat/Rocket.Chat/blob/develop/app/ui-sidenav/client/roomList.js#L180
|
2022-03-08 21:17:05 +00:00
|
|
|
const lastRoomUpdate = room?.lm || mergedSubscription.ts || mergedSubscription._updatedAt;
|
2022-02-21 19:41:49 +00:00
|
|
|
// @ts-ignore Same as above scenario
|
2022-03-08 21:17:05 +00:00
|
|
|
mergedSubscription.roomUpdatedAt = mergedSubscription.lr
|
2022-02-21 19:41:49 +00:00
|
|
|
? // @ts-ignore Same as above scenario
|
2022-03-08 21:17:05 +00:00
|
|
|
Math.max(new Date(mergedSubscription.lr), new Date(lastRoomUpdate))
|
2021-09-13 20:41:05 +00:00
|
|
|
: lastRoomUpdate;
|
2021-02-26 18:31:35 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
mergedSubscription.ro = room?.ro ?? false;
|
|
|
|
mergedSubscription.broadcast = room?.broadcast;
|
|
|
|
mergedSubscription.encrypted = room?.encrypted;
|
|
|
|
mergedSubscription.e2eKeyId = room?.e2eKeyId;
|
|
|
|
mergedSubscription.avatarETag = room?.avatarETag;
|
|
|
|
mergedSubscription.teamId = room?.teamId;
|
|
|
|
mergedSubscription.teamMain = room?.teamMain;
|
|
|
|
if (!mergedSubscription.roles || !mergedSubscription.roles.length) {
|
|
|
|
mergedSubscription.roles = [];
|
2019-04-26 21:13:07 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (!mergedSubscription.ignored?.length) {
|
|
|
|
mergedSubscription.ignored = [];
|
2020-11-30 20:00:31 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.muted?.length) {
|
|
|
|
mergedSubscription.muted = room.muted.filter(muted => !!muted);
|
2018-06-01 17:38:13 +00:00
|
|
|
} else {
|
2022-03-08 16:25:27 +00:00
|
|
|
mergedSubscription.muted = [];
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.v) {
|
|
|
|
mergedSubscription.visitor = room.v;
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.departmentId) {
|
|
|
|
mergedSubscription.departmentId = room.departmentId;
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.servedBy) {
|
|
|
|
mergedSubscription.servedBy = room.servedBy;
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.livechatData) {
|
|
|
|
mergedSubscription.livechatData = room.livechatData;
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
if (room?.tags) {
|
|
|
|
mergedSubscription.tags = room.tags;
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-03-08 16:25:27 +00:00
|
|
|
mergedSubscription.sysMes = room?.sysMes;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
if (!mergedSubscription.name) {
|
|
|
|
mergedSubscription.name = mergedSubscription.fname as string;
|
2018-07-20 19:54:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
if (!mergedSubscription.autoTranslate) {
|
|
|
|
mergedSubscription.autoTranslate = false;
|
2020-06-17 19:22:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
mergedSubscription.blocker = !!mergedSubscription.blocker;
|
|
|
|
mergedSubscription.blocked = !!mergedSubscription.blocked;
|
|
|
|
return mergedSubscription;
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2022-03-08 16:25:27 +00:00
|
|
|
export default async (
|
|
|
|
serverSubscriptions: {
|
|
|
|
update: IServerSubscription[];
|
|
|
|
remove: IServerSubscription[];
|
|
|
|
success: boolean;
|
|
|
|
},
|
|
|
|
serverRooms: {
|
|
|
|
update: IServerRoom[];
|
|
|
|
remove: IServerRoom[];
|
|
|
|
success: boolean;
|
|
|
|
}
|
|
|
|
): Promise<ISubscription[]> => {
|
2022-02-21 19:41:49 +00:00
|
|
|
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
|
|
|
};
|