Skip to content
Snippets Groups Projects
Commit 0808b5f6 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Continued to comment old models and updating integration

parent 2f9b38e8
No related branches found
No related tags found
2 merge requests!40v1.1.2b166,!38Using new database structure
Showing with 191 additions and 290 deletions
...@@ -53,7 +53,7 @@ let package = Package( ...@@ -53,7 +53,7 @@ let package = Package(
dependencies: [ dependencies: [
.package( .package(
url: "https://git.xx.network/elixxir/client-ios-db.git", url: "https://git.xx.network/elixxir/client-ios-db.git",
revision: "e1b3b1a8d1df6d259c99289e8d09e73fbfa9fe63" revision: "2e51a960216340cf12a4c6cbc7ac18a9f07d5ee6"
), ),
.package(url: "https://github.com/Quick/Quick", from: "3.0.0"), .package(url: "https://github.com/Quick/Quick", from: "3.0.0"),
.package(url: "https://github.com/Quick/Nimble", from: "9.0.0"), .package(url: "https://github.com/Quick/Nimble", from: "9.0.0"),
......
import Models import Models
import Foundation
import Combine import Combine
import XXModels
import Foundation
public enum MessageDeliveryStatus { public enum MessageDeliveryStatus {
case sent case sent
......
import Models import Models
import XXModels
import Foundation import Foundation
public struct LookupResult { public struct LookupResult {
......
import Models import Models
import Combine import Combine
import XXModels
import Foundation import Foundation
public final class BindingsMock: BindingsInterface { public final class BindingsMock: BindingsInterface {
......
import Models import Models
import XXModels
import Foundation import Foundation
final class UserDiscoveryMock: UserDiscoveryInterface { final class UserDiscoveryMock: UserDiscoveryInterface {
......
...@@ -51,7 +51,7 @@ extension Session { ...@@ -51,7 +51,7 @@ extension Session {
log(string: "Verified \(contact.username)", type: .info) log(string: "Verified \(contact.username)", type: .info)
do { do {
try self.dbManager.save(contact) try self.dbManager.saveContact(contact)
} catch { } catch {
log(string: error.localizedDescription, type: .error) log(string: error.localizedDescription, type: .error)
} }
...@@ -61,7 +61,7 @@ extension Session { ...@@ -61,7 +61,7 @@ extension Session {
contact.status = .verificationFailed contact.status = .verificationFailed
do { do {
try self.dbManager.save(contact) try self.dbManager.saveContact(contact)
} catch { } catch {
log(string: error.localizedDescription, type: .error) log(string: error.localizedDescription, type: .error)
} }
...@@ -210,38 +210,6 @@ extension Session { ...@@ -210,38 +210,6 @@ extension Session {
} }
} }
public func update(_ contact: Contact) {
do {
if var stored = try dbManager.fetch(.withUsername(contact.username)).first as Contact? {
stored.email = contact.email
stored.photo = contact.photo
stored.phone = contact.phone
stored.nickname = contact.nickname
stored.isRecent = contact.isRecent
stored.createdAt = contact.createdAt
try dbManager.save(stored)
try dbManager.updateAll(
GroupMember.self,
GroupMember.Request.withUserId(stored.userId),
with: [GroupMember.Column.photo.set(to: stored.photo)]
)
}
} catch {
log(string: "Error updating a contact: \(error.localizedDescription)", type: .error)
}
}
public func delete<T: Persistable>(_ model: T, isRequest: Bool = false) {
log(string: "Deleting a model...", type: .info)
do {
try dbManager.delete(model)
} catch {
log(string: "Error deleting a model: \(error.localizedDescription)", type: .error)
}
}
public func find(by username: String) -> Contact? { public func find(by username: String) -> Contact? {
log(string: "Trying to find contact with username: \(username)", type: .info) log(string: "Trying to find contact with username: \(username)", type: .info)
......
...@@ -4,6 +4,7 @@ import Shared ...@@ -4,6 +4,7 @@ import Shared
import Combine import Combine
import Defaults import Defaults
import XXModels import XXModels
import XXDatabase
import Foundation import Foundation
import BackupFeature import BackupFeature
import NetworkMonitor import NetworkMonitor
...@@ -88,7 +89,7 @@ public final class Session: SessionType { ...@@ -88,7 +89,7 @@ public final class Session: SessionType {
os_signpost(.end, log: logHandler, name: "Decrypting", "Finished newClientFromBackup") os_signpost(.end, log: logHandler, name: "Decrypting", "Finished newClientFromBackup")
self.client = client self.client = client
dbManager = GRDBDatabaseManager() dbManager = try Database.inMemory()
let report = try! JSONDecoder().decode(BackupReport.self, from: backupData!) let report = try! JSONDecoder().decode(BackupReport.self, from: backupData!)
...@@ -110,13 +111,11 @@ public final class Session: SessionType { ...@@ -110,13 +111,11 @@ public final class Session: SessionType {
public init(ndf: String) throws { public init(ndf: String) throws {
let network = try! DependencyInjection.Container.shared.resolve() as XXNetworking let network = try! DependencyInjection.Container.shared.resolve() as XXNetworking
self.client = try network.newClient(ndf: ndf) self.client = try network.newClient(ndf: ndf)
dbManager = GRDBDatabaseManager() dbManager = try Database.inMemory()
try continueInitialization() try continueInitialization()
} }
private func continueInitialization() throws { private func continueInitialization() throws {
try dbManager.setup()
setupBindings() setupBindings()
networkMonitor.start() networkMonitor.start()
...@@ -127,13 +126,8 @@ public final class Session: SessionType { ...@@ -127,13 +126,8 @@ public final class Session: SessionType {
registerUnfinishedTransfers() registerUnfinishedTransfers()
if let pendingVerificationUsers: [Contact] = try? dbManager.fetch(.verificationInProgress) { let query = Contact.Query(authStatus: [.verificationInProgress])
pendingVerificationUsers.forEach { _ = try? dbManager.bulkUpdateContacts(query, .init(authStatus: .verificationFailed))
var contact = $0
contact.status = .verificationFailed
_ = try? dbManager.save(contact)
}
}
} }
public func setDummyTraffic(status: Bool) { public func setDummyTraffic(status: Bool) {
...@@ -155,7 +149,7 @@ public final class Session: SessionType { ...@@ -155,7 +149,7 @@ public final class Session: SessionType {
guard self.hasRunningTasks == false else { throw NSError.create("") } guard self.hasRunningTasks == false else { throw NSError.create("") }
}.finalCatch { _ in fatalError("Couldn't delete account because network is not stopping") } }.finalCatch { _ in fatalError("Couldn't delete account because network is not stopping") }
dbManager.drop() try? dbManager.drop()
FileManager.xxCleanup() FileManager.xxCleanup()
email = nil email = nil
...@@ -175,69 +169,39 @@ public final class Session: SessionType { ...@@ -175,69 +169,39 @@ public final class Session: SessionType {
inappnotifications = true inappnotifications = true
} }
public func hideRequestOf(group: Group) {
var group = group
group.status = .hidden
_ = try? dbManager.save(group)
}
public func hideRequestOf(contact: Contact) {
var contact = contact
contact.status = .hidden
_ = try? dbManager.save(contact)
}
public func forceFailMessages() {
if let pendingE2E: [Message] = try? dbManager.fetch(.sending) {
pendingE2E.forEach {
var message = $0
message.status = .failedToSend
_ = try? dbManager.save(message)
}
}
if let pendingGroupMessages: [GroupMessage] = try? dbManager.fetch(.sending) {
pendingGroupMessages.forEach {
var message = $0
message.status = .failed
_ = try? dbManager.save(message)
}
}
}
private func registerUnfinishedTransfers() { private func registerUnfinishedTransfers() {
guard let unfinisheds: [Message] = try? dbManager.fetch(.sendingAttachment), !unfinisheds.isEmpty else { return } // guard let unfinisheds: [Message] = try? dbManager.fetch(.sendingAttachment), !unfinisheds.isEmpty else { return }
//
for var message in unfinisheds { // for var message in unfinisheds {
guard let tid = message.payload.attachment?.transferId else { return } // guard let tid = message.payload.attachment?.transferId else { return }
//
do { // do {
try client.transferManager?.listenUploadFromTransfer(with: tid) { completed, sent, arrived, total, error in // try client.transferManager?.listenUploadFromTransfer(with: tid) { completed, sent, arrived, total, error in
if completed { // if completed {
message.status = .sent // message.status = .sent
message.payload.attachment?.progress = 1.0 // message.payload.attachment?.progress = 1.0
//
if let transfer: FileTransfer = try? self.dbManager.fetch(.withTID(tid)).first { // if let transfer: FileTransfer = try? self.dbManager.fetch(.withTID(tid)).first {
try? self.dbManager.delete(transfer) // try? self.dbManager.delete(transfer)
} // }
} else { // } else {
if let error = error { // if let error = error {
log(string: error.localizedDescription, type: .error) // log(string: error.localizedDescription, type: .error)
message.status = .failedToSend // message.status = .failedToSend
} else { // } else {
let progress = Float(arrived)/Float(total) // let progress = Float(arrived)/Float(total)
message.payload.attachment?.progress = progress // message.payload.attachment?.progress = progress
return // return
} // }
} // }
//
_ = try? self.dbManager.save(message) // _ = try? self.dbManager.save(message)
} // }
} catch { // } catch {
message.status = .sent // message.status = .sent
_ = try? self.dbManager.save(message) // _ = try? self.dbManager.save(message)
} // }
} // }
} }
func updateFactsOnBackup() { func updateFactsOnBackup() {
...@@ -277,7 +241,7 @@ public final class Session: SessionType { ...@@ -277,7 +241,7 @@ public final class Session: SessionType {
}.store(in: &cancellables) }.store(in: &cancellables)
client.requestsSent client.requestsSent
.sink { [unowned self] in _ = try? dbManager.save($0) } .sink { [unowned self] in _ = try? dbManager.saveContact($0) }
.store(in: &cancellables) .store(in: &cancellables)
client.backup client.backup
...@@ -292,7 +256,7 @@ public final class Session: SessionType { ...@@ -292,7 +256,7 @@ public final class Session: SessionType {
/// ///
var contact = $0 var contact = $0
contact.status = .friend contact.status = .friend
_ = try? dbManager.save(contact) _ = try? dbManager.saveContact(contact)
}.store(in: &cancellables) }.store(in: &cancellables)
backupService.settingsPublisher backupService.settingsPublisher
...@@ -321,7 +285,7 @@ public final class Session: SessionType { ...@@ -321,7 +285,7 @@ public final class Session: SessionType {
.store(in: &cancellables) .store(in: &cancellables)
client.groupMessages client.groupMessages
.sink { [unowned self] in _ = try? dbManager.save($0) } .sink { [unowned self] in _ = try? dbManager.saveMessage($0) }
.store(in: &cancellables) .store(in: &cancellables)
client.messages client.messages
...@@ -357,7 +321,7 @@ public final class Session: SessionType { ...@@ -357,7 +321,7 @@ public final class Session: SessionType {
contact.status = .friend contact.status = .friend
contact.isRecent = true contact.isRecent = true
contact.createdAt = Date() contact.createdAt = Date()
_ = try? dbManager.save(contact) _ = try? dbManager.saveContact(contact)
toastController.enqueueToast(model: .init( toastController.enqueueToast(model: .init(
title: contact.nickname ?? contact.username, title: contact.nickname ?? contact.username,
...@@ -367,24 +331,4 @@ public final class Session: SessionType { ...@@ -367,24 +331,4 @@ public final class Session: SessionType {
} }
}.store(in: &cancellables) }.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
}
public func getContactWith(userId: Data) -> Contact? {
let contact: Contact? = try? dbManager.fetch(.withUserId(userId)).first
return contact
}
public func getGroupChatInfoWith(groupId: Data) -> GroupChatInfo? {
let info: GroupChatInfo? = try? dbManager.fetch(.fromGroup(groupId)).first
return info
}
} }
...@@ -12,12 +12,6 @@ public protocol SessionType { ...@@ -12,12 +12,6 @@ public protocol SessionType {
func deleteMyself() throws func deleteMyself() throws
func getId(from: Data) -> Data? func getId(from: Data) -> Data?
func forceFailMessages()
func hideRequestOf(group: Group)
func hideRequestOf(contact: Contact)
func send(imageData: Data, to: Contact, completion: @escaping (Result<Void, Error>) -> Void) func send(imageData: Data, to: Contact, completion: @escaping (Result<Void, Error>) -> Void)
func verify(contact: Contact) func verify(contact: Contact)
...@@ -54,17 +48,11 @@ public protocol SessionType { ...@@ -54,17 +48,11 @@ public protocol SessionType {
func delete(groupMessages: [Int64]) func delete(groupMessages: [Int64])
func send(_: Payload, toContact: Contact) func send(_: Payload, toContact: Contact)
func getTextFromMessage(messageId: Data) -> String?
func getTextFromGroupMessage(messageId: Data) -> String?
// Contacts // Contacts
func update(_: Contact)
func add(_: Contact) throws func add(_: Contact) throws
func confirm(_: Contact) throws func confirm(_: Contact) throws
func find(by: String) -> Contact? func find(by: String) -> Contact?
func delete<T: Persistable>(_: T, isRequest: Bool)
func deleteContact(_: Contact) throws func deleteContact(_: Contact) throws
func retryRequest(_: Contact) throws func retryRequest(_: Contact) throws
...@@ -82,7 +70,4 @@ public protocol SessionType { ...@@ -82,7 +70,4 @@ public protocol SessionType {
members: [Contact], members: [Contact],
_ completion: @escaping (Result<(Group, [GroupMember]), Error>) -> Void _ completion: @escaping (Result<(Group, [GroupMember]), Error>) -> Void
) )
func getContactWith(userId: Data) -> Contact?
func getGroupChatInfoWith(groupId: Data) -> GroupChatInfo?
} }
...@@ -41,83 +41,83 @@ public class IndexedListCollator<Item: IndexableItem> { ...@@ -41,83 +41,83 @@ public class IndexedListCollator<Item: IndexableItem> {
return (sections: sections.filter { !$0.isEmpty }, collation: collation) return (sections: sections.filter { !$0.isEmpty }, collation: collation)
} }
} }
//
public struct Contact: Codable, Hashable, Equatable { //public struct Contact: Codable, Hashable, Equatable {
public enum Request { // public enum Request {
case all // case all
case failed // case failed
case friends // case friends
case received // case received
case requested // case requested
case isRecent // case isRecent
case verificationInProgress // case verificationInProgress
case withUserId(Data) // case withUserId(Data)
case withUserIds([Data]) // case withUserIds([Data])
case withUsername(String) // case withUsername(String)
} // }
//
public enum Status: Int64, Codable { // public enum Status: Int64, Codable {
case friend // case friend
case stranger // case stranger
case verified // case verified
case verificationFailed // case verificationFailed
case verificationInProgress // case verificationInProgress
case requested // case requested
case requesting // case requesting
case requestFailed // case requestFailed
case confirming // case confirming
case confirmationFailed // case confirmationFailed
case hidden // case hidden
} // }
//
public var id: Int64? // public var id: Int64?
public var photo: Data? // public var photo: Data?
public let userId: Data // public let userId: Data
public var email: String? // public var email: String?
public var phone: String? // public var phone: String?
public var status: Status // public var status: Status
public var marshaled: Data // public var marshaled: Data
public var createdAt: Date // public var createdAt: Date
public let username: String // public let username: String
public var nickname: String? // public var nickname: String?
public var isRecent: Bool // public var isRecent: Bool
//
public init( // public init(
photo: Data?, // photo: Data?,
userId: Data, // userId: Data,
email: String?, // email: String?,
phone: String?, // phone: String?,
status: Status, // status: Status,
marshaled: Data, // marshaled: Data,
username: String, // username: String,
nickname: String?, // nickname: String?,
createdAt: Date, // createdAt: Date,
isRecent: Bool // isRecent: Bool
) { // ) {
self.email = email // self.email = email
self.phone = phone // self.phone = phone
self.photo = photo // self.photo = photo
self.status = status // self.status = status
self.userId = userId // self.userId = userId
self.username = username // self.username = username
self.nickname = nickname // self.nickname = nickname
self.marshaled = marshaled // self.marshaled = marshaled
self.createdAt = createdAt // self.createdAt = createdAt
self.isRecent = isRecent // self.isRecent = isRecent
} // }
//
public var differenceIdentifier: Data { userId } // public var differenceIdentifier: Data { userId }
//
public static var databaseTableName: String { "contacts" } // public static var databaseTableName: String { "contacts" }
} //}
//
extension Contact: Differentiable {} //extension Contact: Differentiable {}
extension Contact: IndexableItem { //extension Contact: IndexableItem {
public var indexedOn: NSString { // public var indexedOn: NSString {
guard let nickname = nickname else { // guard let nickname = nickname else {
return "\(username.first!)" as NSString // return "\(username.first!)" as NSString
} // }
//
return "\(nickname.first!)" as NSString // return "\(nickname.first!)" as NSString
} // }
} //}
import Foundation //import Foundation
import KeychainAccess //import KeychainAccess
//
public struct Group: Codable, Equatable, Hashable { //public struct Group: Codable, Equatable, Hashable {
public enum Status: Int64, Codable { // public enum Status: Int64, Codable {
case hidden // case hidden
case pending // case pending
case deleting // case deleting
case participating // case participating
} // }
//
public enum Request { // public enum Request {
case pending // case pending
case accepted // case accepted
case withGroupId(Data) // case withGroupId(Data)
} // }
//
public var id: Int64? // public var id: Int64?
public var name: String // public var name: String
public var leader: Data // public var leader: Data
public var groupId: Data // public var groupId: Data
public var status: Status // public var status: Status
public var serialize: Data // public var serialize: Data
public var createdAt: Date // public var createdAt: Date
public static var databaseTableName: String { "groups" } // public static var databaseTableName: String { "groups" }
//
public init( // public init(
leader: Data, // leader: Data,
name: String, // name: String,
groupId: Data, // groupId: Data,
status: Status, // status: Status,
createdAt: Date, // createdAt: Date,
serialize: Data // serialize: Data
) { // ) {
self.name = name // self.name = name
self.leader = leader // self.leader = leader
self.status = status // self.status = status
self.groupId = groupId // self.groupId = groupId
self.createdAt = createdAt // self.createdAt = createdAt
self.serialize = serialize // self.serialize = serialize
} // }
} //}
import Foundation //import Foundation
//
public struct SingleChatInfo: Codable, Equatable, Hashable { //public struct SingleChatInfo: Codable, Equatable, Hashable {
public enum Request { // public enum Request {
case all // case all
} // }
//
public var contact: Contact // public var contact: Contact
public var lastMessage: Message? // public var lastMessage: Message?
//
public init( // public init(
contact: Contact, // contact: Contact,
lastMessage: Message? // lastMessage: Message?
) { // ) {
self.contact = contact // self.contact = contact
self.lastMessage = lastMessage // self.lastMessage = lastMessage
} // }
} //}
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://git.xx.network/elixxir/client-ios-db.git", "location" : "https://git.xx.network/elixxir/client-ios-db.git",
"state" : { "state" : {
"revision" : "e1b3b1a8d1df6d259c99289e8d09e73fbfa9fe63" "revision" : "2e51a960216340cf12a4c6cbc7ac18a9f07d5ee6"
} }
}, },
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment