Skip to content

Commit

Permalink
feat: Support Pleroma returning dates with no timezone (#1120)
Browse files Browse the repository at this point in the history
Pleroma (and possibly other servers) can return dates that have no
timezone. Previous code would fail to deserialise JSON in this state and
show an error.

Patch around this by assuming anything with a missing timezone is in UTC
(timezone suffix "Z").

Fixes #562
  • Loading branch information
nikclayton authored Nov 21, 2024
1 parent 9f908cf commit 4c7107c
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/src/test/java/app/pachli/StatusComparisonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import app.pachli.core.database.model.TranslationState
import app.pachli.core.network.json.BooleanIfNull
import app.pachli.core.network.json.DefaultIfNull
import app.pachli.core.network.json.Guarded
import app.pachli.core.network.json.LenientRfc3339DateJsonAdapter
import app.pachli.core.network.model.Status
import app.pachli.viewdata.StatusViewData
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapter
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import java.util.Date
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
Expand All @@ -19,7 +19,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class StatusComparisonTest {
private val moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(Date::class.java, LenientRfc3339DateJsonAdapter())
.add(Guarded.Factory())
.add(DefaultIfNull.Factory())
.add(BooleanIfNull.Factory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import app.pachli.core.database.model.TimelineStatusWithAccount
import app.pachli.core.network.json.BooleanIfNull
import app.pachli.core.network.json.DefaultIfNull
import app.pachli.core.network.json.Guarded
import app.pachli.core.network.json.LenientRfc3339DateJsonAdapter
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import java.io.IOException
import java.util.Date
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -63,7 +63,7 @@ class CachedTimelineRemoteMediatorTest {
private lateinit var pagingSourceFactory: InvalidatingPagingSourceFactory<Int, TimelineStatusWithAccount>

private val moshi: Moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(Date::class.java, LenientRfc3339DateJsonAdapter())
.add(Guarded.Factory())
.add(DefaultIfNull.Factory())
.add(BooleanIfNull.Factory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import app.pachli.core.database.model.TranslationState
import app.pachli.core.network.json.BooleanIfNull
import app.pachli.core.network.json.DefaultIfNull
import app.pachli.core.network.json.Guarded
import app.pachli.core.network.json.LenientRfc3339DateJsonAdapter
import app.pachli.core.network.model.Status
import app.pachli.core.network.model.TimelineAccount
import app.pachli.viewdata.StatusViewData
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import java.util.Date

private val fixedDate = Date(1638889052000)
Expand Down Expand Up @@ -101,7 +101,7 @@ fun mockStatusEntityWithAccount(
): TimelineStatusWithAccount {
val mockedStatus = mockStatus(id)
val moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(Date::class.java, LenientRfc3339DateJsonAdapter())
.add(Guarded.Factory())
.add(DefaultIfNull.Factory())
.add(BooleanIfNull.Factory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,20 @@ data class Server(
}
}

PLEROMA -> {
// Pleroma only has v1 filters
c[ORG_JOINMASTODON_FILTERS_CLIENT] = "1.1.0".toVersion()
c[ORG_JOINMASTODON_STATUSES_SCHEDULED] = "1.0.0".toVersion()
}

// Everything else. Assume:
//
// - server side filtering
// - scheduled status support
// - no translation
//
// This may be an incorrect assumption.
AKKOMA, FEDIBIRD, HOMETOWN, ICESHRIMP, PIXELFED, PLEROMA, UNKNOWN -> {
AKKOMA, FEDIBIRD, HOMETOWN, ICESHRIMP, PIXELFED, UNKNOWN -> {
c[ORG_JOINMASTODON_FILTERS_SERVER] = "1.0.0".toVersion()
c[ORG_JOINMASTODON_STATUSES_SCHEDULED] = "1.0.0".toVersion()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,12 @@ class AccountManager @Inject constructor(
.bind()

externalScope.async { contentFiltersRepository.refresh(account.id) }.await()
.mapError { RefreshAccountError.General(it) }.bind()
.orElse {
when (it) {
ContentFiltersError.ServerDoesNotFilter -> Ok(Unit)
else -> Err(RefreshAccountError.General(it))
}
}.bind()

deferEmojis.await().bind()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class ServerTest(
),
arrayOf(
Triple(
"Pleroma can filter, schedule",
"Pleroma can't server filter, can schedule",
NodeInfo.Software("pleroma", "2.6.50-875-g2eb5c453.service-origin+soapbox"),
defaultInstance,
),
Expand All @@ -273,7 +273,7 @@ class ServerTest(
kind = PLEROMA,
version = "2.6.50-875-g2eb5c453.service-origin+soapbox".toVersion(),
capabilities = mapOf(
ORG_JOINMASTODON_FILTERS_SERVER to "1.0.0".toVersion(),
ORG_JOINMASTODON_FILTERS_CLIENT to "1.1.0".toVersion(),
ORG_JOINMASTODON_STATUSES_SCHEDULED to "1.0.0".toVersion(),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package app.pachli.core.network.di.test
import app.pachli.core.model.VersionAdapter
import app.pachli.core.network.di.NetworkModule
import app.pachli.core.network.json.Guarded
import app.pachli.core.network.json.LenientRfc3339DateJsonAdapter
import app.pachli.core.network.model.MediaUploadApi
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
Expand All @@ -42,7 +42,7 @@ object FakeNetworkModule {
@Singleton
fun providesMoshi(): Moshi = Moshi.Builder()
.add(VersionAdapter())
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(Date::class.java, LenientRfc3339DateJsonAdapter())
.add(Guarded.Factory())
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import app.pachli.core.network.json.DefaultIfNull
import app.pachli.core.network.json.EnumConstantConverterFactory
import app.pachli.core.network.json.Guarded
import app.pachli.core.network.json.HasDefault
import app.pachli.core.network.json.LenientRfc3339DateJsonAdapter
import app.pachli.core.network.model.MediaUploadApi
import app.pachli.core.network.retrofit.InstanceSwitchAuthInterceptor
import app.pachli.core.network.retrofit.MastodonApi
Expand All @@ -41,7 +42,6 @@ import app.pachli.core.preferences.SharedPreferencesRepository
import app.pachli.core.preferences.getNonNullString
import at.connyduck.calladapter.networkresult.NetworkResultCallAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -69,7 +69,7 @@ object NetworkModule {
@Provides
@Singleton
fun providesMoshi(): Moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter())
.add(Date::class.java, LenientRfc3339DateJsonAdapter())
.add(VersionAdapter())
.add(Guarded.Factory())
.add(HasDefault.Factory())
Expand Down
Loading

0 comments on commit 4c7107c

Please sign in to comment.