Files
bundesmessenger-ios/Riot/Modules/ContextMenu/Services/UnownedRoomContextActionService.swift
T
Frank Rotermund 67df1a3e95 chore: FOSS Merge 1.27.11 (MESSENGER-7276)
Merge commit 'af0b6d4be985d9f26e5111d3fa01389c7321949f' into feature/7276_FOSS_Merge_1_27_11

# Conflicts:
#	Config/AppVersion.xcconfig
#	Gemfile.lock
#	IDETemplateMacros.plist
#	Podfile
#	Podfile.lock
#	README.md
#	Riot/Modules/Authentication/AuthenticationCoordinator.swift
#	Riot/Modules/Room/CellData/RoomBubbleCellData.m
#	Riot/target.yml
#	RiotNSE/NotificationService.swift
#	RiotSwiftUI/Modules/Authentication/ServerSelection/AuthenticationServerSelectionModels.swift
#	RiotSwiftUI/Modules/Authentication/ServerSelection/AuthenticationServerSelectionViewModel.swift
#	RiotSwiftUI/Modules/Authentication/ServerSelection/Coordinator/AuthenticationServerSelectionCoordinator.swift
#	RiotSwiftUI/Modules/Authentication/ServerSelection/View/AuthenticationServerSelectionScreen.swift
#	RiotSwiftUI/Modules/Room/CompletionSuggestion/Service/CompletionSuggestionService.swift
#	fastlane/Fastfile
2025-05-16 14:06:20 +02:00

84 lines
3.6 KiB
Swift

//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import Foundation
/// `RoomContextActionService` implements all the possible actions for a room not owned by the user (e.g. `MXPublicRoom`, `MXSpaceChildInfo`)
class UnownedRoomContextActionService: NSObject, RoomContextActionServiceProtocol {
// MARK: - RoomContextActionServiceProtocol
internal let roomId: String
internal let session: MXSession
internal weak var delegate: RoomContextActionServiceDelegate?
// MARK: - Properties
private let canonicalAlias: String?
// MARK: - Setup
init(roomId: String, canonicalAlias: String?, session: MXSession, delegate: RoomContextActionServiceDelegate?) {
self.roomId = roomId
self.canonicalAlias = canonicalAlias
self.session = session
self.delegate = delegate
}
// MARK: - Public
func joinRoom() {
self.delegate?.roomContextActionService(self, updateActivityIndicator: true)
if let canonicalAlias = canonicalAlias {
self.session.matrixRestClient.resolveRoomAlias(canonicalAlias) { [weak self] (response) in
guard let self = self else { return }
switch response {
case .success(let resolution):
self.joinRoom(withId: resolution.roomId, via: resolution.servers)
case .failure(let error):
MXLog.warning("[UnownedRoomContextActionService] joinRoom: failed to resolve room alias due to error \(error).")
self.joinRoom(withId: self.roomId, via: nil)
}
}
} else {
MXLog.warning("[UnownedRoomContextActionService] joinRoom: no canonical alias provided.")
joinRoom(withId: self.roomId, via: nil)
}
}
// MARK: - Private
private func joinRoom(withId roomId: String, via viaServers: [String]?) {
self.session.joinRoom(roomId, viaServers: viaServers, withSignUrl: nil) { [weak self] response in
guard let self = self else { return }
switch response {
case .success:
self.delegate?.roomContextActionService(self, updateActivityIndicator: false)
self.delegate?.roomContextActionServiceDidJoinRoom(self)
case .failure(let error):
self.delegate?.roomContextActionService(self, updateActivityIndicator: false)
self.delegate?.roomContextActionService(self, presentAlert: self.roomJoinFailedAlert(with: error))
}
}
}
private func roomJoinFailedAlert(with error: Error) -> UIAlertController {
var message = (error as NSError).userInfo[NSLocalizedDescriptionKey] as? String
if message == "No known servers" {
// minging kludge until https://matrix.org/jira/browse/SYN-678 is fixed
// 'Error when trying to join an empty room should be more explicit'
message = VectorL10n.roomErrorJoinFailedEmptyRoom
} else if message == "Server is banned from room" { // bwi: #5716 show custom error message when the federation was disabled after the user has been invited to a federated room
message = BWIL10n.roomErrorJoinFailedFederationDisabledMessage
}
let alertController = UIAlertController(title: VectorL10n.roomErrorJoinFailedTitle, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: VectorL10n.ok, style: .default, handler: nil))
return alertController
}
}