From 0779c92ec44960bb20230111156aad78e171ad1c Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:13:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=98=8E=E7=A4=BA=E7=9A=84=E3=81=AB?= =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=AD=E3=83=AF=E3=83=BC/=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=AD=E3=82=A4=E3=83=BC/=E6=8A=95=E7=A8=BF?= =?UTF-8?q?=E3=81=AE=E6=95=B0=E3=82=92=E6=9B=B4=E6=96=B0=E3=81=99=E3=82=8B?= =?UTF-8?q?=E9=96=A2=E6=95=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 --- .../core/domain/model/post/PostRepository.kt | 2 ++ .../relationship/RelationshipRepository.kt | 8 +++++-- .../hideout/core/service/user/UserService.kt | 2 ++ .../core/service/user/UserServiceImpl.kt | 21 +++++++++++++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt index d02c8506..390b8d0b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt @@ -30,4 +30,6 @@ interface PostRepository { suspend fun findByApId(apId: String): Post? suspend fun existByApIdWithLock(apId: String): Boolean suspend fun findByActorId(actorId: Long): List + + suspend fun countByActorId(actorId: Long): Int } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt index 827b7f49..ff1dc1cc 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt @@ -52,17 +52,21 @@ interface RelationshipRepository { suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List + suspend fun countByTargetIdAndFollowing(targetId: Long, following: Boolean): Int + + suspend fun countByUserIdAndFollowing(targetId: Long, following: Boolean): Int + @Suppress("FunctionMaxLength") suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest( targetId: Long, followRequest: Boolean, ignoreFollowRequest: Boolean, - page: Page.PageByMaxId + page: Page.PageByMaxId, ): PaginationList suspend fun findByActorIdAndMuting( actorId: Long, muting: Boolean, - page: Page.PageByMaxId + page: Page.PageByMaxId, ): PaginationList } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt index 2c632f57..50e8d485 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt @@ -33,4 +33,6 @@ interface UserService { suspend fun deleteRemoteActor(actorId: Long) suspend fun deleteLocalUser(userId: Long) + + suspend fun updateUserStatistics(userId: Long) } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt index c15e5f79..5a459405 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt @@ -24,6 +24,7 @@ import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.deletedActor.DeletedActor import dev.usbharu.hideout.core.domain.model.deletedActor.DeletedActorRepository +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 dev.usbharu.hideout.core.domain.model.userdetails.UserDetail @@ -47,8 +48,8 @@ class UserServiceImpl( private val reactionRepository: ReactionRepository, private val relationshipRepository: RelationshipRepository, private val postService: PostService, - private val apSendDeleteService: APSendDeleteService - + private val apSendDeleteService: APSendDeleteService, + private val postRepository: PostRepository, ) : UserService { @@ -191,6 +192,22 @@ class UserServiceImpl( deletedActorRepository.save(deletedActor) } + override suspend fun updateUserStatistics(userId: Long) { + val actor = actorRepository.findByIdWithLock(userId) ?: throw UserNotFoundException.withId(userId) + + val followerCount = relationshipRepository.countByTargetIdAndFollowing(userId, true) + val followingCount = relationshipRepository.countByUserIdAndFollowing(userId, true) + val postsCount = postRepository.countByActorId(userId) + + actorRepository.save( + actor.copy( + followersCount = followerCount, + followingCount = followingCount, + postsCount = postsCount + ) + ) + } + companion object { private val logger = LoggerFactory.getLogger(UserServiceImpl::class.java) }