Skip to content

Commit

Permalink
Fix brave#448, add syncTimestamp to Bookmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub committed Jan 14, 2019
1 parent 98ba92b commit 2a49f27
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Data/models/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public final class Bookmark: NSManagedObject, WebsitePresentable, Syncable, CRUD

override public func awakeFromInsert() {
super.awakeFromInsert()
created = Date()
lastVisited = created
}

Expand Down Expand Up @@ -100,6 +99,7 @@ public final class Bookmark: NSManagedObject, WebsitePresentable, Syncable, CRUD
update(customTitle: site.customTitle, url: site.location, newSyncOrder: bookmark.syncOrder)
lastVisited = Date(timeIntervalSince1970: (Double(site.lastAccessedTime ?? 0) / 1000.0))
syncParentUUID = bookmark.parentFolderObjectId
created = record?.syncNativeTimestamp
// No auto-save, must be handled by caller if desired
}

Expand Down Expand Up @@ -196,6 +196,7 @@ public final class Bookmark: NSManagedObject, WebsitePresentable, Syncable, CRUD
bk.isFavorite = bookmark?.isFavorite ?? bk.isFavorite
bk.isFolder = bookmark?.isFolder ?? bk.isFolder
bk.syncUUID = root?.objectId ?? bk.syncUUID ?? SyncCrypto.uniqueSerialBytes(count: 16)
bk.created = root?.syncNativeTimestamp ?? Date()

if let location = site?.location, let url = URL(string: location) {
bk.domain = Domain.getOrCreateForUrl(url, context: context, save: false)
Expand Down
1 change: 1 addition & 0 deletions Data/models/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public final class Device: NSManagedObject, Syncable, CRUD {
guard let root = record as? SyncDevice else { return }
self.name = root.name
self.deviceId = root.deviceId
created = record?.syncNativeTimestamp

// No save currently
}
Expand Down
11 changes: 1 addition & 10 deletions Data/sync/SyncDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ class SyncDevice: SyncRecord {
// MARK: Declaration for string constants to be used to decode and also serialize.
private struct SerializationKeys {
static let name = "name"
static let syncTimestamp = "syncTimestamp"
}

// MARK: Properties
var name: String?
// Not on 'device' object
var syncTimestamp: Int?

var syncNativeTimestamp: Date? {
return Date.fromTimestamp(Timestamp(syncTimestamp ?? 0))
}

required init(record: Syncable?, deviceId: [Int]?, action: Int?) {
super.init(record: record, deviceId: deviceId, action: action)
Expand All @@ -35,8 +28,7 @@ class SyncDevice: SyncRecord {
super.init(json: json)

self.name = json?[SyncObjectDataType.Device.rawValue][SerializationKeys.name].string
self.syncTimestamp = json?[SerializationKeys.syncTimestamp].int


// Preference
self.objectData = nil
}
Expand All @@ -54,7 +46,6 @@ class SyncDevice: SyncRecord {

var dictionary = super.dictionaryRepresentation()
dictionary[SyncObjectDataType.Device.rawValue] = deviceDict
if let value = self.syncTimestamp { dictionary[SerializationKeys.syncTimestamp] = value }

return dictionary
}
Expand Down
22 changes: 18 additions & 4 deletions Data/sync/SyncRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Shared
import CoreData
import SwiftyJSON

private let log = Logger.browserLogger

protocol SyncRecordProtocol {
associatedtype CoreDataParallel: Syncable
// var CoredataParallel: NSManagedObject.Type?
Expand All @@ -19,7 +21,7 @@ public class SyncRecord: SyncRecordProtocol {
static let deviceId = "deviceId"
static let action = "action"
static let objectData = "objectData"
// TODO: Add sync timestamp
static let syncTimestamp = "syncTimestamp"
}

// MARK: Properties
Expand All @@ -28,13 +30,22 @@ public class SyncRecord: SyncRecordProtocol {
var action: Int?
var objectData: SyncObjectDataType?

var syncTimestamp: Int?

// var CoredataParallel: Syncable.Type?
typealias CoreDataParallel = Device

convenience init() {
self.init(json: nil)
}

/// Converts server format for storing timestamp(integer) to Date
var syncNativeTimestamp: Date? {
guard let syncTimestamp = syncTimestamp else { return nil }

return Date.fromTimestamp(Timestamp(syncTimestamp))
}

/// Initiates the instance based on the object.
///
/// - parameter object: The object of either Dictionary or Array kind that was passed.
Expand All @@ -56,7 +67,10 @@ public class SyncRecord: SyncRecordProtocol {

// TODO: Need object type!!

// TOOD: Add sync timestmap
// Initially, a record should have timestamp set to now.
// It should then be updated from resolved-sync-records callback.
let timeStamp = (record?.created ?? Date()).timeIntervalSince1970
syncTimestamp = Int(timeStamp)
}

/// Initiates the instance based on the JSON that was passed.
Expand All @@ -68,7 +82,7 @@ public class SyncRecord: SyncRecordProtocol {
if let items = json?[SerializationKeys.deviceId].array { deviceId = items.map { $0.intValue } }
action = json?[SerializationKeys.action].int
if let item = json?[SerializationKeys.objectData].string { objectData = SyncObjectDataType(rawValue: item) }
// TODO: Add sync timestamp
self.syncTimestamp = json?[SerializationKeys.syncTimestamp].int
}

/// Generates description of the object in the form of a NSDictionary.
Expand All @@ -81,7 +95,7 @@ public class SyncRecord: SyncRecordProtocol {
if let value = deviceId { dictionary[SerializationKeys.deviceId] = value }
if let value = action { dictionary[SerializationKeys.action] = value }
if let value = objectData { dictionary[SerializationKeys.objectData] = value.rawValue }
// TODO: Add sync timestamp
if let value = syncTimestamp { dictionary[SerializationKeys.syncTimestamp] = value }
return dictionary
}
}
Expand Down

0 comments on commit 2a49f27

Please sign in to comment.