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-27 23:41:45 +02:00
|
|
|
import SyncDatabase
|
2020-04-01 18:46:37 +02:00
|
|
|
|
|
|
|
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 {
|
2020-04-28 01:09:10 +02:00
|
|
|
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-28 01:09:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CloudKitArticleStatus {
|
|
|
|
static let recordType = "ArticleStatus"
|
|
|
|
struct Fields {
|
|
|
|
static let webFeedExternalID = "webFeedExternalID"
|
2020-04-01 18:46:37 +02:00
|
|
|
static let read = "read"
|
|
|
|
static let starred = "starred"
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 11:28:42 +02:00
|
|
|
|
2020-04-01 18:46:37 +02:00
|
|
|
init(container: CKContainer) {
|
|
|
|
self.container = container
|
|
|
|
self.database = container.privateCloudDatabase
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:28:42 +02:00
|
|
|
func refreshArticles(completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-04-06 09:15:28 +02:00
|
|
|
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:
|
2020-04-26 11:28:42 +02:00
|
|
|
self.refreshArticles(completion: completion)
|
2020-04-06 09:15:28 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:28:42 +02:00
|
|
|
func saveNewArticles(_ articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-04-23 23:39:09 +02:00
|
|
|
guard !articles.isEmpty else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-27 07:52:53 +02:00
|
|
|
var records = [CKRecord]()
|
2020-04-26 03:20:56 +02:00
|
|
|
|
2020-04-27 07:52:53 +02:00
|
|
|
let saveArticles = articles.filter { $0.status.read == false || $0.status.starred == true }
|
|
|
|
for saveArticle in saveArticles {
|
2020-04-28 06:09:45 +02:00
|
|
|
records.append(makeStatusRecord(saveArticle))
|
|
|
|
records.append(makeArticleRecord(saveArticle))
|
2020-04-27 07:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 18:19:33 +02:00
|
|
|
save(records, completion: completion)
|
2020-04-14 08:25:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 02:07:14 +02:00
|
|
|
func deleteArticles(_ webFeedExternalID: String, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
let predicate = NSPredicate(format: "webFeedExternalID = %@", webFeedExternalID)
|
|
|
|
let ckQuery = CKQuery(recordType: CloudKitArticleStatus.recordType, predicate: predicate)
|
2020-04-27 09:11:20 +02:00
|
|
|
delete(ckQuery: ckQuery, completion: completion)
|
2020-04-26 11:54:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
func modifyArticles(_ statusUpdates: [CloudKitArticleStatusUpdate], completion: @escaping ((Result<Void, Error>) -> Void)) {
|
|
|
|
guard !statusUpdates.isEmpty else {
|
2020-04-26 16:37:36 +02:00
|
|
|
completion(.success(()))
|
|
|
|
return
|
2020-04-26 10:19:20 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 23:41:45 +02:00
|
|
|
var modifyRecords = [CKRecord]()
|
2020-05-10 02:36:03 +02:00
|
|
|
var newRecords = [CKRecord]()
|
2020-04-27 23:41:45 +02:00
|
|
|
var deleteRecordIDs = [CKRecord.ID]()
|
2020-04-27 07:52:53 +02:00
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
for statusUpdate in statusUpdates {
|
|
|
|
switch statusUpdate.record {
|
|
|
|
case .all:
|
|
|
|
modifyRecords.append(makeStatusRecord(statusUpdate))
|
|
|
|
modifyRecords.append(makeArticleRecord(statusUpdate.article!))
|
2020-05-10 02:36:03 +02:00
|
|
|
case .new:
|
|
|
|
newRecords.append(makeStatusRecord(statusUpdate))
|
|
|
|
newRecords.append(makeArticleRecord(statusUpdate.article!))
|
2020-04-29 12:24:35 +02:00
|
|
|
case .delete:
|
|
|
|
deleteRecordIDs.append(CKRecord.ID(recordName: statusID(statusUpdate.articleID), zoneID: Self.zoneID))
|
|
|
|
case .statusOnly:
|
|
|
|
modifyRecords.append(makeStatusRecord(statusUpdate))
|
|
|
|
deleteRecordIDs.append(CKRecord.ID(recordName: articleID(statusUpdate.articleID), zoneID: Self.zoneID))
|
2020-04-27 23:41:45 +02:00
|
|
|
}
|
2020-04-27 07:52:53 +02:00
|
|
|
}
|
2020-04-26 16:37:36 +02:00
|
|
|
|
2020-05-10 02:36:03 +02:00
|
|
|
modify(recordsToSave: modifyRecords, recordIDsToDelete: deleteRecordIDs) { result in
|
2020-04-28 18:19:33 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
2020-05-10 02:36:03 +02:00
|
|
|
self.saveIfNew(newRecords) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 18:19:33 +02:00
|
|
|
case .failure(let error):
|
2020-04-29 12:24:35 +02:00
|
|
|
self.handleModifyArticlesError(error, statusUpdates: statusUpdates, completion: completion)
|
2020-04-06 09:15:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CloudKitArticlesZone {
|
2020-04-28 18:19:33 +02:00
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
func handleModifyArticlesError(_ error: Error, statusUpdates: [CloudKitArticleStatusUpdate], completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-04-28 18:19:33 +02:00
|
|
|
if case CloudKitZoneError.userDeletedZone = error {
|
|
|
|
self.createZoneRecord() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2020-04-29 12:24:35 +02:00
|
|
|
self.modifyArticles(statusUpdates, completion: completion)
|
2020-04-28 18:19:33 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:09:10 +02:00
|
|
|
func statusID(_ id: String) -> String {
|
|
|
|
return "s|\(id)"
|
|
|
|
}
|
|
|
|
|
|
|
|
func articleID(_ id: String) -> String {
|
|
|
|
return "a|\(id)"
|
|
|
|
}
|
|
|
|
|
2020-04-28 06:09:45 +02:00
|
|
|
func makeStatusRecord(_ article: Article) -> CKRecord {
|
|
|
|
let recordID = CKRecord.ID(recordName: statusID(article.articleID), zoneID: Self.zoneID)
|
|
|
|
let record = CKRecord(recordType: CloudKitArticleStatus.recordType, recordID: recordID)
|
|
|
|
if let webFeedExternalID = article.webFeed?.externalID {
|
|
|
|
record[CloudKitArticleStatus.Fields.webFeedExternalID] = webFeedExternalID
|
|
|
|
}
|
|
|
|
record[CloudKitArticleStatus.Fields.read] = article.status.read ? "1" : "0"
|
|
|
|
record[CloudKitArticleStatus.Fields.starred] = article.status.starred ? "1" : "0"
|
|
|
|
return record
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
func makeStatusRecord(_ statusUpdate: CloudKitArticleStatusUpdate) -> CKRecord {
|
|
|
|
let recordID = CKRecord.ID(recordName: statusID(statusUpdate.articleID), zoneID: Self.zoneID)
|
2020-04-28 01:09:10 +02:00
|
|
|
let record = CKRecord(recordType: CloudKitArticleStatus.recordType, recordID: recordID)
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
if let webFeedExternalID = statusUpdate.article?.webFeed?.externalID {
|
2020-04-28 01:09:10 +02:00
|
|
|
record[CloudKitArticleStatus.Fields.webFeedExternalID] = webFeedExternalID
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
record[CloudKitArticleStatus.Fields.read] = statusUpdate.isRead ? "1" : "0"
|
|
|
|
record[CloudKitArticleStatus.Fields.starred] = statusUpdate.isStarred ? "1" : "0"
|
2020-04-28 01:09:10 +02:00
|
|
|
|
|
|
|
return record
|
|
|
|
}
|
2020-04-03 18:25:01 +02:00
|
|
|
|
2020-04-28 06:09:45 +02:00
|
|
|
func makeArticleRecord(_ article: Article) -> CKRecord {
|
2020-04-28 01:09:10 +02:00
|
|
|
let recordID = CKRecord.ID(recordName: articleID(article.articleID), zoneID: Self.zoneID)
|
2020-04-28 06:09:45 +02:00
|
|
|
let record = CKRecord(recordType: CloudKitArticle.recordType, recordID: recordID)
|
2020-04-03 18:25:01 +02:00
|
|
|
|
2020-04-28 01:09:10 +02:00
|
|
|
let articleStatusRecordID = CKRecord.ID(recordName: statusID(article.articleID), zoneID: Self.zoneID)
|
2020-04-28 06:09:45 +02:00
|
|
|
record[CloudKitArticle.Fields.articleStatus] = CKRecord.Reference(recordID: articleStatusRecordID, action: .deleteSelf)
|
|
|
|
record[CloudKitArticle.Fields.webFeedURL] = article.webFeed?.url
|
|
|
|
record[CloudKitArticle.Fields.uniqueID] = article.uniqueID
|
|
|
|
record[CloudKitArticle.Fields.title] = article.title
|
|
|
|
record[CloudKitArticle.Fields.contentHTML] = article.contentHTML
|
|
|
|
record[CloudKitArticle.Fields.contentText] = article.contentText
|
|
|
|
record[CloudKitArticle.Fields.url] = article.url
|
|
|
|
record[CloudKitArticle.Fields.externalURL] = article.externalURL
|
|
|
|
record[CloudKitArticle.Fields.summary] = article.summary
|
|
|
|
record[CloudKitArticle.Fields.imageURL] = article.imageURL
|
|
|
|
record[CloudKitArticle.Fields.datePublished] = article.datePublished
|
|
|
|
record[CloudKitArticle.Fields.dateModified] = article.dateModified
|
2020-04-03 18:25:01 +02:00
|
|
|
|
2020-04-03 20:26:08 +02:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
var parsedAuthors = [String]()
|
2020-04-03 18:25:01 +02:00
|
|
|
|
2020-04-14 15:05:16 +02:00
|
|
|
if let authors = article.authors, !authors.isEmpty {
|
2020-04-03 18:25:01 +02:00
|
|
|
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-28 06:09:45 +02:00
|
|
|
record[CloudKitArticle.Fields.parsedAuthors] = parsedAuthors
|
2020-04-03 01:06:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 06:09:45 +02:00
|
|
|
return record
|
2020-04-26 11:28:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 01:06:47 +02:00
|
|
|
|
2020-04-01 18:46:37 +02:00
|
|
|
}
|