Newer
Older
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Bindings
extension BindingsGroupChat: GroupManagerInterface {
public func send(_ payload: Data, to group: Data) -> Result<(Int64, Data?, String), Error> {
log(type: .crumbs)
do {
let report = try send(group, message: payload)
return .success((
report.getRoundID(),
report.getMessageID(),
report.getRoundURL()
))
} catch {
return .failure(error)
}
}
public func create(
me: Data,
name: String,
welcome: String?,
with ids: [Data],
_ completion: @escaping (Result<Group, Error>) -> Void
) {
log(type: .crumbs)
let list = BindingsIdList()
ids.forEach { try? list.add($0) }
var welcomeData: Data?
DispatchQueue.global().async { [weak self] in
guard let self = self else { return }
if let welcome = welcome {
welcomeData = welcome.data(using: .utf8)
}
let report = self.makeGroup(list, name: name.data(using: .utf8), message: welcomeData)
if let status = report?.getStatus() {
switch status {
case 0:
completion(.failure(NSError.create("An error occurred before any requests could be sent")))
return
case 1, 2:
// 1. All requests failed to send
// 2. Some requests failed and some succeeded
if let id = report?.getGroup()?.getID() {
do {
try self.resendRequest(id)
fallthrough
} catch {
completion(.failure(error))
return
}
}
case 3:
// All good
guard let group = report?.getGroup() else {
let errorContent = "Couldn't get report from group, although status was 3."
completion(.failure(NSError.create(errorContent)))
log(string: errorContent, type: .error)
return
}
completion(.success(.init(
id: group.getID()!,
name: name,
leaderId: me,
createdAt: Date(),
authStatus: .participating,
serialized: group.serialize()!
)))