From 67a951f39e0051d79e5bbc40037549e488a31e5c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 5 Feb 2023 11:05:03 +0100 Subject: [PATCH] Unauthenticated: Use GET when subs <= 100 --- .../java/com/github/libretube/api/PipedApi.kt | 10 ++++++++++ .../libretube/api/SubscriptionHelper.kt | 20 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/PipedApi.kt b/app/src/main/java/com/github/libretube/api/PipedApi.kt index 809fdc6ce..c8fb8fdf7 100644 --- a/app/src/main/java/com/github/libretube/api/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/api/PipedApi.kt @@ -108,6 +108,11 @@ interface PipedApi { @GET("feed") suspend fun getFeed(@Query("authToken") token: String?): List + @GET("feed/unauthenticated") + suspend fun getUnauthenticatedFeed( + @Query("channels") channels: String + ): List + @POST("feed/unauthenticated") suspend fun getUnauthenticatedFeed( @Body channels: List @@ -122,6 +127,11 @@ interface PipedApi { @GET("subscriptions") suspend fun subscriptions(@Header("Authorization") token: String): List + @GET("subscriptions/unauthenticated") + suspend fun unauthenticatedSubscriptions( + @Query("channels") channels: String + ): List + @POST("subscriptions/unauthenticated") suspend fun unauthenticatedSubscriptions( @Body channels: List diff --git a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt index f2b1f0491..a54e8ffa4 100644 --- a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt @@ -17,6 +17,8 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext object SubscriptionHelper { + private const val GET_SUBSCRIPTIONS_LIMIT = 100 + suspend fun subscribe(channelId: String) { val token = PreferenceHelper.getToken() if (token.isNotEmpty()) { @@ -108,7 +110,14 @@ object SubscriptionHelper { RetrofitInstance.authApi.subscriptions(token) } else { val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId } - RetrofitInstance.authApi.unauthenticatedSubscriptions(subscriptions) + when { + subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions( + subscriptions + ) + else -> RetrofitInstance.authApi.unauthenticatedSubscriptions( + subscriptions.joinToString(",") + ) + } } } @@ -118,7 +127,14 @@ object SubscriptionHelper { RetrofitInstance.authApi.getFeed(token) } else { val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId } - RetrofitInstance.authApi.getUnauthenticatedFeed(subscriptions) + when { + subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.getUnauthenticatedFeed( + subscriptions + ) + else -> RetrofitInstance.authApi.getUnauthenticatedFeed( + subscriptions.joinToString(",") + ) + } } } }