mirror of
https://github.com/usbharu/Hideout.git
synced 2026-09-29 16:31:30 +00:00
refactor: postのサービスをリファクタリング
This commit is contained in:
@@ -14,6 +14,7 @@ import dev.usbharu.hideout.repository.IUserRepository
|
||||
import dev.usbharu.hideout.routing.register
|
||||
import dev.usbharu.hideout.service.activitypub.ActivityPubService
|
||||
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
|
||||
import dev.usbharu.hideout.service.api.IPostApiService
|
||||
import dev.usbharu.hideout.service.api.IUserApiService
|
||||
import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService
|
||||
import dev.usbharu.hideout.service.auth.IJwtService
|
||||
@@ -23,7 +24,6 @@ import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import dev.usbharu.hideout.service.core.TwitterSnowflakeIdGenerateService
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.job.KJobJobQueueParentService
|
||||
import dev.usbharu.hideout.service.post.IPostService
|
||||
import dev.usbharu.hideout.service.user.IUserAuthService
|
||||
import dev.usbharu.hideout.service.user.IUserService
|
||||
import dev.usbharu.kjob.exposed.ExposedKJob
|
||||
@@ -107,12 +107,12 @@ fun Application.parent() {
|
||||
inject<JwkProvider>().value,
|
||||
)
|
||||
configureRouting(
|
||||
httpSignatureVerifyService = inject<HttpSignatureVerifyService>().value,
|
||||
activityPubService = inject<ActivityPubService>().value,
|
||||
userService = inject<IUserService>().value,
|
||||
activityPubUserService = inject<ActivityPubUserService>().value,
|
||||
postService = inject<IPostService>().value,
|
||||
userApiService = inject<IUserApiService>().value,
|
||||
httpSignatureVerifyService = inject<HttpSignatureVerifyService>().value,
|
||||
activityPubService = inject<ActivityPubService>().value,
|
||||
userService = inject<IUserService>().value,
|
||||
activityPubUserService = inject<ActivityPubUserService>().value,
|
||||
postService = inject<IPostApiService>().value,
|
||||
userApiService = inject<IUserApiService>().value,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,12 @@ import dev.usbharu.hideout.routing.activitypub.outbox
|
||||
import dev.usbharu.hideout.routing.activitypub.usersAP
|
||||
import dev.usbharu.hideout.routing.api.internal.v1.posts
|
||||
import dev.usbharu.hideout.routing.api.internal.v1.users
|
||||
import dev.usbharu.hideout.routing.api.mastodon.v1.statuses
|
||||
import dev.usbharu.hideout.routing.wellknown.webfinger
|
||||
import dev.usbharu.hideout.service.activitypub.ActivityPubService
|
||||
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
|
||||
import dev.usbharu.hideout.service.api.IPostApiService
|
||||
import dev.usbharu.hideout.service.api.IUserApiService
|
||||
import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService
|
||||
import dev.usbharu.hideout.service.post.IPostService
|
||||
import dev.usbharu.hideout.service.user.IUserService
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.autohead.*
|
||||
@@ -19,12 +18,12 @@ import io.ktor.server.routing.*
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
fun Application.configureRouting(
|
||||
httpSignatureVerifyService: HttpSignatureVerifyService,
|
||||
activityPubService: ActivityPubService,
|
||||
userService: IUserService,
|
||||
activityPubUserService: ActivityPubUserService,
|
||||
postService: IPostService,
|
||||
userApiService: IUserApiService
|
||||
httpSignatureVerifyService: HttpSignatureVerifyService,
|
||||
activityPubService: ActivityPubService,
|
||||
userService: IUserService,
|
||||
activityPubUserService: ActivityPubUserService,
|
||||
postService: IPostApiService,
|
||||
userApiService: IUserApiService
|
||||
) {
|
||||
install(AutoHeadResponse)
|
||||
routing {
|
||||
@@ -32,10 +31,6 @@ fun Application.configureRouting(
|
||||
outbox()
|
||||
usersAP(activityPubUserService, userService)
|
||||
webfinger(userService)
|
||||
|
||||
route("/api/v1") {
|
||||
statuses(postService)
|
||||
}
|
||||
route("/api/internal/v1") {
|
||||
posts(postService)
|
||||
users(userService, userApiService)
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package dev.usbharu.hideout.routing.api.internal.v1
|
||||
|
||||
import dev.usbharu.hideout.config.Config
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
|
||||
import dev.usbharu.hideout.domain.model.hideout.form.Post
|
||||
import dev.usbharu.hideout.exception.ParameterNotExistException
|
||||
import dev.usbharu.hideout.exception.PostNotFoundException
|
||||
import dev.usbharu.hideout.plugins.TOKEN_AUTH
|
||||
import dev.usbharu.hideout.service.post.IPostService
|
||||
import dev.usbharu.hideout.util.AcctUtil
|
||||
import dev.usbharu.hideout.service.api.IPostApiService
|
||||
import dev.usbharu.hideout.util.InstantParseUtil
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
@@ -18,7 +16,7 @@ import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
@Suppress("LongMethod")
|
||||
fun Route.posts(postService: IPostService) {
|
||||
fun Route.posts(postApiService: IPostApiService) {
|
||||
route("/posts") {
|
||||
authenticate(TOKEN_AUTH) {
|
||||
post {
|
||||
@@ -27,14 +25,14 @@ fun Route.posts(postService: IPostService) {
|
||||
|
||||
val receive = call.receive<Post>()
|
||||
val postCreateDto = PostCreateDto(
|
||||
text = receive.text,
|
||||
overview = receive.overview,
|
||||
visibility = receive.visibility,
|
||||
repostId = receive.repostId,
|
||||
repolyId = receive.replyId,
|
||||
userId = userId
|
||||
text = receive.text,
|
||||
overview = receive.overview,
|
||||
visibility = receive.visibility,
|
||||
repostId = receive.repostId,
|
||||
repolyId = receive.replyId,
|
||||
userId = userId
|
||||
)
|
||||
val create = postService.create(postCreateDto)
|
||||
val create = postApiService.createPost(postCreateDto)
|
||||
call.response.header("Location", create.url)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
@@ -47,16 +45,13 @@ fun Route.posts(postService: IPostService) {
|
||||
val minId = call.request.queryParameters["minId"]?.toLong()
|
||||
val maxId = call.request.queryParameters["maxId"]?.toLong()
|
||||
val limit = call.request.queryParameters["limit"]?.toInt()
|
||||
call.respond(HttpStatusCode.OK, postService.findAll(since, until, minId, maxId, limit, userId))
|
||||
call.respond(HttpStatusCode.OK, postApiService.getAll(since, until, minId, maxId, limit, userId))
|
||||
}
|
||||
get("/{id}") {
|
||||
val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong()
|
||||
val id = call.parameters["id"]?.toLong()
|
||||
?: throw ParameterNotExistException("Parameter(id='postsId') does not exist.")
|
||||
val post = (
|
||||
postService.findByIdForUser(id, userId)
|
||||
?: throw PostNotFoundException("$id was not found or is not authorized.")
|
||||
)
|
||||
val post = postApiService.getById(id, userId)
|
||||
call.respond(post)
|
||||
}
|
||||
}
|
||||
@@ -66,28 +61,15 @@ fun Route.posts(postService: IPostService) {
|
||||
get {
|
||||
val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong()
|
||||
val targetUserName = call.parameters["name"]
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
val targetUserId = targetUserName.toLongOrNull()
|
||||
val posts = if (targetUserId == null) {
|
||||
val acct = AcctUtil.parse(targetUserName)
|
||||
postService.findByUserNameAndDomainForUser(
|
||||
acct.username,
|
||||
acct.domain ?: Config.configData.domain,
|
||||
forUserId = userId
|
||||
)
|
||||
} else {
|
||||
postService.findByUserIdForUser(targetUserId, forUserId = userId)
|
||||
}
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
val posts = postApiService.getByUser(targetUserName, userId = userId)
|
||||
call.respond(posts)
|
||||
}
|
||||
get("/{id}") {
|
||||
val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong()
|
||||
val id = call.parameters["id"]?.toLong()
|
||||
?: throw ParameterNotExistException("Parameter(name='postsId' does not exist.")
|
||||
val post = (
|
||||
postService.findByIdForUser(id, userId)
|
||||
?: throw PostNotFoundException("$id was not found or is not authorized.")
|
||||
)
|
||||
?: throw ParameterNotExistException("Parameter(name='postsId' does not exist.")
|
||||
val post = postApiService.getById(id, userId)
|
||||
call.respond(post)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
|
||||
get {
|
||||
val userParameter = (
|
||||
call.parameters["name"]
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
)
|
||||
if (userParameter.toLongOrNull() != null) {
|
||||
return@get call.respond(userApiService.findById(userParameter.toLong()))
|
||||
@@ -91,7 +91,7 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
|
||||
get {
|
||||
val userParameter = (
|
||||
call.parameters["name"]
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
|
||||
)
|
||||
if (userParameter.toLongOrNull() != null) {
|
||||
return@get call.respond(userApiService.findFollowings(userParameter.toLong()))
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
package dev.usbharu.hideout.routing.api.mastodon.v1
|
||||
|
||||
import dev.usbharu.hideout.service.post.IPostService
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
@Suppress("UnusedPrivateMember")
|
||||
fun Route.statuses(postService: IPostService) {
|
||||
// route("/statuses") {
|
||||
// post {
|
||||
// val status: StatusForPost = call.receive()
|
||||
// val post = dev.usbharu.hideout.domain.model.hideout.form.Post(
|
||||
// userId = status.userId,
|
||||
// createdAt = System.currentTimeMillis(),
|
||||
// text = status.status,
|
||||
// visibility = 1
|
||||
// )
|
||||
// postService.create(post)
|
||||
// call.respond(status)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
// @Suppress("UnusedPrivateMember")
|
||||
// fun Route.statuses(postService: IPostService) {
|
||||
// // route("/statuses") {
|
||||
// // post {
|
||||
// // val status: StatusForPost = call.receive()
|
||||
// // val post = dev.usbharu.hideout.domain.model.hideout.form.Post(
|
||||
// // userId = status.userId,
|
||||
// // createdAt = System.currentTimeMillis(),
|
||||
// // text = status.status,
|
||||
// // visibility = 1
|
||||
// // )
|
||||
// // postService.create(post)
|
||||
// // call.respond(status)
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package dev.usbharu.hideout.service.api
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||
import java.time.Instant
|
||||
|
||||
interface IPostApiService {
|
||||
suspend fun createPost(postCreateDto: PostCreateDto): Post
|
||||
suspend fun getById(id: Long, userId: Long?): Post
|
||||
suspend fun getAll(since: Instant? = null,
|
||||
until: Instant? = null,
|
||||
minId: Long? = null,
|
||||
maxId: Long? = null,
|
||||
limit: Int? = null,
|
||||
userId: Long? = null): List<Post>
|
||||
|
||||
suspend fun getByUser(nameOrId: String,
|
||||
since: Instant? = null,
|
||||
until: Instant? = null,
|
||||
minId: Long? = null,
|
||||
maxId: Long? = null,
|
||||
limit: Int? = null,
|
||||
userId: Long? = null): List<Post>
|
||||
}
|
||||
@@ -1,61 +1,9 @@
|
||||
package dev.usbharu.hideout.service.post
|
||||
|
||||
import dev.usbharu.hideout.config.Config
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
|
||||
import dev.usbharu.hideout.domain.model.ap.Note
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||
import java.time.Instant
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
interface IPostService {
|
||||
suspend fun create(post: Post): Post
|
||||
suspend fun create(post: PostCreateDto): Post
|
||||
suspend fun findAll(
|
||||
since: Instant? = null,
|
||||
until: Instant? = null,
|
||||
minId: Long? = null,
|
||||
maxId: Long? = null,
|
||||
limit: Int? = 10,
|
||||
userId: Long? = null
|
||||
): List<Post>
|
||||
|
||||
suspend fun findById(id: String): Post
|
||||
|
||||
/**
|
||||
* 権限を考慮して投稿を取得します。
|
||||
*
|
||||
* @param id
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
suspend fun findByIdForUser(id: Long, userId: Long?): Post?
|
||||
|
||||
/**
|
||||
* 権限を考慮してユーザーの投稿を取得します。
|
||||
*
|
||||
* @param userId
|
||||
* @param forUserId
|
||||
* @return
|
||||
*/
|
||||
suspend fun findByUserIdForUser(
|
||||
userId: Long,
|
||||
since: Instant? = null,
|
||||
until: Instant? = null,
|
||||
minId: Long? = null,
|
||||
maxId: Long? = null,
|
||||
limit: Int? = null,
|
||||
forUserId: Long? = null
|
||||
): List<Post>
|
||||
|
||||
suspend fun findByUserNameAndDomainForUser(
|
||||
userName: String,
|
||||
domain: String = Config.configData.domain,
|
||||
since: Instant? = null,
|
||||
until: Instant? = null,
|
||||
minId: Long? = null,
|
||||
maxId: Long? = null,
|
||||
limit: Int? = null,
|
||||
forUserId: Long? = null
|
||||
): List<Post>
|
||||
|
||||
suspend fun delete(id: String)
|
||||
suspend fun createLocal(post: Post): Post
|
||||
suspend fun createRemote(note: Note): Post
|
||||
}
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
package dev.usbharu.hideout.service.post
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
|
||||
import dev.usbharu.hideout.repository.IPostRepository
|
||||
import dev.usbharu.hideout.repository.Posts
|
||||
import dev.usbharu.hideout.repository.UsersFollowers
|
||||
import dev.usbharu.hideout.repository.toPost
|
||||
import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService
|
||||
import dev.usbharu.hideout.service.user.IUserService
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inSubQuery
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.orIfNotNull
|
||||
import org.jetbrains.exposed.sql.orWhere
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.koin.core.annotation.Single
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.time.Instant
|
||||
|
||||
@Single
|
||||
class PostService(
|
||||
private val postRepository: IPostRepository,
|
||||
private val activityPubNoteService: ActivityPubNoteService,
|
||||
private val userService: IUserService
|
||||
) : IPostService {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||
|
||||
override suspend fun create(post: Post): Post {
|
||||
logger.debug("create post={}", post)
|
||||
val postEntity = postRepository.save(post)
|
||||
activityPubNoteService.createNote(postEntity)
|
||||
return post
|
||||
}
|
||||
|
||||
override suspend fun create(post: PostCreateDto): Post {
|
||||
logger.debug("create post={}", post)
|
||||
val user = userService.findById(post.userId)
|
||||
val id = postRepository.generateId()
|
||||
val postEntity = Post(
|
||||
id = id,
|
||||
userId = user.id,
|
||||
overview = null,
|
||||
text = post.text,
|
||||
createdAt = Instant.now().toEpochMilli(),
|
||||
visibility = Visibility.PUBLIC,
|
||||
url = "${user.url}/posts/$id",
|
||||
repostId = null,
|
||||
replyId = null
|
||||
)
|
||||
postRepository.save(postEntity)
|
||||
return postEntity
|
||||
}
|
||||
|
||||
override suspend fun findAll(
|
||||
since: Instant?,
|
||||
until: Instant?,
|
||||
minId: Long?,
|
||||
maxId: Long?,
|
||||
limit: Int?,
|
||||
userId: Long?
|
||||
): List<Post> {
|
||||
return transaction {
|
||||
val select = Posts.select {
|
||||
Posts.visibility.eq(Visibility.PUBLIC.ordinal)
|
||||
}
|
||||
if (userId != null) {
|
||||
select.orWhere {
|
||||
Posts.userId.inSubQuery(
|
||||
UsersFollowers.slice(UsersFollowers.userId).select(UsersFollowers.followerId eq userId)
|
||||
)
|
||||
}
|
||||
}
|
||||
select.map { it.toPost() }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun findById(id: String): Post {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun findByIdForUser(id: Long, userId: Long?): Post? {
|
||||
return transaction {
|
||||
val select = Posts.select(
|
||||
Posts.id.eq(id).and(
|
||||
Posts.visibility.eq(Visibility.PUBLIC.ordinal).orIfNotNull(
|
||||
userId?.let {
|
||||
Posts.userId.inSubQuery(
|
||||
UsersFollowers.slice(UsersFollowers.userId).select(UsersFollowers.followerId.eq(userId))
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
select.singleOrNull()?.toPost()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun findByUserIdForUser(
|
||||
userId: Long,
|
||||
since: Instant?,
|
||||
until: Instant?,
|
||||
minId: Long?,
|
||||
maxId: Long?,
|
||||
limit: Int?,
|
||||
forUserId: Long?
|
||||
): List<Post> {
|
||||
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<Post> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun delete(id: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user