Skip to content
Snippets Groups Projects

Fixes bug where setting nickname would mess authStatus

3 files
+ 42
26
Compare changes
  • Side-by-side
  • Inline

Files

@@ -118,29 +118,41 @@ extension Session {
}
public func add(_ contact: Contact) throws {
/// Make sure we are not adding ourselves
///
guard contact.username != username else {
throw NSError.create("You can't add yourself")
}
var contactToOperate: Contact!
var contact = contact
if [.requestFailed, .confirmationFailed, .stranger].contains(contact.authStatus) {
contactToOperate = contact
/// Check if this contact is actually
/// being requested/confirmed after failing
///
if [.requestFailed, .confirmationFailed].contains(contact.authStatus) {
/// If it is being re-requested or
/// re-confirmed, no need to save again
///
contact.createdAt = Date()
if contact.authStatus == .confirmationFailed {
try confirm(contact)
return
}
} else {
/// If its not failed, lets make sure that
/// this is an actual new contact
///
if let _ = try? dbManager.fetchContacts(.init(id: [contact.id])).first {
/// Found a user w/ that id already stored
///
throw NSError.create("This user has already been requested")
}
contactToOperate = try dbManager.saveContact(contact)
contact.authStatus = .requesting
}
guard contactToOperate.authStatus != .confirmationFailed else {
contactToOperate.createdAt = Date()
try confirm(contact)
return
}
contactToOperate.authStatus = .requesting
contact = try dbManager.saveContact(contact)
let myself = client.bindings.meMarshalled(
username!,
@@ -148,22 +160,23 @@ extension Session {
phone: isSharingPhone ? phone : nil
)
client.bindings.add(contactToOperate.marshaled!, from: myself) { [weak self, contactToOperate] in
guard let self = self, var contactToOperate = contactToOperate else { return }
client.bindings.add(contact.marshaled!, from: myself) { [weak self, contact] in
guard let self = self else { return }
var contact = contact
do {
switch $0 {
case .success(let success):
contactToOperate.authStatus = success ? .requested : .requestFailed
contactToOperate = try self.dbManager.saveContact(contactToOperate)
contact.authStatus = success ? .requested : .requestFailed
contact = try self.dbManager.saveContact(contact)
case .failure:
contactToOperate.authStatus = .requestFailed
contactToOperate.createdAt = Date()
contactToOperate = try self.dbManager.saveContact(contactToOperate)
contact.createdAt = Date()
contact.authStatus = .requestFailed
contact = try self.dbManager.saveContact(contact)
self.toastController.enqueueToast(model: .init(
title: Localized.Requests.Failed.toast(contactToOperate.nickname ?? contact.username!),
title: Localized.Requests.Failed.toast(contact.nickname ?? contact.username!),
color: Asset.accentDanger.color,
leftImage: Asset.requestFailedToaster.image
))
Loading