feat: Followを送れるように

This commit is contained in:
usbharu 2023-05-22 16:08:28 +09:00
parent acd305d289
commit 194a648862
5 changed files with 57 additions and 6 deletions

View File

@ -0,0 +1,5 @@
package dev.usbharu.hideout.domain.model.hideout.dto
import dev.usbharu.hideout.domain.model.hideout.entity.User
data class SendFollowDto(val userId: User, val followTargetUserId: User)

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.domain.model.hideout.dto.SendFollowDto
interface ActivityPubSendFollowService {
suspend fun sendFollow(sendFollowDto: SendFollowDto)
}

View File

@ -0,0 +1,23 @@
package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.domain.model.ap.Follow
import dev.usbharu.hideout.domain.model.hideout.dto.SendFollowDto
import dev.usbharu.hideout.plugins.postAp
import io.ktor.client.*
import org.koin.core.annotation.Single
@Single
class ActivityPubSendFollowServiceImpl(private val httpClient: HttpClient) : ActivityPubSendFollowService {
override suspend fun sendFollow(sendFollowDto: SendFollowDto) {
val follow = Follow(
name = "Follow",
`object` = sendFollowDto.followTargetUserId.url,
actor = sendFollowDto.userId.url
)
httpClient.postAp(
urlString = sendFollowDto.followTargetUserId.inbox,
username = sendFollowDto.userId.url,
jsonLd = follow
)
}
}

View File

@ -2,17 +2,23 @@ package dev.usbharu.hideout.service.impl
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto
import dev.usbharu.hideout.domain.model.hideout.dto.SendFollowDto
import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto
import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.IUserAuthService
import dev.usbharu.hideout.service.activitypub.ActivityPubSendFollowService
import org.koin.core.annotation.Single
import java.lang.Integer.min
import java.time.Instant
@Single
class UserService(private val userRepository: IUserRepository, private val userAuthService: IUserAuthService) :
class UserService(
private val userRepository: IUserRepository,
private val userAuthService: IUserAuthService,
private val activityPubSendFollowService: ActivityPubSendFollowService
) :
IUserService {
private val maxLimit = 100
@ -105,9 +111,19 @@ class UserService(private val userRepository: IUserRepository, private val userA
}
// TODO APのフォロー処理を作る
override suspend fun follow(id: Long, follower: Long): Boolean {
userRepository.createFollower(id, follower)
return false
override suspend fun follow(id: Long, followerId: Long): Boolean {
val user = userRepository.findById(id) ?: throw UserNotFoundException("$id was not found.")
val follower = userRepository.findById(followerId) ?: throw UserNotFoundException("$followerId was not found.")
if (follower.domain != Config.configData.domain) {
throw IllegalArgumentException("follower is not local user.")
}
return if (user.domain == Config.configData.domain) {
userRepository.createFollower(id, followerId)
true
} else {
activityPubSendFollowService.sendFollow(SendFollowDto(follower, user))
false
}
}
override suspend fun unfollow(id: Long, follower: Long): Boolean {

View File

@ -29,7 +29,7 @@ class UserServiceTest {
onBlocking { hash(anyString()) } doReturn "hashedPassword"
onBlocking { generateKeyPair() } doReturn generateKeyPair
}
val userService = UserService(userRepository, userAuthService)
val userService = UserService(userRepository, userAuthService, mock())
userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test"))
verify(userRepository, times(1)).save(any())
argumentCaptor<dev.usbharu.hideout.domain.model.hideout.entity.User> {
@ -55,7 +55,7 @@ class UserServiceTest {
val userRepository = mock<IUserRepository> {
onBlocking { nextId() } doReturn 113345L
}
val userService = UserService(userRepository, mock())
val userService = UserService(userRepository, mock(), mock())
val user = RemoteUserCreateDto(
"test",
"example.com",