Skip to content

Commit

Permalink
Update comment deserialization to catch up recent update in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroBrine1st committed Mar 25, 2024
1 parent e48084a commit fe996e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/ru/herobrine1st/e621/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import androidx.annotation.IntRange
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import ru.herobrine1st.e621.api.endpoint.favourites.GetFavouritesEndpoint
import ru.herobrine1st.e621.api.endpoint.posts.GetPostCommentsDTextEndpoint
import ru.herobrine1st.e621.api.endpoint.posts.GetPostCommentsHTMLEndpoint
import ru.herobrine1st.e621.api.endpoint.posts.GetPostEndpoint
import ru.herobrine1st.e621.api.endpoint.posts.GetPostsEndpoint
import ru.herobrine1st.e621.api.endpoint.posts.VoteEndpoint
import ru.herobrine1st.e621.api.model.CommentBB
import ru.herobrine1st.e621.api.model.Pool
import ru.herobrine1st.e621.api.model.PostId
import ru.herobrine1st.e621.api.model.Tag
Expand Down Expand Up @@ -94,7 +94,7 @@ interface API : AutocompleteSuggestionsAPI {
id: PostId,
page: Int,
limit: Int, // Default unknown. Maybe 75, but I doubt
): Result<List<CommentBB>>
): Result<GetPostCommentsDTextEndpoint.Response>


suspend fun getPool(poolId: Int): Result<Pool>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@
package ru.herobrine1st.e621.api.endpoint.posts

import io.ktor.resources.Resource
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonEncoder
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.serializer
import ru.herobrine1st.e621.api.HttpMethod
import ru.herobrine1st.e621.api.HttpMethodType
import ru.herobrine1st.e621.api.endpoint.APIEndpoint
Expand All @@ -38,7 +50,38 @@ data class GetPostCommentsDTextEndpoint(
@SerialName("page") val page: Int,
@SerialName("limit") val limit: Int, // Default unknown. Maybe 75, but I doubt
@SerialName("group_by") val groupBy: String = "comment",
): APIEndpoint<Unit, List<CommentBB>> {
) : APIEndpoint<Unit, GetPostCommentsDTextEndpoint.Response> {

@Serializable(with = ResponseSerializer::class)
data class Response(val comments: List<CommentBB>)

class ResponseSerializer : KSerializer<Response> {
override val descriptor: SerialDescriptor
get() = buildClassSerialDescriptor("GetPostCommentsDTextEndpoint.ResponseSerializer")

override fun deserialize(decoder: Decoder): Response {
decoder as JsonDecoder

return Response(
decoder.json.decodeFromJsonElement<List<CommentBB>>(
when (val element = decoder.decodeJsonElement()) {
is JsonArray -> element
is JsonObject -> element["comments"]
?: throw IllegalArgumentException("Comments field is not found in wrapped comments response")

is JsonPrimitive -> throw IllegalArgumentException("Expected JsonArray/JsonObject, got primitive in comments response as root element")
}
)
)
}

override fun serialize(encoder: Encoder, value: Response) {
encoder as JsonEncoder
encoder.encodeSerializableValue(serializer<List<CommentBB>>(), value.comments)
}

}

init {
require(groupBy == "comment")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PostCommentsSource(
val limit = params.requestedSize

return api.getCommentsForPostBBCode(postId, page, limit)
.map { it.asReversed() }
.map { it.comments.asReversed() }
.map {
withContext(Dispatchers.Default) {
it.map {
Expand Down

0 comments on commit fe996e2

Please sign in to comment.