From a0efc7fda9f845225cc62123284411643d1c7649 Mon Sep 17 00:00:00 2001 From: Jeremy Beker Date: Sat, 1 Jun 2019 08:08:19 -0400 Subject: [PATCH] Cleanup tag names, fetch subscriptions --- .../GoogleReaderCompatibleAPICaller.swift | 34 +++++++++---- ...oogleReaderCompatibleAccountDelegate.swift | 8 ++-- .../GoogleReaderCompatibleSubscription.swift | 48 ++++++++++++++++--- .../GoogleReaderCompatibleTag.swift | 2 +- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAPICaller.swift b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAPICaller.swift index ca16ea9e7..05e365e72 100644 --- a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAPICaller.swift +++ b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAPICaller.swift @@ -178,10 +178,10 @@ final class GoogleReaderCompatibleAPICaller: NSObject { return } - //let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.tags] - let request = URLRequest(url: callURL, credentials: credentials) - - transport.send(request: request, resultType: GoogleReaderCompatibleTagWrapper.self) { result in + let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.tags] + let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet) + + transport.send(request: request, resultType: GoogleReaderCompatibleTagContainer.self) { result in switch result { case .success(let (response, wrapper)): @@ -222,17 +222,35 @@ final class GoogleReaderCompatibleAPICaller: NSObject { } func retrieveSubscriptions(completion: @escaping (Result<[GoogleReaderCompatibleSubscription]?, Error>) -> Void) { + guard let baseURL = APIBaseURL else { + completion(.failure(CredentialsError.incompleteCredentials)) + return + } + + // Add query string for getting JSON (probably should break this out as I will be doing it a lot) + guard var components = URLComponents(url: baseURL.appendingPathComponent("/reader/api/0/subscription/list"), resolvingAgainstBaseURL: false) else { + completion(.failure(TransportError.noURL)) + return + } + + components.queryItems = [ + URLQueryItem(name: "output", value: "json") + ] + + guard let callURL = components.url else { + completion(.failure(TransportError.noURL)) + return + } - let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("subscriptions.json") let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.subscriptions] let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet) - transport.send(request: request, resultType: [GoogleReaderCompatibleSubscription].self) { result in + transport.send(request: request, resultType: GoogleReaderCompatibleSubscriptionContainer.self) { result in switch result { - case .success(let (response, subscriptions)): + case .success(let (response, container)): self.storeConditionalGet(key: ConditionalGetKeys.subscriptions, headers: response.allHeaderFields) - completion(.success(subscriptions)) + completion(.success(container?.subscriptions)) case .failure(let error): completion(.failure(error)) } diff --git a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAccountDelegate.swift b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAccountDelegate.swift index c8e9f851b..0c5e4692a 100644 --- a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAccountDelegate.swift +++ b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleAccountDelegate.swift @@ -542,9 +542,7 @@ private extension GoogleReaderCompatibleAccountDelegate { os_log(.debug, log: log, "Syncing folders with %ld tags.", tags.count) - // TODO: filter on folder tag type - // TODO: filter names to get rid of prefixes - let tagNames = tags.map { $0.tagID } + let tagNames = tags.filter { $0.type == "folder" }.map { $0.tagID.replacingOccurrences(of: "user/-/label/", with: "") } // Delete any folders not at GoogleReaderCompatible if let folders = account.folders { @@ -665,7 +663,7 @@ private extension GoogleReaderCompatibleAccountDelegate { feed.homePageURL = subscription.homePageURL } else { let feed = account.createFeed(with: subscription.name, url: subscription.url, feedID: subFeedId, homePageURL: subscription.homePageURL) - feed.subscriptionID = String(subscription.subscriptionID) + feed.subscriptionID = String(subscription.feedID) account.addFeed(feed) } } @@ -854,7 +852,7 @@ private extension GoogleReaderCompatibleAccountDelegate { DispatchQueue.main.async { let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL) - feed.subscriptionID = String(sub.subscriptionID) + feed.subscriptionID = String(sub.feedID) account.addFeed(feed, to: container) { result in switch result { diff --git a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleSubscription.swift b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleSubscription.swift index 6850133fd..7c0c56827 100644 --- a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleSubscription.swift +++ b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleSubscription.swift @@ -10,24 +10,60 @@ import Foundation import RSCore import RSParser +struct GoogleReaderCompatibleSubscriptionContainer: Codable { + let subscriptions: [GoogleReaderCompatibleSubscription] + + enum CodingKeys: String, CodingKey { + case subscriptions = "subscriptions" + } +} + +/* +{ + "id": "feed/1", + "title": "Questionable Content", + "categories": [ + { + "id": "user/-/label/Comics", + "label": "Comics" + } + ], + "url": "http://www.questionablecontent.net/QCRSS.xml", + "htmlUrl": "http://www.questionablecontent.net", + "iconUrl": "https://rss.confusticate.com/f.php?24decabc" +} + +*/ struct GoogleReaderCompatibleSubscription: Codable { - let subscriptionID: Int - let feedID: Int + let feedID: String let name: String? + let categories: [GoogleReaderCompatibleCategory] let url: String let homePageURL: String? + let iconURL: String? enum CodingKeys: String, CodingKey { - case subscriptionID = "id" - case feedID = "feed_id" + case feedID = "id" case name = "title" - case url = "feed_url" - case homePageURL = "site_url" + case categories = "categories" + case url = "url" + case homePageURL = "htmlUrl" + case iconURL = "iconUrl" } } +struct GoogleReaderCompatibleCategory: Codable { + let categoryId: String + let categoryLabel: String + + enum CodingKeys: String, CodingKey { + case categoryId = "id" + case categoryLabel = "label" + } +} + struct GoogleReaderCompatibleCreateSubscription: Codable { let feedURL: String enum CodingKeys: String, CodingKey { diff --git a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleTag.swift b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleTag.swift index f2b1252f8..16fd2695f 100644 --- a/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleTag.swift +++ b/Frameworks/Account/GoogleReaderCompatible/GoogleReaderCompatibleTag.swift @@ -8,7 +8,7 @@ import Foundation -struct GoogleReaderCompatibleTagWrapper: Codable { +struct GoogleReaderCompatibleTagContainer: Codable { let tags: [GoogleReaderCompatibleTag] enum CodingKeys: String, CodingKey {