2020-04-01 18:46:37 +02:00
|
|
|
//
|
|
|
|
// CloudKitArticlesZone.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/1/20.
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
2020-04-03 18:25:01 +02:00
|
|
|
import RSParser
|
2020-04-01 18:46:37 +02:00
|
|
|
import RSWeb
|
|
|
|
import CloudKit
|
2020-04-03 01:06:47 +02:00
|
|
|
import Articles
|
2020-04-01 18:46:37 +02:00
|
|
|
import SyncDatabase
|
|
|
|
|
|
|
|
final class CloudKitArticlesZone: CloudKitZone {
|
|
|
|
|
|
|
|
static var zoneID: CKRecordZone.ID {
|
|
|
|
return CKRecordZone.ID(zoneName: "Articles", ownerName: CKCurrentUserDefaultName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
|
|
|
|
|
|
|
|
weak var container: CKContainer?
|
|
|
|
weak var database: CKDatabase?
|
|
|
|
var delegate: CloudKitZoneDelegate? = nil
|
|
|
|
|
2020-04-03 01:06:47 +02:00
|
|
|
struct CloudKitArticle {
|
|
|
|
static let recordType = "Article"
|
|
|
|
struct Fields {
|
|
|
|
static let articleStatus = "articleStatus"
|
2020-04-03 21:19:31 +02:00
|
|
|
static let webFeedURL = "webFeedURL"
|
2020-04-03 01:06:47 +02:00
|
|
|
static let uniqueID = "uniqueID"
|
|
|
|
static let title = "title"
|
|
|
|
static let contentHTML = "contentHTML"
|
|
|
|
static let contentText = "contentText"
|
|
|
|
static let url = "url"
|
|
|
|
static let externalURL = "externalURL"
|
|
|
|
static let summary = "summary"
|
|
|
|
static let imageURL = "imageURL"
|
|
|
|
static let datePublished = "datePublished"
|
|
|
|
static let dateModified = "dateModified"
|
2020-04-03 20:26:08 +02:00
|
|
|
static let parsedAuthors = "parsedAuthors"
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:46:37 +02:00
|
|
|
struct CloudKitArticleStatus {
|
|
|
|
static let recordType = "ArticleStatus"
|
|
|
|
struct Fields {
|
|
|
|
static let read = "read"
|
|
|
|
static let starred = "starred"
|
|
|
|
static let userDeleted = "userDeleted"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(container: CKContainer) {
|
|
|
|
self.container = container
|
|
|
|
self.database = container.privateCloudDatabase
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:15:28 +02:00
|
|
|
func refreshArticleStatus(completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
fetchChangesInZone() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
if case CloudKitZoneError.userDeletedZone = error {
|
|
|
|
self.createZoneRecord() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.refreshArticleStatus(completion: completion)
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:06:47 +02:00
|
|
|
func sendArticleStatus(_ syncStatuses: [SyncStatus], starredArticles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
var records = makeStatusRecords(syncStatuses)
|
2020-04-03 18:25:01 +02:00
|
|
|
makeArticleRecordsIfNecessary(starredArticles) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articleRecords):
|
|
|
|
records.append(contentsOf: articleRecords)
|
2020-04-06 09:15:28 +02:00
|
|
|
self.modify(recordsToSave: records, recordIDsToDelete: []) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: starredArticles, completion: completion)
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 18:25:01 +02:00
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: starredArticles, completion: completion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSendArticleStatusError(_ error: Error, syncStatuses: [SyncStatus], starredArticles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
if case CloudKitZoneError.userDeletedZone = error {
|
|
|
|
self.createZoneRecord() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.sendArticleStatus(syncStatuses, starredArticles: starredArticles, completion: completion)
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2020-04-03 18:25:01 +02:00
|
|
|
}
|
2020-04-06 09:15:28 +02:00
|
|
|
} else {
|
|
|
|
completion(.failure(error))
|
2020-04-03 18:25:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CloudKitArticlesZone {
|
|
|
|
|
|
|
|
func makeStatusRecords(_ syncStatuses: [SyncStatus]) -> [CKRecord] {
|
2020-04-01 18:46:37 +02:00
|
|
|
var records = [String: CKRecord]()
|
|
|
|
|
|
|
|
for status in syncStatuses {
|
|
|
|
|
|
|
|
var record = records[status.articleID]
|
|
|
|
if record == nil {
|
|
|
|
let recordID = CKRecord.ID(recordName: status.articleID, zoneID: Self.zoneID)
|
|
|
|
record = CKRecord(recordType: CloudKitArticleStatus.recordType, recordID: recordID)
|
|
|
|
records[status.articleID] = record
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status.key {
|
|
|
|
case .read:
|
|
|
|
record![CloudKitArticleStatus.Fields.read] = status.flag ? "1" : "0"
|
|
|
|
case .starred:
|
|
|
|
record![CloudKitArticleStatus.Fields.starred] = status.flag ? "1" : "0"
|
|
|
|
case .userDeleted:
|
|
|
|
record![CloudKitArticleStatus.Fields.userDeleted] = status.flag ? "1" : "0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:06:47 +02:00
|
|
|
return Array(records.values)
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:25:01 +02:00
|
|
|
func makeArticleRecordsIfNecessary(_ articles: Set<Article>, completion: @escaping ((Result<[CKRecord], Error>) -> Void)) {
|
|
|
|
let group = DispatchGroup()
|
2020-04-03 01:06:47 +02:00
|
|
|
var records = [CKRecord]()
|
|
|
|
|
|
|
|
for article in articles {
|
|
|
|
|
2020-04-03 18:25:01 +02:00
|
|
|
let statusRecordID = CKRecord.ID(recordName: article.articleID, zoneID: Self.zoneID)
|
|
|
|
let statusRecordRef = CKRecord.Reference(recordID: statusRecordID, action: .deleteSelf)
|
|
|
|
let predicate = NSPredicate(format: "articleStatus = %@", statusRecordRef)
|
|
|
|
let ckQuery = CKQuery(recordType: CloudKitArticle.recordType, predicate: predicate)
|
2020-04-03 01:06:47 +02:00
|
|
|
|
2020-04-03 18:25:01 +02:00
|
|
|
group.enter()
|
|
|
|
exists(ckQuery) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let recordFound):
|
|
|
|
if !recordFound {
|
|
|
|
records.append(contentsOf: self.makeArticleRecords(article))
|
|
|
|
}
|
2020-04-08 02:02:25 +02:00
|
|
|
case .failure:
|
|
|
|
records.append(contentsOf: self.makeArticleRecords(article))
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
2020-04-03 18:25:01 +02:00
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2020-04-08 02:02:25 +02:00
|
|
|
completion(.success(records))
|
2020-04-03 18:25:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeArticleRecords(_ article: Article) -> [CKRecord] {
|
|
|
|
var records = [CKRecord]()
|
|
|
|
|
|
|
|
let articleRecord = CKRecord(recordType: CloudKitArticle.recordType, recordID: generateRecordID())
|
|
|
|
|
|
|
|
let articleStatusRecordID = CKRecord.ID(recordName: article.articleID, zoneID: Self.zoneID)
|
|
|
|
articleRecord[CloudKitArticle.Fields.articleStatus] = CKRecord.Reference(recordID: articleStatusRecordID, action: .deleteSelf)
|
2020-04-03 21:19:31 +02:00
|
|
|
articleRecord[CloudKitArticle.Fields.webFeedURL] = article.webFeed?.url
|
2020-04-03 18:25:01 +02:00
|
|
|
articleRecord[CloudKitArticle.Fields.uniqueID] = article.uniqueID
|
|
|
|
articleRecord[CloudKitArticle.Fields.title] = article.title
|
|
|
|
articleRecord[CloudKitArticle.Fields.contentHTML] = article.contentHTML
|
|
|
|
articleRecord[CloudKitArticle.Fields.contentText] = article.contentText
|
|
|
|
articleRecord[CloudKitArticle.Fields.url] = article.url
|
|
|
|
articleRecord[CloudKitArticle.Fields.externalURL] = article.externalURL
|
|
|
|
articleRecord[CloudKitArticle.Fields.summary] = article.summary
|
|
|
|
articleRecord[CloudKitArticle.Fields.imageURL] = article.imageURL
|
|
|
|
articleRecord[CloudKitArticle.Fields.datePublished] = article.datePublished
|
|
|
|
articleRecord[CloudKitArticle.Fields.dateModified] = article.dateModified
|
|
|
|
|
2020-04-03 20:26:08 +02:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
var parsedAuthors = [String]()
|
2020-04-03 18:25:01 +02:00
|
|
|
|
|
|
|
if let authors = article.authors {
|
|
|
|
for author in authors {
|
2020-04-03 20:26:08 +02:00
|
|
|
let parsedAuthor = ParsedAuthor(name: author.name,
|
|
|
|
url: author.url,
|
|
|
|
avatarURL: author.avatarURL,
|
|
|
|
emailAddress: author.emailAddress)
|
|
|
|
if let data = try? encoder.encode(parsedAuthor), let encodedParsedAuthor = String(data: data, encoding: .utf8) {
|
|
|
|
parsedAuthors.append(encodedParsedAuthor)
|
|
|
|
}
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:08 +02:00
|
|
|
articleRecord[CloudKitArticle.Fields.parsedAuthors] = parsedAuthors
|
2020-04-03 01:06:47 +02:00
|
|
|
|
2020-04-03 20:26:08 +02:00
|
|
|
records.append(articleRecord)
|
|
|
|
return records
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:08 +02:00
|
|
|
|
2020-04-01 18:46:37 +02:00
|
|
|
}
|