2021-07-02 13:51:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-07-09 07:43:41 +00:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2021-10-28 02:53:09 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
|
2021-07-02 13:51:55 +00:00
|
|
|
|
|
|
|
import { inviteUsersToRoom } from "../RoomInvite";
|
2021-10-08 11:20:43 +00:00
|
|
|
import Modal, { IHandle } from "../Modal";
|
2021-07-02 13:51:55 +00:00
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
import ErrorDialog from "../components/views/dialogs/ErrorDialog";
|
2021-11-11 13:07:41 +00:00
|
|
|
import SpaceStore from "../stores/spaces/SpaceStore";
|
2021-09-08 12:16:31 +00:00
|
|
|
import Spinner from "../components/views/elements/Spinner";
|
2021-07-02 13:51:55 +00:00
|
|
|
|
2021-10-08 11:20:43 +00:00
|
|
|
interface IProgress {
|
|
|
|
roomUpgraded: boolean;
|
|
|
|
roomSynced?: boolean;
|
|
|
|
inviteUsersProgress?: number;
|
|
|
|
inviteUsersTotal: number;
|
|
|
|
updateSpacesProgress?: number;
|
|
|
|
updateSpacesTotal: number;
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:24:31 +00:00
|
|
|
export async function awaitRoomDownSync(cli: MatrixClient, roomId: string): Promise<Room> {
|
|
|
|
const room = cli.getRoom(roomId);
|
|
|
|
if (room) return room; // already have the room
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
return new Promise<Room>((resolve) => {
|
2021-10-27 14:24:31 +00:00
|
|
|
// We have to wait for the js-sdk to give us the room back so
|
|
|
|
// we can more effectively abuse the MultiInviter behaviour
|
|
|
|
// which heavily relies on the Room object being available.
|
2023-01-12 13:25:14 +00:00
|
|
|
const checkForRoomFn = (room: Room): void => {
|
2021-10-27 14:24:31 +00:00
|
|
|
if (room.roomId !== roomId) return;
|
|
|
|
resolve(room);
|
2022-02-22 12:18:08 +00:00
|
|
|
cli.off(ClientEvent.Room, checkForRoomFn);
|
2021-10-27 14:24:31 +00:00
|
|
|
};
|
2022-02-22 12:18:08 +00:00
|
|
|
cli.on(ClientEvent.Room, checkForRoomFn);
|
2021-10-27 14:24:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:51:55 +00:00
|
|
|
export async function upgradeRoom(
|
|
|
|
room: Room,
|
|
|
|
targetVersion: string,
|
|
|
|
inviteUsers = false,
|
2021-07-09 07:43:41 +00:00
|
|
|
handleError = true,
|
|
|
|
updateSpaces = true,
|
2021-09-08 12:16:31 +00:00
|
|
|
awaitRoom = false,
|
2021-10-08 11:20:43 +00:00
|
|
|
progressCallback?: (progress: IProgress) => void,
|
2021-07-09 07:43:41 +00:00
|
|
|
): Promise<string> {
|
2021-07-02 13:51:55 +00:00
|
|
|
const cli = room.client;
|
2023-02-03 15:27:47 +00:00
|
|
|
let spinnerModal: IHandle<any> | undefined;
|
2021-10-08 11:20:43 +00:00
|
|
|
if (!progressCallback) {
|
2023-02-03 15:27:47 +00:00
|
|
|
spinnerModal = Modal.createDialog(Spinner, undefined, "mx_Dialog_spinner");
|
2021-10-08 11:20:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 09:21:27 +00:00
|
|
|
let toInvite: string[] = [];
|
2021-10-08 11:20:43 +00:00
|
|
|
if (inviteUsers) {
|
2022-12-12 11:24:14 +00:00
|
|
|
toInvite = [...room.getMembersWithMembership("join"), ...room.getMembersWithMembership("invite")]
|
|
|
|
.map((m) => m.userId)
|
|
|
|
.filter((m) => m !== cli.getUserId());
|
2021-10-08 11:20:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 09:21:27 +00:00
|
|
|
let parentsToRelink: Room[] = [];
|
2021-10-08 11:20:43 +00:00
|
|
|
if (updateSpaces) {
|
|
|
|
parentsToRelink = Array.from(SpaceStore.instance.getKnownParents(room.roomId))
|
2022-12-12 11:24:14 +00:00
|
|
|
.map((roomId) => cli.getRoom(roomId))
|
2023-02-03 15:27:47 +00:00
|
|
|
.filter((parent) =>
|
|
|
|
parent?.currentState.maySendStateEvent(EventType.SpaceChild, cli.getUserId()!),
|
|
|
|
) as Room[];
|
2021-10-08 11:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const progress: IProgress = {
|
|
|
|
roomUpgraded: false,
|
2022-12-12 11:24:14 +00:00
|
|
|
roomSynced: awaitRoom || inviteUsers ? false : undefined,
|
2021-10-08 11:20:43 +00:00
|
|
|
inviteUsersProgress: inviteUsers ? 0 : undefined,
|
|
|
|
inviteUsersTotal: toInvite.length,
|
|
|
|
updateSpacesProgress: updateSpaces ? 0 : undefined,
|
|
|
|
updateSpacesTotal: parentsToRelink.length,
|
|
|
|
};
|
|
|
|
progressCallback?.(progress);
|
2021-07-02 13:51:55 +00:00
|
|
|
|
2021-07-09 07:43:41 +00:00
|
|
|
let newRoomId: string;
|
2021-07-02 13:51:55 +00:00
|
|
|
try {
|
2021-07-09 07:43:41 +00:00
|
|
|
({ replacement_room: newRoomId } = await cli.upgradeRoom(room.roomId, targetVersion));
|
2021-07-02 13:51:55 +00:00
|
|
|
} catch (e) {
|
2021-07-09 07:43:41 +00:00
|
|
|
if (!handleError) throw e;
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error(e);
|
2021-07-02 13:51:55 +00:00
|
|
|
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2022-12-12 11:24:14 +00:00
|
|
|
title: _t("Error upgrading room"),
|
|
|
|
description: _t("Double check that your server supports the room version chosen and try again."),
|
2021-07-02 13:51:55 +00:00
|
|
|
});
|
|
|
|
throw e;
|
|
|
|
}
|
2021-07-09 07:43:41 +00:00
|
|
|
|
2021-10-08 11:20:43 +00:00
|
|
|
progress.roomUpgraded = true;
|
|
|
|
progressCallback?.(progress);
|
|
|
|
|
2021-09-08 12:16:31 +00:00
|
|
|
if (awaitRoom || inviteUsers) {
|
2021-10-27 14:24:31 +00:00
|
|
|
await awaitRoomDownSync(room.client, newRoomId);
|
2021-10-08 11:20:43 +00:00
|
|
|
progress.roomSynced = true;
|
|
|
|
progressCallback?.(progress);
|
|
|
|
}
|
2021-07-09 07:43:41 +00:00
|
|
|
|
2021-10-08 11:20:43 +00:00
|
|
|
if (toInvite.length > 0) {
|
|
|
|
// Errors are handled internally to this function
|
2022-01-28 10:02:37 +00:00
|
|
|
await inviteUsersToRoom(newRoomId, toInvite, false, () => {
|
2023-02-03 15:27:47 +00:00
|
|
|
progress.inviteUsersProgress!++;
|
2021-10-08 11:20:43 +00:00
|
|
|
progressCallback?.(progress);
|
|
|
|
});
|
2021-07-09 07:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 11:20:43 +00:00
|
|
|
if (parentsToRelink.length > 0) {
|
2021-07-09 07:43:41 +00:00
|
|
|
try {
|
2021-10-08 11:20:43 +00:00
|
|
|
for (const parent of parentsToRelink) {
|
2021-07-09 07:43:41 +00:00
|
|
|
const currentEv = parent.currentState.getStateEvents(EventType.SpaceChild, room.roomId);
|
2022-12-12 11:24:14 +00:00
|
|
|
await cli.sendStateEvent(
|
|
|
|
parent.roomId,
|
|
|
|
EventType.SpaceChild,
|
|
|
|
{
|
|
|
|
...(currentEv?.getContent() || {}), // copy existing attributes like suggested
|
|
|
|
via: [cli.getDomain()],
|
|
|
|
},
|
|
|
|
newRoomId,
|
|
|
|
);
|
2021-10-08 11:20:43 +00:00
|
|
|
await cli.sendStateEvent(parent.roomId, EventType.SpaceChild, {}, room.roomId);
|
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
progress.updateSpacesProgress!++;
|
2021-10-08 11:20:43 +00:00
|
|
|
progressCallback?.(progress);
|
2021-07-09 07:43:41 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// These errors are not critical to the room upgrade itself
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("Failed to update parent spaces during room upgrade", e);
|
2021-07-09 07:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 11:20:43 +00:00
|
|
|
spinnerModal?.close();
|
2021-07-09 07:43:41 +00:00
|
|
|
return newRoomId;
|
2021-07-02 13:51:55 +00:00
|
|
|
}
|