Newer
Older
1
2
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import Retry
import Models
import Shared
import Combine
import Defaults
import Database
import Foundation
import NetworkMonitor
import DependencyInjection
public final class Session: SessionType {
@KeyObject(.theme, defaultValue: nil) var theme: String?
@KeyObject(.email, defaultValue: nil) var email: String?
@KeyObject(.phone, defaultValue: nil) var phone: String?
@KeyObject(.avatar, defaultValue: nil) var avatar: Data?
@KeyObject(.username, defaultValue: nil) var username: String?
@KeyObject(.biometrics, defaultValue: false) var biometrics: Bool
@KeyObject(.hideAppList, defaultValue: false) var hideAppList: Bool
@KeyObject(.requestCounter, defaultValue: 0) var requestCounter: Int
@KeyObject(.sharingEmail, defaultValue: false) var isSharingEmail: Bool
@KeyObject(.sharingPhone, defaultValue: false) var isSharingPhone: Bool
@KeyObject(.recordingLogs, defaultValue: true) var recordingLogs: Bool
@KeyObject(.crashReporting, defaultValue: true) var crashReporting: Bool
@KeyObject(.icognitoKeyboard, defaultValue: false) var icognitoKeyboard: Bool
@KeyObject(.pushNotifications, defaultValue: false) var pushNotifications: Bool
@KeyObject(.inappnotifications, defaultValue: true) var inappnotifications: Bool
@Dependency var networkMonitor: NetworkMonitoring
public let client: Client
public let dbManager: DatabaseManager
private var cancellables = Set<AnyCancellable>()
public var myId: Data { client.bindings.myId }
public var version: String { type(of: client.bindings).version }
public var myQR: Data {
client
.bindings
.meMarshalled(
username!,
email: isSharingEmail ? email : nil,
phone: isSharingPhone ? phone : nil
)
}
public var hasRunningTasks: Bool {
client.bindings.hasRunningTasks
}
public var isOnline: AnyPublisher<Bool, Never> {
networkMonitor.statusPublisher.map { $0 == .available }.eraseToAnyPublisher()
}
lazy public var groups: (Group.Request) -> AnyPublisher<[Group], Never> = {
self.dbManager.publisher(fetch: Group.self, $0).catch { _ in Just([]) }.eraseToAnyPublisher()
}
lazy public var contacts: (Contact.Request) -> AnyPublisher<[Contact], Never> = {
self.dbManager.publisher(fetch: Contact.self, $0).catch { _ in Just([]) }.eraseToAnyPublisher()
}
lazy public var singleMessages: (Contact) -> AnyPublisher<[Message], Never> = {
self.dbManager.publisher(fetch: Message.self, .withContact($0.userId)).catch { _ in Just([]) }.eraseToAnyPublisher()
}
lazy public var groupMessages: (Group) -> AnyPublisher<[GroupMessage], Never> = {
self.dbManager.publisher(fetch: GroupMessage.self, .fromGroup($0.groupId)).catch { _ in Just([]) }.eraseToAnyPublisher()
}
lazy public var groupChats: (GroupChatInfo.Request) -> AnyPublisher<[GroupChatInfo], Never> = {
self.dbManager.publisher(fetch: GroupChatInfo.self, $0).catch { _ in Just([]) }.eraseToAnyPublisher()
}
lazy public var singleChats: (SingleChatInfo.Request) -> AnyPublisher<[SingleChatInfo], Never> = { _ in
self.dbManager.publisher(fetch: Contact.self, .friends)
.flatMap { [unowned self] contactList -> AnyPublisher<[SingleChatInfo], Error> in
let contactIds = contactList.map { $0.userId }
let messagesPublisher: AnyPublisher<[Message], Error> = dbManager
.publisher(fetch: .latestOnesFromContactIds(contactIds))
.map { $0.sorted(by: { $0.timestamp > $1.timestamp }) }
.eraseToAnyPublisher()
return messagesPublisher.map { messages -> [SingleChatInfo] in
contactList.map { contact -> SingleChatInfo in
SingleChatInfo(contact: contact, lastMessage: messages.first {
$0.sender == contact.userId || $0.receiver == contact.userId
})
}
}
.eraseToAnyPublisher()
}
.catch { _ in Just([]) }
.map { $0.filter { $0.lastMessage != nil }}
.map { $0.sorted(by: { $0.lastMessage!.timestamp > $1.lastMessage!.timestamp })}
.eraseToAnyPublisher()
}
public init(ndf: String) throws {
let network = try! DependencyInjection.Container.shared.resolve() as XXNetworking
self.client = try network.newClient(ndf: ndf)
dbManager = GRDBDatabaseManager()
try dbManager.setup()
setupBindings()
networkMonitor.start()
networkMonitor.statusPublisher
.filter { $0 == .available }.first()
.sink { [unowned self] _ in client.bindings.replayRequests() }
.store(in: &cancellables)
registerUnfinishedTransfers()
if let pendingVerificationUsers: [Contact] = try? dbManager.fetch(.verificationInProgress) {
pendingVerificationUsers.forEach {
var contact = $0
contact.status = .verificationFailed
do {
_ = try dbManager.save(contact)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
}
}
public func setDummyTraffic(status: Bool) {
client.dummyManager?.setStatus(status: status)
}
public func deleteMyself() throws {
log(string: "Will start deleting account process", type: .crumbs)
guard let username = username, let ud = client.userDiscovery else {
log(string: "Failed deleting account. No username or UD", type: .error)
return
}
do {
try unregisterNotifications()
} catch {
log(string: "Failed to unregister for notifications", type: .error)
}
try ud.deleteMyself(username)
log(string: "Deleted myself from User Discovery", type: .info)
stop()
log(string: "Requested network stop", type: .crumbs)
cleanUp()
}
private func cleanUp() {
retry(max: 10, retryStrategy: .delay(seconds: 1)) { [unowned self] in
guard self.hasRunningTasks == false else {
let string = "Tried to clean up database and defaults but network hasn't stopped yet. Sleeping for a second..."
log(string: string, type: .error)
throw NSError.create("")
}
}.finalCatch { _ in fatalError("Couldn't delete account because network is not stopping") }
dbManager.drop()
log(string: "Dropped database", type: .info)
FileManager.xxCleanup()
log(string: "Wiped disk", type: .info)
email = nil
phone = nil
theme = nil
avatar = nil
self.username = nil
isSharingEmail = false
isSharingPhone = false
requestCounter = 0
biometrics = false
hideAppList = false
recordingLogs = true
crashReporting = true
icognitoKeyboard = false
pushNotifications = false
inappnotifications = true
log(string: "Wiped defaults", type: .info)
}
public func forceFailMessages() {
if let pendingE2E: [Message] = try? dbManager.fetch(.sending) {
pendingE2E.forEach {
var message = $0
message.status = .failedToSend
do {
try dbManager.save(message)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
}
if let pendingGroupMessages: [GroupMessage] = try? dbManager.fetch(.sending) {
pendingGroupMessages.forEach {
var message = $0
message.status = .failed
do {
try dbManager.save(message)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
}
}
private func registerUnfinishedTransfers() {
guard let unfinisheds: [Message] = try? dbManager.fetch(.sendingAttachment), !unfinisheds.isEmpty else { return }
log(string: "There are unfinished transfers from the last session. Re-registering their upload progress", type: .crumbs)
for var message in unfinisheds {
guard let tid = message.payload.attachment?.transferId else {
log(string: "Impossible to resume a transfer that had no TID", type: .error)
return
}
do {
try client.transferManager?.listenUploadFromTransfer(with: tid) { completed, sent, arrived, total, error in
if completed {
message.status = .sent
message.payload.attachment?.progress = 1.0
log(string: "FT Up finished", type: .info)
if let transfer: FileTransfer = try? self.dbManager.fetch(.withTID(tid)).first {
do {
try self.dbManager.delete(transfer)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
} else {
if let error = error {
log(string: error.localizedDescription, type: .error)
message.status = .failedToSend
} else {
let progress = Float(arrived)/Float(total)
message.payload.attachment?.progress = progress
log(string: "FT Up: \(progress)", type: .crumbs)
return
}
}
do {
_ = try self.dbManager.save(message)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
} catch {
log(string: "An error occurred when trying to register unfinished FT: \(error.localizedDescription). Switching it to 'sent'", type: .error)
message.status = .sent
do {
_ = try self.dbManager.save(message)
} catch {
log(string: error.localizedDescription, type: .error)
}
}
}
}
private func setupBindings() {
client.requests
.sink { [unowned self] request in
if let _: Contact = try? dbManager.fetch(.withUserId(request.userId)).first { return }
if self.inappnotifications {
DeviceFeedback.sound(.contactAdded)
DeviceFeedback.shake(.notification)
}
verify(contact: request)
}
.store(in: &cancellables)
client.groupMessages
.sink { [unowned self] in
do {
_ = try dbManager.save($0)
} catch {
log(string: "Failed to save an incoming group message: \(error.localizedDescription)", type: .error)
}
}.store(in: &cancellables)
client.messages
.sink { [unowned self] in
do {
_ = try dbManager.save($0)
} catch {
log(string: "Failed to save an incoming direct message: \(error.localizedDescription)", type: .error)
}
}.store(in: &cancellables)
client.network
.sink { [unowned self] in networkMonitor.update($0) }
.store(in: &cancellables)
client.incomingTransfers
.sink { [unowned self] in handle(incomingTransfer: $0) }
.store(in: &cancellables)
client.groupRequests
.sink { [unowned self] request in
if let _: Group = try? dbManager.fetch(.withGroupId(request.0.groupId)).first { return }
DispatchQueue.global().async { [weak self] in
self?.processGroupCreation(request.0, memberIds: request.1, welcome: request.2)
}
}.store(in: &cancellables)
client.confirmations
.sink { [unowned self] in
guard var contact: Contact = try? dbManager.fetch(.withUserId($0.userId)).first else { return }
contact.status = .friend
do {
try dbManager.save(contact)
} catch {
log(string: error.localizedDescription, type: .error)
}
}.store(in: &cancellables)
}
public func getTextFromMessage(messageId: Data) -> String? {
guard let message: Message = try? dbManager.fetch(.withUniqueId(messageId)).first else { return nil }
return message.payload.text
}
public func getTextFromGroupMessage(messageId: Data) -> String? {
guard let message: GroupMessage = try? dbManager.fetch(.withUniqueId(messageId)).first else { return nil }
return message.payload.text
}
}