Skip to content

Commit dd47834

Browse files
committed
Merge branch 'relayselector-cannot-resolve-locations-under-certain-ios-580'
2 parents e859f00 + f3afd74 commit dd47834

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

ios/MullvadREST/Relay/RelaySelector.swift

+33-9
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ public enum RelaySelector {
140140
_ constraints: RelayConstraints,
141141
relays: [RelayWithLocation<T>]
142142
) -> [RelayWithLocation<T>] {
143-
return relays.filter { relayWithLocation -> Bool in
143+
// Filter on active status, filter, and location.
144+
let filteredRelays = relays.filter { relayWithLocation -> Bool in
145+
guard relayWithLocation.relay.active else {
146+
return false
147+
}
148+
144149
switch constraints.filter {
145150
case .any:
146151
break
@@ -150,17 +155,37 @@ public enum RelaySelector {
150155
}
151156
}
152157

153-
switch constraints.locations {
158+
return switch constraints.locations {
154159
case .any:
155-
return true
160+
true
156161
case let .only(relayConstraint):
157162
// At least one location must match the relay under test.
158-
return relayConstraint.locations.contains { location in
163+
relayConstraint.locations.contains { location in
159164
relayWithLocation.matches(location: location)
160165
}
161166
}
162-
}.filter { relayWithLocation -> Bool in
163-
relayWithLocation.relay.active
167+
}
168+
169+
// Filter on country inclusion.
170+
let includeInCountryFilteredRelays = filteredRelays.filter { relayWithLocation in
171+
return switch constraints.locations {
172+
case .any:
173+
true
174+
case let .only(relayConstraint):
175+
relayConstraint.locations.contains { location in
176+
if case .country = location {
177+
return relayWithLocation.relay.includeInCountry
178+
}
179+
return false
180+
}
181+
}
182+
}
183+
184+
// If no relays should be included in the matched country, instead accept all.
185+
if includeInCountryFilteredRelays.isEmpty {
186+
return filteredRelays
187+
} else {
188+
return includeInCountryFilteredRelays
164189
}
165190
}
166191

@@ -301,10 +326,9 @@ struct RelayWithLocation<T: AnyRelay> {
301326
let serverLocation: Location
302327

303328
func matches(location: RelayLocation) -> Bool {
304-
switch location {
329+
return switch location {
305330
case let .country(countryCode):
306-
serverLocation.countryCode == countryCode &&
307-
relay.includeInCountry
331+
serverLocation.countryCode == countryCode
308332

309333
case let .city(countryCode, cityCode):
310334
serverLocation.countryCode == countryCode &&

0 commit comments

Comments
 (0)