Skip to content

Commit 03cf298

Browse files
committed
Fix choosing cross region server error
1 parent f1b57c6 commit 03cf298

File tree

5 files changed

+38
-43
lines changed

5 files changed

+38
-43
lines changed

Flat/Api/Requests/Flat/JoinRoomRequest.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ struct JoinRoomRequest: FlatRequest {
1313

1414
var path: String { "/v1/room/join" }
1515
var task: Task { .requestJSONEncodable(encodable: ["uuid": info.periodicUUID ?? info.roomUUID]) }
16-
var customBaseURL: String? {
17-
for server in Env().servers {
18-
if info.roomUUID.hasPrefix(server.classroomUUIDPrefix) {
19-
return server.baseURL
20-
}
21-
if info.roomUUID.hasPrefix(server.classroomInviteCode.description) {
22-
return server.baseURL
23-
}
24-
}
25-
return nil
26-
}
16+
var customBaseURL: String? { Env().customBaseUrlFor(roomUUID: info.roomUUID) }
2717
let responseType = RoomPlayInfo.self
2818
}

Flat/Api/Requests/Flat/MemberRequest.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ struct MemberRequest: FlatRequest, Encodable {
6464
let usersUUID: [String]?
6565

6666
var path: String { "/v1/room/info/users" }
67-
var customBaseURL: String? {
68-
for server in Env().servers {
69-
if roomUUID.hasPrefix(server.classroomUUIDPrefix) {
70-
return server.baseURL
71-
}
72-
}
73-
return nil
74-
}
67+
var customBaseURL: String? { Env().customBaseUrlFor(roomUUID: roomUUID) }
7568
let responseType = MemberResponse.self
7669

7770
var decoder: JSONDecoder {

Flat/EnvConfigs/Env.swift

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Env {
2626
"https://\(apiURL)"
2727
}
2828
}
29-
29+
3030
enum Region: String {
3131
case CN
3232
case US
@@ -42,11 +42,11 @@ struct Env {
4242
var disabledLoginTypes: [LoginType] {
4343
(value(for: "DISABLE_LOGIN_TYPES") as String).split(separator: ",").compactMap { LoginType(rawValue: String($0)) }
4444
}
45-
45+
4646
var preferPhoneAccount: Bool {
4747
(value(for: "PREFER_PHONE_ACCOUNT") as String) == "1"
4848
}
49-
49+
5050
var forceBindPhone: Bool {
5151
(value(for: "FORCE_BIND_PHONE") as String) == "1"
5252
}
@@ -78,7 +78,7 @@ struct Env {
7878
var name: String {
7979
Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""
8080
}
81-
81+
8282
var servers: [ServerGroupItem] {
8383
do {
8484
let str = value(for: "SERVER_GROUP") as String
@@ -138,7 +138,7 @@ extension Env {
138138
let urlString = githubAuthBaseUrl + queryString
139139
return URL(string: urlString)!
140140
}
141-
141+
142142
func githubLoginURLWith(authUUID uuid: String) -> URL {
143143
let redirectUri = baseURL + "/v1/login/github/callback"
144144
let queryString = "?client_id=\(githubClientId)&redirect_uri=\(redirectUri)&state=\(uuid)"
@@ -153,7 +153,7 @@ extension Env {
153153
let urlString = googleAuthBaseUrl + queryString
154154
return URL(string: urlString)!
155155
}
156-
156+
157157
func googleLoginURLWith(authUUID uuid: String) -> URL {
158158
let redirectUrl = baseURL + "/v1/login/google/callback"
159159
let scope = ["openid", "https://www.googleapis.com/auth/userinfo.profile"].joined(separator: " ").addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
@@ -168,3 +168,30 @@ extension Env {
168168
!slsSk.isEmpty && !slsAk.isEmpty && !slsEndpoint.isEmpty && !slsProject.isEmpty
169169
}
170170
}
171+
172+
extension Env {
173+
func customBaseUrlFor(roomUUID: String) -> String? {
174+
let isAllNumer = roomUUID.allSatisfy(\.isNumber)
175+
if isAllNumer, roomUUID.count == 10 {
176+
return nil // Old invite code. Drop it.
177+
}
178+
for server in servers {
179+
if isAllNumer {
180+
if roomUUID.hasPrefix(server.classroomInviteCode.description) {
181+
return server.baseURL
182+
}
183+
}
184+
if roomUUID.hasPrefix(server.classroomUUIDPrefix) {
185+
return server.baseURL
186+
}
187+
}
188+
return nil
189+
}
190+
191+
func isCrossRegion(roomUUID: String) -> Bool {
192+
if let url = customBaseUrlFor(roomUUID: roomUUID) {
193+
return url != baseURL
194+
}
195+
return false
196+
}
197+
}

Flat/Models/Flat/RoomBasicInfo.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,7 @@ private struct RoomInfoRequest: FlatRequest {
9595
var path: String { "/v1/room/info/ordinary" }
9696
var task: Task { .requestJSONEncodable(encodable: ["roomUUID": roomUUID]) }
9797
let responseType = RawRoomInfo.self
98-
var customBaseURL: String? {
99-
for server in Env().servers {
100-
if roomUUID.hasPrefix(server.classroomUUIDPrefix) {
101-
return server.baseURL
102-
}
103-
if roomUUID.hasPrefix(server.classroomInviteCode.description) {
104-
return server.baseURL
105-
}
106-
}
107-
return nil
108-
}
98+
var customBaseURL: String? { Env().customBaseUrlFor(roomUUID: roomUUID) }
10999
}
110100

111101
// Middle Struct

Flat/Modules/Home/RoomOperation/RoomOperation.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,8 @@ extension RoomBasicInfo {
4848
}
4949

5050
// Return no action for cross region classroom.
51-
for server in Env().servers {
52-
if room.roomUUID.hasPrefix(server.classroomUUIDPrefix) {
53-
return []
54-
}
55-
if room.roomUUID.hasPrefix(server.classroomInviteCode.description) {
56-
return []
57-
}
51+
if Env().isCrossRegion(roomUUID: room.roomUUID) {
52+
return []
5853
}
5954

6055
switch room.roomStatus {

0 commit comments

Comments
 (0)