From 3dce318fa553faec54ee21204e2813816d341b69 Mon Sep 17 00:00:00 2001 From: y9Kap Date: Thu, 7 Mar 2024 23:09:17 +0300 Subject: [PATCH] feat(#93-add-getSubscriptions-and-getSubscribers-methods): support relationships was added --- .../sdk/engine/ktor/KtorMeetacyEngine.kt | 2 + .../ktor/requests/friends/FriendsEngine.kt | 38 +++++++++++++-- .../engine/requests/GetSubscribersRequest.kt | 17 +++++++ .../requests/GetSubscriptionsRequest.kt | 17 +++++++ .../sdk/friends/AuthorizedFriendsApi.kt | 24 ++++++++++ .../app/meetacy/sdk/friends/FriendsApi.kt | 46 +++++++++++++++++-- 6 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscribersRequest.kt create mode 100644 api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscriptionsRequest.kt diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt index 425db344..1fd7a727 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt @@ -124,6 +124,8 @@ public class KtorMeetacyEngine( is AddFriendRequest -> friends.add(request) as T is DeleteFriendRequest -> friends.delete(request) as T is ListFriendsRequest -> friends.list(request) as T + is GetSubscriptionsRequest -> friends.subscriptions(request) as T + is GetSubscribersRequest -> friends.subscribers(request) as T is EmitFriendsLocationRequest -> friends.streamFriendsLocation(request) as T // users is GetMeRequest -> users.getMe(request) as T diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt index b0b566c6..b58455f5 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt @@ -7,10 +7,7 @@ import app.meetacy.sdk.engine.ktor.handleRSocketExceptions import app.meetacy.sdk.engine.ktor.response.StatusTrueResponse import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess import app.meetacy.sdk.engine.ktor.token -import app.meetacy.sdk.engine.requests.AddFriendRequest -import app.meetacy.sdk.engine.requests.DeleteFriendRequest -import app.meetacy.sdk.engine.requests.EmitFriendsLocationRequest -import app.meetacy.sdk.engine.requests.ListFriendsRequest +import app.meetacy.sdk.engine.requests.* import app.meetacy.sdk.types.annotation.UnsafeConstructor import app.meetacy.sdk.types.datetime.DateTime import app.meetacy.sdk.types.location.Location @@ -102,6 +99,39 @@ internal class FriendsEngine( return ListFriendsRequest.Response(response) } + suspend fun subscriptions(request: GetSubscriptionsRequest): GetSubscriptionsRequest.Response { + val url = baseUrl / "relationship" / "subscriptions" + + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + parameter("id", request.userId?.string) + parameter("amount", request.amount.int) + parameter("pagingId", request.pagingId?.string) + }.bodyAsSuccess>() + .type() + .mapItems { user -> user.type() as RegularUser } + + return GetSubscriptionsRequest.Response(response) + } + + suspend fun subscribers(request: GetSubscribersRequest): GetSubscribersRequest.Response { + val url = baseUrl / "relationship" / "subscribers" + + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + parameter("id", request.userId?.string) + parameter("amount", request.amount.int) + parameter("pagingId", request.pagingId?.string) + }.bodyAsSuccess>() + .type() + .mapItems { user -> user.type() as RegularUser } + + return GetSubscribersRequest.Response(response) + } + + suspend fun streamFriendsLocation(request: EmitFriendsLocationRequest) = handleRSocketExceptions(json) { val url = baseUrl.replaceProtocolWithWebsocket() / "location" / "stream" diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscribersRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscribersRequest.kt new file mode 100644 index 00000000..901a1789 --- /dev/null +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscribersRequest.kt @@ -0,0 +1,17 @@ +package app.meetacy.sdk.engine.requests + +import app.meetacy.sdk.types.amount.Amount +import app.meetacy.sdk.types.auth.Token +import app.meetacy.sdk.types.paging.PagingId +import app.meetacy.sdk.types.paging.PagingResponse +import app.meetacy.sdk.types.user.RegularUser +import app.meetacy.sdk.types.user.UserId + +public data class GetSubscribersRequest( + val token: Token, + val amount: Amount, + val pagingId: PagingId?, + val userId: UserId?, +) : MeetacyRequest { + public data class Response(val paging: PagingResponse) +} diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscriptionsRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscriptionsRequest.kt new file mode 100644 index 00000000..639ac563 --- /dev/null +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetSubscriptionsRequest.kt @@ -0,0 +1,17 @@ +package app.meetacy.sdk.engine.requests + +import app.meetacy.sdk.types.amount.Amount +import app.meetacy.sdk.types.auth.Token +import app.meetacy.sdk.types.paging.PagingId +import app.meetacy.sdk.types.paging.PagingResponse +import app.meetacy.sdk.types.user.RegularUser +import app.meetacy.sdk.types.user.UserId + +public data class GetSubscriptionsRequest( + val token: Token, + val amount: Amount, + val pagingId: PagingId?, + val userId: UserId?, +) : MeetacyRequest { + public data class Response(val paging: PagingResponse) +} diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/friends/AuthorizedFriendsApi.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/friends/AuthorizedFriendsApi.kt index af17ccc6..5b8b959b 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/friends/AuthorizedFriendsApi.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/friends/AuthorizedFriendsApi.kt @@ -36,6 +36,30 @@ public class AuthorizedFriendsApi(private val api: AuthorizedMeetacyApi) { ) } + public suspend fun subscriptions( + amount: Amount, + pagingId: PagingId? = null, + userId: UserId? = null + ): PagingRepository = + base.subscriptions(token, amount, pagingId, userId).mapItems { user -> + AuthorizedRegularUserRepository( + data = user.data, + api = api + ) + } + + public suspend fun subscribers( + amount: Amount, + pagingId: PagingId? = null, + userId: UserId? = null + ): PagingRepository = + base.subscribers(token, amount, pagingId, userId).mapItems { user -> + AuthorizedRegularUserRepository( + data = user.data, + api = api + ) + } + public fun paging( chunkSize: Amount, startPagingId: PagingId? = null, diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt index b3c3830a..dbaebaee 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt @@ -1,9 +1,7 @@ package app.meetacy.sdk.friends import app.meetacy.sdk.MeetacyApi -import app.meetacy.sdk.engine.requests.AddFriendRequest -import app.meetacy.sdk.engine.requests.DeleteFriendRequest -import app.meetacy.sdk.engine.requests.ListFriendsRequest +import app.meetacy.sdk.engine.requests.* import app.meetacy.sdk.friends.location.FriendsLocationApi import app.meetacy.sdk.users.RegularUserRepository import app.meetacy.sdk.types.amount.Amount @@ -44,6 +42,48 @@ public class FriendsApi(private val api: MeetacyApi) { } } + public suspend fun subscriptions( + token: Token, + amount: Amount, + pagingId: PagingId? = null, + userId: UserId? = null + ): PagingRepository = PagingRepository( + amount = amount, + startPagingId = pagingId + ) { currentAmount, currentPagingId -> + api.engine.execute( + request = GetSubscriptionsRequest( + token = token, + userId = userId, + amount = currentAmount, + pagingId = currentPagingId + ) + ).paging.mapItems { regularUser -> + RegularUserRepository(regularUser, api) + } + } + + public suspend fun subscribers( + token: Token, + amount: Amount, + pagingId: PagingId? = null, + userId: UserId? = null, + ): PagingRepository = PagingRepository( + amount = amount, + startPagingId = pagingId + ) { currentAmount, currentPagingId -> + api.engine.execute( + request = GetSubscribersRequest( + token = token, + userId = userId, + amount = currentAmount, + pagingId = currentPagingId + ) + ).paging.mapItems { regularUser -> + RegularUserRepository(regularUser, api) + } + } + public fun paging( token: Token, chunkSize: Amount,