mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-28 23:32:41 +00:00
Add HomeserverConfiguration that exposes final HS configuration.
This commit is contained in:
parent
74dd724838
commit
73eac93fdd
3 changed files with 186 additions and 0 deletions
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// Copyright 2020 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
|
||||
|
||||
@objcMembers
|
||||
final class HomeserverConfiguration: NSObject {
|
||||
|
||||
// Note: Use an object per configuration subject when there is multiple properties related
|
||||
let jitsi: HomeserverJitsiConfiguration
|
||||
let isE2EEByDefaultEnabled: Bool
|
||||
|
||||
init(jitsi: HomeserverJitsiConfiguration,
|
||||
isE2EEByDefaultEnabled: Bool) {
|
||||
self.jitsi = jitsi
|
||||
self.isE2EEByDefaultEnabled = isE2EEByDefaultEnabled
|
||||
|
||||
super.init()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
//
|
||||
// Copyright 2020 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
|
||||
|
||||
@objcMembers
|
||||
final class HomeserverConfigurationBuilder: NSObject {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let vectorWellKnownParser = VectorWellKnownParser()
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func build(from wellKnown: MXWellKnown?) -> HomeserverConfiguration {
|
||||
|
||||
let isE2EEByDefaultEnabled: Bool
|
||||
let jitsiPreferredDomain: String
|
||||
|
||||
var vectorWellKnownEncryptionConfiguration: VectorWellKnownEncryptionConfiguration?
|
||||
var vectorWellKnownJitsiConfiguration: VectorWellKnownJitsiConfiguration?
|
||||
|
||||
if let wellKnown = wellKnown, let vectorWellKnown = self.vectorWellKnownParser.parse(jsonDictionary: wellKnown.jsonDictionary()) {
|
||||
vectorWellKnownEncryptionConfiguration = self.getEncryptionConfiguration(from: vectorWellKnown)
|
||||
vectorWellKnownJitsiConfiguration = self.getJitsiConfiguration(from: vectorWellKnown)
|
||||
}
|
||||
|
||||
// Encryption configuration
|
||||
if let vectorWellKnownEncryptionConfig = vectorWellKnownEncryptionConfiguration {
|
||||
isE2EEByDefaultEnabled = vectorWellKnownEncryptionConfig.isE2EEByDefaultEnabled
|
||||
} else {
|
||||
// Enable E2EE by default when there is no value
|
||||
isE2EEByDefaultEnabled = true
|
||||
}
|
||||
|
||||
// Jitsi configuration
|
||||
let jitsiServerURL: URL
|
||||
let hardcodedJitsiServerURL: URL = BuildSettings.jitsiServerUrl
|
||||
|
||||
if let vectorWellKnownJitsiConfig = vectorWellKnownJitsiConfiguration {
|
||||
jitsiPreferredDomain = vectorWellKnownJitsiConfig.preferredDomain
|
||||
jitsiServerURL = self.jitsiServerURL(from: jitsiPreferredDomain) ?? hardcodedJitsiServerURL
|
||||
} else {
|
||||
guard let hardcodedJitsiDomain = hardcodedJitsiServerURL.host else {
|
||||
fatalError("[HomeserverConfigurationBuilder] Fail to get Jitsi domain from hardcoded Jitsi URL")
|
||||
}
|
||||
jitsiPreferredDomain = hardcodedJitsiDomain
|
||||
jitsiServerURL = hardcodedJitsiServerURL
|
||||
}
|
||||
|
||||
// Create HomeserverConfiguration
|
||||
|
||||
let jitsiConfiguration = HomeserverJitsiConfiguration(serverDomain: jitsiPreferredDomain,
|
||||
serverURL: jitsiServerURL)
|
||||
|
||||
return HomeserverConfiguration(jitsi: jitsiConfiguration, isE2EEByDefaultEnabled: isE2EEByDefaultEnabled)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func getJitsiConfiguration(from vectorWellKnown: VectorWellKnown) -> VectorWellKnownJitsiConfiguration? {
|
||||
|
||||
let jitsiConfiguration: VectorWellKnownJitsiConfiguration?
|
||||
|
||||
if let lastJitsiConfiguration = vectorWellKnown.jitsi {
|
||||
jitsiConfiguration = lastJitsiConfiguration
|
||||
} else if let deprecatedJitsiConfiguration = vectorWellKnown.deprecatedJitsi {
|
||||
NSLog("[HomeserverConfigurationBuilder] getJitsiConfiguration - Use deprecated configuration")
|
||||
jitsiConfiguration = deprecatedJitsiConfiguration
|
||||
} else {
|
||||
NSLog("[HomeserverConfigurationBuilder] getJitsiConfiguration - No configuration found")
|
||||
jitsiConfiguration = nil
|
||||
}
|
||||
|
||||
return jitsiConfiguration
|
||||
}
|
||||
|
||||
private func getEncryptionConfiguration(from vectorWellKnown: VectorWellKnown) -> VectorWellKnownEncryptionConfiguration? {
|
||||
|
||||
let encryptionConfiguration: VectorWellKnownEncryptionConfiguration?
|
||||
|
||||
if let lastEncryptionConfiguration = vectorWellKnown.encryption {
|
||||
encryptionConfiguration = lastEncryptionConfiguration
|
||||
} else if let deprecatedEncryptionConfiguration = vectorWellKnown.deprecatedEncryption {
|
||||
NSLog("[HomeserverConfigurationBuilder] getEncryptionConfiguration - Use deprecated configuration")
|
||||
encryptionConfiguration = deprecatedEncryptionConfiguration
|
||||
} else {
|
||||
NSLog("[HomeserverConfigurationBuilder] getEncryptionConfiguration - No configuration found")
|
||||
encryptionConfiguration = nil
|
||||
}
|
||||
|
||||
return encryptionConfiguration
|
||||
}
|
||||
|
||||
private func jitsiServerURL(from jitsiServerDomain: String) -> URL? {
|
||||
let jitsiStringURL: String
|
||||
if jitsiServerDomain.starts(with: "http") {
|
||||
jitsiStringURL = jitsiServerDomain
|
||||
} else {
|
||||
jitsiStringURL = "https://\(jitsiServerDomain)"
|
||||
}
|
||||
|
||||
guard let jitsiServerURL = URL(string: jitsiStringURL) else {
|
||||
NSLog("[HomeserverConfigurationBuilder] Jitsi server URL is not valid")
|
||||
return nil
|
||||
}
|
||||
|
||||
return jitsiServerURL
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
//
|
||||
// Copyright 2020 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
|
||||
|
||||
@objcMembers
|
||||
final class HomeserverJitsiConfiguration: NSObject {
|
||||
let serverDomain: String
|
||||
let serverURL: URL
|
||||
|
||||
init(serverDomain: String, serverURL: URL) {
|
||||
self.serverDomain = serverDomain
|
||||
self.serverURL = serverURL
|
||||
|
||||
super.init()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue