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

Cleaned Session+Contacts.swift

parent 5e9b0438
No related branches found
No related tags found
2 merge requests!40v1.1.2b166,!38Using new database structure
...@@ -10,8 +10,6 @@ extension Session { ...@@ -10,8 +10,6 @@ extension Session {
} }
public func verify(contact: Contact) { public func verify(contact: Contact) {
log(string: "Requested verification of \(contact.username)", type: .crumbs)
var contact = contact var contact = contact
contact.authStatus = .verificationInProgress contact.authStatus = .verificationInProgress
...@@ -31,14 +29,10 @@ extension Session { ...@@ -31,14 +29,10 @@ extension Session {
return return
} }
log(string: "Network is available. Verifying \(contact.username)", type: .crumbs)
let resultClosure: (Result<Contact, Error>) -> Void = { result in let resultClosure: (Result<Contact, Error>) -> Void = { result in
switch result { switch result {
case .success(let mightBe): case .success(let mightBe):
guard try! self.client.bindings.verify(marshaled: contact.marshaled!, verifiedMarshaled: mightBe.marshaled!) else { guard try! self.client.bindings.verify(marshaled: contact.marshaled!, verifiedMarshaled: mightBe.marshaled!) else {
log(string: "\(contact.username) is fake. Deleted!", type: .info)
do { do {
try self.dbManager.deleteContact(contact) try self.dbManager.deleteContact(contact)
} catch { } catch {
...@@ -49,7 +43,6 @@ extension Session { ...@@ -49,7 +43,6 @@ extension Session {
} }
contact.authStatus = .verified contact.authStatus = .verified
log(string: "Verified \(contact.username)", type: .info)
do { do {
try self.dbManager.saveContact(contact) try self.dbManager.saveContact(contact)
...@@ -57,8 +50,7 @@ extension Session { ...@@ -57,8 +50,7 @@ extension Session {
log(string: error.localizedDescription, type: .error) log(string: error.localizedDescription, type: .error)
} }
case .failure(let error): case .failure:
log(string: "Verification of \(contact.username) failed: \(error.localizedDescription)", type: .error)
contact.authStatus = .verificationFailed contact.authStatus = .verificationFailed
do { do {
...@@ -135,7 +127,7 @@ extension Session { ...@@ -135,7 +127,7 @@ extension Session {
if contact.authStatus == .requestFailed || contact.authStatus == .confirmationFailed { if contact.authStatus == .requestFailed || contact.authStatus == .confirmationFailed {
contactToOperate = contact contactToOperate = contact
} else { } else {
guard (try? dbManager.fetch(.withUsername(contact.username)).first as Contact?) == nil else { if let _ = try? dbManager.fetchContacts(.init(id: [contact.id])).first {
throw NSError.create("This user has already been requested") throw NSError.create("This user has already been requested")
} }
...@@ -165,7 +157,7 @@ extension Session { ...@@ -165,7 +157,7 @@ extension Session {
contactToOperate.authStatus = success ? .requested : .requestFailed contactToOperate.authStatus = success ? .requested : .requestFailed
contactToOperate = try self.dbManager.saveContact(contactToOperate) contactToOperate = try self.dbManager.saveContact(contactToOperate)
case .failure(let error): case .failure:
contactToOperate.authStatus = .requestFailed contactToOperate.authStatus = .requestFailed
contactToOperate.createdAt = Date() contactToOperate.createdAt = Date()
contactToOperate = try self.dbManager.saveContact(contactToOperate) contactToOperate = try self.dbManager.saveContact(contactToOperate)
...@@ -203,11 +195,13 @@ extension Session { ...@@ -203,11 +195,13 @@ extension Session {
} }
public func deleteContact(_ contact: Contact) throws { public func deleteContact(_ contact: Contact) throws {
if let _: FileTransfer = try? dbManager.fetch(.withContactId(contact.userId)).first { // TODO: If there's an ongoing FT with this contact, it cannot be deleted.
throw NSError.create("There is an ongoing file transfer with this contact as you are receiving or sending a file, please try again later once it’s done")
} else { // if let _: FileTransfer = try? dbManager.fetch(.withContactId(contact.userId)).first {
print("No pending transfer with this contact. Free to delete") // throw NSError.create("There is an ongoing file transfer with this contact as you are receiving or sending a file, please try again later once it’s done")
} // } else {
// print("No pending transfer with this contact. Free to delete")
// }
try client.bindings.removeContact(contact.marshaled!) try client.bindings.removeContact(contact.marshaled!)
try dbManager.deleteContact(contact) try dbManager.deleteContact(contact)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment