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
This commit is contained in:
Nik Clayton 2024-10-19 14:38:00 +02:00 committed by GitHub
parent ffb585b349
commit e68ab54f95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 1 deletions

View File

@ -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<List<Suggestion>, 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)
}
}