From cf5c396d320bdb7559132e20f8b9e628aa89cb4e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 27 Jan 2024 13:12:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20NotificationStore=E3=81=AE=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=81=A8=E3=83=AD=E3=82=B0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/NotificationServiceImpl.kt | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImpl.kt index 7e37e7d7..f208587e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImpl.kt @@ -1,11 +1,13 @@ package dev.usbharu.hideout.core.service.notification +import dev.usbharu.hideout.application.config.ApplicationConfig import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.notification.Notification import dev.usbharu.hideout.core.domain.model.notification.NotificationRepository import dev.usbharu.hideout.core.domain.model.post.PostRepository import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository +import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.time.Instant @@ -17,13 +19,22 @@ class NotificationServiceImpl( private val notificationRepository: NotificationRepository, private val actorRepository: ActorRepository, private val postRepository: PostRepository, - private val reactionRepository: ReactionRepository + private val reactionRepository: ReactionRepository, + private val applicationConfig: ApplicationConfig ) : NotificationService { - @Suppress("ReplaceNotNullAssertionWithElvisReturn") override suspend fun publishNotify(notificationRequest: NotificationRequest): Notification? { + logger.debug("NOTIFICATION REQUEST user: {} type: {}", notificationRequest.userId, notificationRequest.type) + logger.trace("NotificationRequest: {}", notificationRequest) + + val user = actorRepository.findById(notificationRequest.userId) + if (user == null || user.domain != applicationConfig.url.host) { + logger.debug("NOTIFICATION REQUEST is rejected. (Remote Actor or user not found.)") + return null + } // とりあえず個人間のRelationshipに基づいてきめる。今後増やす if (!relationship(notificationRequest)) { + logger.debug("NOTIFICATION REQUEST is rejected. (relationship)") return null } @@ -34,16 +45,27 @@ class NotificationServiceImpl( val savedNotification = notificationRepository.save(notification) - // saveで参照整合性違反が発生するはずなので - val user = actorRepository.findById(savedNotification.userId)!! val sourceActor = savedNotification.sourceActorId?.let { actorRepository.findById(it) } val post = savedNotification.postId?.let { postRepository.findById(it) } val reaction = savedNotification.reactionId?.let { reactionRepository.findById(it) } + logger.info( + "NOTIFICATION id: {} user: {} type: {}", + savedNotification.id, + savedNotification.userId, + savedNotification.type + ) + + logger.debug("push to {} notification store.", notificationStoreList.size) for (it in notificationStoreList) { - it.publishNotification(savedNotification, user, sourceActor, post, reaction) + try { + it.publishNotification(savedNotification, user, sourceActor, post, reaction) + } catch (e: Exception) { + logger.warn("FAILED Publish to notification.", e) + } } + logger.debug("SUCCESS Notification id: {}", savedNotification.id) return savedNotification } @@ -67,4 +89,8 @@ class NotificationServiceImpl( relationshipRepository.findByUserIdAndTargetUserId(notificationRequest.userId, targetActorId) ?: return true return relationshipNotificationManagementService.sendNotification(relationship, notificationRequest) } + + companion object { + private val logger = LoggerFactory.getLogger(NotificationServiceImpl::class.java) + } }