Merge pull request #7215 from Bnyro/master

refactor: remove channel info api calls when subscribing to channels locally
This commit is contained in:
Bnyro 2025-03-16 17:23:39 +01:00 committed by GitHub
commit 246e49e145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 47 additions and 25 deletions

View File

@ -43,7 +43,10 @@ object SubscriptionHelper {
else -> PipedNoAccountFeedRepository() else -> PipedNoAccountFeedRepository()
} }
suspend fun subscribe(channelId: String) = subscriptionsRepository.subscribe(channelId) suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) = subscriptionsRepository.subscribe(channelId, name, uploaderAvatar, verified)
suspend fun unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId) suspend fun unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId)
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId) suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
suspend fun importSubscriptions(newChannels: List<String>) = suspend fun importSubscriptions(newChannels: List<String>) =

View File

@ -6,10 +6,12 @@ import com.github.libretube.api.obj.Subscription
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
class AccountSubscriptionsRepository: SubscriptionsRepository { class AccountSubscriptionsRepository : SubscriptionsRepository {
private val token get() = PreferenceHelper.getToken() private val token get() = PreferenceHelper.getToken()
override suspend fun subscribe(channelId: String) { override suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) {
runCatching { runCatching {
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId)) RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
} }

View File

@ -7,16 +7,15 @@ import com.github.libretube.extensions.parallelMap
import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL
import org.schabi.newpipe.extractor.channel.ChannelInfo import org.schabi.newpipe.extractor.channel.ChannelInfo
class LocalSubscriptionsRepository: SubscriptionsRepository { class LocalSubscriptionsRepository : SubscriptionsRepository {
override suspend fun subscribe(channelId: String) { override suspend fun subscribe(
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}" channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
val channelInfo = ChannelInfo.getInfo(channelUrl) ) {
val localSubscription = LocalSubscription( val localSubscription = LocalSubscription(
channelId = channelInfo.id, channelId = channelId,
name = channelInfo.name, name = name,
avatar = channelInfo.avatars.maxByOrNull { it.height }?.url, avatar = uploaderAvatar,
verified = channelInfo.isVerified verified = verified
) )
Database.localSubscriptionDao().insert(localSubscription) Database.localSubscriptionDao().insert(localSubscription)
@ -33,7 +32,10 @@ class LocalSubscriptionsRepository: SubscriptionsRepository {
override suspend fun importSubscriptions(newChannels: List<String>) { override suspend fun importSubscriptions(newChannels: List<String>) {
for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) { for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) {
chunk.parallelMap { channelId -> chunk.parallelMap { channelId ->
runCatching { subscribe(channelId) } val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
val channelInfo = ChannelInfo.getInfo(channelUrl)
runCatching { subscribe(channelId, channelInfo.name, channelInfo.avatars.maxByOrNull { it.height }?.url, channelInfo.isVerified) }
} }
} }
} }

View File

@ -6,8 +6,10 @@ import com.github.libretube.api.obj.Subscription
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.LocalSubscription import com.github.libretube.db.obj.LocalSubscription
class PipedLocalSubscriptionsRepository: SubscriptionsRepository { class PipedLocalSubscriptionsRepository : SubscriptionsRepository {
override suspend fun subscribe(channelId: String) { override suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) {
// further meta info is not needed when using Piped local subscriptions // further meta info is not needed when using Piped local subscriptions
Database.localSubscriptionDao().insert(LocalSubscription(channelId)) Database.localSubscriptionDao().insert(LocalSubscription(channelId))
} }
@ -29,9 +31,9 @@ class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
val channelIds = getSubscriptionChannelIds() val channelIds = getSubscriptionChannelIds()
return when { return when {
channelIds.size > GET_SUBSCRIPTIONS_LIMIT -> channelIds.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
RetrofitInstance.authApi channelIds
.unauthenticatedSubscriptions(channelIds) )
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions( else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
channelIds.joinToString(",") channelIds.joinToString(",")

View File

@ -3,7 +3,7 @@ package com.github.libretube.repo
import com.github.libretube.api.obj.Subscription import com.github.libretube.api.obj.Subscription
interface SubscriptionsRepository { interface SubscriptionsRepository {
suspend fun subscribe(channelId: String) suspend fun subscribe(channelId: String, name: String, uploaderAvatar: String?, verified: Boolean)
suspend fun unsubscribe(channelId: String) suspend fun unsubscribe(channelId: String)
suspend fun isSubscribed(channelId: String): Boolean? suspend fun isSubscribed(channelId: String): Boolean?
suspend fun importSubscriptions(newChannels: List<String>) suspend fun importSubscriptions(newChannels: List<String>)

View File

@ -159,7 +159,12 @@ class SearchResultsAdapter(
} }
var subscribed = false var subscribed = false
binding.searchSubButton.setupSubscriptionButton(item.url.toID(), item.name?.toID()) { binding.searchSubButton.setupSubscriptionButton(
item.url.toID(),
item.name.orEmpty(),
item.uploaderAvatar,
item.uploaderVerified ?: false
) {
subscribed = it subscribed = it
} }

View File

@ -52,6 +52,8 @@ class SubscriptionChannelAdapter :
subscriptionSubscribe.setupSubscriptionButton( subscriptionSubscribe.setupSubscriptionButton(
subscription.url.toID(), subscription.url.toID(),
subscription.name, subscription.name,
subscription.avatar,
subscription.verified,
notificationBell, notificationBell,
true true
) )

View File

@ -15,7 +15,9 @@ import kotlinx.coroutines.withContext
fun TextView.setupSubscriptionButton( fun TextView.setupSubscriptionButton(
channelId: String?, channelId: String?,
channelName: String?, channelName: String,
channelAvatar: String?,
channelVerified: Boolean,
notificationBell: MaterialButton? = null, notificationBell: MaterialButton? = null,
isSubscribed: Boolean? = null, isSubscribed: Boolean? = null,
onIsSubscribedChange: (Boolean) -> Unit = {} onIsSubscribedChange: (Boolean) -> Unit = {}
@ -53,7 +55,7 @@ fun TextView.setupSubscriptionButton(
} else { } else {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
SubscriptionHelper.subscribe(channelId) SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
} }
text = context.getString(R.string.unsubscribe) text = context.getString(R.string.unsubscribe)

View File

@ -150,7 +150,9 @@ class ChannelFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel)
binding.channelSubscribe.setupSubscriptionButton( binding.channelSubscribe.setupSubscriptionButton(
channelId, channelId,
channelName, response.name.orEmpty(),
response.avatarUrl,
response.verified,
binding.notificationBell binding.notificationBell
) { isSubscribed -> ) { isSubscribed ->
_binding?.addToGroup?.isVisible = isSubscribed _binding?.addToGroup?.isVisible = isSubscribed

View File

@ -1147,8 +1147,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
// update the subscribed state // update the subscribed state
binding.playerSubscribe.setupSubscriptionButton( binding.playerSubscribe.setupSubscriptionButton(
this.streams.uploaderUrl.toID(), streams.uploaderUrl.toID(),
this.streams.uploader streams.uploader,
streams.uploaderAvatar,
streams.uploaderVerified
) )
// seekbar preview setup // seekbar preview setup