diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-04-10 13:34:23 +0100 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-04-10 13:34:23 +0100 |
| commit | 78400d587ea5367d3424333913ff4f94ca3f1908 (patch) | |
| tree | 2cf5f5ff8069740b0b7dd00853a4ea8c13d6e05c /src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt | |
| parent | d5a608143ad2250d8b35b9e4a488d39faaf5a021 (diff) | |
| download | reddit-lite-78400d587ea5367d3424333913ff4f94ca3f1908.tar.xz reddit-lite-78400d587ea5367d3424333913ff4f94ca3f1908.zip | |
Reimplement in Kotlin
Diffstat (limited to 'src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt')
| -rw-r--r-- | src/main/kotlin/io/jamesbarnett/redditlite/model/Comment.kt | 44 |
1 files changed, 44 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 |