From 7e234ff4dd06a7e79bb7ced20e6b40156e28de58 Mon Sep 17 00:00:00 2001 From: DareFox Date: Fri, 22 Apr 2022 15:53:41 +0300 Subject: [PATCH] UserAPI: Add functions getAll*() --- src/main/kotlin/kmtt/api/user/AuthUserAPI.kt | 77 ++++++++++++++++++- .../kotlin/kmtt/api/user/IPublicUserAPI.kt | 42 ++++++++++ .../kotlin/kmtt/api/user/PublicUserAPI.kt | 38 +++++++++ .../kmtt/base/api/user/AuthUserAPITest.kt | 29 +++++++ 4 files changed, 185 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/kmtt/api/user/AuthUserAPI.kt b/src/main/kotlin/kmtt/api/user/AuthUserAPI.kt index 61668d4c..25aed7b2 100644 --- a/src/main/kotlin/kmtt/api/user/AuthUserAPI.kt +++ b/src/main/kotlin/kmtt/api/user/AuthUserAPI.kt @@ -2,7 +2,6 @@ package kmtt.api.user import io.ktor.client.request.* import io.ktor.http.* -import kmtt.constants.Content import kmtt.ktor.IHttpClient import kmtt.ktor.request import kmtt.models.Notification @@ -102,6 +101,25 @@ internal class AuthUserAPI( return response.result } + override suspend fun getAllMyComments(): List { + val user = getMe() + + val commentCount = user.counters?.comments + requireNotNull(commentCount) { + "comment count in null" + } + + val comments = mutableListOf() + + for (offset in 0 until commentCount step 50) { + val comment = getMyComments(50, offset.toInt()) + + comments += comment + } + + return comments + } + override suspend fun getMyEntries(count: Int, offset: Int): List { val endpointURL = "/user/me/comments" val params = mutableListOf>() @@ -122,6 +140,25 @@ internal class AuthUserAPI( return response.result } + override suspend fun getAllMyEntries(): List { + val user = getMe() + + val entriesCounter = user.counters?.entries + requireNotNull(entriesCounter) { + "comment count in null" + } + + val entries = mutableListOf() + + for (offset in 0 until entriesCounter step 50) { + val entry = getMyEntries(50, offset.toInt()) + + entries += entry + } + + return entries + } + override suspend fun getMyFavoriteEntries(count: Int, offset: Int): List { val endpointURL = "/user/me/favorites/entries" val params = mutableListOf>() @@ -142,6 +179,25 @@ internal class AuthUserAPI( return response.result } + override suspend fun getAllMyFavoriteEntries(): List { + val user = getMe() + + val entriesCounter = user.counters?.favorites + requireNotNull(entriesCounter) { + "comment count in null" + } + + val entries = mutableListOf() + + for (offset in 0 until entriesCounter step 50) { + val entry = getMyFavoriteEntries(50, offset.toInt()) + + entries += entry + } + + return entries + } + override suspend fun getMyFavoriteComments(count: Int, offset: Int): List { val endpointURL = "/user/me/favorites/comments" val params = mutableListOf>() @@ -162,4 +218,23 @@ internal class AuthUserAPI( return response.result } + override suspend fun getAllMyFavoriteComments(): List { + val user = getMe() + + val entriesCounter = user.counters?.favorites + requireNotNull(entriesCounter) { + "comment count in null" + } + + val comments = mutableListOf() + + for (offset in 0 until entriesCounter step 50) { + val comment = getMyFavoriteComments( 50, offset.toInt()) + + comments += comment + } + + return comments + } + } \ No newline at end of file diff --git a/src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt b/src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt index cd472663..5d0c89a8 100644 --- a/src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt +++ b/src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt @@ -14,13 +14,27 @@ interface IPublicUserAPI { /** * Получить комментарии пользователя + * + * Max count: 50 */ suspend fun getUserComments(userID: Long, count: Int, offset: Int): List + /** + * Получить все комментарии пользователя + */ + suspend fun getAllUserComments(userID: Long): List + /** * Получить записи пользователя + * + * Max count: 50 */ suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List + + /** + * Получить записи пользователя + */ + suspend fun getAllUserEntries(userID: Long): List } interface IAuthUserAPI : IPublicUserAPI, Authable { @@ -52,21 +66,49 @@ interface IAuthUserAPI : IPublicUserAPI, Authable { /** * Получить комментарии текущего пользователя + * + * Max count: 50 */ suspend fun getMyComments(count: Int, offset: Int): List + /** + * Получить все комментарии текущего пользователя + */ + suspend fun getAllMyComments(): List + /** * Получить записи текущего пользователя + * + * Max count: 50 */ suspend fun getMyEntries(count: Int, offset: Int): List + /** + * Получить все записи текущего пользователя + */ + suspend fun getAllMyEntries(): List + /** * Получить избранные записи текущего пользователя + * + * Max count: 50 */ suspend fun getMyFavoriteEntries(count: Int, offset: Int): List + /** + * Получить все избранные записи текущего пользователя + */ + suspend fun getAllMyFavoriteEntries(): List + /** * Получить избранные комментарии текущего пользователя + * + * Max count: 50 */ suspend fun getMyFavoriteComments(count: Int, offset: Int): List + + /** + * Получить все избранные комментарии текущего пользователя + */ + suspend fun getAllMyFavoriteComments(): List } \ No newline at end of file diff --git a/src/main/kotlin/kmtt/api/user/PublicUserAPI.kt b/src/main/kotlin/kmtt/api/user/PublicUserAPI.kt index 65b6210e..05f0c4a7 100644 --- a/src/main/kotlin/kmtt/api/user/PublicUserAPI.kt +++ b/src/main/kotlin/kmtt/api/user/PublicUserAPI.kt @@ -55,6 +55,25 @@ internal class PublicUserAPI(private val httpClient: IHttpClient, private val si return response.result } + override suspend fun getAllUserComments(userID: Long): List { + val user = getUserByID(userID) + + val commentCount = user.counters?.comments + requireNotNull(commentCount) { + "comment count in null" + } + + val comments = mutableListOf() + + for (offset in 0 until commentCount step 50) { + val comment = getUserComments(userID, 50, offset.toInt()) + + comments += comment + } + + return comments + } + override suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List { val endpointURL = "/user/$userID/entries" val params = mutableListOf>() @@ -74,4 +93,23 @@ internal class PublicUserAPI(private val httpClient: IHttpClient, private val si return response.result } + + override suspend fun getAllUserEntries(userID: Long): List { + val user = getUserByID(userID) + + val entriesCounter = user.counters?.entries + requireNotNull(entriesCounter) { + "comment count in null" + } + + val entries = mutableListOf() + + for (offset in 0 until entriesCounter step 50) { + val entry = getUserEntries(userID, 50, offset.toInt()) + + entries += entry + } + + return entries + } } \ No newline at end of file diff --git a/src/test/kotlin/kmtt/base/api/user/AuthUserAPITest.kt b/src/test/kotlin/kmtt/base/api/user/AuthUserAPITest.kt index 0e1404f3..c0090b51 100644 --- a/src/test/kotlin/kmtt/base/api/user/AuthUserAPITest.kt +++ b/src/test/kotlin/kmtt/base/api/user/AuthUserAPITest.kt @@ -53,6 +53,14 @@ internal class AuthUserAPITest { } } + @Test + fun getAllMyComments() { + assertDoesNotThrow { + api.getAllMyComments() + } + } + + @Test fun getMyEntries() { assertDoesNotThrow { @@ -60,6 +68,13 @@ internal class AuthUserAPITest { } } + @Test + fun getAllMyEntries() { + assertDoesNotThrow { + api.getAllMyEntries() + } + } + @Test fun getMyFavoriteEntries() { assertDoesNotThrow { @@ -67,6 +82,13 @@ internal class AuthUserAPITest { } } + @Test + fun getAllMyFavoriteEntries() { + assertDoesNotThrow { + api.getAllMyFavoriteEntries() + } + } + @Test fun getMyFavoriteComments() { assertDoesNotThrow { @@ -74,6 +96,13 @@ internal class AuthUserAPITest { } } + @Test + fun getAllMyFavoriteComments() { + assertDoesNotThrow { + api.getAllMyFavoriteComments() + } + } + @Test fun getUserByID() { Shared.publicProfileID.forEach {