Skip to content
Snippets Groups Projects
SearchLeftViewModel.swift 10.7 KiB
Newer Older
import HUD
Bruno Muniz's avatar
Bruno Muniz committed
import Retry
import UIKit
Bruno Muniz's avatar
Bruno Muniz committed
import Models
Bruno Muniz's avatar
Bruno Muniz committed
import Shared
import Combine
import XXModels
import XXClient
import Countries
import CustomDump
import ReportingFeature
import CombineSchedulers
Bruno Muniz's avatar
Bruno Muniz committed
import XXMessengerClient
Bruno Muniz's avatar
Bruno Muniz committed
import DependencyInjection

typealias SearchSnapshot = NSDiffableDataSourceSnapshot<SearchSection, SearchItem>

struct SearchLeftViewState {
    var input = ""
    var snapshot: SearchSnapshot?
    var country: Country = .fromMyPhone()
    var item: SearchSegmentedControl.Item = .username
final class SearchLeftViewModel {
    @Dependency var database: Database
Bruno Muniz's avatar
Bruno Muniz committed
    @Dependency var messenger: Messenger
    @Dependency var reportingStatus: ReportingStatus
    @Dependency var networkMonitor: NetworkMonitoring

    @KeyObject(.username, defaultValue: nil) var username: String?

