Add support for cross posts

This commit is contained in:
Maurice Parker 2020-05-06 22:19:06 -05:00
parent 9429f36095
commit 15dfd159da
1 changed files with 16 additions and 2 deletions

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
struct RedditLink: Codable { final class RedditLink: Codable {
let kind: String? let kind: String?
let data: RedditLinkData? let data: RedditLinkData?
@ -20,12 +20,13 @@ struct RedditLink: Codable {
} }
struct RedditLinkData: Codable { final class RedditLinkData: Codable {
let title: String? let title: String?
let permalink: String? let permalink: String?
let url: String? let url: String?
let id: String? let id: String?
let subredditNamePrefixed: String?
let selfHTML: String? let selfHTML: String?
let selfText: String? let selfText: String?
let postHint: String? let postHint: String?
@ -35,12 +36,14 @@ struct RedditLinkData: Codable {
let media: RedditMedia? let media: RedditMedia?
let mediaEmbed: RedditMediaEmbed? let mediaEmbed: RedditMediaEmbed?
let preview: RedditPreview? let preview: RedditPreview?
let crossPostParents: [RedditLinkData]?
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case title = "title" case title = "title"
case permalink = "permalink" case permalink = "permalink"
case url = "url" case url = "url"
case id = "id" case id = "id"
case subredditNamePrefixed = "subreddit_name_prefixed"
case selfHTML = "selftext_html" case selfHTML = "selftext_html"
case selfText = "selftext" case selfText = "selftext"
case postHint = "post_hint" case postHint = "post_hint"
@ -50,6 +53,7 @@ struct RedditLinkData: Codable {
case media = "media" case media = "media"
case mediaEmbed = "media_embed" case mediaEmbed = "media_embed"
case preview = "preview" case preview = "preview"
case crossPostParents = "crosspost_parent_list"
} }
var createdDate: Date? { var createdDate: Date? {
@ -71,6 +75,16 @@ struct RedditLinkData: Codable {
func renderURLAsHTML() -> String? { func renderURLAsHTML() -> String? {
guard let url = url else { return nil } guard let url = url else { return nil }
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() ?? ""
html += "</blockquote>"
return html
}
if url.hasSuffix(".gif") { if url.hasSuffix(".gif") {
return "<img src=\"\(url)\">" return "<img src=\"\(url)\">"
} }