2021-03-01 18:10:17 +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.
|
|
|
|
*/
|
|
|
|
|
2021-03-26 11:43:01 +00:00
|
|
|
import React from "react";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2022-05-03 21:04:37 +00:00
|
|
|
import { RoomType, EventType } from "matrix-js-sdk/src/@types/event";
|
2021-11-30 18:08:46 +00:00
|
|
|
import { JoinRule } from "matrix-js-sdk/src/@types/partials";
|
2023-01-12 13:25:14 +00:00
|
|
|
import { ICreateRoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2021-03-01 18:10:17 +00:00
|
|
|
|
2021-07-02 15:07:17 +00:00
|
|
|
import { calculateRoomVia } from "./permalinks/Permalinks";
|
2021-03-02 10:34:28 +00:00
|
|
|
import Modal from "../Modal";
|
2021-03-02 13:28:05 +00:00
|
|
|
import CreateRoomDialog from "../components/views/dialogs/CreateRoomDialog";
|
2023-02-28 10:31:48 +00:00
|
|
|
import createRoom from "../createRoom";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { _t } from "../languageHandler";
|
2021-03-26 11:43:01 +00:00
|
|
|
import SpacePublicShare from "../components/views/spaces/SpacePublicShare";
|
|
|
|
import InfoDialog from "../components/views/dialogs/InfoDialog";
|
|
|
|
import { showRoomInviteDialog } from "../RoomInvite";
|
2021-07-23 07:46:20 +00:00
|
|
|
import CreateSubspaceDialog from "../components/views/dialogs/CreateSubspaceDialog";
|
|
|
|
import AddExistingSubspaceDialog from "../components/views/dialogs/AddExistingSubspaceDialog";
|
2021-07-28 18:08:59 +00:00
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import { Action } from "../dispatcher/actions";
|
2021-07-21 08:57:10 +00:00
|
|
|
import Spinner from "../components/views/elements/Spinner";
|
2022-03-02 17:37:18 +00:00
|
|
|
import { shouldShowComponent } from "../customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../settings/UIFeature";
|
2022-03-24 22:13:36 +00:00
|
|
|
import { OpenSpacePreferencesPayload, SpacePreferenceTab } from "../dispatcher/payloads/OpenSpacePreferencesPayload";
|
|
|
|
import { OpenSpaceSettingsPayload } from "../dispatcher/payloads/OpenSpaceSettingsPayload";
|
2022-03-25 00:01:09 +00:00
|
|
|
import { OpenAddExistingToSpaceDialogPayload } from "../dispatcher/payloads/OpenAddExistingToSpaceDialogPayload";
|
2022-10-19 12:07:03 +00:00
|
|
|
import { SdkContextClass } from "../contexts/SDKContext";
|
2021-03-01 19:05:50 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const shouldShowSpaceSettings = (space: Room): boolean => {
|
2023-02-03 15:27:47 +00:00
|
|
|
const userId = space.client.getUserId()!;
|
2021-03-01 18:10:17 +00:00
|
|
|
return (
|
|
|
|
space.getMyMembership() === "join" &&
|
|
|
|
(space.currentState.maySendStateEvent(EventType.RoomAvatar, userId) ||
|
|
|
|
space.currentState.maySendStateEvent(EventType.RoomName, userId) ||
|
|
|
|
space.currentState.maySendStateEvent(EventType.RoomTopic, userId) ||
|
|
|
|
space.currentState.maySendStateEvent(EventType.RoomJoinRules, userId))
|
|
|
|
);
|
|
|
|
};
|
2021-03-01 19:05:50 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const makeSpaceParentEvent = (room: Room, canonical = false): ICreateRoomStateEvent => ({
|
2021-03-01 19:05:50 +00:00
|
|
|
type: EventType.SpaceParent,
|
|
|
|
content: {
|
|
|
|
via: calculateRoomVia(room),
|
|
|
|
canonical: canonical,
|
|
|
|
},
|
|
|
|
state_key: room.roomId,
|
|
|
|
});
|
2021-03-02 10:34:28 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function showSpaceSettings(space: Room): void {
|
2022-05-03 21:04:37 +00:00
|
|
|
defaultDispatcher.dispatch<OpenSpaceSettingsPayload>({
|
2022-03-24 22:13:36 +00:00
|
|
|
action: Action.OpenSpaceSettings,
|
2021-03-02 10:34:28 +00:00
|
|
|
space,
|
2022-03-24 22:13:36 +00:00
|
|
|
});
|
|
|
|
}
|
2021-03-02 13:28:05 +00:00
|
|
|
|
2021-07-28 18:08:59 +00:00
|
|
|
export const showAddExistingRooms = (space: Room): void => {
|
2022-05-03 21:04:37 +00:00
|
|
|
defaultDispatcher.dispatch<OpenAddExistingToSpaceDialogPayload>({
|
2022-03-25 00:01:09 +00:00
|
|
|
action: Action.OpenAddToExistingSpaceDialog,
|
|
|
|
space,
|
2022-04-01 01:38:00 +00:00
|
|
|
});
|
2021-03-02 13:32:24 +00:00
|
|
|
};
|
|
|
|
|
2022-04-01 14:36:10 +00:00
|
|
|
export const showCreateNewRoom = async (space: Room, type?: RoomType): Promise<boolean> => {
|
2023-02-28 10:31:48 +00:00
|
|
|
const modal = Modal.createDialog(CreateRoomDialog, {
|
2022-06-14 16:51:51 +00:00
|
|
|
type,
|
|
|
|
defaultPublic: space.getJoinRule() === JoinRule.Public,
|
|
|
|
parentSpace: space,
|
|
|
|
});
|
2021-03-02 13:28:05 +00:00
|
|
|
const [shouldCreate, opts] = await modal.finished;
|
|
|
|
if (shouldCreate) {
|
2023-05-30 09:36:34 +00:00
|
|
|
await createRoom(space.client, opts);
|
2021-03-02 13:28:05 +00:00
|
|
|
}
|
2023-03-16 10:35:17 +00:00
|
|
|
return !!shouldCreate;
|
2021-03-02 13:28:05 +00:00
|
|
|
};
|
2021-03-26 11:43:01 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const shouldShowSpaceInvite = (space: Room): boolean =>
|
2023-02-03 15:27:47 +00:00
|
|
|
((space?.getMyMembership() === "join" && space.canInvite(space.client.getUserId()!)) ||
|
2022-03-02 17:37:18 +00:00
|
|
|
space.getJoinRule() === JoinRule.Public) &&
|
|
|
|
shouldShowComponent(UIComponent.InviteUsers);
|
2021-11-30 18:08:46 +00:00
|
|
|
|
2021-07-28 18:08:59 +00:00
|
|
|
export const showSpaceInvite = (space: Room, initialText = ""): void => {
|
2021-03-26 11:43:01 +00:00
|
|
|
if (space.getJoinRule() === "public") {
|
2022-06-14 16:51:51 +00:00
|
|
|
const modal = Modal.createDialog(InfoDialog, {
|
2021-03-26 11:43:01 +00:00
|
|
|
title: _t("Invite to %(spaceName)s", { spaceName: space.name }),
|
|
|
|
description: (
|
|
|
|
<React.Fragment>
|
|
|
|
<span>{_t("Share your public space")}</span>
|
|
|
|
<SpacePublicShare space={space} onFinished={() => modal.close()} />
|
|
|
|
</React.Fragment>
|
|
|
|
),
|
|
|
|
fixedWidth: false,
|
|
|
|
button: false,
|
|
|
|
className: "mx_SpacePanel_sharePublicSpace",
|
|
|
|
hasCloseButton: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
showRoomInviteDialog(space.roomId, initialText);
|
|
|
|
}
|
|
|
|
};
|
2021-07-21 08:57:10 +00:00
|
|
|
|
2021-07-28 18:08:59 +00:00
|
|
|
export const showAddExistingSubspace = (space: Room): void => {
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(
|
|
|
|
AddExistingSubspaceDialog,
|
|
|
|
{
|
|
|
|
space,
|
|
|
|
onCreateSubspaceClick: () => showCreateNewSubspace(space),
|
|
|
|
onFinished: (added: boolean) => {
|
2022-10-19 12:07:03 +00:00
|
|
|
if (added && SdkContextClass.instance.roomViewStore.getRoomId() === space.roomId) {
|
2022-06-14 16:51:51 +00:00
|
|
|
defaultDispatcher.fire(Action.UpdateSpaceHierarchy);
|
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
2021-07-23 07:46:20 +00:00
|
|
|
},
|
2022-06-14 16:51:51 +00:00
|
|
|
"mx_AddExistingToSpaceDialog_wrapper",
|
|
|
|
);
|
2021-07-23 07:46:20 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 18:08:59 +00:00
|
|
|
export const showCreateNewSubspace = (space: Room): void => {
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(
|
|
|
|
CreateSubspaceDialog,
|
|
|
|
{
|
|
|
|
space,
|
|
|
|
onAddExistingSpaceClick: () => showAddExistingSubspace(space),
|
|
|
|
onFinished: (added: boolean) => {
|
2022-10-19 12:07:03 +00:00
|
|
|
if (added && SdkContextClass.instance.roomViewStore.getRoomId() === space.roomId) {
|
2022-06-14 16:51:51 +00:00
|
|
|
defaultDispatcher.fire(Action.UpdateSpaceHierarchy);
|
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
2021-07-23 07:46:20 +00:00
|
|
|
},
|
2022-06-14 16:51:51 +00:00
|
|
|
"mx_CreateSubspaceDialog_wrapper",
|
|
|
|
);
|
2021-07-23 07:46:20 +00:00
|
|
|
};
|
2021-07-29 14:22:52 +00:00
|
|
|
|
2021-09-17 14:34:49 +00:00
|
|
|
export const bulkSpaceBehaviour = async (
|
|
|
|
space: Room,
|
|
|
|
children: Room[],
|
|
|
|
fn: (room: Room) => Promise<unknown>,
|
|
|
|
): Promise<void> => {
|
2023-02-03 15:27:47 +00:00
|
|
|
const modal = Modal.createDialog(Spinner, undefined, "mx_Dialog_spinner");
|
2021-09-17 14:34:49 +00:00
|
|
|
try {
|
|
|
|
for (const room of children) {
|
|
|
|
await fn(room);
|
|
|
|
}
|
|
|
|
await fn(space);
|
|
|
|
} finally {
|
|
|
|
modal.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const showSpacePreferences = (space: Room, initialTabId?: SpacePreferenceTab): void => {
|
2022-05-03 21:04:37 +00:00
|
|
|
defaultDispatcher.dispatch<OpenSpacePreferencesPayload>({
|
2022-03-24 22:13:36 +00:00
|
|
|
action: Action.OpenSpacePreferences,
|
2021-12-17 09:26:32 +00:00
|
|
|
space,
|
2022-03-24 22:13:36 +00:00
|
|
|
initialTabId,
|
|
|
|
});
|
2022-04-01 01:38:43 +00:00
|
|
|
};
|