Skip to content

Commit b280bf7

Browse files
committed
Revert search API.
1 parent ab9f21a commit b280bf7

1 file changed

Lines changed: 9 additions & 46 deletions

File tree

app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import me.him188.ani.datasources.api.topic.UnifiedCollectionType
3737
import me.him188.ani.datasources.bangumi.models.BangumiSubjectType
3838
import me.him188.ani.datasources.bangumi.models.search.BangumiSort
3939
import me.him188.ani.utils.logging.logger
40-
import me.him188.ani.utils.logging.warn
4140
import kotlin.coroutines.CoroutineContext
4241
import kotlin.coroutines.cancellation.CancellationException
4342

@@ -81,7 +80,7 @@ class SubjectSearchRepository(
8180
val offset = params.key
8281
?: return@withContext LoadResult.Error(IllegalArgumentException("Key is null"))
8382
return@withContext try {
84-
val res = /*bangumiSubjectSearchService.searchSubjectIds*/aniSubjectSearchService.searchSubjects(
83+
val res = bangumiSubjectSearchService.searchSubjectIds(
8584
searchQuery.keywords,
8685
useNewApi = useNewApi(),
8786
offset = offset,
@@ -90,20 +89,23 @@ class SubjectSearchRepository(
9089
sort = searchQuery.sort.toBangumiSort(),
9190
)
9291

93-
val filtered = if (ignoreDoneAndDropped()) {
92+
val filteredIds = if (ignoreDoneAndDropped()) {
9493
val excludedIds = subjectCollectionRepository.getSubjectIdsByCollectionType(
9594
types = listOf(UnifiedCollectionType.DONE, UnifiedCollectionType.DROPPED),
9695
).first()
9796

98-
buildList {
99-
res.forEach { if (it.subjectInfo.subjectId !in excludedIds) add(it) }
97+
MutableIntList().apply {
98+
res.forEach { if (it !in excludedIds) add(it) }
10099
}
101100
} else {
102101
res
103102
}
104103

105104
// 在分页源中直接过滤掉不符合条件的数据 #2380
106-
val subjectInfos = filterSubjectsBySort(filtered, searchQuery.sort)
105+
val subjectInfos = filterSubjectsBySort(
106+
subjectService.batchGetSubjectDetails(filteredIds),
107+
searchQuery.sort,
108+
)
107109

108110
return@withContext LoadResult.Page(
109111
subjectInfos,
@@ -113,45 +115,7 @@ class SubjectSearchRepository(
113115
} catch (e: CancellationException) {
114116
throw e
115117
} catch (e: Exception) {
116-
try {
117-
Companion.logger.warn(e) { "Failed to search by ANI api, trying fallback Bangumi api." }
118-
119-
val res = bangumiSubjectSearchService.searchSubjectIds(
120-
searchQuery.keywords,
121-
useNewApi = useNewApi(),
122-
offset = offset,
123-
limit = params.loadSize,
124-
filters = filters,
125-
sort = searchQuery.sort.toBangumiSort(),
126-
)
127-
128-
val filtered = if (ignoreDoneAndDropped()) {
129-
val excludedIds = subjectCollectionRepository.getSubjectIdsByCollectionType(
130-
types = listOf(UnifiedCollectionType.DONE, UnifiedCollectionType.DROPPED),
131-
).first()
132-
133-
MutableIntList().apply {
134-
res.forEach { if (it !in excludedIds) add(it) }
135-
}
136-
} else {
137-
res
138-
}
139-
140-
val subjectInfos = filterSubjectsBySort(
141-
subjectService.batchGetSubjectDetails(filtered),
142-
searchQuery.sort,
143-
)
144-
145-
return@withContext LoadResult.Page(
146-
subjectInfos,
147-
prevKey = if (offset == 0) null else offset,
148-
nextKey = if (subjectInfos.isEmpty()) null else offset + params.loadSize,
149-
)
150-
} catch (e: CancellationException) {
151-
throw e
152-
} catch (e: Exception) {
153-
LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e))
154-
}
118+
LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e))
155119
}
156120
}
157121

@@ -273,4 +237,3 @@ class SubjectSearchRepository(
273237
private fun SubjectType.toBangumiSubjectType(): BangumiSubjectType = when (this) {
274238
SubjectType.ANIME -> BangumiSubjectType.Anime
275239
}
276-

0 commit comments

Comments
 (0)