diff --git a/Package.swift b/Package.swift index bcec957c2be3321cb1b9f1bcc1dd176814764b54..08f6fef2fb77a9c65ba50627b93abcb367a5a6f6 100644 --- a/Package.swift +++ b/Package.swift @@ -722,6 +722,13 @@ let package = Package( dependencies: ["DependencyInjection"] ), + // MARK: - AppTests + + .testTarget( + name: "AppTests", + dependencies: ["App"] + ), + // MARK: - ProfileFeatureTests .testTarget( diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift index fdf6090ebec6b8062d3befee5c1f6a3176116ed1..b20f3d6c86ce711a5049291b83e19682e934565b 100644 --- a/Sources/App/AppDelegate.swift +++ b/Sources/App/AppDelegate.swift @@ -143,10 +143,8 @@ public class AppDelegate: UIResponder, UIApplicationDelegate { open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool { - if let components = URLComponents(url: url, resolvingAgainstBaseURL: false), - let invitation = components.queryItems?.first(where: { $0.name == "invitation" }), - let username = invitation.value { - self.invitation = username + if let invitationUsername = getUsernameFromInvitationDeepLink(url) { + self.invitation = invitationUsername return true } else { return dropboxService.handleOpenUrl(url) @@ -154,6 +152,18 @@ public class AppDelegate: UIResponder, UIApplicationDelegate { } } +func getUsernameFromInvitationDeepLink(_ url: URL) -> String? { + if let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + components.scheme == "xxnetwork", + components.host == "messenger", + let queryItem = components.queryItems?.first(where: { $0.name == "invitation" }), + let username = queryItem.value { + return username + } + + return nil +} + // MARK: Notifications extension AppDelegate: UNUserNotificationCenterDelegate { diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift index e9c6e68d19f6ce11cd798bcec3c7f584c7480617..cab4842ca9f44ca0f8985f941387bd5692bc155a 100644 --- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift +++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift @@ -50,19 +50,15 @@ final class SearchLeftViewModel { networkMonitor.statusPublisher .first { $0 == .available } - .map { [unowned self] _ in - session.waitForNodes(timeout: 5).first() - .sink { - if case .failure(let error) = $0 { - self.hudSubject.send(.error(.init(with: error))) - } - networkCancellable.removeAll() - } receiveValue: { _ in - self.didStartSearching() - networkCancellable.removeAll() - }.store(in: &networkCancellable) - }.sink(receiveValue: { _ in }) - .store(in: &networkCancellable) + .eraseToAnyPublisher() + .flatMap { _ in self.session.waitForNodes(timeout: 5) } + .sink { + if case .failure(let error) = $0 { + self.hudSubject.send(.error(.init(with: error))) + } + } receiveValue: { + self.didStartSearching() + }.store(in: &networkCancellable) } } diff --git a/Tests/AppTests/General/InvitationTests.swift b/Tests/AppTests/General/InvitationTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..bbd80d6e2e1507fb35fd4751fa087fa4be687788 --- /dev/null +++ b/Tests/AppTests/General/InvitationTests.swift @@ -0,0 +1,21 @@ +import XCTest + +@testable import App + +final class AppDelegateTests: XCTestCase { + func test_invitationDeeplink() { + let host = "messenger" + let query = "invitation" + let scheme = "xxnetwork" + let username = "john_doe" + + let url = URL(string: "\(scheme)://\(host)?\(query)=\(username)")! + XCTAssertEqual(getUsernameFromInvitationDeepLink(url), username) + + let malformedURL = URL(string: "\(scheme)\(host)\(query)\(username)")! + XCTAssertNil(getUsernameFromInvitationDeepLink(malformedURL)) + + let urlAnotherUsername = URL(string: "\(scheme)://\(host)?\(query)=asdfg")! + XCTAssertNotEqual(getUsernameFromInvitationDeepLink(urlAnotherUsername), username) + } +}