diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/UserResponse.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/UserResponse.kt index 7e73a81f..e74a4a72 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/UserResponse.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/UserResponse.kt @@ -1,5 +1,7 @@ package dev.usbharu.hideout.domain.model.hideout.dto +import dev.usbharu.hideout.domain.model.hideout.entity.User + data class UserResponse( val id: Long, val name: String, @@ -8,4 +10,18 @@ data class UserResponse( val description: String = "", val url: String, val createdAt: Long -) +) { + companion object { + fun from(user: User): UserResponse { + return UserResponse( + user.id, + user.name, + user.domain, + user.screenName, + user.description, + user.url, + user.createdAt.toEpochMilli() + ) + } + } +} 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 index a4bea389..92297e11 100644 --- 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 @@ -5,6 +5,7 @@ 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.IUserApiService import dev.usbharu.hideout.service.impl.IUserService import dev.usbharu.hideout.util.AcctUtil import io.ktor.http.* @@ -15,10 +16,10 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Route.users(userService: IUserService) { +fun Route.users(userService: IUserService, userApiService: IUserApiService) { route("/users") { get { - call.respond(userService.findAllForUser()) + call.respond(userApiService.findAll()) } post { val userCreate = call.receive() @@ -43,10 +44,10 @@ fun Route.users(userService: IUserService) { val userParameter = (call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")) if (userParameter.toLongOrNull() != null) { - return@get call.respond(userService.findById(userParameter.toLong())) + return@get call.respond(userApiService.findById(userParameter.toLong())) } else { val acct = AcctUtil.parse(userParameter) - return@get call.respond(userService.findByNameAndDomain(acct.username, acct.domain)) + return@get call.respond(userApiService.findByAcct(acct)) } } } @@ -56,10 +57,10 @@ fun Route.users(userService: IUserService) { 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())) + return@get call.respond(userApiService.findFollowers(userParameter.toLong())) } val acct = AcctUtil.parse(userParameter) - return@get call.respond(userService.findFollowersByNameAndDomain(acct.username, acct.domain)) + return@get call.respond(userApiService.findFollowersByAcct(acct)) } authenticate(TOKEN_AUTH) { @@ -76,7 +77,7 @@ fun Route.users(userService: IUserService) { } } val acct = AcctUtil.parse(userParameter) - val targetUser = userService.findByNameAndDomain(acct.username, acct.domain) + val targetUser = userApiService.findByAcct(acct) if (userService.addFollowers(targetUser.id, userId)) { return@post call.respond(HttpStatusCode.OK) } else { @@ -90,10 +91,10 @@ fun Route.users(userService: IUserService) { 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())) + return@get call.respond(userApiService.findFollowings(userParameter.toLong())) } val acct = AcctUtil.parse(userParameter) - return@get call.respond(userService.findFollowingByNameAndDomain(acct.username, acct.domain)) + return@get call.respond(userApiService.findFollowingsByAcct(acct)) } } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IUserApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IUserApiService.kt new file mode 100644 index 00000000..dd79e471 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/IUserApiService.kt @@ -0,0 +1,24 @@ +package dev.usbharu.hideout.service + +import dev.usbharu.hideout.domain.model.Acct +import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse + +interface IUserApiService { + suspend fun findAll(limit: Int? = 100, offset: Long = 0): List + + suspend fun findById(id: Long): UserResponse + + suspend fun findByIds(ids: List): List + + suspend fun findByAcct(acct: Acct): UserResponse + + suspend fun findByAccts(accts: List): List + + suspend fun findFollowers(userId: Long): List + + suspend fun findFollowings(userId: Long): List + + suspend fun findFollowersByAcct(acct: Acct): List + + suspend fun findFollowingsByAcct(acct: Acct): List +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/UserApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/UserApiServiceImpl.kt new file mode 100644 index 00000000..f3e70e10 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/UserApiServiceImpl.kt @@ -0,0 +1,38 @@ +package dev.usbharu.hideout.service + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.Acct +import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse +import dev.usbharu.hideout.service.impl.IUserService +import org.koin.core.annotation.Single + +@Single +class UserApiServiceImpl(private val userService: IUserService) : IUserApiService { + override suspend fun findAll(limit: Int?, offset: Long): List = + userService.findAll(limit, offset).map { UserResponse.from(it) } + + override suspend fun findById(id: Long): UserResponse = UserResponse.from(userService.findById(id)) + + override suspend fun findByIds(ids: List): List = + userService.findByIds(ids).map { UserResponse.from(it) } + + override suspend fun findByAcct(acct: Acct): UserResponse = + UserResponse.from(userService.findByNameAndDomain(acct.username, acct.domain)) + + override suspend fun findByAccts(accts: List): List { + return userService.findByNameAndDomains(accts.map { it.username to (it.domain ?: Config.configData.domain) }) + .map { UserResponse.from(it) } + } + + override suspend fun findFollowers(userId: Long): List = + userService.findFollowersById(userId).map { UserResponse.from(it) } + + override suspend fun findFollowings(userId: Long): List = + userService.findFollowingById(userId).map { UserResponse.from(it) } + + override suspend fun findFollowersByAcct(acct: Acct): List = + userService.findFollowersByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) } + + override suspend fun findFollowingsByAcct(acct: Acct): List = + userService.findFollowingByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) } +} 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 1fc6922e..ab726ac1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/IUserService.kt @@ -2,15 +2,12 @@ package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto -import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse import dev.usbharu.hideout.domain.model.hideout.entity.User @Suppress("TooManyFunctions") interface IUserService { suspend fun findAll(limit: Int? = 100, offset: Long? = 0): List - suspend fun findAllForUser(limit: Int? = 100, offset: Long? = 0): List - suspend fun findById(id: Long): User suspend fun findByIds(ids: List): List 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 b4f3157c..1500d145 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt @@ -24,7 +24,7 @@ class UserService(private val userRepository: IUserRepository, private val userA ) } - override suspend fun findAllForUser(limit: Int?, offset: Long?): List { + suspend fun findAllForUser(limit: Int?, offset: Long?): List { TODO("Not yet implemented") } diff --git a/src/main/resources/openapi/documentation.yaml b/src/main/resources/openapi/documentation.yaml index d4e6d624..6c90a035 100644 --- a/src/main/resources/openapi/documentation.yaml +++ b/src/main/resources/openapi/documentation.yaml @@ -112,6 +112,15 @@ paths: required: false schema: type: "integer" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "array" + items: + $ref: "#/components/schemas/Post" post: description: "" requestBody: @@ -127,6 +136,61 @@ paths: '*/*': schema: type: "object" + /api/internal/v1/posts/{id}: + get: + description: "" + parameters: + - name: "id" + in: "path" + required: true + schema: + type: "number" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + $ref: "#/components/schemas/Post" + /api/internal/v1/users/{name}/posts: + get: + description: "" + parameters: + - name: "name" + in: "path" + required: true + schema: + type: "string" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "array" + items: + $ref: "#/components/schemas/Post" + /api/internal/v1/users/{name}/posts/{id}: + get: + description: "" + parameters: + - name: "id" + in: "path" + required: true + schema: + type: "number" + - name: "name" + in: "path" + required: true + schema: + type: "string" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + $ref: "#/components/schemas/Post" /inbox: get: description: "" @@ -346,10 +410,19 @@ components: Post: type: "object" properties: - text: - type: "string" + id: + type: "integer" + format: "int64" + userId: + type: "integer" + format: "int64" overview: type: "string" + text: + type: "string" + createdAt: + type: "integer" + format: "int64" visibility: type: "string" enum: @@ -357,6 +430,8 @@ components: - "UNLISTED" - "FOLLOWERS" - "DIRECT" + url: + type: "string" repostId: type: "integer" format: "int64"