Add subreddit name to meta feeds

This commit is contained in:
Maurice Parker 2020-05-09 07:33:26 -05:00
parent faaf280d12
commit de15c1c78d
3 changed files with 15 additions and 8 deletions

View File

@ -29,7 +29,7 @@ public protocol FeedProvider {
/// Provide the iconURL of the given URL /// Provide the iconURL of the given URL
func iconURL(_ urlComponents: URLComponents, completion: @escaping (Result<String, Error>) -> Void) func iconURL(_ urlComponents: URLComponents, completion: @escaping (Result<String, Error>) -> Void)
/// Construct the associated metadata for the new feed. If the URL won't be recognized by this Feed Provider, it will error here. /// Construct the associated metadata for the new feed
func metaData(_ urlComponents: URLComponents, completion: @escaping (Result<FeedProviderFeedMetaData, Error>) -> Void) func metaData(_ urlComponents: URLComponents, completion: @escaping (Result<FeedProviderFeedMetaData, Error>) -> Void)
/// Refresh all the article entries (ParsedItems) /// Refresh all the article entries (ParsedItems)

View File

@ -164,10 +164,12 @@ public final class RedditFeedProvider: FeedProvider {
api = "\(urlComponents.path).json" api = "\(urlComponents.path).json"
} }
let identifySubreddit = !urlComponents.path.hasPrefix("/r/")
fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in
switch result { switch result {
case .success(let linkListing): case .success(let linkListing):
let parsedItems = self.makeParsedItems(webFeed.url, linkListing) let parsedItems = self.makeParsedItems(webFeed.url, identifySubreddit, linkListing)
completion(.success(parsedItems)) completion(.success(parsedItems))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
@ -316,7 +318,7 @@ private extension RedditFeedProvider {
} }
} }
func makeParsedItems(_ webFeedURL: String, _ linkListing: RedditLinkListing) -> Set<ParsedItem> { func makeParsedItems(_ webFeedURL: String,_ identifySubreddit: Bool, _ linkListing: RedditLinkListing) -> Set<ParsedItem> {
var parsedItems = Set<ParsedItem>() var parsedItems = Set<ParsedItem>()
guard let linkDatas = linkListing.data?.children?.compactMap({ $0.data }), !linkDatas.isEmpty else { guard let linkDatas = linkListing.data?.children?.compactMap({ $0.data }), !linkDatas.isEmpty else {
@ -333,7 +335,7 @@ private extension RedditFeedProvider {
externalURL: linkData.url, externalURL: linkData.url,
title: linkData.title, title: linkData.title,
language: nil, language: nil,
contentHTML: linkData.renderAsHTML(), contentHTML: linkData.renderAsHTML(identifySubreddit: identifySubreddit),
contentText: linkData.selfText, contentText: linkData.selfText,
summary: nil, summary: nil,
imageURL: nil, imageURL: nil,

View File

@ -61,13 +61,19 @@ final class RedditLinkData: Codable {
return Date(timeIntervalSince1970: created) return Date(timeIntervalSince1970: created)
} }
func renderAsHTML() -> String { func renderAsHTML(identifySubreddit: Bool) -> String {
var html = String()
if identifySubreddit, let subredditNamePrefixed = subredditNamePrefixed {
html += "<h3><a href=\"https://www.reddit.com/\(subredditNamePrefixed)\">\(subredditNamePrefixed)</a></h3>"
}
if let parent = crossPostParents?.first { if let parent = crossPostParents?.first {
var html = "<blockquote>" html += "<blockquote>"
if let subreddit = parent.subredditNamePrefixed { if let subreddit = parent.subredditNamePrefixed {
html += "<p><a href=\"https://www.reddit.com/\(subreddit)\">\(subreddit)</a></p>" html += "<p><a href=\"https://www.reddit.com/\(subreddit)\">\(subreddit)</a></p>"
} }
let parentHTML = parent.renderAsHTML() let parentHTML = parent.renderAsHTML(identifySubreddit: false)
if parentHTML.isEmpty { if parentHTML.isEmpty {
html += renderURLAsHTML() html += renderURLAsHTML()
} else { } else {
@ -77,7 +83,6 @@ final class RedditLinkData: Codable {
return html return html
} }
var html = String()
if let selfHTML = selfHTML { if let selfHTML = selfHTML {
html += selfHTML html += selfHTML
} }