From aa930f3a6fe96e55bb123dda3f1971521cc8110d Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Mon, 22 Apr 2024 21:34:22 +0200 Subject: [PATCH] feat: Fetch more trending posts, links, and hashtags (#634) The previous code didn't set a limit for the number of posts, links, and hashtags to fetch on the trending pages, so used the conservative defaults. Increase these to the API maximums to show the user more information. --- .../viewmodel/NetworkTimelineRemoteMediator.kt | 11 ++++++++++- .../components/trending/TrendingLinksRepository.kt | 11 ++++++++++- .../trending/viewmodel/TrendingTagsViewModel.kt | 11 ++++++++++- .../app/pachli/core/network/retrofit/MastodonApi.kt | 12 +++++++++--- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/app/pachli/components/timeline/viewmodel/NetworkTimelineRemoteMediator.kt b/app/src/main/java/app/pachli/components/timeline/viewmodel/NetworkTimelineRemoteMediator.kt index df0f47a7e..290915001 100644 --- a/app/src/main/java/app/pachli/components/timeline/viewmodel/NetworkTimelineRemoteMediator.kt +++ b/app/src/main/java/app/pachli/components/timeline/viewmodel/NetworkTimelineRemoteMediator.kt @@ -125,7 +125,7 @@ class NetworkTimelineRemoteMediator( Timeline.Home -> api.homeTimeline(maxId = maxId, minId = minId, limit = loadSize) Timeline.PublicFederated -> api.publicTimeline(local = false, maxId = maxId, minId = minId, limit = loadSize) Timeline.PublicLocal -> api.publicTimeline(local = true, maxId = maxId, minId = minId, limit = loadSize) - Timeline.TrendingStatuses -> api.trendingStatuses() + Timeline.TrendingStatuses -> api.trendingStatuses(limit = LIMIT_TRENDING_STATUSES) is Timeline.Hashtags -> { val firstHashtag = timeline.tags.first() val additionalHashtags = timeline.tags.subList(1, timeline.tags.size) @@ -167,4 +167,13 @@ class NetworkTimelineRemoteMediator( else -> throw IllegalStateException("NetworkTimelineRemoteMediator does not support $timeline") } } + + companion object { + /** + * How many trending statuses to fetch. These are not paged, so fetch the + * documented (https://docs.joinmastodon.org/methods/trends/#query-parameters-1) + * maximum. + */ + const val LIMIT_TRENDING_STATUSES = 40 + } } diff --git a/app/src/main/java/app/pachli/components/trending/TrendingLinksRepository.kt b/app/src/main/java/app/pachli/components/trending/TrendingLinksRepository.kt index 7aea9310b..ac5c795d2 100644 --- a/app/src/main/java/app/pachli/components/trending/TrendingLinksRepository.kt +++ b/app/src/main/java/app/pachli/components/trending/TrendingLinksRepository.kt @@ -23,5 +23,14 @@ import javax.inject.Inject class TrendingLinksRepository @Inject constructor( private val api: MastodonApi, ) { - suspend fun getTrendingLinks() = api.trendingLinks() + suspend fun getTrendingLinks() = api.trendingLinks(limit = LIMIT_TRENDING_LINKS) + + companion object { + /** + * How many trending links to fetch. These are not paged, so fetch the + * documented (https://docs.joinmastodon.org/methods/trends/#query-parameters-2) + * maximum. + */ + const val LIMIT_TRENDING_LINKS = 20 + } } diff --git a/app/src/main/java/app/pachli/components/trending/viewmodel/TrendingTagsViewModel.kt b/app/src/main/java/app/pachli/components/trending/viewmodel/TrendingTagsViewModel.kt index 1dd0aed18..5a8545f99 100644 --- a/app/src/main/java/app/pachli/components/trending/viewmodel/TrendingTagsViewModel.kt +++ b/app/src/main/java/app/pachli/components/trending/viewmodel/TrendingTagsViewModel.kt @@ -88,7 +88,7 @@ class TrendingTagsViewModel @Inject constructor( val deferredFilters = async { mastodonApi.getFilters() } - mastodonApi.trendingTags().fold( + mastodonApi.trendingTags(limit = LIMIT_TRENDING_HASHTAGS).fold( { tagResponse -> val firstTag = tagResponse.firstOrNull() @@ -129,4 +129,13 @@ class TrendingTagsViewModel @Inject constructor( return map { TrendingViewData.Tag.from(it, maxTrendingValue) } } + + companion object { + /** + * How many trending hashtags to fetch. These are not paged, so fetch the + * documented (https://docs.joinmastodon.org/methods/trends/#query-parameters) + * maximum. + */ + const val LIMIT_TRENDING_HASHTAGS = 20 + } } diff --git a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt index f64d0eec3..42ebcad4c 100644 --- a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt +++ b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt @@ -787,11 +787,17 @@ interface MastodonApi { suspend fun unfollowTag(@Path("name") name: String): NetworkResult @GET("api/v1/trends/tags") - suspend fun trendingTags(): NetworkResult> + suspend fun trendingTags( + @Query("limit") limit: Int? = null, + ): NetworkResult> @GET("api/v1/trends/links") - suspend fun trendingLinks(): NetworkResult> + suspend fun trendingLinks( + @Query("limit") limit: Int? = null, + ): NetworkResult> @GET("api/v1/trends/statuses") - suspend fun trendingStatuses(): Response> + suspend fun trendingStatuses( + @Query("limit") limit: Int? = null, + ): Response> }