Skip to content

Commit

Permalink
WIP(#76-support-search): added DateTime in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Oct 30, 2023
1 parent 541f268 commit 2c74059
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class KtorMeetacyEngine(
private val meetings = MeetingsEngine(baseUrl, this.httpClient)
private val files = FilesEngine(baseUrl, this.httpClient)
private val invitations = InvitationsEngine(baseUrl, this.httpClient)
private val notifications = NotificationsEngine(baseUrl, this.httpClient, this.json)
private val notifications = NotificationsEngine(baseUrl, this.httpClient)
private val search = SearchEngine(baseUrl, this.httpClient)
private val updates = UpdatesEngine(baseUrl, this.httpClient, this.json)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
package app.meetacy.sdk.engine.ktor.requests.notifications

import app.meetacy.sdk.engine.ktor.mapToNotification
import app.meetacy.sdk.engine.ktor.requests.extencion.post
import app.meetacy.sdk.engine.ktor.models.ListNotificationsResponse
import app.meetacy.sdk.engine.ktor.models.StatusTrueResponse
import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.response.ListMeetingsResponse
import app.meetacy.sdk.engine.ktor.response.ListNotificationsResponse
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.ktor.token
import app.meetacy.sdk.engine.requests.CancelInvitationRequest
import app.meetacy.sdk.engine.requests.ListNotificationsRequest
import app.meetacy.sdk.engine.requests.ReadNotificationRequest
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.invitation.InvitationIdSerializable
import app.meetacy.sdk.types.serializable.invitation.serializable
import app.meetacy.sdk.types.serializable.paging.PagingIdSerializable
import app.meetacy.sdk.types.serializable.paging.serializable
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import io.ktor.client.request.*
import kotlinx.serialization.Serializable

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

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

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

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

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

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

val paging = PagingResponse(
data = response.data.map { it.mapToNotification() },
data = response.data.map { it },
nextPagingId = response.nextPagingId?.let(::PagingId)
)

ListNotificationsRequest.Response(paging)
return ListNotificationsRequest.Response(paging)
}

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

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

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

return json.decodeFromString<app.meetacy.sdk.engine.ktor.models.StatusTrueResponse>(string)
}
}
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.notification.NotificationSerializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal data class ListNotificationsResponse (
@SerialName("data")
val data: List<NotificationSerializable>,
@SerialName("nextPagingId")
val nextPagingId: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.meetacy.sdk.types.serializable.datetime

import app.meetacy.sdk.types.datetime.DateOrTime
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

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

public fun DateOrTimeSerializable.type(): DateOrTime = DateOrTime.parse(string)
public fun DateOrTime.serializable(): DateOrTimeSerializable = DateOrTimeSerializable(iso8601)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.meetacy.sdk.types.serializable.datetime

import app.meetacy.sdk.types.datetime.DateTime
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

@Serializable
@JvmInline
public value class DateTimeSerializable(public val iso8601: String)

public fun DateTimeSerializable.type(): DateTime = DateTime.parse(iso8601)
public fun DateTime.serializable(): DateTimeSerializable = DateTimeSerializable(iso8601)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.meetacy.sdk.types.serializable.notification

import app.meetacy.sdk.types.notification.NotificationId
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline


@Serializable
@JvmInline
public value class NotificationIdSerializable(public val string: String)
public fun NotificationIdSerializable.type(): NotificationId = NotificationId(string)
public fun NotificationId.serializable(): NotificationIdSerializable = NotificationIdSerializable(string)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package app.meetacy.sdk.types.serializable.notification

import app.meetacy.sdk.types.notification.Notification
import app.meetacy.sdk.types.serializable.datetime.DateTimeSerializable
import app.meetacy.sdk.types.serializable.datetime.type
import app.meetacy.sdk.types.serializable.meeting.MeetingSerializable
import app.meetacy.sdk.types.serializable.meeting.type
import app.meetacy.sdk.types.serializable.user.UserSerializable
import app.meetacy.sdk.types.serializable.user.type
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public sealed class NotificationSerializable {
public abstract val id: NotificationIdSerializable
public abstract val isNew: Boolean
public abstract val date: DateTimeSerializable

@SerialName("subscription")
@Serializable
public class Subscription(
override val isNew: Boolean,
override val id: NotificationIdSerializable,
override val date: DateTimeSerializable,
public val subscriber: UserSerializable,
) : NotificationSerializable()

@SerialName("meeting_invitation")
@Serializable
public class Invitation(
override val id: NotificationIdSerializable,
override val isNew: Boolean,
override val date: DateTimeSerializable,
public val meeting: MeetingSerializable,
public val inviter: UserSerializable,
) : NotificationSerializable()
}

public fun NotificationSerializable.type(): Notification =
when (this) {
is NotificationSerializable.Invitation -> Notification.Invitation(
id = id.type(),
isNew = isNew,
meeting = meeting.type(),
date = date.type(),
inviter = inviter.type()
)
is NotificationSerializable.Subscription -> Notification.Subscription(
id = id.serializable(),
isNew = isNew,
subscriber = subscriber.mapToEndpoint(),
date = date.serializable()
)
}

0 comments on commit 2c74059

Please sign in to comment.