Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Shared
7
import Storage
8
import SwiftyJSON
9
10
open class HistoryPayload: CleartextPayloadJSON {
11
open class func fromJSON(_ json: JSON) -> HistoryPayload? {
12
let p = HistoryPayload(json)
13
if p.isValid() {
14
return p
15
}
16
return nil
17
}
18
19
override open func isValid() -> Bool {
20
if !super.isValid() {
21
return false
22
}
23
24
if self["deleted"].bool ?? false {
25
return true
26
}
27
28
return self["histUri"].string != nil && // TODO: validate URI.
29
self["title"].isStringOrNull() &&
30
self["visits"].isArray()
31
}
32
33
open func asPlace() -> Place {
34
return Place(guid: self.id, url: self.histURI, title: self.title)
35
}
36
37
var visits: [Visit] {
38
let visits = self["visits"].arrayObject as! [[String: Any]]
39
return optFilter(visits.map(Visit.fromJSON))
40
}
41
42
fileprivate var histURI: String {
43
return self["histUri"].string!
44
}
45
46
var historyURI: URL {
47
return self.histURI.asURL!
48
}
49
50
var title: String {
51
return self["title"].string ?? ""
52
}
53
54
override open func equalPayloads(_ obj: CleartextPayloadJSON) -> Bool {
55
if let p = obj as? HistoryPayload {
56
if !super.equalPayloads(p) {
57
return false
58
}
59
60
if p.deleted {
61
return self.deleted == p.deleted
62
}
63
64
// If either record is deleted, these other fields might be missing.
65
// But we just checked, so we're good to roll on.
66
67
if p.title != self.title {
68
return false
69
}
70
71
if p.historyURI != self.historyURI {
72
return false
73
}
74
75
// TODO: compare visits.
76
77
return true
78
}
79
80
return false
81
}
82
}