    var myId: Data {
Bruno Muniz's avatar
Bruno Muniz committed
        try! messenger.e2e.get()!.getContact().getId()
    var hudPublisher: AnyPublisher<HUDStatus, Never> {
        hudSubject.eraseToAnyPublisher()
    }

Bruno Muniz's avatar
Bruno Muniz committed
    var successPublisher: AnyPublisher<XXModels.Contact, Never> {
        successSubject.eraseToAnyPublisher()
    }

    var statePublisher: AnyPublisher<SearchLeftViewState, Never> {
        stateSubject.eraseToAnyPublisher()
    }

    var backgroundScheduler: AnySchedulerOf<DispatchQueue> = DispatchQueue.global().eraseToAnyScheduler()

    private var invitation: String?
    private var searchCancellables = Set<AnyCancellable>()
Bruno Muniz's avatar
Bruno Muniz committed
    private let successSubject = PassthroughSubject<XXModels.Contact, Never>()
    private let hudSubject = CurrentValueSubject<HUDStatus, Never>(.none)
    private let stateSubject = CurrentValueSubject<SearchLeftViewState, Never>(.init())
    private var networkCancellable = Set<AnyCancellable>()

    init(_ invitation: String? = nil) {
        self.invitation = invitation
    }

    func viewDidAppear() {
        if let pendingInvitation = invitation {
            invitation = nil
            stateSubject.value.input = pendingInvitation
            hudSubject.send(.onAction(Localized.Ud.Search.cancel))

Bruno Muniz's avatar
Bruno Muniz committed
            networkCancellable.removeAll()

Bruno Muniz's avatar
Bruno Muniz committed
//            networkMonitor.statusPublisher
//                .first { $0 == .available }
//                .eraseToAnyPublisher()
//                .flatMap { _ in self.session.waitForNodes(timeout: 5) }
//                .sink {
//                    if case .failure(let error) = $0 {
//                        self.hudSubject.send(.error(.init(with: error)))
//                    }
//                } receiveValue: {
//                    self.didStartSearching()
//                }.store(in: &networkCancellable)

    func didEnterInput(_ string: String) {
        stateSubject.value.input = string
    }

    func didPick(country: Country) {
        stateSubject.value.country = country
    }

    func didSelectItem(_ item: SearchSegmentedControl.Item) {
        stateSubject.value.item = item
    }

    func didTapCancelSearch() {
        searchCancellables.forEach { $0.cancel() }
        searchCancellables.removeAll()
        hudSubject.send(.none)
    }

    func didStartSearching() {
Bruno Muniz's avatar
Bruno Muniz committed
        guard stateSubject.value.input.isEmpty == false else { return }

Bruno Muniz's avatar
Bruno Muniz committed
        hudSubject.send(.onAction(Localized.Ud.Search.cancel))
        var content = stateSubject.value.input
        if stateSubject.value.item == .phone {
            content += stateSubject.value.country.code
        }

Bruno Muniz's avatar
Bruno Muniz committed
        enum NodeRegistrationError: Error {
            case unhealthyNet
            case belowMinimum
        }

        retry(max: 5, retryStrategy: .delay(seconds: 2)) { [weak self] in
            guard let self = self else { return }

            do {
                let nrr = try self.messenger.cMix.get()!.getNodeRegistrationStatus()
                if nrr.ratio < 0.8 { throw NodeRegistrationError.belowMinimum }
            } catch {
                throw NodeRegistrationError.unhealthyNet
            }
        }.finalCatch { [weak self] in
            guard let self = self else { return }

            if case .unhealthyNet = $0 as? NodeRegistrationError {
                self.hudSubject.send(.error(.init(content: "Network is not healthy yet, try again within the next minute or so.")))
            } else if case .belowMinimum = $0 as? NodeRegistrationError {
                self.hudSubject.send(.error(.init(content: "Node registration ratio is still below 80%, try again within the next minute or so.")))
            } else {
                self.hudSubject.send(.error(.init(with: $0)))
            }

            return
        }
Bruno Muniz's avatar
Bruno Muniz committed

        backgroundScheduler.schedule { [weak self] in
            guard let self = self else { return }

            do {
                let report = try SearchUD.live(
Bruno Muniz's avatar
Bruno Muniz committed
                    e2eId: self.messenger.e2e.get()!.getId(),
                    udContact: self.messenger.ud.get()!.getContact(),
Bruno Muniz's avatar
Bruno Muniz committed
                    facts: [Fact(fact: content, type: self.stateSubject.value.item.rawValue)],
                    callback: .init(handle: {
                        switch $0 {
Bruno Muniz's avatar
Bruno Muniz committed
                        case .success(let results):
Bruno Muniz's avatar
Bruno Muniz committed
                             self.hudSubject.send(.none)
Bruno Muniz's avatar
Bruno Muniz committed
                             self.appendToLocalSearch(
                                XXModels.Contact(
                                    id: try! results.first!.getId(),
                                    marshaled: results.first!.data,
                                    username: try! results.first?.getFacts().first(where: { $0.type == FactType.username.rawValue })?.fact,
                                    email: try? results.first?.getFacts().first(where: { $0.type == FactType.email.rawValue })?.fact,
                                    phone: try? results.first?.getFacts().first(where: { $0.type == FactType.phone.rawValue })?.fact,
                                    nickname: nil,
                                    photo: nil,
                                    authStatus: .stranger,
Bruno Muniz's avatar
Bruno Muniz committed
                                    isRecent: true,
Bruno Muniz's avatar
Bruno Muniz committed
                                    isBlocked: false,
                                    isBanned: false,
                                    createdAt: Date()
                                )
                             )

                        case .failure(let error):
Bruno Muniz's avatar
Bruno Muniz committed
                            print("^^^ searchUD error: \(error.localizedDescription)")
                            self.appendToLocalSearch(nil)
                            self.hudSubject.send(.error(.init(with: error)))
                        }
                    })
                )

Bruno Muniz's avatar
Bruno Muniz committed
                print("^^^ report: \(report))")
Bruno Muniz's avatar
Bruno Muniz committed
                print("^^^ exception: \(error.localizedDescription)")
Bruno Muniz's avatar
Bruno Muniz committed
        }
Bruno Muniz's avatar
Bruno Muniz committed
    func didTapResend(contact: XXModels.Contact) {
Bruno Muniz's avatar
Bruno Muniz committed
        hudSubject.send(.on)

        var contact = contact
        contact.authStatus = .requesting

        backgroundScheduler.schedule { [weak self] in
            guard let self = self else { return }

            do {
                try self.database.saveContact(contact)

Bruno Muniz's avatar
Bruno Muniz committed
                var myFacts = try self.messenger.ud.get()!.getFacts()
                myFacts.append(Fact(fact: self.username!, type: FactType.username.rawValue))

Bruno Muniz's avatar
Bruno Muniz committed
                let _ = try self.messenger.e2e.get()!.requestAuthenticatedChannel(
Bruno Muniz's avatar
Bruno Muniz committed
                    partner: .live(contact.marshaled!),
                    myFacts: myFacts
                )

                contact.authStatus = .requested
                contact = try self.database.saveContact(contact)

                self.hudSubject.send(.none)
            } catch {
                contact.authStatus = .requestFailed
                _ = try? self.database.saveContact(contact)
                self.hudSubject.send(.error(.init(with: error)))
            }
Bruno Muniz's avatar
Bruno Muniz committed
    func didTapRequest(contact: XXModels.Contact) {
Bruno Muniz's avatar
Bruno Muniz committed
        hudSubject.send(.on)
        var contact = contact
        contact.nickname = contact.username
        contact.authStatus = .requesting

        backgroundScheduler.schedule { [weak self] in
            guard let self = self else { return }
            do {
                try self.database.saveContact(contact)

Bruno Muniz's avatar
Bruno Muniz committed
                var myFacts = try self.messenger.ud.get()!.getFacts()
                myFacts.append(Fact(fact: self.username!, type: FactType.username.rawValue))

Bruno Muniz's avatar
Bruno Muniz committed
                let _ = try self.messenger.e2e.get()!.requestAuthenticatedChannel(
Bruno Muniz's avatar
Bruno Muniz committed
                    partner: .live(contact.marshaled!),
                    myFacts: myFacts
                )

                contact.authStatus = .requested
                contact = try self.database.saveContact(contact)

                self.hudSubject.send(.none)
                self.successSubject.send(contact)
            } catch {
                contact.authStatus = .requestFailed
                _ = try? self.database.saveContact(contact)
                self.hudSubject.send(.error(.init(with: error)))
            }
Bruno Muniz's avatar
Bruno Muniz committed
    func didSet(nickname: String, for contact: XXModels.Contact) {
        if var contact = try? database.fetchContacts(.init(id: [contact.id])).first {
            contact.nickname = nickname
            _ = try? database.saveContact(contact)
Bruno Muniz's avatar
Bruno Muniz committed
    private func appendToLocalSearch(_ user: XXModels.Contact?) {
        var snapshot = SearchSnapshot()

        if var user = user {
            if let contact = try? database.fetchContacts(.init(id: [user.id])).first {
                user.isBanned = contact.isBanned
                user.isBlocked = contact.isBlocked
                user.authStatus = contact.authStatus
            }

            if user.authStatus != .friend, !reportingStatus.isEnabled() {
                snapshot.appendSections([.stranger])
                snapshot.appendItems([.stranger(user)], toSection: .stranger)
            } else if user.authStatus != .friend, reportingStatus.isEnabled(), !user.isBanned, !user.isBlocked {
                snapshot.appendSections([.stranger])
                snapshot.appendItems([.stranger(user)], toSection: .stranger)
            }
Bruno Muniz's avatar
Bruno Muniz committed
        let localsQuery = Contact.Query(
            text: stateSubject.value.input,
            authStatus: [.friend],
            isBlocked: reportingStatus.isEnabled() ? false : nil,
            isBanned: reportingStatus.isEnabled() ? false : nil
Bruno Muniz's avatar
Bruno Muniz committed
        )
        if let locals = try? database.fetchContacts(localsQuery),
Bruno Muniz's avatar
Bruno Muniz committed
           let localsWithoutMe = removeMyself(from: locals),
           localsWithoutMe.isEmpty == false {
            snapshot.appendSections([.connections])
Bruno Muniz's avatar
Bruno Muniz committed
            snapshot.appendItems(
                localsWithoutMe.map(SearchItem.connection),
                toSection: .connections
            )
        }

        stateSubject.value.snapshot = snapshot
    }
Bruno Muniz's avatar
Bruno Muniz committed
    private func removeMyself(from collection: [XXModels.Contact]) -> [XXModels.Contact]? {
        collection.filter { $0.id != myId }