|
1 | 1 | import Foundation
|
2 | 2 |
|
3 | 3 | extension PushChat {
|
4 |
| - public static func getGroup(chatId: String, env: ENV) async throws -> PushChat.PushGroup? { |
5 |
| - let url = try PushEndpoint.getGroup(chatId: chatId, env: env).url |
6 |
| - var request = URLRequest(url: url) |
7 |
| - request.httpMethod = "GET" |
8 |
| - request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
9 |
| - |
10 |
| - let (data, res) = try await URLSession.shared.data(for: request) |
11 |
| - guard let httpResponse = res as? HTTPURLResponse else { |
12 |
| - throw URLError(.badServerResponse) |
| 4 | + public static func getGroup(chatId: String, env: ENV) async throws -> PushChat.PushGroup? { |
| 5 | + let url = try PushEndpoint.getGroup(chatId: chatId, env: env).url |
| 6 | + var request = URLRequest(url: url) |
| 7 | + request.httpMethod = "GET" |
| 8 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 9 | + |
| 10 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 11 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 12 | + throw URLError(.badServerResponse) |
| 13 | + } |
| 14 | + |
| 15 | + if httpResponse.statusCode == 400 { |
| 16 | + return nil |
| 17 | + } |
| 18 | + |
| 19 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 20 | + throw URLError(.badServerResponse) |
| 21 | + } |
| 22 | + |
| 23 | + let groupData = try JSONDecoder().decode(PushGroup.self, from: data) |
| 24 | + return groupData |
13 | 25 | }
|
14 | 26 |
|
15 |
| - if httpResponse.statusCode == 400 { |
16 |
| - return nil |
17 |
| - } |
| 27 | + public static func getGroupInfoDTO(chatId: String, env: ENV) async throws -> PushChat |
| 28 | + .PushGroupInfoDTO { |
| 29 | + let url = try PushEndpoint.getGroup(chatId: chatId, apiVersion: "v2", env: env).url |
| 30 | + var request = URLRequest(url: url) |
| 31 | + request.httpMethod = "GET" |
| 32 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 33 | + |
| 34 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 35 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 36 | + throw URLError(.badServerResponse) |
| 37 | + } |
| 38 | + |
| 39 | + if httpResponse.statusCode == 400 { |
| 40 | + throw URLError(.badServerResponse) |
| 41 | + } |
| 42 | + |
| 43 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 44 | + throw URLError(.badServerResponse) |
| 45 | + } |
18 | 46 |
|
19 |
| - guard (200...299).contains(httpResponse.statusCode) else { |
20 |
| - throw URLError(.badServerResponse) |
| 47 | + let groupData = try JSONDecoder().decode(PushGroupInfoDTO.self, from: data) |
| 48 | + return groupData |
21 | 49 | }
|
22 | 50 |
|
23 |
| - let groupData = try JSONDecoder().decode(PushGroup.self, from: data) |
24 |
| - return groupData |
25 |
| - } |
26 |
| - |
27 |
| - public static func getGroupInfoDTO(chatId: String, env: ENV) async throws -> PushChat |
28 |
| - .PushGroupInfoDTO |
29 |
| - { |
30 |
| - let url = try PushEndpoint.getGroup(chatId: chatId, apiVersion: "v2", env: env).url |
31 |
| - var request = URLRequest(url: url) |
32 |
| - request.httpMethod = "GET" |
33 |
| - request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
34 |
| - |
35 |
| - let (data, res) = try await URLSession.shared.data(for: request) |
36 |
| - guard let httpResponse = res as? HTTPURLResponse else { |
37 |
| - throw URLError(.badServerResponse) |
| 51 | + public static func getGroupSessionKey(sessionKey: String, env: ENV) async throws -> String { |
| 52 | + let url = try PushEndpoint.getGroupSession(chatId: sessionKey, apiVersion: "v1", env: env).url |
| 53 | + var request = URLRequest(url: url) |
| 54 | + request.httpMethod = "GET" |
| 55 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 56 | + |
| 57 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 58 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 59 | + throw URLError(.badServerResponse) |
| 60 | + } |
| 61 | + |
| 62 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 63 | + throw URLError(.badServerResponse) |
| 64 | + } |
| 65 | + |
| 66 | + struct SecretSessionRes: Codable { |
| 67 | + var encryptedSecret: String |
| 68 | + } |
| 69 | + |
| 70 | + let groupData = try JSONDecoder().decode(SecretSessionRes.self, from: data) |
| 71 | + |
| 72 | + return groupData.encryptedSecret |
38 | 73 | }
|
39 | 74 |
|
40 |
| - if httpResponse.statusCode == 400 { |
41 |
| - throw URLError(.badServerResponse) |
| 75 | + public static func getGroupAccess(chatId: String, did: String, env: ENV) async throws -> GroupAccess? { |
| 76 | + let url = try PushEndpoint.getGroupAccess(chatId: chatId, did: did, env: env).url |
| 77 | + var request = URLRequest(url: url) |
| 78 | + request.httpMethod = "GET" |
| 79 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 80 | + |
| 81 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 82 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 83 | + throw URLError(.badServerResponse) |
| 84 | + } |
| 85 | + |
| 86 | + if httpResponse.statusCode == 400 { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 91 | + throw URLError(.badServerResponse) |
| 92 | + } |
| 93 | + |
| 94 | + return try JSONDecoder().decode(GroupAccess.self, from: data) |
42 | 95 | }
|
43 | 96 |
|
44 |
| - guard (200...299).contains(httpResponse.statusCode) else { |
45 |
| - throw URLError(.badServerResponse) |
| 97 | + public static func getGroupMemberCount(chatId: String, env: ENV) async throws -> TotalMembersCount? { |
| 98 | + let url = try PushEndpoint.getGroupMemberCount(chatId: chatId, env: env).url |
| 99 | + var request = URLRequest(url: url) |
| 100 | + request.httpMethod = "GET" |
| 101 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 102 | + |
| 103 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 104 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 105 | + throw URLError(.badServerResponse) |
| 106 | + } |
| 107 | + |
| 108 | + if httpResponse.statusCode == 400 { |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 113 | + throw URLError(.badServerResponse) |
| 114 | + } |
| 115 | + |
| 116 | + let groupData = try JSONDecoder().decode(ChatMemberCounts.self, from: data) |
| 117 | + return groupData.totalMembersCount |
46 | 118 | }
|
47 | 119 |
|
48 |
| - let groupData = try JSONDecoder().decode(PushGroupInfoDTO.self, from: data) |
49 |
| - return groupData |
50 |
| - } |
| 120 | + public static func getGroupMemberStatus(chatId: String, did: String, env: ENV) async throws -> GroupMemberStatus? { |
| 121 | + let url = try PushEndpoint.getGroupMemberStatus(chatId: chatId, did: walletToPCAIP10(account: did), env: env).url |
| 122 | + var request = URLRequest(url: url) |
| 123 | + request.httpMethod = "GET" |
| 124 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 125 | + |
| 126 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 127 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 128 | + throw URLError(.badServerResponse) |
| 129 | + } |
51 | 130 |
|
52 |
| - public static func getGroupSessionKey(sessionKey: String, env: ENV) async throws -> String { |
53 |
| - let url = try PushEndpoint.getGroupSession(chatId: sessionKey, apiVersion: "v1", env: env).url |
54 |
| - var request = URLRequest(url: url) |
55 |
| - request.httpMethod = "GET" |
56 |
| - request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 131 | + if httpResponse.statusCode == 400 { |
| 132 | + return nil |
| 133 | + } |
57 | 134 |
|
58 |
| - let (data, res) = try await URLSession.shared.data(for: request) |
59 |
| - guard let httpResponse = res as? HTTPURLResponse else { |
60 |
| - throw URLError(.badServerResponse) |
| 135 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 136 | + throw URLError(.badServerResponse) |
| 137 | + } |
| 138 | + |
| 139 | + let groupData = try JSONDecoder().decode(GroupMemberStatus.self, from: data) |
| 140 | + return groupData |
61 | 141 | }
|
62 | 142 |
|
63 |
| - guard (200...299).contains(httpResponse.statusCode) else { |
64 |
| - throw URLError(.badServerResponse) |
| 143 | + public struct FetchChatGroupInfo { |
| 144 | + var chatId: String |
| 145 | + var page: Int |
| 146 | + var limit: Int |
| 147 | + var pending: Bool? |
| 148 | + var role: String? |
| 149 | + |
| 150 | + init(chatId: String, page: Int = 1, limit: Int = 20, pending: Bool? = nil, role: String? = nil) { |
| 151 | + self.chatId = chatId |
| 152 | + self.page = page |
| 153 | + self.limit = limit |
| 154 | + self.pending = pending |
| 155 | + self.role = role |
| 156 | + } |
65 | 157 | }
|
66 | 158 |
|
67 |
| - struct SecretSessionRes: Codable { |
68 |
| - var encryptedSecret: String |
| 159 | + public static func getGroupMembers(options: FetchChatGroupInfo, env: ENV) async throws -> [ChatMemberProfile]? { |
| 160 | + let url = try PushEndpoint.getGroupMembers(options: options, env: env).url |
| 161 | + var request = URLRequest(url: url) |
| 162 | + request.httpMethod = "GET" |
| 163 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 164 | + |
| 165 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 166 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 167 | + throw URLError(.badServerResponse) |
| 168 | + } |
| 169 | + |
| 170 | + if httpResponse.statusCode == 400 { |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 175 | + throw URLError(.badServerResponse) |
| 176 | + } |
| 177 | + |
| 178 | + let groupData = try JSONDecoder().decode(GetMembersResponse.self, from: data) |
| 179 | + return groupData.members |
69 | 180 | }
|
70 | 181 |
|
71 |
| - let groupData = try JSONDecoder().decode(SecretSessionRes.self, from: data) |
| 182 | + public static func getGroupMembersPublicKeys(chatId: String, page: Int = 1, limit: Int = 20, env: ENV) async throws -> [GroupMemberPublicKey]? { |
| 183 | + let url = try PushEndpoint.getGroupMembersPublicKeys(chatId: chatId, page: page, limit: limit, env: env).url |
| 184 | + var request = URLRequest(url: url) |
| 185 | + request.httpMethod = "GET" |
| 186 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 187 | + |
| 188 | + let (data, res) = try await URLSession.shared.data(for: request) |
| 189 | + guard let httpResponse = res as? HTTPURLResponse else { |
| 190 | + throw URLError(.badServerResponse) |
| 191 | + } |
72 | 192 |
|
73 |
| - return groupData.encryptedSecret |
74 |
| - } |
| 193 | + if httpResponse.statusCode == 400 { |
| 194 | + return nil |
| 195 | + } |
75 | 196 |
|
| 197 | + guard (200 ... 299).contains(httpResponse.statusCode) else { |
| 198 | + throw URLError(.badServerResponse) |
| 199 | + } |
| 200 | + |
| 201 | + let groupData = try JSONDecoder().decode(GetMemberPublicKeysResponse.self, from: data) |
| 202 | + return groupData.members |
| 203 | + } |
| 204 | + |
| 205 | + public static func getAllGroupMembersPublicKeysV2(chatId: String, env: ENV) async throws -> [GroupMemberPublicKey]? { |
| 206 | + let count = try await getGroupMemberCount(chatId: chatId, env: env) |
| 207 | + let limit = 5000 |
| 208 | + |
| 209 | + let totalPages = Int(ceil(Double(count?.overallCount ?? 0) / Double(limit))) |
| 210 | + var members = [GroupMemberPublicKey]() |
| 211 | + |
| 212 | + for i in 1 ..< totalPages { |
| 213 | + let page = try await getGroupMembersPublicKeys(chatId: chatId, page: i, limit: limit, env: env) |
| 214 | + members = members + (page ?? []) |
| 215 | + } |
| 216 | + |
| 217 | + return members |
| 218 | + } |
76 | 219 | }
|
0 commit comments