Skip to content
Snippets Groups Projects
Bindings.swift 18.9 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
import Shared
import Models
import Bindings
import DependencyInjection
import Foundation

public let evaluateNotification: NotificationEvaluation = BindingsNotificationsForMe

public protocol NotificationReportProtocol {
    func forMe() -> Bool
    func type() -> String
}

public protocol NotificationManyReportProtocol {
    func len() -> Int
    func get(index: Int) throws -> NotificationReportProtocol
}

extension BindingsNotificationForMeReport: NotificationReportProtocol {}

extension BindingsManyNotificationForMeReport: NotificationManyReportProtocol {
    public func get(index: Int) throws -> NotificationReportProtocol {
        try get(index)
    }
}

extension BindingsClient: BindingsInterface {
    public func removeContact(_ data: Data) throws {
        do {
            try deleteContact(data)
            log(string: "Deleted a contact", type: .info)
        } catch {
            log(string: "Failed to delete a contact: \(error.localizedDescription)", type: .error)
            throw error.friendly()
        }
    }

    func dumpThreads() {
        log(type: .crumbs)

        var error: NSError?
        let string = BindingsDumpStack(&error)

        if let error = error {
            log(string: error.localizedDescription, type: .error)
            return
        }

        log(string: string, type: .bindings)
    }

    public func verify(marshaled: Data, verifiedMarshaled: Data) throws -> Bool {
        var bool: ObjCBool = false
        try verifyOwnership(marshaled, verifiedMarshaled: verifiedMarshaled, ret0_: &bool)
        log(string: "Onwership verification: \(bool.boolValue)", type: bool.boolValue ? .info : .error)
        return bool.boolValue
    }

    public func compress(
        image: Data,
        _ completion: @escaping(Result<Data, Error>) -> Void
    ) {
        var error: NSError?
        let compressed = BindingsCompressJpeg(image, &error)

        guard error == nil else {
            log(string: "Error when compressing jpeg: \(error!.localizedDescription)", type: .error)
            completion(.failure(error!.friendly()))
            return
        }

        guard let compressed = compressed else {
            let extensiveLog =
            """
            #### Important: No error was written but CompressJpeg() returned nothing.
            - Params:
            -- imageData = \(image.base64EncodedString())
            - Error description: \(error!.localizedDescription)
            """
            log(string: extensiveLog, type: .error)
            log(string: "An image compression failed without any specific error", type: .error)
            completion(.failure(NSError.create("Image compression failed without error")))
            return
        }

        let compressionRate = String(format: "%.4f", Float(compressed.count)/Float(image.count))
        log(string: "Compressed image x\(compressionRate) (\(image.count) -> \(compressed.count))", type: .info)
        completion(.success(compressed))
    }

    public var hasRunningTasks: Bool {
        hasRunningProcessies()
    }

    public var myId: Data {
        guard let user = getUser(), let contact = user.getContact(), let id = contact.getID() else {
            fatalError("Couldn't get my ID")
        }

        return id
    }

    public var meMarshalled: Data {
        guard let user = getUser(), let contact = user.getContact(), let marshal = try? contact.marshal() else {
            fatalError("Couldn't get my own contact marshalled")
        }

        return marshal
    }

    public func getPreImages() -> String {
        getPreimages(receptionId)
    }

    public func meMarshalled(_ username: String, email: String?, phone: String?) -> Data {
        guard let user = getUser(), let contact = user.getContact(), let factList = contact.getFactList() else {
            let extensiveLog =
            """
            #### Important: It was impossible to get the fact list of my own contact.
            - getUser was nil? = \(getUser() == nil)
            - getContact was nil? = \(getUser()?.getContact() == nil)
            """
            log(string: extensiveLog, type: .error)
            dumpThreads()
            fatalError(extensiveLog)
        }

        do {
            try factList.add(username, factType: FactType.username.rawValue)
        } catch {
            log(string: error.localizedDescription, type: .error)
            dumpThreads()
            fatalError()
        }

        if let email = email {
            do {
                try factList.add(email, factType: FactType.email.rawValue)
            } catch {
                log(string: error.localizedDescription, type: .error)
                dumpThreads()
                fatalError()
            }
        }

        if let phone = phone {
            do {
                try factList.add(phone, factType: FactType.phone.rawValue)
            } catch {
                log(string: error.localizedDescription, type: .error)
                dumpThreads()
                fatalError()
            }
        }

        do {
            return try contact.marshal()
        } catch {
            log(string: error.localizedDescription, type: .error)
            dumpThreads()
            fatalError()
        }
    }

