From e68ab54f95aaa007e11ad4e0fcffafdf45d68704 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Sat, 19 Oct 2024 14:38:00 +0200 Subject: [PATCH] feat: Ignore suggested accounts that haven't posted in 28 days (#1031) The "Suggested accounts" API returns dormant accounts. See this bug report: https://github.com/mastodon/mastodon/issues/30674. That's bad UX, so filter out any account that hasn't posted in the last 28 days. Fixes #1029 --- .../NetworkSuggestionsRepository.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/core/data/src/main/kotlin/app/pachli/core/data/repository/NetworkSuggestionsRepository.kt b/core/data/src/main/kotlin/app/pachli/core/data/repository/NetworkSuggestionsRepository.kt index 2adc3b424..92143c0f3 100644 --- a/core/data/src/main/kotlin/app/pachli/core/data/repository/NetworkSuggestionsRepository.kt +++ b/core/data/src/main/kotlin/app/pachli/core/data/repository/NetworkSuggestionsRepository.kt @@ -27,6 +27,8 @@ import com.github.michaelbull.result.Result import com.github.michaelbull.result.coroutines.binding.binding import com.github.michaelbull.result.map import com.github.michaelbull.result.mapError +import java.time.Duration +import java.time.Instant import javax.inject.Inject import javax.inject.Singleton import kotlinx.coroutines.CoroutineScope @@ -38,8 +40,18 @@ class NetworkSuggestionsRepository @Inject constructor( private val api: MastodonApi, ) : SuggestionsRepository { override suspend fun getSuggestions(): Result, GetSuggestionsError> = binding { + val now = Instant.now() api.getSuggestions(limit = 80) - .map { response -> response.body.map { Suggestion.from(it) } } + .map { response -> + response.body + // Filter out accounts that haven't posted in the last LAST_STATUS_CUTOFF_DURATION. + .filter { + it.account.lastStatusAt?.let { lastStatusAt -> + Duration.between(lastStatusAt.toInstant(), now) < LAST_STATUS_CUTOFF_DURATION + } == true + } + .map { Suggestion.from(it) } + } .mapError { GetSuggestionsError(it) } .bind() } @@ -55,4 +67,12 @@ class NetworkSuggestionsRepository @Inject constructor( api.followSuggestedAccount(accountId).mapError { FollowAccountError(it) }.bind() }.await() } + + companion object { + /** + * Duration that specifies the maximum time in the past the account must have + * posted a status for inclusion. + */ + private val LAST_STATUS_CUTOFF_DURATION = Duration.ofDays(28) + } }