From ee88e2977040742162aa6ee93a05547a89e37e61 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 29 Jan 2024 19:44:50 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Notifications=20API=E3=82=92Paginat?= =?UTF-8?q?ion=20API=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MastodonNotificationApiController.kt | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/notification/MastodonNotificationApiController.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/notification/MastodonNotificationApiController.kt index ba83cb37..1c8905c1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/notification/MastodonNotificationApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/notification/MastodonNotificationApiController.kt @@ -1,5 +1,8 @@ package dev.usbharu.hideout.mastodon.interfaces.api.notification +import dev.usbharu.hideout.application.config.ApplicationConfig +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.toHttpHeader import dev.usbharu.hideout.controller.mastodon.generated.NotificationsApi import dev.usbharu.hideout.core.infrastructure.springframework.security.LoginUserContextHolder import dev.usbharu.hideout.domain.mastodon.model.generated.Notification @@ -14,7 +17,8 @@ import org.springframework.stereotype.Controller @Controller class MastodonNotificationApiController( private val loginUserContextHolder: LoginUserContextHolder, - private val notificationApiService: NotificationApiService + private val notificationApiService: NotificationApiService, + private val applicationConfig: ApplicationConfig ) : NotificationsApi { override suspend fun apiV1NotificationsClearPost(): ResponseEntity { notificationApiService.clearAll(loginUserContextHolder.getLoginUserId()) @@ -30,17 +34,28 @@ class MastodonNotificationApiController( excludeTypes: List?, accountId: List? ): ResponseEntity> = runBlocking { - val notificationFlow = notificationApiService.notifications( + val notifications = notificationApiService.notifications( loginUser = loginUserContextHolder.getLoginUserId(), - maxId = maxId?.toLong(), - minId = minId?.toLong(), - sinceId = sinceId?.toLong(), - limit = limit ?: 20, types = types.orEmpty().mapNotNull { NotificationType.parse(it) }, excludeTypes = excludeTypes.orEmpty().mapNotNull { NotificationType.parse(it) }, - accountId = accountId.orEmpty().mapNotNull { it.toLongOrNull() } - ).asFlow() - ResponseEntity.ok(notificationFlow) + accountId = accountId.orEmpty().mapNotNull { it.toLongOrNull() }, + page = Page.of( + maxId?.toLongOrNull(), + sinceId?.toLongOrNull(), + minId?.toLongOrNull(), + limit?.coerceIn(0, 80) ?: 40 + ) + ) + + val httpHeader = notifications.toHttpHeader( + { "${applicationConfig.url}/api/v1/notifications?max_id=$it" }, + { "${applicationConfig.url}/api/v1/notifications?min_id=$it" } + ) ?: return@runBlocking ResponseEntity.ok( + notifications.asFlow() + ) + + ResponseEntity.ok().header("Link", httpHeader).body(notifications.asFlow()) + } override suspend fun apiV1NotificationsIdDismissPost(id: String): ResponseEntity {