    public var receptionId: Data {
        guard let user = getUser(), let recId = user.getReceptionID() else {
            let extendedLog =
            """
            #### Important: It was impossible to get my own reception id.
            - getUser was nil? = \(getUser() == nil)
            """
            log(string: extendedLog, type: .error)
            dumpThreads()
            fatalError(extendedLog)
        }

        return recId
    }

    public static let version: String = {
        return BindingsGetVersion()
    }()

    public static let secret: (Int) -> Data? = BindingsGenerateSecret

    public static let login: (String?, Data?, String?, NSErrorPointer) -> BindingsInterface? = BindingsLogin

    public static let newClient: (String?, String?, Data?, String?, NSErrorPointer) -> Bool = BindingsNewClient

    public static func updateNDF(
        for env: NetworkEnvironment,
        _ completion: @escaping (Result<Data?, Error>) -> Void
    ) {
        log(type: .crumbs)

        var error: NSError?
        let ndf = BindingsDownloadAndVerifySignedNdfWithUrl(env.url, env.cert, &error)

        if let error = error {
            log(string: error.localizedDescription, type: .error)
            completion(.failure(error))
        } else {
            completion(.success(ndf))
        }
    }

    /// Fetches a JSON with up-to-date error descriptions
    /// then passes it to the bindings that will emit cleaner
    /// errors
    ///
    /// - ToDo: Request status codes for errors
    ///
    public static func updateErrors() {
        log(type: .crumbs)

        var error: NSError?
        if let dbErrors = BindingsDownloadErrorDB(&error) {
            var otherError: NSError?
            BindingsUpdateCommonErrors(String(data: dbErrors, encoding: .utf8), &otherError)

            if let otherError = otherError {
                log(string: otherError.localizedDescription, type: .error)
            }
        }

        if let error = error {
            log(string: error.localizedDescription, type: .error)
        }
    }

    /// Starts the network
    ///
    /// If network status is != 0 it means the network is
    /// not ready yet or the device is not ready. A recursion was applied
    /// as a temporary solution in order to retry indefinitely
    ///
    /// - ToDo: Split function into smaller functions
    ///
    public func startNetwork() {
        log(type: .crumbs)

        var error: NSError?
        let status = networkFollowerStatus()

        BindingsLogLevel(1, &error)
        registerErrorCallback(BindingsError())

        guard status == 0 else {
            log(string: "Network is not ready yet. Let's give it a second...", type: .error)
            sleep(1)
            startNetwork()
            return
        }

        try! startNetworkFollower(10000)
        log(string: "Starting the network...", type: .info)
    }

    /// (Tries) to stop the network
    ///
    /// - Warning: This function tries to stop several
    ///            threads and it may take some time.
    ///            That's why we register a background
    ///            task on AppDelegate.swift
    ///
    public func stopNetwork() {
        log(type: .crumbs)

        try! stopNetworkFollower()
        log(string: "Stopping the network...", type: .info)
    }

    /// Extracts *user id* from a contact
    ///
    /// - Parameters:
    ///   - from: Byte array containing contact object
    ///
    /// - Returns: Optional byte array, if *user id* could be retrieved
    ///
    public func getId(from marshaled: Data) -> Data? {
        log(type: .crumbs)

        var error: NSError?
        let contact = BindingsUnmarshalContact(marshaled, &error)

        if let error = error {
            log(string: error.localizedDescription, type: .error)
            return nil
        }

        return contact?.getID()
    }

    public func add(_ contact: Data, from me: Data, _ completion: @escaping (Result<Bool, Error>) -> Void) {
        log(type: .crumbs)

        do {
            var roundId = Int()
            try requestAuthenticatedChannel(contact, meMarshaled: me, message: nil, ret0_: &roundId)
            completion(.success(true))
        } catch {
            log(string: error.localizedDescription, type: .error)
            completion(.failure(error.friendly()))
        }
    }

