Filter changes

This commit is contained in:
Justin Mazzocchi 2021-01-17 12:24:50 -08:00
parent d127eb51f2
commit fa162fde77
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 97 additions and 49 deletions

View file

@ -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";

View file

@ -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<String>(
hashes: [.djb232, .djb2a32, .sdbm32, .fnv132, .fnv1a32],
data: Data([
0, 0, 0, 16, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 2, 8, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0,
128, 0, 0, 32, 0, 128, 0, 0, 0, 4, 16, 4, 32, 0, 0, 16, 16, 4, 32, 0, 0, 128, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4,
0, 0, 0, 0, 4, 0, 0, 3, 2, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 132, 0, 0, 64, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 64, 8, 0, 0, 0, 0, 0, 0, 0, 0,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 0, 0, 0, 64, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0
]))
var filter: BloomFilter<String> {

View file

@ -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<String>(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
}
}
}

View file

@ -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)
}

View file

@ -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: "")
}
}
}