From 61e875afa5b70ac291d2b1a22d9367ce9786da6a Mon Sep 17 00:00:00 2001 From: Denisia Enasescu Date: Mon, 22 Jan 2024 18:28:57 +0200 Subject: [PATCH 1/2] Implements a factory method to return the `MXSpaceCreationParameters` with .restricted join-rule together with default parameters. This object is then used as the parameter object for `createSpace` with parameter method. --- .../MXSpaceCreationParametersFactory.swift | 66 +++++++++++++++++++ .../SpaceCreationPostProcessService.swift | 14 ++-- 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/MXSpaceCreationParametersFactory.swift diff --git a/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/MXSpaceCreationParametersFactory.swift b/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/MXSpaceCreationParametersFactory.swift new file mode 100644 index 0000000000..0fc5828a0a --- /dev/null +++ b/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/MXSpaceCreationParametersFactory.swift @@ -0,0 +1,66 @@ +// +// Copyright 2024 New Vector Ltd +// +// 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 Foundation +import MatrixSDK + +struct MXSpaceCreationParametersFactory { + private let creationParams: SpaceCreationParameters + private let parentSpaceId: String? + private let stateEventBuilder: MXRoomInitialStateEventBuilder + private let homeServers: [String] + + init(creationParams: SpaceCreationParameters, parentSpaceId: String?, stateEventBuilder: MXRoomInitialStateEventBuilder, homeServers: [String]) { + self.creationParams = creationParams + self.parentSpaceId = parentSpaceId + self.stateEventBuilder = stateEventBuilder + self.homeServers = homeServers + } + + func make() -> MXSpaceCreationParameters { + var alias = creationParams.address + if let userDefinedAlias = creationParams.userDefinedAddress, !userDefinedAlias.isEmpty { + alias = userDefinedAlias + } + let userIdInvites = creationParams.inviteType == .userId ? creationParams.userIdInvites : [] + let isPublic = creationParams.isPublic + + let parameters = MXSpaceCreationParameters() + parameters.name = creationParams.name + parameters.topic = creationParams.topic + parameters.preset = isPublic ? kMXRoomPresetPublicChat : kMXRoomPresetPrivateChat + parameters.visibility = isPublic ? kMXRoomDirectoryVisibilityPublic : kMXRoomDirectoryVisibilityPrivate + parameters.inviteArray = userIdInvites + + if isPublic { + parameters.roomAlias = alias + let guestAccessStateEvent = stateEventBuilder.buildGuestAccessEvent(withAccess: .canJoin) + parameters.addOrUpdateInitialStateEvent(guestAccessStateEvent) + let historyVisibilityStateEvent = stateEventBuilder.buildHistoryVisibilityEvent(withVisibility: .worldReadable) + parameters.addOrUpdateInitialStateEvent(historyVisibilityStateEvent) + parameters.powerLevelContentOverride?.invite = 0 // default + } else { + parameters.powerLevelContentOverride?.invite = 50 // moderator + + if let parentSpaceId = parentSpaceId, creationParams.isShared { + let guestAccessStateEvent = stateEventBuilder.buildJoinRuleEvent(withJoinRule: .restricted, allowedParentsList: [parentSpaceId]) + parameters.addOrUpdateInitialStateEvent(guestAccessStateEvent) + } + } + + return parameters + } +} diff --git a/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/SpaceCreationPostProcessService.swift b/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/SpaceCreationPostProcessService.swift index 4fd4fe06db..c2d672fc38 100644 --- a/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/SpaceCreationPostProcessService.swift +++ b/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationPostProcess/Service/MatrixSDK/SpaceCreationPostProcessService.swift @@ -159,12 +159,8 @@ class SpaceCreationPostProcessService: SpaceCreationPostProcessServiceProtocol { private func createSpace(andUpdate task: SpaceCreationPostProcessTask) { updateCurrentTask(with: .started) - var alias = creationParams.address - if let userDefinedAlias = creationParams.userDefinedAddress, !userDefinedAlias.isEmpty { - alias = userDefinedAlias - } - let userIdInvites = creationParams.inviteType == .userId ? creationParams.userIdInvites : [] - session.spaceService.createSpace(withName: creationParams.name, topic: creationParams.topic, isPublic: creationParams.isPublic, aliasLocalPart: alias, inviteArray: userIdInvites) { [weak self] response in + let parameters = makeSpaceCreationParameters() + session.spaceService.createSpace(with: parameters) { [weak self] response in guard let self = self else { return } if response.isFailure { @@ -189,6 +185,12 @@ class SpaceCreationPostProcessService: SpaceCreationPostProcessServiceProtocol { } } + private func makeSpaceCreationParameters() -> MXSpaceCreationParameters { + let homeserverWellknown = session.credentials.homeServerName() ?? "" + let spaceCreationParametersFactory = MXSpaceCreationParametersFactory(creationParams: creationParams, parentSpaceId: parentSpaceId, stateEventBuilder: stateEventBuilder, homeServers: [homeserverWellknown]) + return spaceCreationParametersFactory.make() + } + private func uploadAvatar(andUpdate task: SpaceCreationPostProcessTask) { updateCurrentTask(with: .started) From ba5a2d2143f6a2ce160c2546734b4a060de9c076 Mon Sep 17 00:00:00 2001 From: Denisia Enasescu Date: Mon, 22 Jan 2024 18:57:48 +0200 Subject: [PATCH 2/2] Add changelog file Signed-off-by: Denisia Enasescu --- changelog.d/7741.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7741.bugfix diff --git a/changelog.d/7741.bugfix b/changelog.d/7741.bugfix new file mode 100644 index 0000000000..446ff2cdfa --- /dev/null +++ b/changelog.d/7741.bugfix @@ -0,0 +1 @@ +Create space: if the `space-members` access type is selected, it will be set upon creation. \ No newline at end of file