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 {
case .success(let response):
// let jsonString = String(data: response.data, encoding: .utf8)
// let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("reddit.json")
// print("******** writing to: \(url.path)")
// try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
let jsonString = String(data: response.data, encoding: .utf8)
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("reddit.json")
print("******** writing to: \(url.path)")
try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
if let remaining = response.response.value(forHTTPHeaderField: "X-Ratelimit-Remaining") {
self.rateLimitRemaining = Int(remaining)

View File

@ -61,30 +61,33 @@ final class RedditLinkData: Codable {
return Date(timeIntervalSince1970: created)
}
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 }
func renderAsHTML() -> String {
if let parent = crossPostParents?.first {
var html = "<blockquote>"
if let subreddit = parent.subredditNamePrefixed {
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>"
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") {
return "<img src=\"\(url)\">"
}