2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// ArticleUtilities.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/25/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-09-17 21:20:32 +02:00
|
|
|
import Data
|
2017-09-17 21:34:10 +02:00
|
|
|
import Account
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
// These handle multiple accounts.
|
|
|
|
|
2017-10-29 19:14:10 +01:00
|
|
|
@discardableResult
|
|
|
|
func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
let d: [String: Set<Article>] = accountAndArticlesDictionary(articles)
|
2017-10-29 19:14:10 +01:00
|
|
|
var updatedArticles = Set<Article>()
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
for (accountID, accountArticles) in d {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
guard let account = AccountManager.shared.existingAccount(with: accountID) else {
|
2017-10-29 19:14:10 +01:00
|
|
|
continue
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-10-29 19:14:10 +01:00
|
|
|
|
|
|
|
if let accountUpdatedArticles = account.markArticles(accountArticles, statusKey: statusKey, flag: flag) {
|
|
|
|
updatedArticles.formUnion(accountUpdatedArticles)
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-10-29 19:14:10 +01:00
|
|
|
|
|
|
|
return updatedArticles
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String: Set<Article>] {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
let d = Dictionary(grouping: articles, by: { $0.accountID })
|
|
|
|
return d.mapValues{ Set($0) }
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
extension Article {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-09-18 02:03:58 +02:00
|
|
|
var feed: Feed? {
|
2018-02-14 22:14:25 +01:00
|
|
|
return account?.existingFeed(with: feedID)
|
2017-09-18 02:03:58 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 01:30:45 +02:00
|
|
|
var preferredLink: String? {
|
2018-02-14 22:14:25 +01:00
|
|
|
return url ?? externalURL
|
2017-09-18 01:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: String? {
|
2018-02-14 22:14:25 +01:00
|
|
|
return contentHTML ?? contentText ?? summary
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-09-18 02:03:58 +02:00
|
|
|
|
|
|
|
var logicalDatePublished: Date {
|
2018-02-14 22:14:25 +01:00
|
|
|
return datePublished ?? dateModified ?? status.dateArrived
|
2017-09-18 02:03:58 +02:00
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|