Skip to content

Commit

Permalink
filter nsfw content if not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 17, 2024
1 parent 3b2eb19 commit c778162
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ data object NetworkModule {
)
}
bindSingleton<TraceRepository> {
val appSettings = instance<Settings.PlatformAppSettings>()

TraceRepository(
trace = instance(),
nsfw = appSettings.adultContent
)
}
bindSingleton<NekosRepository> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable

class TraceRepository(
private val trace: Trace
private val trace: Trace,
private val nsfw: Flow<Boolean> = flowOf(false),
) {

private val byteArray = MutableStateFlow<ByteArray?>(null)

@OptIn(ExperimentalCoroutinesApi::class)
val response: Flow<State> = byteArray.transformLatest {
return@transformLatest if (it == null || it.isEmpty()) {
val response: Flow<State> = combine(byteArray, nsfw.distinctUntilChanged()) { t1, t2 ->
t1 to t2
}.transformLatest { (t1, t2) ->
return@transformLatest if (t1 == null || t1.isEmpty()) {
emit(State.None)
} else {
emit(
State.fromResponse(
CatchResult.repeat(2) {
trace.search(it)
}.asNullableSuccess()
response = CatchResult.repeat(2) {
trace.search(t1)
}.asNullableSuccess(),
nsfw = t2
)
)
}
Expand All @@ -44,11 +48,13 @@ class TraceRepository(
data object Error : State

companion object {
fun fromResponse(response: SearchResponse?): State {
return if (response == null || response.isError) {
fun fromResponse(response: SearchResponse?, nsfw: Boolean): State {
val nsfwAware = response?.nsfwAware(nsfw)

return if (nsfwAware == null || nsfwAware.isError) {
Error
} else {
Success(response)
Success(nsfwAware)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class SearchResponse(
) {

@Transient
val isError = !error.isNullOrBlank()
val isError = !error.isNullOrBlank() || result.isEmpty()

@Transient
val combinedResults: Set<CombinedResult> = result.groupBy { it.aniList.id }.mapValues { entry ->
Expand All @@ -25,6 +25,17 @@ data class SearchResponse(
)
}.values.toSet()

fun nsfwAware(allowed: Boolean): SearchResponse {
return if (allowed) {
this
} else {
SearchResponse(
error = error,
result = result.filterNot { it.aniList.isAdult }
)
}
}

@Serializable
data class Result(
@SerialName("anilist") val aniList: AniList,
Expand Down

0 comments on commit c778162

Please sign in to comment.