2021-07-02 15:51:55 +02: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 09:43:41 +02:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2021-07-02 15:51:55 +02:00
|
|
|
|
|
|
|
import { inviteUsersToRoom } from "../RoomInvite";
|
2021-10-08 13:20:43 +02:00
|
|
|
import Modal, { IHandle } from "../Modal";
|
2021-07-02 15:51:55 +02:00
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
import ErrorDialog from "../components/views/dialogs/ErrorDialog";
|
2021-07-09 09:43:41 +02:00
|
|
|
import SpaceStore from "../stores/SpaceStore";
|
2021-09-08 14:16:31 +02:00
|
|
|
import Spinner from "../components/views/elements/Spinner";
|
2021-07-02 15:51:55 +02:00
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
interface IProgress {
|
|
|
|
roomUpgraded: boolean;
|
|
|
|
roomSynced?: boolean;
|
|
|
|
inviteUsersProgress?: number;
|
|
|
|
inviteUsersTotal: number;
|
|
|
|
updateSpacesProgress?: number;
|
|
|
|
updateSpacesTotal: number;
|
|
|
|
}
|
|
|
|
|
2021-07-02 15:51:55 +02:00
|
|
|
export async function upgradeRoom(
|
|
|
|
room: Room,
|
|
|
|
targetVersion: string,
|
|
|
|
inviteUsers = false,
|
2021-07-09 09:43:41 +02:00
|
|
|
handleError = true,
|
|
|
|
updateSpaces = true,
|
2021-09-08 14:16:31 +02:00
|
|
|
awaitRoom = false,
|
2021-10-08 13:20:43 +02:00
|
|
|
progressCallback?: (progress: IProgress) => void,
|
2021-07-09 09:43:41 +02:00
|
|
|
): Promise<string> {
|
2021-07-02 15:51:55 +02:00
|
|
|
const cli = room.client;
|
2021-10-08 13:20:43 +02:00
|
|
|
let spinnerModal: IHandle<any>;
|
|
|
|
if (!progressCallback) {
|
|
|
|
spinnerModal = Modal.createDialog(Spinner, null, "mx_Dialog_spinner");
|
|
|
|
}
|
|
|
|
|
|
|
|
let toInvite: string[];
|
|
|
|
if (inviteUsers) {
|
|
|
|
toInvite = [
|
|
|
|
...room.getMembersWithMembership("join"),
|
|
|
|
...room.getMembersWithMembership("invite"),
|
|
|
|
].map(m => m.userId).filter(m => m !== cli.getUserId());
|
|
|
|
}
|
|
|
|
|
|
|
|
let parentsToRelink: Room[];
|
|
|
|
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 15:51:55 +02:00
|
|
|
|
2021-07-09 09:43:41 +02:00
|
|
|
let newRoomId: string;
|
2021-07-02 15:51:55 +02:00
|
|
|
try {
|
2021-07-09 09:43:41 +02:00
|
|
|
({ replacement_room: newRoomId } = await cli.upgradeRoom(room.roomId, targetVersion));
|
2021-07-02 15:51:55 +02:00
|
|
|
} catch (e) {
|
2021-07-09 09:43:41 +02:00
|
|
|
if (!handleError) throw e;
|
2021-07-02 15:51:55 +02:00
|
|
|
console.error(e);
|
|
|
|
|
2021-07-09 09:43:41 +02:00
|
|
|
Modal.createTrackedDialog("Room Upgrade Error", "", ErrorDialog, {
|
2021-07-02 15:51:55 +02: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 09:43:41 +02:00
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
progress.roomUpgraded = true;
|
|
|
|
progressCallback?.(progress);
|
|
|
|
|
2021-09-08 14:16:31 +02:00
|
|
|
if (awaitRoom || inviteUsers) {
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
// already have the room
|
|
|
|
if (room.client.getRoom(newRoomId)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2021-07-09 09:43:41 +02:00
|
|
|
|
2021-09-08 14:16:31 +02: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.
|
|
|
|
const checkForRoomFn = (newRoom: Room) => {
|
|
|
|
if (newRoom.roomId !== newRoomId) return;
|
|
|
|
resolve();
|
|
|
|
cli.off("Room", checkForRoomFn);
|
|
|
|
};
|
|
|
|
cli.on("Room", checkForRoomFn);
|
|
|
|
});
|
2021-07-09 09:43:41 +02:00
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
progress.roomSynced = true;
|
|
|
|
progressCallback?.(progress);
|
|
|
|
}
|
2021-07-09 09:43:41 +02:00
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
if (toInvite.length > 0) {
|
|
|
|
// Errors are handled internally to this function
|
|
|
|
await inviteUsersToRoom(newRoomId, toInvite, () => {
|
|
|
|
progress.inviteUsersProgress++;
|
|
|
|
progressCallback?.(progress);
|
|
|
|
});
|
2021-07-09 09:43:41 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
if (parentsToRelink.length > 0) {
|
2021-07-09 09:43:41 +02:00
|
|
|
try {
|
2021-10-08 13:20:43 +02:00
|
|
|
for (const parent of parentsToRelink) {
|
2021-07-09 09:43:41 +02:00
|
|
|
const currentEv = parent.currentState.getStateEvents(EventType.SpaceChild, room.roomId);
|
2021-10-08 13:20:43 +02:00
|
|
|
await cli.sendStateEvent(parent.roomId, EventType.SpaceChild, {
|
2021-07-09 09:43:41 +02:00
|
|
|
...(currentEv?.getContent() || {}), // copy existing attributes like suggested
|
|
|
|
via: [cli.getDomain()],
|
|
|
|
}, newRoomId);
|
2021-10-08 13:20:43 +02:00
|
|
|
await cli.sendStateEvent(parent.roomId, EventType.SpaceChild, {}, room.roomId);
|
|
|
|
|
|
|
|
progress.updateSpacesProgress++;
|
|
|
|
progressCallback?.(progress);
|
2021-07-09 09:43:41 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// These errors are not critical to the room upgrade itself
|
|
|
|
console.warn("Failed to update parent spaces during room upgrade", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:20:43 +02:00
|
|
|
spinnerModal?.close();
|
2021-07-09 09:43:41 +02:00
|
|
|
return newRoomId;
|
2021-07-02 15:51:55 +02:00
|
|
|
}
|