    /// Confirms a contact request
    ///
    /// - Parameters:
    ///   - contact: Byte array containing *contact object*
    ///   - completion: Result callback with associated
    ///                 values *boolean* = success &&
    ///                 !timedOut or *Error* upon throwing
    ///
    public func confirm(_ contact: Data, _ completion: @escaping (Result<Bool, Error>) -> Void) {
        log(type: .crumbs)

        do {
            var roundId = Int()
            try confirmAuthenticatedChannel(contact, ret0_: &roundId)
            completion(.success(true))
        } catch {
            log(string: error.localizedDescription, type: .error)
            completion(.failure(error.friendly()))
        }
    }

    /// Sends a message over CMIX
    ///
    /// - Parameters:
    ///   - recipient: Byte array containing *user id*
    ///   - payload: Byte array containing *message payload*
    ///
    /// - Returns: Result w/ associated values
    ///            byte array containing *SentReport*
    ///            or *Error* upon throwing
    ///
    public func send(_ payload: Data, to recipient: Data) -> Result<E2ESendReportType, Error> {
        log(type: .crumbs)

        do {
            let report = try sendE2E(recipient, payload: payload, messageType: 2, parameters: nil)

            var roundIds = [Int]()

            if let roundList = report.getRoundList(), let payloadUnwrapped = try? Payload(with: payload) {
                let length = roundList.len()
                for index in 0..<length {
                    var integer: Int = 0
                    do {
                        try roundList.get(index, ret0_: &integer)
                        roundIds.append(integer)
                    } catch {
                        log(string: "Error trying to inspect round list: \(error.localizedDescription)", type: .error)
                    }
                }

                log(string: "Round ids for \(payloadUnwrapped.text.prefix(5))... = \(roundIds)", type: .info)
            }

            return .success(report)
        } catch {
            log(string: error.localizedDescription, type: .error)
            return .failure(error)
        }
    }

    /// Listens to the delivery of a message through a report
    ///
    /// - Note: Delivery actually refers to the
    ///         gateway, not necessarily the other end
    ///         received/read this message yet.
    ///
    /// - Parameters:
    ///   - report: SentReport marshalled
    ///   - completion: Result callback w/ associated
    ///                 values *completed* or *Error*
    ///                 upon throwing
    ///
    public func listen(report: Data, _ completion: @escaping (Result<Bool, Error>) -> Void) {
        do {
            try listenDelivery(of: report) { msgId, delivered, timedOut, roundResults in
                if delivered == false {
                    let extendedLogs =
                    """
                    Round delivery callback from wait(forMessageDelivery:)
                    - timedOut = \(timedOut)
                    - delivered = \(delivered)
                    """
                    log(string: extendedLogs, type: .error)
                    log(string: extendedLogs, type: .error)
                }

                completion(.success(delivered))
            }
        } catch {
            completion(.failure(error))
        }
    }

    /// Registers device token on backend for push notifications
    ///
    /// - Parameters:
    ///   - string: Device token provided by APNS
    ///
    /// - Throws: If an exception was raised on
    ///           backend such as timing out
    ///
    public func registerNotifications(_ token: String) throws {
        log(type: .crumbs)

        do {
            try register(forNotifications: token)
            log(string: "Registered for notifications using token: \(token)", type: .info)
        } catch {
            log(string: error.localizedDescription, type: .error)
            throw error.friendly()
        }
    }

    /// Unregisters device token on backend
    ///
    /// - Throws: If when trying to unregister
    ///           some exception come up such as
    ///           timing out or user is not registered
    ///
    public func unregisterNotifications() throws {
        log(type: .crumbs)

        do {
            try unregisterForNotifications()
            log(string: "Unregistered notifications", type: .info)
        } catch {
            log(string: error.localizedDescription, type: .error)
            throw error.friendly()
        }
    }

