Skip to content

Commit

Permalink
WIP(#79-support-search): friends was fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Oct 27, 2023
1 parent ae4cc3c commit b3dbdc7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class KtorMeetacyEngine(

private val auth = AuthEngine(baseUrl, this.httpClient)
private val users = UsersEngine(baseUrl, this.httpClient)
private val friends = FriendsEngine(baseUrl, this.httpClient)
private val friends = FriendsEngine(baseUrl, this.httpClient, this.json)
private val meetings = MeetingsEngine(baseUrl, this.httpClient, this.json)
private val files = FilesEngine(baseUrl, this.httpClient)
private val invitations = InvitationsEngine(baseUrl, this.httpClient, this.json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@

package app.meetacy.sdk.engine.ktor.requests.friends

import app.meetacy.sdk.engine.ktor.*
import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.handleRSocketExceptions
import app.meetacy.sdk.engine.ktor.response.ListFriendsResponse
import app.meetacy.sdk.engine.ktor.response.StatusTrueResponse
import app.meetacy.sdk.engine.requests.*
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.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.datetime.DateTime
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingResponse
import app.meetacy.sdk.types.serializable.amount.AmountSerializable
import app.meetacy.sdk.types.serializable.amount.serializable
import app.meetacy.sdk.types.serializable.location.LocationSerializable
import app.meetacy.sdk.types.serializable.location.type
import app.meetacy.sdk.types.serializable.pagingId.PagingIdSerializable
import app.meetacy.sdk.types.serializable.pagingId.serializable
import app.meetacy.sdk.types.serializable.user.UserIdSerializable
import app.meetacy.sdk.types.serializable.user.UserSerializable
import app.meetacy.sdk.types.serializable.user.serializable
import app.meetacy.sdk.types.serializable.user.type
import app.meetacy.sdk.types.url.Url
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.UserLocationSnapshot
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.rsocket.kotlin.ktor.client.rSocket
import io.rsocket.kotlin.payload.Payload
import io.rsocket.kotlin.payload.buildPayload
Expand All @@ -33,7 +46,8 @@ import kotlinx.serialization.json.put

internal class FriendsEngine(
baseUrl: Url,
private val httpClient: HttpClient
private val httpClient: HttpClient,
val json: Json
) {
private val baseUrl = baseUrl / "friends"

Expand All @@ -52,33 +66,40 @@ internal class FriendsEngine(
return response
}

suspend fun delete(request: DeleteFriendRequest): app.meetacy.sdk.engine.ktor.models.StatusTrueResponse {
val url = baseUrl / "delete"

val jsonObject = buildJsonObject {
put("friendId", request.friendId.string)
}

val string = post(url.string, jsonObject, httpClient, request)
@Serializable
private data class DeleteFriendBody(val friendId: UserIdSerializable)
private fun DeleteFriendRequest.toBody() = DeleteFriendBody(friendId.serializable())

return json.decodeFromString<app.meetacy.sdk.engine.ktor.models.StatusTrueResponse>(string)
suspend fun delete(request: DeleteFriendRequest): StatusTrueResponse {
val url = baseUrl / "delete"
val body = request.toBody()
val response = httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.body<StatusTrueResponse>()
return response
}

@Serializable
private data class ListFriendsBody(
val amount: AmountSerializable,
val pagingId: PagingIdSerializable?
)
private fun ListFriendsRequest.toBody() = ListFriendsBody(amount.serializable(), pagingId?.serializable() )

suspend fun list(request: ListFriendsRequest): ListFriendsRequest.Response {
val url = baseUrl / "list"

val jsonObject = buildJsonObject {
put("amount", request.amount.int)
put("pagingId", request.pagingId?.string)
}

val string = post(url.string, jsonObject, httpClient, request)

val response = Json.decodeFromString<app.meetacy.sdk.engine.ktor.models.ListFriendsResponse>(string).result
val body = request.toBody()
val response = httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.body<ListFriendsResponse>()

val paging = PagingResponse(
nextPagingId = response.nextPagingId?.let(::PagingId),
data = response.data.map { user -> user.mapToRegularUser() }
data = response.data.map { user -> user.type() as RegularUser }
)

return ListFriendsRequest.Response(paging)
Expand Down Expand Up @@ -128,17 +149,17 @@ private fun Location.encodeToPayload(): Payload = buildPayload {

@Serializable
private data class ModelUserLocationSnapshotSerializable(
val user: ModelUser,
val location: ModelLocation,
val user: UserSerializable,
val location: LocationSerializable,
val capturedAt: String
)

private fun Payload.decodeToUserLocationSnapshot(json: Json): UserLocationSnapshot {
val deserialized = json.decodeFromString<ModelUserLocationSnapshotSerializable>(data.readText())

return UserLocationSnapshot(
user = deserialized.user.mapToUser() as RegularUser,
location = deserialized.location.mapToLocation(),
user = deserialized.user.type() as RegularUser,
location = deserialized.location.type(),
capturedAt = DateTime(deserialized.capturedAt)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.meetacy.sdk.engine.ktor.response

import app.meetacy.sdk.types.serializable.user.UserSerializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal data class ListFriendsResponse (
@SerialName("data")
val data: List<UserSerializable>,
@SerialName("nextPagingId")
val nextPagingId: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.user.UserId

public data class DeleteFriendRequest(
override val token: Token,
val token: Token,
val friendId: UserId
) : SimpleMeetacyRequest, SimpleMeetacyRequestWithToken
) : SimpleMeetacyRequest
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import app.meetacy.sdk.types.paging.PagingResponse
import app.meetacy.sdk.types.user.RegularUser

public data class ListFriendsRequest(
override val token: Token,
val token: Token,
val amount: Amount,
val pagingId: PagingId?
) : MeetacyRequest<ListFriendsRequest.Response>, MeetacyRequestWithToken<ListFriendsRequest.Response> {
) : MeetacyRequest<ListFriendsRequest.Response> {
public data class Response(val paging: PagingResponse<RegularUser>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@file:OptIn(UnsafeConstructor::class)

package app.meetacy.sdk.types.serializable.amount

import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

@Serializable
@JvmInline
public value class AmountSerializable(public val int: Int)

public fun AmountSerializable.type(): Amount = Amount(int)
public fun Amount.serializable(): AmountSerializable = AmountSerializable(int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app.meetacy.sdk.types.serializable.location

import app.meetacy.sdk.types.location.Location
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public data class LocationSerializable(
@SerialName("latitude")
val latitude: Double,
@SerialName("longitude")
val longitude: Double
)

public fun LocationSerializable.type(): Location = Location(latitude, longitude)
public fun Location.serializable(): LocationSerializable = LocationSerializable(latitude, longitude)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.meetacy.sdk.types.serializable.pagingId

import app.meetacy.sdk.types.paging.PagingId
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

@Serializable
@JvmInline
public value class PagingIdSerializable(public val string: String)

public fun PagingIdSerializable.type(): PagingId = PagingId(string)

public fun PagingId.serializable(): PagingIdSerializable = PagingIdSerializable(string)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.meetacy.sdk.types.paging

import app.meetacy.sdk.types.annotation.UnsafeConstructor
import kotlin.jvm.JvmInline

@JvmInline
public value class PagingId(public val string: String)
public value class PagingId @UnsafeConstructor constructor(public val string: String)

0 comments on commit b3dbdc7

Please sign in to comment.