diff options
Diffstat (limited to 'src/main/kotlin/io/jamesbarnett/redditlite/model')
3 files changed, 94 insertions, 0 deletions
diff --git a/src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt b/src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt new file mode 100644 index 0000000..219270d --- /dev/null +++ b/src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt @@ -0,0 +1,44 @@ +package io.jamesbarnett.redditlite.model + +import com.fasterxml.jackson.annotation.JsonIgnore +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.github.marlonlom.utilities.timeago.TimeAgo +import org.apache.commons.text.StringEscapeUtils +import java.time.Instant + +@JsonIgnoreProperties(ignoreUnknown = true) +data class Comment( + val author: String?, + val score: Int, + @JsonProperty("is_submitter") + val isSubmitter: Boolean, + @JsonProperty("created_utc") + val createdDate: Instant?, + @JsonProperty("author_flair_text") + val flairText: String?, + val body: String?, + @JsonProperty("body_html") + val bodyHtml: String?, + val depth: Int, + @JsonIgnore + val replies: MutableList<Comment> = mutableListOf() +) { + companion object { + operator fun invoke(jsonNode: JsonNode, objectMapper: ObjectMapper): Comment { + val topLevelComment = objectMapper.treeToValue(jsonNode, Comment::class.java) + jsonNode + .path("replies") + .path("data") + .path("children") + .findValues("data") + .forEach {topLevelComment.replies.add(invoke(it, objectMapper))} + return topLevelComment + } + } + val isChild get() = depth > 0 + val relativeCreatedDate: String? get() = createdDate?.let { TimeAgo.using(createdDate.toEpochMilli()) } + val bodyHtmlUnescaped: String? get() = StringEscapeUtils.unescapeHtml4(bodyHtml) +}
\ No newline at end of file diff --git a/src/main/kotlin/io/jamesbarnett/redditlite/model/Post.kt b/src/main/kotlin/io/jamesbarnett/redditlite/model/Post.kt new file mode 100644 index 0000000..4cd723d --- /dev/null +++ b/src/main/kotlin/io/jamesbarnett/redditlite/model/Post.kt @@ -0,0 +1,41 @@ +package io.jamesbarnett.redditlite.model + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonProperty +import com.github.marlonlom.utilities.timeago.TimeAgo +import java.time.Instant + +@JsonIgnoreProperties(ignoreUnknown = true) +data class Post ( + val id: String, + val name: String, + val title: String, + val domain: String, + val score: Int, + val author: String, + private val thumbnail: String?, + val ups: Int, + @JsonProperty("num_comments") + val commentCount: Int, + val url: String, + @JsonProperty("created_utc") + val createdDate: Instant, + @JsonProperty("is_self") + val isSelfPost: Boolean, + @JsonProperty("selftext_html") + val selftextHtml: String?, + val subreddit: String +) { + val primaryLink: String get() { + return if (isSelfPost) { + "/r/${subreddit}/comments/${id}" + } else { + url + } + } + val subredditPath get() = "/r/${subreddit}" + val relativeCreatedDate: String get() = TimeAgo.using(createdDate.toEpochMilli()) + val thumbnailUrl get() = thumbnail?.let { if(thumbnail.startsWith("http")) thumbnail else null } +//val thumbnail get() = if(thumbnailRaw?.startsWith("http") == true) thumbnailRaw else null + +}
\ No newline at end of file diff --git a/src/main/kotlin/io/jamesbarnett/redditlite/model/PostDetail.kt b/src/main/kotlin/io/jamesbarnett/redditlite/model/PostDetail.kt new file mode 100644 index 0000000..c5b58f0 --- /dev/null +++ b/src/main/kotlin/io/jamesbarnett/redditlite/model/PostDetail.kt @@ -0,0 +1,9 @@ +package io.jamesbarnett.redditlite.model + +import org.apache.commons.text.StringEscapeUtils + +data class PostDetail(val post: Post, val comments: List<Comment>) { + val isSelfPost get() = post.isSelfPost + val selftextHtmlUnescaped: String? get() = StringEscapeUtils.unescapeHtml4(post.selftextHtml) + val commentCount get() = post.commentCount +} |