From 1612dcc905ba6f0c5273a630fdbf67e116994268 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 29 Jan 2024 19:37:30 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Notification=20API=E3=82=92Paginati?= =?UTF-8?q?on=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 --- .../model/MastodonNotificationRepository.kt | 10 ++++ .../ExposedMastodonNotificationRepository.kt | 20 ++++++++ ...goMastodonNotificationRepositoryWrapper.kt | 29 ++++++++++++ .../notification/NotificationApiService.kt | 10 ++++ .../NotificationApiServiceImpl.kt | 46 +++++++++++++++++++ 5 files changed, 115 insertions(+) diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt index 8d903a56..a89ffdca 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt @@ -1,5 +1,8 @@ package dev.usbharu.hideout.mastodon.domain.model +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.PaginationList + interface MastodonNotificationRepository { suspend fun save(mastodonNotification: MastodonNotification): MastodonNotification suspend fun deleteById(id: Long) @@ -16,6 +19,13 @@ interface MastodonNotificationRepository { accountId: List ): List + suspend fun findByUserIdAndInTypesAndInSourceActorId( + loginUser: Long, + types: List, + accountId: List, + page: Page + ): PaginationList + suspend fun deleteByUserId(userId: Long) suspend fun deleteByUserIdAndId(userId: Long, id: Long) } diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/exposedrepository/ExposedMastodonNotificationRepository.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/exposedrepository/ExposedMastodonNotificationRepository.kt index 14279e69..6181f3ef 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/exposedrepository/ExposedMastodonNotificationRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/exposedrepository/ExposedMastodonNotificationRepository.kt @@ -1,5 +1,8 @@ package dev.usbharu.hideout.mastodon.infrastructure.exposedrepository +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.PaginationList +import dev.usbharu.hideout.application.infrastructure.exposed.pagination import dev.usbharu.hideout.core.infrastructure.exposedrepository.AbstractRepository import dev.usbharu.hideout.core.infrastructure.exposedrepository.Timelines import dev.usbharu.hideout.mastodon.domain.model.MastodonNotification @@ -88,6 +91,23 @@ class ExposedMastodonNotificationRepository : MastodonNotificationRepository, Ab return@query result.map { it.toMastodonNotification() } } + override suspend fun findByUserIdAndInTypesAndInSourceActorId( + loginUser: Long, + types: List, + accountId: List, + page: Page + ): PaginationList = query { + val query = MastodonNotifications.select { + MastodonNotifications.userId eq loginUser + } + val result = query + .pagination(page, MastodonNotifications.id) + .orderBy(Timelines.createdAt, SortOrder.DESC) + + val notifications = result.map { it.toMastodonNotification() } + return@query PaginationList(notifications, notifications.lastOrNull()?.id, notifications.firstOrNull()?.id) + } + override suspend fun deleteByUserId(userId: Long) { MastodonNotifications.deleteWhere { MastodonNotifications.userId eq userId diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/mongorepository/MongoMastodonNotificationRepositoryWrapper.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/mongorepository/MongoMastodonNotificationRepositoryWrapper.kt index 96c7a278..06f4dadd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/mongorepository/MongoMastodonNotificationRepositoryWrapper.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/infrastructure/mongorepository/MongoMastodonNotificationRepositoryWrapper.kt @@ -1,5 +1,7 @@ package dev.usbharu.hideout.mastodon.infrastructure.mongorepository +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.PaginationList import dev.usbharu.hideout.mastodon.domain.model.MastodonNotification import dev.usbharu.hideout.mastodon.domain.model.MastodonNotificationRepository import dev.usbharu.hideout.mastodon.domain.model.NotificationType @@ -57,6 +59,33 @@ class MongoMastodonNotificationRepositoryWrapper( return mongoTemplate.find(query, MastodonNotification::class.java) } + override suspend fun findByUserIdAndInTypesAndInSourceActorId( + loginUser: Long, + types: List, + accountId: List, + page: Page + ): PaginationList { + val query = Query() + + if (page.minId != null) { + page.minId?.let { query.addCriteria(Criteria.where("id").gt(it)) } + page.maxId?.let { query.addCriteria(Criteria.where("id").lt(it)) } + } else { + query.with(Sort.by(Sort.Direction.DESC, "createdAt")) + page.sinceId?.let { query.addCriteria(Criteria.where("id").gt(it)) } + page.maxId?.let { query.addCriteria(Criteria.where("id").lt(it)) } + } + + page.limit?.let { query.limit(it) } + + val mastodonNotifications = mongoTemplate.find(query, MastodonNotification::class.java) + return PaginationList( + mastodonNotifications, + mastodonNotifications.lastOrNull()?.id, + mastodonNotifications.firstOrNull()?.id + ) + } + override suspend fun deleteByUserId(userId: Long) { mongoMastodonNotificationRepository.deleteByUserId(userId) } diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiService.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiService.kt index 0849d69a..c336ccc6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiService.kt @@ -1,5 +1,7 @@ package dev.usbharu.hideout.mastodon.service.notification +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.PaginationList import dev.usbharu.hideout.domain.mastodon.model.generated.Notification import dev.usbharu.hideout.mastodon.domain.model.NotificationType @@ -16,6 +18,14 @@ interface NotificationApiService { accountId: List ): List + suspend fun notifications( + loginUser: Long, + types: List, + excludeTypes: List, + accountId: List, + page: Page + ): PaginationList + suspend fun fingById(loginUser: Long, notificationId: Long): Notification? suspend fun clearAll(loginUser: Long) diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiServiceImpl.kt index 01b1bfb1..528c454c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/notification/NotificationApiServiceImpl.kt @@ -1,6 +1,8 @@ package dev.usbharu.hideout.mastodon.service.notification import dev.usbharu.hideout.application.external.Transaction +import dev.usbharu.hideout.application.infrastructure.exposed.Page +import dev.usbharu.hideout.application.infrastructure.exposed.PaginationList import dev.usbharu.hideout.domain.mastodon.model.generated.Notification import dev.usbharu.hideout.mastodon.domain.model.MastodonNotificationRepository import dev.usbharu.hideout.mastodon.domain.model.NotificationType @@ -65,6 +67,50 @@ class NotificationApiServiceImpl( } } + override suspend fun notifications( + loginUser: Long, + types: List, + excludeTypes: List, + accountId: List, + page: Page + ): PaginationList = transaction.transaction { + val typesTmp = mutableListOf() + + typesTmp.addAll(types) + typesTmp.removeAll(excludeTypes) + + val mastodonNotifications = + mastodonNotificationRepository.findByUserIdAndInTypesAndInSourceActorId( + loginUser, + typesTmp, + accountId, + page + ) + + val accounts = accountService.findByIds( + mastodonNotifications.map { + it.accountId + } + ).associateBy { it.id.toLong() } + + val statuses = statusQueryService.findByPostIds(mastodonNotifications.mapNotNull { it.statusId }) + .associateBy { it.id.toLong() } + + val notifications = mastodonNotifications.map { + Notification( + id = it.id.toString(), + type = convertNotificationType(it.type), + createdAt = it.createdAt.toString(), + account = accounts.getValue(it.accountId), + status = statuses[it.statusId], + report = null, + relationshipSeveranceEvent = null + ) + } + + return@transaction PaginationList(notifications, mastodonNotifications.next, mastodonNotifications.prev) + } + override suspend fun fingById(loginUser: Long, notificationId: Long): Notification? { val findById = mastodonNotificationRepository.findById(notificationId) ?: return null