Change cross posts to pull from current link if original link is sparse

This commit is contained in:
Maurice Parker 2020-05-07 19:47:23 -05:00
parent 4d9975f28a
commit 73627a60ca
2 changed files with 22 additions and 19 deletions

View File

@ -268,10 +268,10 @@ private extension RedditFeedProvider {
switch result { switch result {
case .success(let response): case .success(let response):
// let jsonString = String(data: response.data, encoding: .utf8) let jsonString = String(data: response.data, encoding: .utf8)
// let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("reddit.json") let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("reddit.json")
// print("******** writing to: \(url.path)") print("******** writing to: \(url.path)")
// try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8) try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
if let remaining = response.response.value(forHTTPHeaderField: "X-Ratelimit-Remaining") { if let remaining = response.response.value(forHTTPHeaderField: "X-Ratelimit-Remaining") {
self.rateLimitRemaining = Int(remaining) self.rateLimitRemaining = Int(remaining)

View File

@ -61,30 +61,33 @@ final class RedditLinkData: Codable {
return Date(timeIntervalSince1970: created) return Date(timeIntervalSince1970: created)
} }
func renderAsHTML() -> String? { func renderAsHTML() -> String {
var html = String()
if let selfHTML = selfHTML {
html += selfHTML
}
if let urlHTML = renderURLAsHTML() {
html += urlHTML
}
return html
}
func renderURLAsHTML() -> String? {
guard let url = url else { return nil }
if let parent = crossPostParents?.first { if let parent = crossPostParents?.first {
var html = "<blockquote>" var 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>"
} }
html += parent.renderAsHTML() ?? "" let parentHTML = parent.renderAsHTML()
if parentHTML.isEmpty {
html += renderURLAsHTML()
} else {
html += parentHTML
}
html += "</blockquote>" html += "</blockquote>"
return html return html
} }
var html = String()
if let selfHTML = selfHTML {
html += selfHTML
}
html += renderURLAsHTML()
return html
}
func renderURLAsHTML() -> String {
guard let url = url else { return "" }
if url.hasSuffix(".gif") { if url.hasSuffix(".gif") {
return "<img src=\"\(url)\">" return "<img src=\"\(url)\">"
} }