diff --git a/Riot.xcodeproj/project.pbxproj b/Riot.xcodeproj/project.pbxproj index 2462a7188..d49c6ebdd 100644 --- a/Riot.xcodeproj/project.pbxproj +++ b/Riot.xcodeproj/project.pbxproj @@ -331,6 +331,7 @@ B190F55922CE356800AEB493 /* EditHistoryHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B190F55822CE356800AEB493 /* EditHistoryHeaderView.swift */; }; B190F55B22CE35FD00AEB493 /* EditHistoryHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = B190F55A22CE35FD00AEB493 /* EditHistoryHeaderView.xib */; }; B190F55D22CE5A9700AEB493 /* EditHistorySection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B190F55C22CE5A9600AEB493 /* EditHistorySection.swift */; }; + B1920713255068B300F5062F /* RoomIDComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1920712255068B200F5062F /* RoomIDComponents.swift */; }; B1963B2B228F1C4900CBA17F /* BubbleReactionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1963B25228F1C4800CBA17F /* BubbleReactionsView.swift */; }; B1963B2C228F1C4900CBA17F /* BubbleReactionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = B1963B26228F1C4800CBA17F /* BubbleReactionViewCell.xib */; }; B1963B2D228F1C4900CBA17F /* BubbleReactionsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1963B27228F1C4800CBA17F /* BubbleReactionsViewModel.swift */; }; @@ -1415,6 +1416,7 @@ B190F55822CE356800AEB493 /* EditHistoryHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditHistoryHeaderView.swift; sourceTree = ""; }; B190F55A22CE35FD00AEB493 /* EditHistoryHeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = EditHistoryHeaderView.xib; sourceTree = ""; }; B190F55C22CE5A9600AEB493 /* EditHistorySection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditHistorySection.swift; sourceTree = ""; }; + B1920712255068B200F5062F /* RoomIDComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoomIDComponents.swift; sourceTree = ""; }; B1963B25228F1C4800CBA17F /* BubbleReactionsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BubbleReactionsView.swift; sourceTree = ""; }; B1963B26228F1C4800CBA17F /* BubbleReactionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BubbleReactionViewCell.xib; sourceTree = ""; }; B1963B27228F1C4800CBA17F /* BubbleReactionsViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BubbleReactionsViewModel.swift; sourceTree = ""; }; @@ -3264,6 +3266,14 @@ path = SelfVerifyWait; sourceTree = ""; }; + B19207112550683500F5062F /* Room */ = { + isa = PBXGroup; + children = ( + B1920712255068B200F5062F /* RoomIDComponents.swift */, + ); + path = Room; + sourceTree = ""; + }; B1963B24228F1C4800CBA17F /* BubbleReactions */ = { isa = PBXGroup; children = ( @@ -4505,6 +4515,7 @@ B1B5598A20EFC42100210D55 /* Settings */, 32242F0B21E8FBA900725742 /* Theme */, B1B5598020EFC3DF00210D55 /* Widgets */, + B19207112550683500F5062F /* Room */, ); path = Managers; sourceTree = ""; @@ -6658,6 +6669,7 @@ EC51E7A62514D2E100AAE7DB /* RoomInfoCoordinatorBridgePresenter.swift in Sources */, EC1CA87524C8259700DE9EBF /* KeychainStore.swift in Sources */, B1B5575220EE6C4D00210D55 /* RoomKeyRequestViewController.m in Sources */, + B1920713255068B300F5062F /* RoomIDComponents.swift in Sources */, 32A6001A22C661100042C1D9 /* EditHistoryCoordinator.swift in Sources */, F083BD1E1E7009ED00A9B29C /* LegacyAppDelegate.m in Sources */, B1B558E620EF768F00210D55 /* RoomIncomingAttachmentWithoutSenderInfoBubbleCell.m in Sources */, diff --git a/Riot/Managers/Room/RoomIDComponents.swift b/Riot/Managers/Room/RoomIDComponents.swift new file mode 100644 index 000000000..2433329a3 --- /dev/null +++ b/Riot/Managers/Room/RoomIDComponents.swift @@ -0,0 +1,64 @@ +/* + Copyright 2019 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 + +/// A structure that parses Matrix Room ID and constructs their constituent parts. +struct RoomIDComponents { + + // MARK: - Constants + + private enum Constants { + static let matrixRoomIdPrefix = "!" + static let homeServerSeparator: Character = ":" + } + + // MARK: - Properties + + let localRoomID: String + let homeServer: String + + // MARK: - Setup + + init?(matrixID: String) { + guard MXTools.isMatrixRoomIdentifier(matrixID), + let (localRoomID, homeServer) = RoomIDComponents.getLocalRoomIDAndHomeServer(from: matrixID) else { + return nil + } + + self.localRoomID = localRoomID + self.homeServer = homeServer + } + + // MARK: - Private + + /// Extract local room id and homeserver from Matrix ID + /// + /// - Parameter matrixID: A Matrix ID + /// - Returns: A tuple with local room ID and homeserver. + private static func getLocalRoomIDAndHomeServer(from matrixID: String) -> (String, String)? { + let matrixIDParts = matrixID.split(separator: Constants.homeServerSeparator) + + guard matrixIDParts.count == 2 else { + return nil + } + + let localRoomID = matrixIDParts[0].replacingOccurrences(of: Constants.matrixRoomIdPrefix, with: "") + let homeServer = String(matrixIDParts[1]) + + return (localRoomID, homeServer) + } +}