This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 819
Deduplicate join rule management between rooms and spaces #6724
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
979bc71
Deduplicate join rule management between rooms and spaces
t3chguy 600375c
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy fa60b24
fix react error in console
t3chguy 2eb64de
Tweak edge case behaviour
t3chguy 6308359
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy 2a8e8b9
add comment
t3chguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
/* | ||
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 React from "react"; | ||
import { IJoinRuleEventContent, JoinRule, RestrictedAllowType } from "matrix-js-sdk/src/@types/partials"; | ||
import { Room } from "matrix-js-sdk/src/models/room"; | ||
import { EventType } from "matrix-js-sdk/src/@types/event"; | ||
|
||
import StyledRadioGroup, { IDefinition } from "../elements/StyledRadioGroup"; | ||
import { _t } from "../../../languageHandler"; | ||
import AccessibleButton from "../elements/AccessibleButton"; | ||
import RoomAvatar from "../avatars/RoomAvatar"; | ||
import SpaceStore from "../../../stores/SpaceStore"; | ||
import { MatrixClientPeg } from "../../../MatrixClientPeg"; | ||
import Modal from "../../../Modal"; | ||
import ManageRestrictedJoinRuleDialog from "../dialogs/ManageRestrictedJoinRuleDialog"; | ||
import RoomUpgradeWarningDialog from "../dialogs/RoomUpgradeWarningDialog"; | ||
import { upgradeRoom } from "../../../utils/RoomUpgrade"; | ||
import { arrayHasDiff } from "../../../utils/arrays"; | ||
import { useLocalEcho } from "../../../hooks/useLocalEcho"; | ||
import dis from "../../../dispatcher/dispatcher"; | ||
import { ROOM_SECURITY_TAB } from "../dialogs/RoomSettingsDialog"; | ||
|
||
interface IProps { | ||
room: Room; | ||
promptUpgrade?: boolean; | ||
closeSettingsFn(): void; | ||
onError(error: Error): void; | ||
beforeChange?(joinRule: JoinRule): Promise<boolean>; // if returns false then aborts the change | ||
} | ||
|
||
const JoinRuleSettings = ({ room, promptUpgrade, onError, beforeChange, closeSettingsFn }: IProps) => { | ||
const cli = room.client; | ||
|
||
const restrictedRoomCapabilities = SpaceStore.instance.restrictedJoinRuleSupport; | ||
const roomSupportsRestricted = Array.isArray(restrictedRoomCapabilities?.support) | ||
&& restrictedRoomCapabilities.support.includes(room.getVersion()); | ||
const preferredRestrictionVersion = !roomSupportsRestricted && promptUpgrade | ||
? restrictedRoomCapabilities?.preferred | ||
: undefined; | ||
|
||
const disabled = !room.currentState.mayClientSendStateEvent(EventType.RoomJoinRules, cli); | ||
|
||
const [content, setContent] = useLocalEcho<IJoinRuleEventContent>( | ||
() => room.currentState.getStateEvents(EventType.RoomJoinRules, "")?.getContent(), | ||
content => cli.sendStateEvent(room.roomId, EventType.RoomJoinRules, content, ""), | ||
onError, | ||
); | ||
|
||
const { join_rule: joinRule } = content; | ||
const restrictedAllowRoomIds = joinRule === JoinRule.Restricted | ||
? content.allow.filter(o => o.type === RestrictedAllowType.RoomMembership).map(o => o.room_id) | ||
: undefined; | ||
|
||
const editRestrictedRoomIds = async (): Promise<string[] | undefined> => { | ||
let selected = restrictedAllowRoomIds; | ||
if (!selected?.length && SpaceStore.instance.activeSpace) { | ||
selected = [SpaceStore.instance.activeSpace.roomId]; | ||
} | ||
|
||
const matrixClient = MatrixClientPeg.get(); | ||
const { finished } = Modal.createTrackedDialog('Edit restricted', '', ManageRestrictedJoinRuleDialog, { | ||
matrixClient, | ||
room, | ||
selected, | ||
}, "mx_ManageRestrictedJoinRuleDialog_wrapper"); | ||
|
||
const [roomIds] = await finished; | ||
return roomIds; | ||
}; | ||
|
||
const definitions: IDefinition<JoinRule>[] = [{ | ||
value: JoinRule.Invite, | ||
label: _t("Private (invite only)"), | ||
description: _t("Only invited people can join."), | ||
checked: joinRule === JoinRule.Invite || (joinRule === JoinRule.Restricted && !restrictedAllowRoomIds?.length), | ||
}, { | ||
value: JoinRule.Public, | ||
label: _t("Public"), | ||
description: _t("Anyone can find and join."), | ||
}]; | ||
|
||
if (roomSupportsRestricted || preferredRestrictionVersion || joinRule === JoinRule.Restricted) { | ||
let upgradeRequiredPill; | ||
if (preferredRestrictionVersion) { | ||
upgradeRequiredPill = <span className="mx_SecurityRoomSettingsTab_upgradeRequired"> | ||
{ _t("Upgrade required") } | ||
</span>; | ||
} | ||
|
||
let description; | ||
if (joinRule === JoinRule.Restricted && restrictedAllowRoomIds?.length) { | ||
// only show the first 4 spaces we know about, so that the UI doesn't grow out of proportion there are lots. | ||
const shownSpaces = restrictedAllowRoomIds | ||
.map(roomId => cli.getRoom(roomId)) | ||
.filter(room => room?.isSpaceRoom()) | ||
.slice(0, 4); | ||
t3chguy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let moreText; | ||
if (shownSpaces.length < restrictedAllowRoomIds.length) { | ||
if (shownSpaces.length > 0) { | ||
moreText = _t("& %(count)s more", { | ||
count: restrictedAllowRoomIds.length - shownSpaces.length, | ||
}); | ||
} else { | ||
moreText = _t("Currently, %(count)s spaces have access", { | ||
count: restrictedAllowRoomIds.length, | ||
}); | ||
} | ||
} | ||
|
||
const onRestrictedRoomIdsChange = (newAllowRoomIds: string[]) => { | ||
if (!arrayHasDiff(restrictedAllowRoomIds || [], newAllowRoomIds)) return; | ||
|
||
if (!newAllowRoomIds.length) { | ||
setContent({ | ||
join_rule: JoinRule.Invite, | ||
}); | ||
return; | ||
} | ||
|
||
setContent({ | ||
join_rule: JoinRule.Restricted, | ||
allow: newAllowRoomIds.map(roomId => ({ | ||
"type": RestrictedAllowType.RoomMembership, | ||
"room_id": roomId, | ||
})), | ||
}); | ||
}; | ||
|
||
const onEditRestrictedClick = async () => { | ||
const restrictedAllowRoomIds = await editRestrictedRoomIds(); | ||
if (!Array.isArray(restrictedAllowRoomIds)) return; | ||
if (restrictedAllowRoomIds.length > 0) { | ||
onRestrictedRoomIdsChange(restrictedAllowRoomIds); | ||
} else { | ||
onChange(JoinRule.Invite); | ||
} | ||
}; | ||
|
||
description = <div> | ||
<span> | ||
{ _t("Anyone in a space can find and join. <a>Edit which spaces can access here.</a>", {}, { | ||
a: sub => <AccessibleButton | ||
disabled={disabled} | ||
onClick={onEditRestrictedClick} | ||
kind="link" | ||
> | ||
{ sub } | ||
</AccessibleButton>, | ||
}) } | ||
</span> | ||
|
||
<div className="mx_SecurityRoomSettingsTab_spacesWithAccess"> | ||
<h4>{ _t("Spaces with access") }</h4> | ||
{ shownSpaces.map(room => { | ||
return <span key={room.roomId}> | ||
<RoomAvatar room={room} height={32} width={32} /> | ||
{ room.name } | ||
</span>; | ||
}) } | ||
{ moreText && <span>{ moreText }</span> } | ||
</div> | ||
</div>; | ||
} else if (SpaceStore.instance.activeSpace) { | ||
description = _t("Anyone in <spaceName/> can find and join. You can select other spaces too.", {}, { | ||
spaceName: () => <b>{ SpaceStore.instance.activeSpace.name }</b>, | ||
}); | ||
} else { | ||
description = _t("Anyone in a space can find and join. You can select multiple spaces."); | ||
} | ||
|
||
definitions.splice(1, 0, { | ||
value: JoinRule.Restricted, | ||
label: <> | ||
{ _t("Space members") } | ||
{ upgradeRequiredPill } | ||
</>, | ||
description, | ||
// if there are 0 allowed spaces then render it as invite only instead | ||
checked: joinRule === JoinRule.Restricted && !!restrictedAllowRoomIds?.length, | ||
}); | ||
} | ||
|
||
const onChange = async (joinRule: JoinRule) => { | ||
const beforeJoinRule = content.join_rule; | ||
|
||
let restrictedAllowRoomIds: string[]; | ||
if (joinRule === JoinRule.Restricted) { | ||
if (beforeJoinRule === JoinRule.Restricted || roomSupportsRestricted) { | ||
// Have the user pick which spaces to allow joins from | ||
restrictedAllowRoomIds = await editRestrictedRoomIds(); | ||
if (!Array.isArray(restrictedAllowRoomIds)) return; | ||
} else if (preferredRestrictionVersion) { | ||
// Block this action on a room upgrade otherwise it'd make their room unjoinable | ||
const targetVersion = preferredRestrictionVersion; | ||
Modal.createTrackedDialog('Restricted join rule upgrade', '', RoomUpgradeWarningDialog, { | ||
roomId: room.roomId, | ||
targetVersion, | ||
description: _t("This upgrade will allow members of selected spaces " + | ||
"access to this room without an invite."), | ||
onFinished: async (resp) => { | ||
if (!resp?.continue) return; | ||
const roomId = await upgradeRoom(room, targetVersion, resp.invite, true, true, true); | ||
closeSettingsFn(); | ||
// switch to the new room in the background | ||
dis.dispatch({ | ||
action: "view_room", | ||
room_id: roomId, | ||
}); | ||
// open new settings on this tab | ||
dis.dispatch({ | ||
action: "open_room_settings", | ||
initial_tab_id: ROOM_SECURITY_TAB, | ||
}); | ||
}, | ||
}); | ||
return; | ||
} | ||
|
||
// when setting to 0 allowed rooms/spaces set to invite only instead as per the note | ||
if (!restrictedAllowRoomIds.length) { | ||
joinRule = JoinRule.Invite; | ||
} | ||
} | ||
|
||
if (beforeJoinRule === joinRule && !restrictedAllowRoomIds) return; | ||
if (beforeChange && !await beforeChange(joinRule)) return; | ||
|
||
const newContent: IJoinRuleEventContent = { | ||
join_rule: joinRule, | ||
}; | ||
|
||
// pre-set the accepted spaces with the currently viewed one as per the microcopy | ||
if (joinRule === JoinRule.Restricted) { | ||
newContent.allow = restrictedAllowRoomIds.map(roomId => ({ | ||
"type": RestrictedAllowType.RoomMembership, | ||
"room_id": roomId, | ||
})); | ||
} | ||
|
||
setContent(newContent); | ||
}; | ||
|
||
return ( | ||
<StyledRadioGroup | ||
name="joinRule" | ||
value={joinRule} | ||
onChange={onChange} | ||
definitions={definitions} | ||
disabled={disabled} | ||
/> | ||
); | ||
}; | ||
|
||
export default JoinRuleSettings; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.