    /// Checks if number of nodes already registered is enough
    ///
    /// Whenever the user wants to do an operation that involves
    /// *User Discovery*, the app should make sure that a minimum
    /// amount of nodes already know about this user
    ///
    /// - Throws: `NodeRegistrationError.amountIsTooLow` if
    ///            the ratio is below minimum (currently 85%).
    ///            `NodeRegistrationError.networkIsNotHealthyYet`
    ///            when trying to fetch registration status and
    ///            network is not healthy yet
    ///
    public func nodeRegistrationStatus() throws {
        log(type: .crumbs)

        enum NodeRegistrationError: Error {
            case amountIsTooLow
        }

        var shortRatio: String?

        do {
            let status = try getNodeRegistrationStatus()
            let registered = Float(status.getRegistered())
            let total = Float(status.getTotal())
            let ratio = Float(registered/total)

            let nf = NumberFormatter()
            nf.roundingMode = .down
            nf.maximumFractionDigits = 2
            nf.numberStyle = .percent
            shortRatio = nf.string(from: NSNumber(value: ratio))

            guard ratio >= 0.85 else { throw NodeRegistrationError.amountIsTooLow }
            log(string: "Node registration rate: \(shortRatio ?? "")", type: .info)
        } catch NodeRegistrationError.amountIsTooLow {

            let string = "Node registration rate is still below 85% (\(shortRatio ?? ""))"
            log(string: string, type: .error)

            let userError = "We are still establishing a secure registration with the decentralized network. Please try again in a few seconds."

            throw NSError.create(userError)
        } catch {
            log(string: error.localizedDescription, type: .error)
            throw error
        }
    }

    /// Instantiates a transfer manager
    ///
    /// - Returns: An instance of *BindingsFileTransfer (TransferManager)*
    ///
    /// - Throws: `FTError.noInstance` if no error was thrown
    ///            but also no instance was created
    ///
    public func generateTransferManager(
        _ callback: @escaping (Data, String?, String?, Data?) -> Void
    ) throws -> TransferManagerInterface {
        log(type: .crumbs)

        let incomingTransferCallback = IncomingTransferCallback { tid, name, type, sender, size, preview in
            guard let tid = tid else { fatalError("An incoming transfer has no TID?") }

            callback(tid, name, type, sender)
        }

        var error: NSError?
        let manager = BindingsNewFileTransferManager(self, incomingTransferCallback, "", &error)

        guard let error = error else { return manager! }
        throw error.friendly()
    }

    public func generateDummyTraficManager() throws -> DummyTrafficManaging {
        var error: NSError?
        let manager = BindingsNewDummyTrafficManager(self, 5, 30000, 25000, &error)

        guard let error = error else { return manager! }
        throw error.friendly()
    }

    /// Instantiates user discovery
    ///
    /// - Returns: An instance of *UD (User discovery)*
    ///
    /// - Throws: `UDError.noInstance` if no error was thrown
    ///            but also no instance was created
    ///
    public func generateUD() throws -> UserDiscoveryInterface {
        log(type: .crumbs)

        var error: NSError?
        let udb = BindingsNewUserDiscovery(self, &error)

        guard let error = error else { return udb! }
        throw error.friendly()
    }
}

extension BindingsContact {

    /// Scans the contact instance for a specified fact
    ///
    /// - Parameters:
    ///   - fact: enum defined in ```FactType```
    ///           that specifies the type we're
    ///           searching
    ///
    /// - Note: Since GoLang does not support collections
    ///         We need to do this workaround *length* and
    ///         *get* instead of subscripting as in Swift.
    ///
    /// - Returns: Optional string in case we find the the fact
    ///
    /// - ToDo: Return a struct that contains all possible facts (?)
    ///
    func retrieve(fact: FactType) -> String? {
        log(type: .crumbs)

        guard let factList = getFactList() else { return nil }
        for index in 0..<factList.num() {
            if let actualFact = factList.get(index) {
                if actualFact.type() == fact.rawValue {
                    return String(actualFact.stringify().dropFirst())
                }
            }
        }
        return nil
    }
}

extension BindingsSendReport: E2ESendReportType {
    public var marshalled: Data { try! marshal() }
    public var timestamp: Int64 { getTimestampNano() }
    public var uniqueId: Data? { getMessageID() }
    public var roundURL: String { getRoundURL() }
}


public protocol DummyTrafficManaging {
    var status: Bool { get }
    func setStatus(status: Bool)
}

extension BindingsDummyTraffic: DummyTrafficManaging {
    public var status: Bool {
        getStatus()
    }

    public func setStatus(status: Bool) {
        try? setStatus(status)
    }
}