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

Merge branch 'fix-nickname-sent' into 'development'

Fixes bug where setting nickname would mess authStatus

See merge request elixxir/client-ios!49
parents fc508cfd 046a3749
No related branches found
No related tags found
2 merge requests!54Releasing 1.1.4,!49Fixes bug where setting nickname would mess authStatus
...@@ -118,29 +118,41 @@ extension Session { ...@@ -118,29 +118,41 @@ extension Session {
} }
public func add(_ contact: Contact) throws { public func add(_ contact: Contact) throws {
/// Make sure we are not adding ourselves
///
guard contact.username != username else { guard contact.username != username else {
throw NSError.create("You can't add yourself") throw NSError.create("You can't add yourself")
} }
var contactToOperate: Contact! var contact = contact
if [.requestFailed, .confirmationFailed, .stranger].contains(contact.authStatus) { /// Check if this contact is actually
contactToOperate = contact /// 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 { } 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 { 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") throw NSError.create("This user has already been requested")
} }
contactToOperate = try dbManager.saveContact(contact) contact.authStatus = .requesting
} }
guard contactToOperate.authStatus != .confirmationFailed else { contact = try dbManager.saveContact(contact)
contactToOperate.createdAt = Date()
try confirm(contact)
return
}
contactToOperate.authStatus = .requesting
let myself = client.bindings.meMarshalled( let myself = client.bindings.meMarshalled(
username!, username!,
...@@ -148,22 +160,23 @@ extension Session { ...@@ -148,22 +160,23 @@ extension Session {
phone: isSharingPhone ? phone : nil phone: isSharingPhone ? phone : nil
) )
client.bindings.add(contactToOperate.marshaled!, from: myself) { [weak self, contactToOperate] in client.bindings.add(contact.marshaled!, from: myself) { [weak self, contact] in
guard let self = self, var contactToOperate = contactToOperate else { return } guard let self = self else { return }
var contact = contact
do { do {
switch $0 { switch $0 {
case .success(let success): case .success(let success):
contactToOperate.authStatus = success ? .requested : .requestFailed contact.authStatus = success ? .requested : .requestFailed
contactToOperate = try self.dbManager.saveContact(contactToOperate) contact = try self.dbManager.saveContact(contact)
case .failure: case .failure:
contactToOperate.authStatus = .requestFailed contact.createdAt = Date()
contactToOperate.createdAt = Date() contact.authStatus = .requestFailed
contactToOperate = try self.dbManager.saveContact(contactToOperate) contact = try self.dbManager.saveContact(contact)
self.toastController.enqueueToast(model: .init( 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, color: Asset.accentDanger.color,
leftImage: Asset.requestFailedToaster.image leftImage: Asset.requestFailedToaster.image
)) ))
......
...@@ -3,6 +3,7 @@ import UIKit ...@@ -3,6 +3,7 @@ import UIKit
import Models import Models
import Shared import Shared
import Combine import Combine
import XXModels
import Integration import Integration
import ToastFeature import ToastFeature
import CombineSchedulers import CombineSchedulers
...@@ -32,7 +33,12 @@ final class RequestsSentViewModel { ...@@ -32,7 +33,12 @@ final class RequestsSentViewModel {
var backgroundScheduler: AnySchedulerOf<DispatchQueue> = DispatchQueue.global().eraseToAnyScheduler() var backgroundScheduler: AnySchedulerOf<DispatchQueue> = DispatchQueue.global().eraseToAnyScheduler()
init() { init() {
session.dbManager.fetchContactsPublisher(.init(authStatus: [.requested])) let query = Contact.Query(authStatus: [
.requested,
.requesting
])
session.dbManager.fetchContactsPublisher(query)
.assertNoFailure() .assertNoFailure()
.removeDuplicates() .removeDuplicates()
.map { data -> NSDiffableDataSourceSnapshot<Section, RequestSent> in .map { data -> NSDiffableDataSourceSnapshot<Section, RequestSent> in
......
...@@ -163,12 +163,9 @@ final class SearchViewModel { ...@@ -163,12 +163,9 @@ final class SearchViewModel {
} }
func didSet(nickname: String, for contact: Contact) { func didSet(nickname: String, for contact: Contact) {
var contact = contact if var contact = try? session.dbManager.fetchContacts(.init(id: [contact.id])).first {
contact.nickname = nickname contact.nickname = nickname
_ = try? session.dbManager.saveContact(contact)
backgroundScheduler.schedule { [weak self] in
guard let self = self else { return }
_ = try? self.session.dbManager.saveContact(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