Skip to content

Commit c3755e3

Browse files
Send message with messageObj (#32)
* feat: add scallability funcs -getGroupAccess -getGroupMemberCount -getGroupMemberStatus -getGroupMembers -getGroupMembersPublicKeys -getAllGroupMembersPublicKeysV2 * feat: add updateGroupMember * feat: add test s for public group * feat: send message to new user massage & messageObj are unencrypted * feat: send message to public and private group --------- Co-authored-by: Gbogboade Ayomide <[email protected]>
1 parent fe87978 commit c3755e3

File tree

18 files changed

+2819
-962
lines changed

18 files changed

+2819
-962
lines changed

Sources/Chat/Group/CreateGroup.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ extension PushChat {
55
public static func createGroup(options: CreateGroupOptions) async throws -> PushGroupInfoDTO {
66
do {
77
let payload = try CreateGroupPlayload(options: options)
8-
print("got payload")
98
return try await createGroupService(payload: payload, env: options.env)
109
} catch {
1110
throw GroupChatError.RUNTIME_ERROR(

Sources/Chat/Group/GetGroup.swift

Lines changed: 194 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,219 @@
11
import Foundation
22

33
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
1325
}
1426

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+
}
1846

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
2149
}
2250

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
3873
}
3974

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)
4295
}
4396

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
46118
}
47119

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+
}
51130

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+
}
57134

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
61141
}
62142

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+
}
65157
}
66158

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
69180
}
70181

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+
}
72192

73-
return groupData.encryptedSecret
74-
}
193+
if httpResponse.statusCode == 400 {
194+
return nil
195+
}
75196

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+
}
76219
}

0 commit comments

Comments
 (0)