fix loading posts with invalid published_at in preview card (#4993)

closes #4992
This commit is contained in:
Konrad Pozniak 2025-03-19 08:20:44 +01:00 committed by GitHub
commit b5c1a57a39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 6 deletions

View file

@ -82,8 +82,8 @@ object NetworkModule {
@Provides
@Singleton
fun providesMoshi(): Moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(GuardedAdapter.ANNOTATION_FACTORY)
.add(Date::class.java, Rfc3339DateJsonAdapter())
// Enum types with fallback value
.add(
Attachment.Type::class.java,

View file

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.entity
import com.keylesspalace.tusky.json.Guarded
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.util.Date
@ -27,7 +28,8 @@ data class PreviewCard(
val authors: List<PreviewCardAuthor> = emptyList(),
@Json(name = "author_name") val authorName: String? = null,
@Json(name = "provider_name") val providerName: String? = null,
@Json(name = "published_at") val publishedAt: Date?,
// sometimes this date is invalid https://github.com/tuskyapp/Tusky/issues/4992
@Json(name = "published_at") @Guarded val publishedAt: Date?,
val image: String? = null,
val type: String,
val width: Int = 0,

View file

@ -17,8 +17,8 @@
package com.keylesspalace.tusky.json
import android.util.Log
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
@ -35,10 +35,12 @@ class GuardedAdapter<T> private constructor(
override fun fromJson(reader: JsonReader): T? {
return try {
delegate.fromJson(reader)
} catch (e: JsonDataException) {
reader.skipValue()
reader.peekJson().use { delegate.fromJson(it) }
} catch (e: Exception) {
Log.w("GuardedAdapter", "failed to read json", e)
null
} finally {
reader.skipValue()
}
}