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 b2ca2e00..f4073332 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 @@ -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) diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/notifications/NotificationsEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/notifications/NotificationsEngine.kt index 29c90517..6eeb9aaf 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/notifications/NotificationsEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/notifications/NotificationsEngine.kt @@ -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(string).result + val body = request.toBody() + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + setBody(body) + }.bodyAsSuccess() 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(string) } } diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/ListNotificationResponse.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/ListNotificationResponse.kt new file mode 100644 index 00000000..f2792c30 --- /dev/null +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/ListNotificationResponse.kt @@ -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, + @SerialName("nextPagingId") + val nextPagingId: String? = null +) diff --git a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateOrTimeSerializable.kt b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateOrTimeSerializable.kt new file mode 100644 index 00000000..707a36a3 --- /dev/null +++ b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateOrTimeSerializable.kt @@ -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) \ No newline at end of file diff --git a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateTimeSerializable.kt b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateTimeSerializable.kt new file mode 100644 index 00000000..030d5c17 --- /dev/null +++ b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateTimeSerializable.kt @@ -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) \ No newline at end of file diff --git a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationIdSerializable.kt b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationIdSerializable.kt new file mode 100644 index 00000000..5e6623e5 --- /dev/null +++ b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationIdSerializable.kt @@ -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) \ No newline at end of file diff --git a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationSerializable.kt b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationSerializable.kt new file mode 100644 index 00000000..5d8ab610 --- /dev/null +++ b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationSerializable.kt @@ -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() + ) + } \ No newline at end of file