-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
425 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package me.him188.ani.app.data | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import me.him188.ani.datasources.api.PageBasedSearchSession | ||
import me.him188.ani.datasources.api.Paged | ||
import me.him188.ani.datasources.api.processPagedResponse | ||
import me.him188.ani.datasources.bangumi.BangumiClient | ||
import me.him188.ani.utils.logging.logger | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import org.openapitools.client.infrastructure.ClientException | ||
import org.openapitools.client.models.Revision | ||
|
||
sealed interface RevisionRepository { | ||
fun getCommentsByEpisodeId(episodeId: Int): Flow<Comment> | ||
} | ||
|
||
interface EpisodeRevisionRepository : RevisionRepository | ||
interface SubjectRevisionRepository : RevisionRepository | ||
|
||
class Comment( | ||
val id: String, | ||
val type: Int, | ||
val summary: String, | ||
val createdAt: Long, // timestamp millis | ||
val authorUsername: String?, | ||
) | ||
|
||
class EpisodeRevisionRepositoryImpl : EpisodeRevisionRepository, KoinComponent { | ||
private val client by inject<BangumiClient>() | ||
private val logger = logger(EpisodeRepositoryImpl::class) | ||
|
||
override fun getCommentsByEpisodeId(episodeId: Int): Flow<Comment> { | ||
return PageBasedSearchSession { page -> | ||
try { | ||
val pageSize = 30 | ||
client.api.getEpisodeRevisions( | ||
offset = page * pageSize, limit = pageSize, | ||
episodeId = episodeId, | ||
).run { | ||
Paged.processPagedResponse(total, pageSize, data?.map { | ||
it.toComment() | ||
}) | ||
} | ||
} catch (e: ClientException) { | ||
logger.warn("Exception in getCollections", e) | ||
null | ||
} | ||
}.results | ||
} | ||
// override fun getCommentById(commentId: Int): Comment? { | ||
// return client.api.getEpisodeRevisionByRevisionId( | ||
// revisionId = commentId, | ||
// ) | ||
// } | ||
} | ||
|
||
private fun Revision.toComment(): Comment { | ||
return Comment( | ||
id = id.toString(), | ||
type = type, | ||
summary = summary, | ||
createdAt = createdAt.toEpochSecond() * 1000, | ||
authorUsername = creator?.username, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.him188.ani.app.data | ||
|
||
import me.him188.ani.datasources.bangumi.BangumiClient | ||
import me.him188.ani.utils.logging.logger | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import org.openapitools.client.models.User | ||
|
||
interface UserRepository { | ||
fun getUserByUsername(username: String): User? | ||
} | ||
|
||
class UserRepositoryImpl : UserRepository, KoinComponent { | ||
private val client by inject<BangumiClient>() | ||
private val logger = logger(UserRepositoryImpl::class) | ||
|
||
override fun getUserByUsername(username: String): User? { | ||
return kotlin.runCatching { | ||
client.api.getUserByName(username) | ||
}.onFailure { | ||
logger.warn("Exception in getUserByUsername", it) | ||
}.getOrNull() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.