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-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-07-09 07:43:41 +00:00
|
|
|
import SpaceStore from "../stores/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-15 14:30:53 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-10-27 14:24:31 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2021-10-15 14:30:53 +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
|
|
|
|
|
|
|
|
return new Promise<Room>(resolve => {
|
|
|
|
// 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.
|
|
|
|
const checkForRoomFn = (room: Room) => {
|
|
|
|
if (room.roomId !== roomId) return;
|
|
|
|
resolve(room);
|
|
|
|
cli.off("Room", checkForRoomFn);
|
|
|
|
};
|
|
|
|
cli.on("Room", checkForRoomFn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
2021-10-08 11:20:43 +00:00
|
|
|
let spinnerModal: IHandle<any>;
|
|
|
|
if (!progressCallback) {
|
|
|
|
spinnerModal = Modal.createDialog(Spinner, null, "mx_Dialog_spinner");
|
|
|
|
}
|
|
|
|
|
2021-10-26 09:21:27 +00:00
|
|
|
let toInvite: string[] = [];
|
2021-10-08 11:20:43 +00:00
|
|
|
if (inviteUsers) {
|
|
|
|
toInvite = [
|
|
|
|
...room.getMembersWithMembership("join"),
|
|
|
|
...room.getMembersWithMembership("invite"),
|
|
|
|
].map(m => m.userId).filter(m => m !== cli.getUserId());
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
.map(roomId => cli.getRoom(roomId))
|
|
|
|
.filter(parent => parent?.currentState.maySendStateEvent(EventType.SpaceChild, cli.getUserId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const progress: IProgress = {
|
|
|
|
roomUpgraded: false,
|
|
|
|
roomSynced: (awaitRoom || inviteUsers) ? false : undefined,
|
|
|
|
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
|
|
|
|
2021-07-09 07:43:41 +00:00
|
|
|
Modal.createTrackedDialog("Room Upgrade Error", "", ErrorDialog, {
|
2021-07-02 13:51:55 +00:00
|
|
|
title: _t('Error upgrading room'),
|
|
|
|
description: _t('Double check that your server supports the room version chosen and try again.'),
|
|
|
|
});
|
|
|
|
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
|
|
|
|
await inviteUsersToRoom(newRoomId, toInvite, () => {
|
|
|
|
progress.inviteUsersProgress++;
|
|
|
|
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);
|
2021-10-08 11:20:43 +00:00
|
|
|
await cli.sendStateEvent(parent.roomId, EventType.SpaceChild, {
|
2021-07-09 07:43:41 +00:00
|
|
|
...(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);
|
|
|
|
|
|
|
|
progress.updateSpacesProgress++;
|
|
|
|
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
|
|
|
}
|