diff --git a/assets/l10n/intl_en.arb b/assets/l10n/intl_en.arb index 84703a551..c8224dccb 100644 --- a/assets/l10n/intl_en.arb +++ b/assets/l10n/intl_en.arb @@ -3970,5 +3970,8 @@ "capacitySetTooLow": "Room capacity cannot be set below the current number of non-admins.", "roomCapacityExplanation": "Room capacity limits the number of non-admins allowed in a room.", "enterNumber": "Please enter a whole number value.", - "buildTranslation": "Build your translation from the choices above" + "buildTranslation": "Build your translation from the choices above", + "nonexistentSelection": "Selection no longer exists.", + "cantAddSpaceChild": "You do not have permission to add a child to this space.", + "roomAddedToSpace": "Room(s) have been added to the selected space." } \ No newline at end of file diff --git a/lib/pages/chat_list/chat_list.dart b/lib/pages/chat_list/chat_list.dart index 47fb51235..6397846dd 100644 --- a/lib/pages/chat_list/chat_list.dart +++ b/lib/pages/chat_list/chat_list.dart @@ -797,6 +797,10 @@ class ChatListController extends State // Pangea# Future addToSpace() async { + // #Pangea + final firstSelectedRoom = + Matrix.of(context).client.getRoomById(selectedRoomIds.toList().first); + // Pangea# final selectedSpace = await showConfirmationDialog( context: context, title: L10n.of(context)!.addToSpace, @@ -815,8 +819,9 @@ class ChatListController extends State && selectedRoomIds .map((id) => Matrix.of(context).client.getRoomById(id)) - .where((e) => !(e?.isPangeaClass ?? true)) - .every((e) => r.canIAddSpaceChild(e)), + // Only show non-recursion-causing spaces + // Performs a few other checks as well + .every((e) => r.canAddAsParentOf(e)), //Pangea# ) .map( @@ -826,6 +831,13 @@ class ChatListController extends State // label: space // .getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)), label: space.nameIncludingParents(context), + // If user is not admin of space, button is grayed out + textStyle: TextStyle( + color: (firstSelectedRoom == null || + (firstSelectedRoom.isSpace && !space.isRoomAdmin)) + ? Theme.of(context).colorScheme.outline + : Theme.of(context).colorScheme.surfaceTint, + ), // Pangea# ), ) @@ -837,12 +849,20 @@ class ChatListController extends State future: () async { final space = Matrix.of(context).client.getRoomById(selectedSpace)!; // #Pangea + if (firstSelectedRoom == null) { + throw L10n.of(context)!.nonexistentSelection; + } + // If user is not admin of the would-be parent space, does not allow + if (firstSelectedRoom.isSpace && !space.isRoomAdmin) { + throw L10n.of(context)!.cantAddSpaceChild; + } await pangeaAddToSpace( space, selectedRoomIds.toList(), context, pangeaController, ); + // if (space.canSendDefaultStates) { // for (final roomId in selectedRoomIds) { // await space.setSpaceChild(roomId); @@ -855,7 +875,10 @@ class ChatListController extends State if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( - content: Text(L10n.of(context)!.chatHasBeenAddedToThisSpace), + // #Pangea + // content: Text(L10n.of(context)!.chatHasBeenAddedToThisSpace), + content: Text(L10n.of(context)!.roomAddedToSpace), + // Pangea# ), ); } diff --git a/lib/pangea/extensions/pangea_room_extension/children_and_parents_extension.dart b/lib/pangea/extensions/pangea_room_extension/children_and_parents_extension.dart index 78ab91cc8..24dad6c41 100644 --- a/lib/pangea/extensions/pangea_room_extension/children_and_parents_extension.dart +++ b/lib/pangea/extensions/pangea_room_extension/children_and_parents_extension.dart @@ -127,4 +127,14 @@ extension ChildrenAndParentsRoomExtension on Room { } return childIds; } + + // Checks if has permissions to add child chat + // Or whether potential child space is ancestor of this + bool _canAddAsParentOf(Room? child) { + if (child == null || !child.isSpace) { + return _canIAddSpaceChild(child); + } + if (id == child.id) return false; + return !child._allSpaceChildRoomIds.contains(id); + } } diff --git a/lib/pangea/extensions/pangea_room_extension/pangea_room_extension.dart b/lib/pangea/extensions/pangea_room_extension/pangea_room_extension.dart index edcd80b04..25694901c 100644 --- a/lib/pangea/extensions/pangea_room_extension/pangea_room_extension.dart +++ b/lib/pangea/extensions/pangea_room_extension/pangea_room_extension.dart @@ -114,6 +114,8 @@ extension PangeaRoom on Room { List get allSpaceChildRoomIds => _allSpaceChildRoomIds; + bool canAddAsParentOf(Room? child) => _canAddAsParentOf(child); + // class_and_exchange_settings DateTime? get rulesUpdatedAt => _rulesUpdatedAt; diff --git a/lib/pangea/widgets/class/add_space_toggles.dart b/lib/pangea/widgets/class/add_space_toggles.dart index cfea4dd7b..c2ed32efe 100644 --- a/lib/pangea/widgets/class/add_space_toggles.dart +++ b/lib/pangea/widgets/class/add_space_toggles.dart @@ -144,9 +144,13 @@ class AddToSpaceState extends State { Widget getAddToSpaceToggleItem(int index) { final Room possibleParent = possibleParents[index]; - final bool canAdd = !(!possibleParent.isRoomAdmin && - widget.mode == AddToClassMode.exchange) && - possibleParent.canIAddSpaceChild(room); + final bool canAdd = (room?.isSpace ?? false) + // Room is space + ? possibleParent.isRoomAdmin && + room!.isRoomAdmin && + possibleParent.canAddAsParentOf(room) + // Room is null or chat + : possibleParent.canAddAsParentOf(room); return Opacity( opacity: canAdd ? 1 : 0.5,