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) 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