refactor: Notification APIをPagination APIに対応

This commit is contained in:
usbharu 2024-01-29 19:37:30 +09:00
parent a1cf528a59
commit 0f4d008406
5 changed files with 115 additions and 0 deletions

View File

@ -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<Long>
): List<MastodonNotification>
suspend fun findByUserIdAndInTypesAndInSourceActorId(
loginUser: Long,
types: List<NotificationType>,
accountId: List<Long>,
page: Page
): PaginationList<MastodonNotification, Long>
suspend fun deleteByUserId(userId: Long)
suspend fun deleteByUserIdAndId(userId: Long, id: Long)
}

View File

@ -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<NotificationType>,
accountId: List<Long>,
page: Page
): PaginationList<MastodonNotification, Long> = 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

View File

@ -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<NotificationType>,
accountId: List<Long>,
page: Page
): PaginationList<MastodonNotification, Long> {
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)
}

View File

@ -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<Long>
): List<Notification>
suspend fun notifications(
loginUser: Long,
types: List<NotificationType>,
excludeTypes: List<NotificationType>,
accountId: List<Long>,
page: Page
): PaginationList<Notification, Long>
suspend fun fingById(loginUser: Long, notificationId: Long): Notification?
suspend fun clearAll(loginUser: Long)

View File

@ -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<NotificationType>,
excludeTypes: List<NotificationType>,
accountId: List<Long>,
page: Page
): PaginationList<Notification, Long> = transaction.transaction {
val typesTmp = mutableListOf<NotificationType>()
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