From fa162fde77769c7f2d5868b2cbaa5375dba2a277 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 17 Jan 2021 12:24:50 -0800 Subject: [PATCH] Filter changes --- Localizations/Localizable.strings | 1 + .../Services/InstanceURLService.swift | 70 ++++++++++--------- .../InstanceURLServiceTests.swift | 56 +++++++++++---- .../ViewModels/AddIdentityViewModel.swift | 11 ++- Views/AddIdentityView.swift | 8 ++- 5 files changed, 97 insertions(+), 49 deletions(-) diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index cdc3498..79e6ee3 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -28,6 +28,7 @@ "add-identity.instance-url" = "Instance URL"; "add-identity.log-in" = "Log in"; "add-identity.browse" = "Browse"; +"add-identity.instance-not-supported" = "In order to provide a safe experience to all users and comply with the App Store Review Guidelines, this instance is not supported."; "add-identity.join" = "Join"; "add-identity.request-invite" = "Request an invite"; "add-identity.unable-to-connect-to-instance" = "Unable to connect to instance"; diff --git a/ServiceLayer/Sources/ServiceLayer/Services/InstanceURLService.swift b/ServiceLayer/Sources/ServiceLayer/Services/InstanceURLService.swift index ec6d284..632de38 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/InstanceURLService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/InstanceURLService.swift @@ -7,6 +7,10 @@ import HTTP import Mastodon import MastodonAPI +public enum InstanceURLError: Error { + case instanceNotSupported +} + public struct InstanceURLService { private let httpClient: HTTPClient private var appPreferences: AppPreferences @@ -34,7 +38,7 @@ public extension InstanceURLService { } if isFiltered(url: url) { - return .failure(URLError(.badURL)) + return .failure(InstanceURLError.instanceNotSupported) } return .success(url) @@ -85,38 +89,38 @@ private extension InstanceURLService { static let defaultFilter = BloomFilter( hashes: [.djb232, .djb2a32, .sdbm32, .fnv132, .fnv1a32], data: Data([ - 0, 0, 0, 16, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0 ])) var filter: BloomFilter { diff --git a/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift b/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift index 558a446..5957305 100644 --- a/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift +++ b/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift @@ -12,16 +12,26 @@ final class InstanceURLServiceTests: XCTestCase { func testFiltering() throws { let sut = InstanceURLService(environment: .mock()) - if case .failure = sut.url(text: "unfiltered.instance") { + guard case .success = sut.url(text: "unfiltered.instance") else { XCTFail("Expected success") + + return } - if case .success = sut.url(text: "filtered.instance") { - XCTFail("Expected failure") + guard case let .failure(error) = sut.url(text: "filtered.instance"), + case InstanceURLError.instanceNotSupported = error + else { + XCTFail("Expected instance not supported error") + + return } - if case .success = sut.url(text: "subdomain.filtered.instance") { - XCTFail("Expected failure") + guard case .failure = sut.url(text: "subdomain.filtered.instance"), + case InstanceURLError.instanceNotSupported = error + else { + XCTFail("Expected instance not supported error") + + return } } @@ -29,12 +39,18 @@ final class InstanceURLServiceTests: XCTestCase { let environment = AppEnvironment.mock() var sut = InstanceURLService(environment: environment) - if case .success = sut.url(text: "filtered.instance") { - XCTFail("Expected failure") + guard case let .failure(error0) = sut.url(text: "filtered.instance"), + case InstanceURLError.instanceNotSupported = error0 + else { + XCTFail("Expected instance not supported error") + + return } - if case .failure = sut.url(text: "instance.filtered") { + guard case .success = sut.url(text: "instance.filtered") else { XCTFail("Expected success") + + return } var updatedFilter = BloomFilter(hashes: [.djb232, .sdbm32], byteCount: 16) @@ -50,22 +66,34 @@ final class InstanceURLServiceTests: XCTestCase { _ = try wait(for: updateRecorder.next(), timeout: 1) - if case .failure = sut.url(text: "filtered.instance") { + guard case .success = sut.url(text: "filtered.instance") else { XCTFail("Expected success") + + return } - if case .success = sut.url(text: "instance.filtered") { - XCTFail("Expected failure") + guard case let .failure(error1) = sut.url(text: "instance.filtered"), + case InstanceURLError.instanceNotSupported = error1 + else { + XCTFail("Expected instance not supported error") + + return } sut = InstanceURLService(environment: environment) - if case .failure = sut.url(text: "filtered.instance") { + guard case .success = sut.url(text: "filtered.instance") else { XCTFail("Expected success") + + return } - if case .success = sut.url(text: "instance.filtered") { - XCTFail("Expected failure") + guard case let .failure(error2) = sut.url(text: "instance.filtered"), + case InstanceURLError.instanceNotSupported = error2 + else { + XCTFail("Expected instance not supported error") + + return } } } diff --git a/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift b/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift index 67fcab9..48aa697 100644 --- a/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift +++ b/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift @@ -7,6 +7,7 @@ import ServiceLayer public enum AddIdentityError: Error { case unableToConnectToInstance + case instanceNotSupported } public final class AddIdentityViewModel: ObservableObject { @@ -101,7 +102,15 @@ private extension AddIdentityViewModel { return } - let displayedError = error is URLError ? AddIdentityError.unableToConnectToInstance : error + let displayedError: Error + + if case InstanceURLError.instanceNotSupported = error { + displayedError = AddIdentityError.instanceNotSupported + } else if error is URLError { + displayedError = AddIdentityError.unableToConnectToInstance + } else { + displayedError = error + } self.alertItem = AlertItem(error: displayedError) } diff --git a/Views/AddIdentityView.swift b/Views/AddIdentityView.swift index 727be67..c79bdae 100644 --- a/Views/AddIdentityView.swift +++ b/Views/AddIdentityView.swift @@ -71,7 +71,13 @@ struct AddIdentityView: View { extension AddIdentityError: LocalizedError { public var errorDescription: String? { - NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "") + switch self { + case .unableToConnectToInstance: + return NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "") + case .instanceNotSupported: + return NSLocalizedString("add-identity.instance-not-supported", comment: "") + } + } }