ContactRequestModel.swift 2.51 KB
Newer Older
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
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import Contacts
import SwiftyBeaver

class ContactRequestModel {

    let ringId: String
    let accountId: String
28
    var vCard: CNContact? = nil
29
    var receivedDate: Date
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    enum ContactRequestKey: String {
        case from
        case payload
        case received
    }

    private let log = SwiftyBeaver.self

    init(withRingId ringId: String, vCard: CNContact?, receivedDate: Date, accountId: String) {
        self.ringId = ringId
        self.vCard = vCard
        self.receivedDate = receivedDate
        self.accountId = accountId
    }

46
    init(withDictionary dictionary: [String: String], accountId: String) {
47 48 49 50 51 52 53 54

        if let ringId = dictionary[ContactRequestKey.from.rawValue] {
            self.ringId = ringId
        } else {
            self.ringId = ""
        }

        if let vCardString = dictionary[ContactRequestKey.payload.rawValue] {
55 56 57 58 59 60 61 62 63 64
            if let data = vCardString.data(using: String.Encoding.utf8), !data.isEmpty {
                do {
                    let vCards = try CNContactVCardSerialization.contacts(with: data)
                    if let contactVCard = vCards.first {
                        self.vCard = contactVCard
                    }
                } catch {
                    log.error("Unable to serialize the vCard : \(error)")
                    self.vCard = CNContact()
                }
65 66 67 68 69 70 71 72 73 74 75 76 77
            }
        }

        if let receivedDateString = dictionary[ContactRequestKey.received.rawValue] {
            let timestamp = Double(receivedDateString)
            self.receivedDate = Date(timeIntervalSince1970: timestamp!)
        } else {
            self.receivedDate = Date()
        }

        self.accountId = accountId
    }
}