<commit_before>//
//  SpotlightClient.swift
//  Rocket.Chat
//
//  Created by <NAME> on 3/28/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import RealmSwift

struct SpotlightClient: APIClient {
    let api: AnyAPIFetcher
    init(api: AnyAPIFetcher) {
        self.api = api
    }

    func search(query: String, realm: Realm? = Realm.current, completion: @escaping RequestCompletion) {
        api.fetch(SpotlightRequest(query: query)) { response in
            switch response {
            case .resource(let resource):
                guard resource.success else {
                    completion(nil, true)
                    return
                }

                realm?.execute({ (realm) in
                    var subscriptions: [Subscription] = []

                    resource.rooms.forEach { object in
                        if let roomIdentifier = object["_id"].string {
                            if let subscription = Subscription.find(rid: roomIdentifier, realm: realm) {
                                subscription.map(object, realm: realm)
                                subscription.mapRoom(object, realm: realm)
                                subscriptions.append(subscription)
                            }
                        }
                    }

                    resource.users.forEach { object in<commit_msg>Create a subscription from a room when it doesn't exists on our local database<commit_after>//
//  SpotlightClient.swift
//  Rocket.Chat
//
//  Created by <NAME> on 3/28/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import RealmSwift

struct SpotlightClient: APIClient {
    let api: AnyAPIFetcher
    init(api: AnyAPIFetcher) {
        self.api = api
    }

    func search(query: String, realm: Realm? = Realm.current, completion: @escaping RequestCompletion) {
        api.fetch(SpotlightRequest(query: query)) { response in
            switch response {
            case .resource(let resource):
                guard resource.success else {
                    completion(nil, true)
                    return
                }

                realm?.execute({ (realm) in
                    var subscriptions: [Subscription] = []

                    resource.rooms.forEach { object in
                        if let roomIdentifier = object["_id"].string {
                            if let subscription = Subscription.find(rid: roomIdentifier, realm: realm) {
                                subscription.map(object, realm: realm)
                                subscription.mapRoom(object, realm: realm)
                                subscriptions.append(subscription)
                            } else {
                                let subscription = Subscription()
                                subscription.identifier = String.random()
                                subscription.rid = roomIdentifier
                                subscription.name = object["name"].string ?? ""

                                if let typeRaw = object["t"].string, let type = SubscriptionType(rawValue: typeRaw) {
                                    subscription.type = type
                                }

                                subscriptions.append(subscription)
                            }
                        }
                    }

                    resource.users.forEach { object in
{'fraction_non_alphanumeric': 0.08108108108108109, 'fraction_numerical': 0.005287896592244419, 'mean_word_length': 1.266977363515313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5704720', 'n_tokens_mistral': 811, 'n_tokens_neox': 760, 'n_words': 195}