From cd8e8d530bc98b48aa80a4e4506790cd71340411 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 18 May 2023 15:11:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20users=E3=81=AEAPI=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/hideout/form/UserCreate.kt | 3 + .../hideout/routing/api/internal/v1/Users.kt | 89 +++++++++++++++++++ .../hideout/service/impl/IUserService.kt | 17 +++- .../hideout/service/impl/PostService.kt | 25 ++++++ .../hideout/service/impl/UserService.kt | 21 ++++- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/UserCreate.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/UserCreate.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/UserCreate.kt new file mode 100644 index 00000000..40d96a92 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/UserCreate.kt @@ -0,0 +1,3 @@ +package dev.usbharu.hideout.domain.model.hideout.form + +data class UserCreate(val username: String, val password: String) diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt new file mode 100644 index 00000000..a16f28b7 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt @@ -0,0 +1,89 @@ +package dev.usbharu.hideout.routing.api.internal.v1 + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto +import dev.usbharu.hideout.domain.model.hideout.form.UserCreate +import dev.usbharu.hideout.exception.ParameterNotExistException +import dev.usbharu.hideout.plugins.TOKEN_AUTH +import dev.usbharu.hideout.service.impl.IUserService +import dev.usbharu.hideout.util.AcctUtil +import io.ktor.http.* +import io.ktor.server.application.* +import io.ktor.server.auth.* +import io.ktor.server.auth.jwt.* +import io.ktor.server.request.* +import io.ktor.server.response.* +import io.ktor.server.routing.* + +fun Route.users(userService: IUserService) { + route("/users") { + get { + call.respond(userService.findAll()) + } + post { + val userCreate = call.receive() + if (userService.usernameAlreadyUse(userCreate.username)) { + return@post call.respond(HttpStatusCode.BadRequest) + } + val user = userService.createLocalUser( + UserCreateDto( + userCreate.username, + userCreate.username, + "", + userCreate.password + ) + ) + call.response.header("Location", "${Config.configData.url}/api/internal/v1/users/${user.name}") + call.respond(HttpStatusCode.OK) + } + route("/{name}") { + + route("/followers") { + get { + val userParameter = call.parameters["name"] + ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.") + if (userParameter.toLongOrNull() != null) { + return@get call.respond(userService.findFollowersById(userParameter.toLong())) + } + val acct = AcctUtil.parse(userParameter) + return@get call.respond(userService.findFollowersByNameAndDomain(acct.username, acct.domain)) + } + authenticate(TOKEN_AUTH) { + + post { + val userId = call.principal()?.payload?.getClaim("uid")?.asLong() + ?: throw IllegalStateException("no principal") + val userParameter = call.parameters["name"] + ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.") + if (userParameter.toLongOrNull() != null) { + if (userService.addFollowers(userParameter.toLong(), userId)) { + return@post call.respond(HttpStatusCode.OK) + } else { + return@post call.respond(HttpStatusCode.Accepted) + } + } + val acct = AcctUtil.parse(userParameter) + val targetUser = userService.findByNameAndDomain(acct.username, acct.domain) + if (userService.addFollowers(targetUser.id, userId)) { + return@post call.respond(HttpStatusCode.OK) + } else { + return@post call.respond(HttpStatusCode.Accepted) + } + } + } + } + route("/following") { + get { + val userParameter = (call.parameters["name"] + ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")) + if (userParameter.toLongOrNull() != null) { + return@get call.respond(userService.findFollowingById(userParameter.toLong())) + } + val acct = AcctUtil.parse(userParameter) + return@get call.respond(userService.findFollowingByNameAndDomain(acct.username, acct.domain)) + } + } + } + + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt index d2d61b1b..ab726ac1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt @@ -16,6 +16,8 @@ interface IUserService { suspend fun findByNameLocalUser(name: String): User + suspend fun findByNameAndDomain(name: String, domain: String? = null): User + suspend fun findByNameAndDomains(names: List>): List suspend fun findByUrl(url: String): User @@ -30,5 +32,18 @@ interface IUserService { suspend fun findFollowersById(id: Long): List - suspend fun addFollowers(id: Long, follower: Long) + suspend fun findFollowersByNameAndDomain(name: String, domain: String?): List + + suspend fun findFollowingById(id: Long): List + + suspend fun findFollowingByNameAndDomain(name: String, domain: String?): List + + /** + * フォロワーを追加する + * + * @param id + * @param follower + * @return リクエストが成功したか + */ + suspend fun addFollowers(id: Long, follower: Long): Boolean } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt index e017610d..28fa7c9c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt @@ -92,6 +92,31 @@ class PostService( } } + override suspend fun findByUserIdForUser( + userId: Long, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + forUserId: Long? + ): List { + TODO("Not yet implemented") + } + + override suspend fun findByUserNameAndDomainForUser( + userName: String, + domain: String, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + forUserId: Long? + ): List { + TODO("Not yet implemented") + } + override suspend fun delete(id: String) { TODO("Not yet implemented") } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt index 599e274f..490c3859 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt @@ -35,6 +35,10 @@ class UserService(private val userRepository: IUserRepository, private val userA ?: throw UserNotFoundException("$name was not found.") } + override suspend fun findByNameAndDomain(name: String, domain: String?): User { + TODO("Not yet implemented") + } + override suspend fun findByNameAndDomains(names: List>): List = userRepository.findByNameAndDomains(names) @@ -87,6 +91,21 @@ class UserService(private val userRepository: IUserRepository, private val userA } override suspend fun findFollowersById(id: Long): List = userRepository.findFollowersById(id) + override suspend fun findFollowersByNameAndDomain(name: String, domain: String?): List { + TODO("Not yet implemented") + } - override suspend fun addFollowers(id: Long, follower: Long) = userRepository.createFollower(id, follower) + override suspend fun findFollowingById(id: Long): List { + TODO("Not yet implemented") + } + + override suspend fun findFollowingByNameAndDomain(name: String, domain: String?): List { + TODO("Not yet implemented") + } + + //TODO APのフォロー処理を作る + override suspend fun addFollowers(id: Long, follower: Long): Boolean { + userRepository.createFollower(id, follower) + return false + } }