diff --git a/Riot/Modules/CreateRoom/EnterNewRoomDetails/EnterNewRoomDetailsViewModel.swift b/Riot/Modules/CreateRoom/EnterNewRoomDetails/EnterNewRoomDetailsViewModel.swift index 539ad4ab2..997e2cd4a 100644 --- a/Riot/Modules/CreateRoom/EnterNewRoomDetails/EnterNewRoomDetailsViewModel.swift +++ b/Riot/Modules/CreateRoom/EnterNewRoomDetails/EnterNewRoomDetailsViewModel.swift @@ -123,7 +123,7 @@ final class EnterNewRoomDetailsViewModel: EnterNewRoomDetailsViewModelType, Obse return alias } else if BWIBuildSettings.shared.bwiAutoCreateAliasOnRoomCreation { if let roomName = roomCreationParameters.name { - return self.createAlias(from: roomName) + return MXTools.createAlias(from: roomName) // here? } } @@ -248,14 +248,4 @@ final class EnterNewRoomDetailsViewModel: EnterNewRoomDetailsViewModelType, Obse self.currentOperation?.cancel() } - // MARK bwi Alias creation - - private func createAlias(from roomName: String) -> String { - var alias = roomName.trimmingCharacters(in: .whitespacesAndNewlines).filter { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".contains($0) } - - let timeInterval = String(Int(NSDate().timeIntervalSince1970)) - alias.append(timeInterval) - - return alias - } } diff --git a/Riot/Modules/Room/Settings/RoomSettingsViewController.m b/Riot/Modules/Room/Settings/RoomSettingsViewController.m index cf85ea023..5ecfcca70 100644 --- a/Riot/Modules/Room/Settings/RoomSettingsViewController.m +++ b/Riot/Modules/Room/Settings/RoomSettingsViewController.m @@ -1709,6 +1709,9 @@ BOOL reloadToggleCell = false; if (serverACLRule) { + // bwi: #5383 set alias + [mxRoom setAliasIfNeeded]; + NSDictionary *content = [mxRoom createServerACLContentWithServerACL:serverACLRule]; pendingOperation = [mxRoom sendStateEventOfType:kMXEventTypeStringRoomServerACL content:content stateKey:nil success:^(NSString *eventId) { diff --git a/bwi/Extentions/MXTools+Alias.swift b/bwi/Extentions/MXTools+Alias.swift new file mode 100644 index 000000000..70029e8a4 --- /dev/null +++ b/bwi/Extentions/MXTools+Alias.swift @@ -0,0 +1,30 @@ +// +/* + * Copyright (c) 2024 BWI GmbH + * + * 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 + +extension MXTools { + + static func createAlias(from roomName: String) -> String { + var alias = roomName.trimmingCharacters(in: .whitespacesAndNewlines).filter { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".contains($0) } + + let timeInterval = String(Int(NSDate().timeIntervalSince1970)) + alias.append(timeInterval) + + return alias + } +} diff --git a/bwi/Federation/MXRoom+Federation.swift b/bwi/Federation/MXRoom+Federation.swift index e27a44383..8781048b4 100644 --- a/bwi/Federation/MXRoom+Federation.swift +++ b/bwi/Federation/MXRoom+Federation.swift @@ -145,4 +145,38 @@ import MatrixSDK return content } + // #5383 Check if no alias exists, create alias from name and timestamp and add as canonical alias + func setAliasIfNeeded() { + var needsAlias: Bool = true + if let summary = summary { + if let aliases = summary.aliases { + for alias in aliases { + if !alias.isEmpty { + needsAlias = false + return + } + } + } + } + + if needsAlias { + if let displayName = displayName { + // Create new alias + var newAlias = MXTools.createAlias(from: displayName) + // Generate local alias + newAlias = MXTools.fullLocalAlias(from: newAlias, with: mxSession) + + // Add local alias + addAlias(newAlias) { [self] response in + if response.isSuccess { + // Set new alias as canonical alias + setCanonicalAlias(newAlias) { response in + return + } + } + return + } + } + } + } } diff --git a/bwi/Federation/UI/RoomFederationDecisionView.swift b/bwi/Federation/UI/RoomFederationDecisionView.swift index c25b91d1f..be543018f 100644 --- a/bwi/Federation/UI/RoomFederationDecisionView.swift +++ b/bwi/Federation/UI/RoomFederationDecisionView.swift @@ -163,6 +163,8 @@ struct RoomFederationDecisionView: View { func setServerACL(isFederated: Bool) { let content: [String:Any] = room.createServerACL(isFederated: isFederated) isUpdatingServerACLs = true + // #5383 set alias + room.setAliasIfNeeded() pendingRequest = room.sendStateEvent(MXEventType.roomServerACL, content: content, stateKey: "") { response in isUpdatingServerACLs = false if response.isSuccess {