Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,34 @@ sealed interface HomeRowConfig {
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): GetItems = this.copy(viewOptions = viewOptions)
}

/**
* Fetch items from an arbitrary Jellyfin endpoint that returns a QueryResult<BaseItemDto>.
* Lets third-party plugins (e.g. home-sections) contribute rows without Wholphin needing
* specific knowledge of them.
*
* Headers/Query are a list of [KeyValueEntry] (not a Map) because the server-side plugin
* config is persisted as XML, which doesn't support IDictionary.
*/
@Serializable
@SerialName("CustomEndpoint")
data class CustomEndpoint(
val endpoint: String,
val title: String,
val headers: List<KeyValueEntry>? = null,
val query: List<KeyValueEntry>? = null,
override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(),
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): CustomEndpoint = this.copy(viewOptions = viewOptions)
}
}

@Serializable
data class KeyValueEntry(
val key: String,
val value: String,
)

/**
* Root class for home page settings
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.damontecres.wholphin.services

import androidx.core.net.toUri
import com.github.damontecres.wholphin.R
import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.BaseItem
Expand All @@ -8,6 +9,7 @@ import com.github.damontecres.wholphin.data.model.ServerUserConfig
import com.github.damontecres.wholphin.data.model.createGenreDestination
import com.github.damontecres.wholphin.data.model.createStudioDestination
import com.github.damontecres.wholphin.preferences.HomePagePreferences
import com.github.damontecres.wholphin.services.hilt.AuthOkHttpClient
import com.github.damontecres.wholphin.ui.HomeItemFields
import com.github.damontecres.wholphin.ui.ProgramItemFields
import com.github.damontecres.wholphin.ui.components.getGenreImageMap
Expand All @@ -32,10 +34,18 @@ import com.github.damontecres.wholphin.util.HomeRowLoadingState
import com.github.damontecres.wholphin.util.HomeRowLoadingState.Success
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.decodeFromStream
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import org.jellyfin.sdk.api.client.ApiClient
import org.jellyfin.sdk.api.client.exception.InvalidStatusException
import org.jellyfin.sdk.api.client.extensions.liveTvApi
import org.jellyfin.sdk.api.client.extensions.userLibraryApi
import org.jellyfin.sdk.model.DateTime
import org.jellyfin.sdk.model.api.BaseItemDto
import org.jellyfin.sdk.model.api.BaseItemDtoQueryResult
import org.jellyfin.sdk.model.api.BaseItemKind
import org.jellyfin.sdk.model.api.CollectionType
import org.jellyfin.sdk.model.api.GetProgramsDto
Expand All @@ -51,15 +61,18 @@ import org.jellyfin.sdk.model.api.request.GetLiveTvChannelsRequest
import org.jellyfin.sdk.model.api.request.GetPersonsRequest
import org.jellyfin.sdk.model.api.request.GetRecordingsRequest
import org.jellyfin.sdk.model.api.request.GetStudiosRequest
import timber.log.Timber
import java.time.LocalDateTime
import javax.inject.Inject
import javax.inject.Singleton

@OptIn(ExperimentalSerializationApi::class)
@Singleton
class HomeDataService
@Inject
constructor(
private val api: ApiClient,
@param:AuthOkHttpClient private val authOkHttpClient: OkHttpClient,
private val serverRepository: ServerRepository,
private val latestNextUpService: LatestNextUpService,
private val imageUrlService: ImageUrlService,
Expand Down Expand Up @@ -669,7 +682,67 @@ class HomeDataService
)
}
}

is HomeRowConfig.CustomEndpoint -> {
val title = StringStringProvider(row.title)
try {
val items =
fetchCustomEndpointItems(row)
.map { BaseItem(it, row.viewOptions.useSeries) }
Success(title, items, row.viewOptions, rowType = row)
} catch (ex: Exception) {
Timber.w(ex, "Custom endpoint %s failed", row.endpoint)
HomeRowLoadingState.Error(title, exception = ex)
}
}
}

private suspend fun fetchCustomEndpointItems(row: HomeRowConfig.CustomEndpoint): List<BaseItemDto> {
val base =
api.baseUrl?.toHttpUrlOrNull()
?: throw IllegalStateException("Jellyfin baseUrl not set")
val endpointUri = row.endpoint.toUri()
if (!endpointUri.isRelative) {
throw IllegalArgumentException("Custom endpoint must be an path relative to Jellyfin baseUrl: ${row.endpoint}")
}
val params =
buildMap {
serverRepository.currentUser
?.id
?.toString()
?.let { put("userId", it) }
row.query?.forEach { put(it.key, it.value) }
}
val resolved =
base
.resolve(row.endpoint)
?.newBuilder()
?.apply {
params.forEach { (k, v) -> addQueryParameter(k, v) }
}?.build()
?: throw IllegalStateException("Could not resolve endpoint: ${row.endpoint}")
val request =
Request
.Builder()
.url(resolved)
.get()
.apply {
row.headers?.forEach { header(it.key, it.value) }
}.build()
val response =
authOkHttpClient
.newCall(request)
.execute()
return response.use {
if (!it.isSuccessful) {
throw InvalidStatusException(it.code, null)
}
val queryResult =
HomeSettingsService.jsonParser.decodeFromStream<BaseItemDtoQueryResult>(it.body.byteStream())
Timber.v("Got %s items from custom endpoint", queryResult.items.size)
queryResult.items
}
}
}

private val Library?.itemFields: List<ItemFields>
Expand All @@ -681,9 +754,10 @@ private val Library?.itemFields: List<ItemFields>
HomeItemFields
}

private val Library.includeItemTypes: List<BaseItemKind>? get() =
collectionType.let { collectionType ->
getTypeFor(collectionType)?.let { type -> listOf(type) }
}
private val Library.includeItemTypes: List<BaseItemKind>?
get() =
collectionType.let { collectionType ->
getTypeFor(collectionType)?.let { type -> listOf(type) }
}

private val HomeItemFieldsBoxSets get() = HomeItemFields + listOf(ItemFields.CHILD_COUNT)
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ class HomeSettingsService
config,
)
}

is HomeRowConfig.CustomEndpoint -> {
HomeRowConfigDisplay(
id = id,
title = StringStringProvider(config.title),
config,
)
}
}

private suspend fun getItemName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ class HomeSettingsViewModel
is HomeRowConfig.TvChannels -> {
it.config.updateViewOptions(preset.liveTv)
}

is HomeRowConfig.CustomEndpoint -> {
it.config
}
}
it.copy(config = newConfig)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.damontecres.wholphin.test
import com.github.damontecres.wholphin.data.model.HomeGetItemsFilter
import com.github.damontecres.wholphin.data.model.HomeRowConfig
import com.github.damontecres.wholphin.data.model.HomeRowViewOptions
import com.github.damontecres.wholphin.data.model.KeyValueEntry
import com.github.damontecres.wholphin.preferences.PrefContentScale
import com.github.damontecres.wholphin.services.HomeSettingsService
import com.github.damontecres.wholphin.ui.AspectRatio
Expand Down Expand Up @@ -77,6 +78,12 @@ class TestHomeRowSamples {
HomeRowConfig.TvPrograms(),
HomeRowConfig.TvChannels(),
HomeRowConfig.Suggestions(parentId = UUID.randomUUID()),
HomeRowConfig.CustomEndpoint(
endpoint = "/path/to/items",
title = "Items",
query = listOf(KeyValueEntry("key", "value")),
headers = listOf(KeyValueEntry("X-Header", "1234")),
),
)
}

Expand All @@ -100,6 +107,7 @@ class TestHomeRowSamples {
is HomeRowConfig.Suggestions -> foundTypes.add(it::class)
is HomeRowConfig.TvChannels -> foundTypes.add(it::class)
is HomeRowConfig.Studios -> foundTypes.add(it::class)
is HomeRowConfig.CustomEndpoint -> foundTypes.add(it::class)
}
}
Assert.assertEquals(HomeRowConfig::class.sealedSubclasses.size, foundTypes.size)
Expand Down
Loading