diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 2af9ef54..0acd75e5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -17,15 +17,15 @@ import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.routing.register import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.APUserService -import dev.usbharu.hideout.service.api.IPostApiService -import dev.usbharu.hideout.service.api.IUserApiService +import dev.usbharu.hideout.service.api.PostApiService +import dev.usbharu.hideout.service.api.UserApiService import dev.usbharu.hideout.service.api.UserAuthApiService import dev.usbharu.hideout.service.api.WebFingerApiService import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService import dev.usbharu.hideout.service.core.* import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.KJobJobQueueParentService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import dev.usbharu.kjob.exposed.ExposedKJob import io.ktor.client.* import io.ktor.client.engine.cio.* @@ -94,26 +94,26 @@ fun Application.parent() { configureKoin(module, HideoutModule().module) configureStatusPages() runBlocking { - inject().value.init() + inject().value.init() } configureCompression() configureHTTP() configureStaticRouting() configureMonitoring() configureSerialization() - register(inject().value) + register(inject().value) configureSecurity( inject().value, - inject().value + inject().value ) configureRouting( httpSignatureVerifyService = inject().value, apService = inject().value, - userService = inject().value, + userService = inject().value, apUserService = inject().value, - postService = inject().value, - userApiService = inject().value, + postService = inject().value, + userApiService = inject().value, userQueryService = inject().value, followerQueryService = inject().value, userAuthApiService = inject().value, diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt index bb30773b..f501fa98 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt @@ -4,7 +4,7 @@ import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.ap.JsonLd import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.UserAuthService +import dev.usbharu.hideout.service.user.UserAuthServiceImpl import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.client.* import io.ktor.client.plugins.api.* @@ -73,7 +73,7 @@ val httpSignaturePlugin = createClientPlugin("HttpSign", ::HttpSignaturePluginCo println("Digest !!") // UserAuthService.sha256.reset() val digest = - Base64.getEncoder().encodeToString(UserAuthService.sha256.digest(body.toByteArray(Charsets.UTF_8))) + Base64.getEncoder().encodeToString(UserAuthServiceImpl.sha256.digest(body.toByteArray(Charsets.UTF_8))) request.headers.append("Digest", "sha-256=$digest") } diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt index 0567db26..178127b5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt @@ -11,13 +11,13 @@ import dev.usbharu.hideout.routing.api.internal.v1.users import dev.usbharu.hideout.routing.wellknown.webfinger import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.APUserService -import dev.usbharu.hideout.service.api.IPostApiService -import dev.usbharu.hideout.service.api.IUserApiService +import dev.usbharu.hideout.service.api.PostApiService +import dev.usbharu.hideout.service.api.UserApiService import dev.usbharu.hideout.service.api.UserAuthApiService import dev.usbharu.hideout.service.api.WebFingerApiService import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.server.application.* import io.ktor.server.plugins.autohead.* import io.ktor.server.routing.* @@ -26,10 +26,10 @@ import io.ktor.server.routing.* fun Application.configureRouting( httpSignatureVerifyService: HttpSignatureVerifyService, apService: APService, - userService: IUserService, + userService: UserService, apUserService: APUserService, - postService: IPostApiService, - userApiService: IUserApiService, + postService: PostApiService, + userApiService: UserApiService, userQueryService: UserQueryService, followerQueryService: FollowerQueryService, userAuthApiService: UserAuthApiService, diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt index 84966882..a4d7d34d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.plugins import com.auth0.jwk.JwkProvider import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.service.core.IMetaService +import dev.usbharu.hideout.service.core.MetaService import dev.usbharu.hideout.util.JsonWebKeyUtil import io.ktor.http.* import io.ktor.server.application.* @@ -16,7 +16,7 @@ const val TOKEN_AUTH = "jwt-auth" @Suppress("MagicNumber") fun Application.configureSecurity( jwkProvider: JwkProvider, - metaService: IMetaService + metaService: MetaService ) { val issuer = Config.configData.url install(Authentication) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IUserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IUserRepository.kt deleted file mode 100644 index e717f7b0..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IUserRepository.kt +++ /dev/null @@ -1,18 +0,0 @@ -package dev.usbharu.hideout.repository - -import dev.usbharu.hideout.domain.model.hideout.entity.User - -@Suppress("TooManyFunctions") -interface IUserRepository { - suspend fun save(user: User): User - - suspend fun findById(id: Long): User? - - suspend fun delete(id: Long) - - suspend fun deleteFollowRequest(id: Long, follower: Long) - - suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean - - suspend fun nextId(): Long -} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepository.kt similarity index 88% rename from src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt rename to src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepository.kt index 19f8774a..d6bc5638 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepository.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken -interface IJwtRefreshTokenRepository { +interface JwtRefreshTokenRepository { suspend fun generateId(): Long suspend fun save(token: JwtRefreshToken) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt index d93797d2..0fdc79dd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt @@ -13,7 +13,7 @@ class JwtRefreshTokenRepositoryImpl( private val database: Database, private val idGenerateService: IdGenerateService ) : - IJwtRefreshTokenRepository { + JwtRefreshTokenRepository { init { transaction(database) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepository.kt similarity index 85% rename from src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt rename to src/main/kotlin/dev/usbharu/hideout/repository/MetaRepository.kt index b90be212..5fda5200 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepository.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Meta -interface IMetaRepository { +interface MetaRepository { suspend fun save(meta: Meta) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt index 86dbc786..d0de63a1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt @@ -7,7 +7,7 @@ import org.koin.core.annotation.Single import java.util.* @Single -class MetaRepositoryImpl(private val database: Database) : IMetaRepository { +class MetaRepositoryImpl(private val database: Database) : MetaRepository { init { transaction(database) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepository.kt similarity index 90% rename from src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt rename to src/main/kotlin/dev/usbharu/hideout/repository/PostRepository.kt index 2e0faf91..21a9bec8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepository.kt @@ -3,7 +3,7 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Post @Suppress("LongParameterList") -interface IPostRepository { +interface PostRepository { suspend fun generateId(): Long suspend fun save(post: Post): Post suspend fun delete(id: Long) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index b9ca5f77..8782a54f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -9,7 +9,7 @@ import org.jetbrains.exposed.sql.transactions.transaction import org.koin.core.annotation.Single @Single -class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository { +class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : PostRepository { init { transaction(database) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index b9064bf8..17344d24 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -1,137 +1,18 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.User -import dev.usbharu.hideout.service.core.IdGenerateService -import org.jetbrains.exposed.dao.id.LongIdTable -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction -import org.koin.core.annotation.Single -import java.time.Instant -@Single -class UserRepository(private val database: Database, private val idGenerateService: IdGenerateService) : - IUserRepository { - init { - transaction(database) { - SchemaUtils.create(Users) - SchemaUtils.create(UsersFollowers) - SchemaUtils.createMissingTablesAndColumns(Users) - SchemaUtils.createMissingTablesAndColumns(UsersFollowers) - SchemaUtils.create(FollowRequests) - SchemaUtils.createMissingTablesAndColumns(FollowRequests) - } - } +@Suppress("TooManyFunctions") +interface UserRepository { + suspend fun save(user: User): User - override suspend fun save(user: User): User { - val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull() - if (singleOrNull == null) { - Users.insert { - it[id] = user.id - it[name] = user.name - it[domain] = user.domain - it[screenName] = user.screenName - it[description] = user.description - it[password] = user.password - it[inbox] = user.inbox - it[outbox] = user.outbox - it[url] = user.url - it[createdAt] = user.createdAt.toEpochMilli() - it[publicKey] = user.publicKey - it[privateKey] = user.privateKey - } - } else { - Users.update({ Users.id eq user.id }) { - it[name] = user.name - it[domain] = user.domain - it[screenName] = user.screenName - it[description] = user.description - it[password] = user.password - it[inbox] = user.inbox - it[outbox] = user.outbox - it[url] = user.url - it[createdAt] = user.createdAt.toEpochMilli() - it[publicKey] = user.publicKey - it[privateKey] = user.privateKey - } - } - return user - } + suspend fun findById(id: Long): User? - override suspend fun findById(id: Long): User? { - return Users.select { Users.id eq id }.map { - it.toUser() - }.singleOrNull() - } + suspend fun delete(id: Long) - override suspend fun deleteFollowRequest(id: Long, follower: Long) { - FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) } - } + suspend fun deleteFollowRequest(id: Long, follower: Long) - override suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean { - return FollowRequests.select { (FollowRequests.userId eq id) and (FollowRequests.followerId eq follower) } - .singleOrNull() != null - } + suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean - override suspend fun delete(id: Long) { - Users.deleteWhere { Users.id.eq(id) } - } - - override suspend fun nextId(): Long = idGenerateService.generateId() -} - -object Users : Table("users") { - val id = long("id") - val name = varchar("name", length = 64) - val domain = varchar("domain", length = 255) - val screenName = varchar("screen_name", length = 64) - val description = varchar("description", length = 600) - val password = varchar("password", length = 255).nullable() - val inbox = varchar("inbox", length = 255).uniqueIndex() - val outbox = varchar("outbox", length = 255).uniqueIndex() - val url = varchar("url", length = 255).uniqueIndex() - val publicKey = varchar("public_key", length = 10000) - val privateKey = varchar("private_key", length = 10000).nullable() - val createdAt = long("created_at") - - override val primaryKey: PrimaryKey = PrimaryKey(id) - - init { - uniqueIndex(name, domain) - } -} - -fun ResultRow.toUser(): User { - return User( - id = this[Users.id], - name = this[Users.name], - domain = this[Users.domain], - screenName = this[Users.screenName], - description = this[Users.description], - password = this[Users.password], - inbox = this[Users.inbox], - outbox = this[Users.outbox], - url = this[Users.url], - publicKey = this[Users.publicKey], - privateKey = this[Users.privateKey], - createdAt = Instant.ofEpochMilli((this[Users.createdAt])) - ) -} - -object UsersFollowers : LongIdTable("users_followers") { - val userId = long("user_id").references(Users.id).index() - val followerId = long("follower_id").references(Users.id) - - init { - uniqueIndex(userId, followerId) - } -} - -object FollowRequests : LongIdTable("follow_requests") { - val userId = long("user_id").references(Users.id) - val followerId = long("follower_id").references(Users.id) - - init { - uniqueIndex(userId, followerId) - } + suspend fun nextId(): Long } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt new file mode 100644 index 00000000..43ea37ef --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt @@ -0,0 +1,137 @@ +package dev.usbharu.hideout.repository + +import dev.usbharu.hideout.domain.model.hideout.entity.User +import dev.usbharu.hideout.service.core.IdGenerateService +import org.jetbrains.exposed.dao.id.LongIdTable +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.transactions.transaction +import org.koin.core.annotation.Single +import java.time.Instant + +@Single +class UserRepositoryImpl(private val database: Database, private val idGenerateService: IdGenerateService) : + UserRepository { + init { + transaction(database) { + SchemaUtils.create(Users) + SchemaUtils.create(UsersFollowers) + SchemaUtils.createMissingTablesAndColumns(Users) + SchemaUtils.createMissingTablesAndColumns(UsersFollowers) + SchemaUtils.create(FollowRequests) + SchemaUtils.createMissingTablesAndColumns(FollowRequests) + } + } + + override suspend fun save(user: User): User { + val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull() + if (singleOrNull == null) { + Users.insert { + it[id] = user.id + it[name] = user.name + it[domain] = user.domain + it[screenName] = user.screenName + it[description] = user.description + it[password] = user.password + it[inbox] = user.inbox + it[outbox] = user.outbox + it[url] = user.url + it[createdAt] = user.createdAt.toEpochMilli() + it[publicKey] = user.publicKey + it[privateKey] = user.privateKey + } + } else { + Users.update({ Users.id eq user.id }) { + it[name] = user.name + it[domain] = user.domain + it[screenName] = user.screenName + it[description] = user.description + it[password] = user.password + it[inbox] = user.inbox + it[outbox] = user.outbox + it[url] = user.url + it[createdAt] = user.createdAt.toEpochMilli() + it[publicKey] = user.publicKey + it[privateKey] = user.privateKey + } + } + return user + } + + override suspend fun findById(id: Long): User? { + return Users.select { Users.id eq id }.map { + it.toUser() + }.singleOrNull() + } + + override suspend fun deleteFollowRequest(id: Long, follower: Long) { + FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) } + } + + override suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean { + return FollowRequests.select { (FollowRequests.userId eq id) and (FollowRequests.followerId eq follower) } + .singleOrNull() != null + } + + override suspend fun delete(id: Long) { + Users.deleteWhere { Users.id.eq(id) } + } + + override suspend fun nextId(): Long = idGenerateService.generateId() +} + +object Users : Table("users") { + val id = long("id") + val name = varchar("name", length = 64) + val domain = varchar("domain", length = 255) + val screenName = varchar("screen_name", length = 64) + val description = varchar("description", length = 600) + val password = varchar("password", length = 255).nullable() + val inbox = varchar("inbox", length = 255).uniqueIndex() + val outbox = varchar("outbox", length = 255).uniqueIndex() + val url = varchar("url", length = 255).uniqueIndex() + val publicKey = varchar("public_key", length = 10000) + val privateKey = varchar("private_key", length = 10000).nullable() + val createdAt = long("created_at") + + override val primaryKey: PrimaryKey = PrimaryKey(id) + + init { + uniqueIndex(name, domain) + } +} + +fun ResultRow.toUser(): User { + return User( + id = this[Users.id], + name = this[Users.name], + domain = this[Users.domain], + screenName = this[Users.screenName], + description = this[Users.description], + password = this[Users.password], + inbox = this[Users.inbox], + outbox = this[Users.outbox], + url = this[Users.url], + publicKey = this[Users.publicKey], + privateKey = this[Users.privateKey], + createdAt = Instant.ofEpochMilli((this[Users.createdAt])) + ) +} + +object UsersFollowers : LongIdTable("users_followers") { + val userId = long("user_id").references(Users.id).index() + val followerId = long("follower_id").references(Users.id) + + init { + uniqueIndex(userId, followerId) + } +} + +object FollowRequests : LongIdTable("follow_requests") { + val userId = long("user_id").references(Users.id) + val followerId = long("follower_id").references(Users.id) + + init { + uniqueIndex(userId, followerId) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/RegisterRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/RegisterRouting.kt index 2e01c939..8628f340 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/RegisterRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/RegisterRouting.kt @@ -1,6 +1,6 @@ package dev.usbharu.hideout.routing -import dev.usbharu.hideout.service.api.IUserApiService +import dev.usbharu.hideout.service.api.UserApiService import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -8,7 +8,7 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Application.register(userApiService: IUserApiService) { +fun Application.register(userApiService: UserApiService) { routing { get("/register") { val principal = call.principal() diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt index bbc23835..a3c3cd23 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt @@ -4,7 +4,7 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post import dev.usbharu.hideout.domain.model.hideout.form.Reaction import dev.usbharu.hideout.exception.ParameterNotExistException import dev.usbharu.hideout.plugins.TOKEN_AUTH -import dev.usbharu.hideout.service.api.IPostApiService +import dev.usbharu.hideout.service.api.PostApiService import dev.usbharu.hideout.util.InstantParseUtil import io.ktor.http.* import io.ktor.server.application.* @@ -15,7 +15,7 @@ import io.ktor.server.response.* import io.ktor.server.routing.* @Suppress("LongMethod") -fun Route.posts(postApiService: IPostApiService) { +fun Route.posts(postApiService: PostApiService) { route("/posts") { authenticate(TOKEN_AUTH) { post { 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 6bf0e5ad..1a73c694 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,8 +5,8 @@ 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.api.IUserApiService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.api.UserApiService +import dev.usbharu.hideout.service.user.UserService import dev.usbharu.hideout.util.AcctUtil import io.ktor.http.* import io.ktor.server.application.* @@ -17,7 +17,7 @@ import io.ktor.server.response.* import io.ktor.server.routing.* @Suppress("LongMethod", "CognitiveComplexMethod") -fun Route.users(userService: IUserService, userApiService: IUserApiService) { +fun Route.users(userService: UserService, userApiService: UserApiService) { route("/users") { get { call.respond(userApiService.findAll()) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APAcceptService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APAcceptService.kt index 951a8c78..b1fbc9b2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APAcceptService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APAcceptService.kt @@ -6,7 +6,7 @@ import dev.usbharu.hideout.domain.model.ap.Accept import dev.usbharu.hideout.domain.model.ap.Follow import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.http.* import org.koin.core.annotation.Single @@ -16,7 +16,7 @@ interface APAcceptService { @Single class APAcceptServiceImpl( - private val userService: IUserService, + private val userService: UserService, private val userQueryService: UserQueryService ) : APAcceptService { override suspend fun receiveAccept(accept: Accept): ActivityPubResponse { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt index e8be8ec7..0313acf8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt @@ -7,7 +7,7 @@ import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.reaction.IReactionService +import dev.usbharu.hideout.service.reaction.ReactionService import io.ktor.http.* import org.koin.core.annotation.Single @@ -17,7 +17,7 @@ interface APLikeService { @Single class APLikeServiceImpl( - private val reactionService: IReactionService, + private val reactionService: ReactionService, private val apUserService: APUserService, private val apNoteService: APNoteService, private val userQueryService: UserQueryService, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt index f32f7b4e..2cf9d080 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt @@ -13,7 +13,7 @@ import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.FollowerQueryService import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IPostRepository +import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.service.job.JobQueueParentService import io.ktor.client.* import io.ktor.client.statement.* @@ -35,7 +35,7 @@ interface APNoteService { class APNoteServiceImpl( private val httpClient: HttpClient, private val jobQueueParentService: JobQueueParentService, - private val postRepository: IPostRepository, + private val postRepository: PostRepository, private val apUserService: APUserService, private val userQueryService: UserQueryService, private val followerQueryService: FollowerQueryService, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt index e8b21f84..a7ca1533 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt @@ -11,7 +11,7 @@ import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.FollowerQueryService import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IPostRepository +import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.service.job.JobQueueParentService import io.ktor.client.* import kjob.core.job.JobProps @@ -28,7 +28,7 @@ interface APReactionService { @Single class APReactionServiceImpl( private val jobQueueParentService: JobQueueParentService, - private val iPostRepository: IPostRepository, + private val postRepository: PostRepository, private val httpClient: HttpClient, private val userQueryService: UserQueryService, private val followerQueryService: FollowerQueryService, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt index 54c0d39d..9156abfd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt @@ -11,7 +11,7 @@ import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction import dev.usbharu.hideout.service.job.JobQueueParentService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.client.* import io.ktor.http.* import kjob.core.job.JobProps @@ -26,7 +26,7 @@ interface APReceiveFollowService { class APReceiveFollowServiceImpl( private val jobQueueParentService: JobQueueParentService, private val apUserService: APUserService, - private val userService: IUserService, + private val userService: UserService, private val httpClient: HttpClient, private val userQueryService: UserQueryService, private val transaction: Transaction diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APUndoService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APUndoService.kt index a058dbd5..8ae5ab13 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APUndoService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APUndoService.kt @@ -6,7 +6,7 @@ import dev.usbharu.hideout.domain.model.ap.Follow import dev.usbharu.hideout.domain.model.ap.Undo import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.http.* import org.koin.core.annotation.Single @@ -17,7 +17,7 @@ interface APUndoService { @Single @Suppress("UnsafeCallOnNullableType") class APUndoServiceImpl( - private val userService: IUserService, + private val userService: UserService, private val apUserService: APUserService, private val userQueryService: UserQueryService, private val transaction: Transaction diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APUserService.kt index c31803eb..caf9f91b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APUserService.kt @@ -10,7 +10,7 @@ import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.plugins.getAp import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.client.* import io.ktor.client.request.* @@ -33,7 +33,7 @@ interface APUserService { @Single class APUserServiceImpl( - private val userService: IUserService, + private val userService: UserService, private val httpClient: HttpClient, private val userQueryService: UserQueryService, private val transaction: Transaction diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiService.kt similarity index 92% rename from src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/api/PostApiService.kt index 61715cc0..8edee9a7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiService.kt @@ -7,16 +7,16 @@ import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse import dev.usbharu.hideout.domain.model.hideout.form.Post import dev.usbharu.hideout.query.PostResponseQueryService import dev.usbharu.hideout.query.ReactionQueryService -import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.post.IPostService -import dev.usbharu.hideout.service.reaction.IReactionService +import dev.usbharu.hideout.service.post.PostService +import dev.usbharu.hideout.service.reaction.ReactionService import dev.usbharu.hideout.util.AcctUtil import org.koin.core.annotation.Single import java.time.Instant @Suppress("LongParameterList") -interface IPostApiService { +interface PostApiService { suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): PostResponse suspend fun getById(id: Long, userId: Long?): PostResponse suspend fun getAll( @@ -45,13 +45,13 @@ interface IPostApiService { @Single class PostApiServiceImpl( - private val postService: IPostService, - private val userRepository: IUserRepository, + private val postService: PostService, + private val userRepository: UserRepository, private val postResponseQueryService: PostResponseQueryService, private val reactionQueryService: ReactionQueryService, - private val reactionService: IReactionService, + private val reactionService: ReactionService, private val transaction: Transaction -) : IPostApiService { +) : PostApiService { override suspend fun createPost(postForm: Post, userId: Long): PostResponse { return transaction.transaction { val createdPost = postService.createLocal( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiService.kt similarity index 95% rename from src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/api/UserApiService.kt index 26135f8a..fb8ce555 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiService.kt @@ -8,11 +8,11 @@ import dev.usbharu.hideout.exception.UsernameAlreadyExistException import dev.usbharu.hideout.query.FollowerQueryService import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import org.koin.core.annotation.Single import kotlin.math.min -interface IUserApiService { +interface UserApiService { suspend fun findAll(limit: Int? = 100, offset: Long = 0): List suspend fun findById(id: Long): UserResponse @@ -36,9 +36,9 @@ interface IUserApiService { class UserApiServiceImpl( private val userQueryService: UserQueryService, private val followerQueryService: FollowerQueryService, - private val userService: IUserService, + private val userService: UserService, private val transaction: Transaction -) : IUserApiService { +) : UserApiService { override suspend fun findAll(limit: Int?, offset: Long): List = userQueryService.findAll(min(limit ?: 100, 100), offset).map { UserResponse.from(it) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/UserAuthApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/UserAuthApiService.kt index d56bbc4f..e576f8ef 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/UserAuthApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/UserAuthApiService.kt @@ -5,9 +5,9 @@ import dev.usbharu.hideout.domain.model.hideout.dto.JwtToken import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken import dev.usbharu.hideout.exception.InvalidUsernameOrPasswordException import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.service.auth.IJwtService +import dev.usbharu.hideout.service.auth.JwtService import dev.usbharu.hideout.service.core.Transaction -import dev.usbharu.hideout.service.user.UserAuthService +import dev.usbharu.hideout.service.user.UserAuthServiceImpl import org.koin.core.annotation.Single interface UserAuthApiService { @@ -17,9 +17,9 @@ interface UserAuthApiService { @Single class UserAuthApiServiceImpl( - private val userAuthService: UserAuthService, + private val userAuthService: UserAuthServiceImpl, private val userQueryService: UserQueryService, - private val jwtService: IJwtService, + private val jwtService: JwtService, private val transaction: Transaction ) : UserAuthApiService { override suspend fun login(username: String, password: String): JwtToken { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/IJwtService.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtService.kt similarity index 92% rename from src/main/kotlin/dev/usbharu/hideout/service/auth/IJwtService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/auth/JwtService.kt index 2f74c2fa..462430ac 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/IJwtService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtService.kt @@ -10,8 +10,8 @@ import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken import dev.usbharu.hideout.exception.InvalidRefreshTokenException import dev.usbharu.hideout.query.JwtRefreshTokenQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IJwtRefreshTokenRepository -import dev.usbharu.hideout.service.core.IMetaService +import dev.usbharu.hideout.repository.JwtRefreshTokenRepository +import dev.usbharu.hideout.service.core.MetaService import dev.usbharu.hideout.util.RsaUtil import kotlinx.coroutines.runBlocking import org.koin.core.annotation.Single @@ -19,7 +19,7 @@ import java.time.Instant import java.time.temporal.ChronoUnit import java.util.* -interface IJwtService { +interface JwtService { suspend fun createToken(user: User): JwtToken suspend fun refreshToken(refreshToken: RefreshToken): JwtToken @@ -31,11 +31,11 @@ interface IJwtService { @Suppress("InjectDispatcher") @Single class JwtServiceImpl( - private val metaService: IMetaService, - private val refreshTokenRepository: IJwtRefreshTokenRepository, + private val metaService: MetaService, + private val refreshTokenRepository: JwtRefreshTokenRepository, private val userQueryService: UserQueryService, private val refreshTokenQueryService: JwtRefreshTokenQueryService -) : IJwtService { +) : JwtService { private val privateKey = runBlocking { RsaUtil.decodeRsaPrivateKey(metaService.getJwtMeta().privateKey) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/core/IMetaService.kt b/src/main/kotlin/dev/usbharu/hideout/service/core/MetaService.kt similarity index 91% rename from src/main/kotlin/dev/usbharu/hideout/service/core/IMetaService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/core/MetaService.kt index 763dc96a..91da1a90 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/core/IMetaService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/core/MetaService.kt @@ -3,7 +3,7 @@ package dev.usbharu.hideout.service.core import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta -interface IMetaService { +interface MetaService { suspend fun getMeta(): Meta suspend fun updateMeta(meta: Meta) suspend fun getJwtMeta(): Jwt diff --git a/src/main/kotlin/dev/usbharu/hideout/service/core/MetaServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/core/MetaServiceImpl.kt index c971c177..e35ff3f7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/core/MetaServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/core/MetaServiceImpl.kt @@ -3,12 +3,12 @@ package dev.usbharu.hideout.service.core import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta import dev.usbharu.hideout.exception.NotInitException -import dev.usbharu.hideout.repository.IMetaRepository +import dev.usbharu.hideout.repository.MetaRepository import org.koin.core.annotation.Single @Single -class MetaServiceImpl(private val metaRepository: IMetaRepository, private val transaction: Transaction) : - IMetaService { +class MetaServiceImpl(private val metaRepository: MetaRepository, private val transaction: Transaction) : + MetaService { override suspend fun getMeta(): Meta = transaction.transaction { metaRepository.get() ?: throw NotInitException("Meta is null") } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/core/IServerInitialiseService.kt b/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseService.kt similarity index 64% rename from src/main/kotlin/dev/usbharu/hideout/service/core/IServerInitialiseService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseService.kt index c54eaccc..d65f8fa6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/core/IServerInitialiseService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseService.kt @@ -1,5 +1,5 @@ package dev.usbharu.hideout.service.core -interface IServerInitialiseService { +interface ServerInitialiseService { suspend fun init() } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImpl.kt index 1ca3c25f..4fc950c1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImpl.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.service.core import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta -import dev.usbharu.hideout.repository.IMetaRepository +import dev.usbharu.hideout.repository.MetaRepository import dev.usbharu.hideout.util.ServerUtil import org.koin.core.annotation.Single import org.slf4j.Logger @@ -12,10 +12,10 @@ import java.util.* @Single class ServerInitialiseServiceImpl( - private val metaRepository: IMetaRepository, + private val metaRepository: MetaRepository, private val transaction: Transaction ) : - IServerInitialiseService { + ServerInitialiseService { val logger: Logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt similarity index 90% rename from src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt index 4459b8d2..28c90710 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt @@ -3,6 +3,6 @@ package dev.usbharu.hideout.service.post import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.entity.Post -interface IPostService { +interface PostService { suspend fun createLocal(post: PostCreateDto): Post } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt index 0ca22f77..d184cbae 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -3,18 +3,18 @@ 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.exception.UserNotFoundException -import dev.usbharu.hideout.repository.IPostRepository -import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.repository.PostRepository +import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.service.ap.APNoteService import org.koin.core.annotation.Single import java.time.Instant @Single class PostServiceImpl( - private val postRepository: IPostRepository, - private val userRepository: IUserRepository, + private val postRepository: PostRepository, + private val userRepository: UserRepository, private val apNoteService: APNoteService -) : IPostService { +) : PostService { override suspend fun createLocal(post: PostCreateDto): Post { val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") val id = postRepository.generateId() diff --git a/src/main/kotlin/dev/usbharu/hideout/service/reaction/IReactionService.kt b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionService.kt similarity index 90% rename from src/main/kotlin/dev/usbharu/hideout/service/reaction/IReactionService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionService.kt index 28b56673..a7b9ed0d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/reaction/IReactionService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionService.kt @@ -1,6 +1,6 @@ package dev.usbharu.hideout.service.reaction -interface IReactionService { +interface ReactionService { suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) suspend fun sendReaction(name: String, userId: Long, postId: Long) suspend fun removeReaction(userId: Long, postId: Long) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt index 4eb42ea3..f8c24df9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt @@ -11,7 +11,7 @@ class ReactionServiceImpl( private val reactionRepository: ReactionRepository, private val apReactionService: APReactionService, private val reactionQueryService: ReactionQueryService -) : IReactionService { +) : ReactionService { override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) { if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) { reactionRepository.save( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserAuthService.kt deleted file mode 100644 index 35896355..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserAuthService.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.usbharu.hideout.service.user - -import java.security.KeyPair - -interface IUserAuthService { - fun hash(password: String): String - - suspend fun usernameAlreadyUse(username: String): Boolean - - suspend fun generateKeyPair(): KeyPair - - suspend fun verifyAccount(username: String, password: String): Boolean -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt deleted file mode 100644 index 14c6d8ab..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt +++ /dev/null @@ -1,34 +0,0 @@ -package dev.usbharu.hideout.service.user - -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.entity.User - -@Suppress("TooManyFunctions") -interface IUserService { - - suspend fun usernameAlreadyUse(username: String): Boolean - - suspend fun createLocalUser(user: UserCreateDto): User - - suspend fun createRemoteUser(user: RemoteUserCreateDto): User - - /** - * フォローリクエストを送信する - * - * @param id - * @param followerId - * @return リクエストが成功したか - */ - suspend fun followRequest(id: Long, followerId: Long): Boolean - - /** - * フォローする - * - * @param id - * @param followerId - */ - suspend fun follow(id: Long, followerId: Long) - - suspend fun unfollow(id: Long, followerId: Long): Boolean -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthService.kt index c32249dc..61853b73 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthService.kt @@ -1,53 +1,13 @@ package dev.usbharu.hideout.service.user -import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.query.UserQueryService -import io.ktor.util.* -import org.koin.core.annotation.Single -import java.security.* -import java.util.* +import java.security.KeyPair -@Single -class UserAuthService( - val userQueryService: UserQueryService -) : IUserAuthService { +interface UserAuthService { + fun hash(password: String): String - override fun hash(password: String): String { - val digest = sha256.digest(password.toByteArray(Charsets.UTF_8)) - return hex(digest) - } + suspend fun usernameAlreadyUse(username: String): Boolean - override suspend fun usernameAlreadyUse(username: String): Boolean { - userQueryService.findByName(username) - return true - } + suspend fun generateKeyPair(): KeyPair - override suspend fun verifyAccount(username: String, password: String): Boolean { - val userEntity = userQueryService.findByNameAndDomain(username, Config.configData.domain) - return userEntity.password == hash(password) - } - - override suspend fun generateKeyPair(): KeyPair { - val keyPairGenerator = KeyPairGenerator.getInstance("RSA") - keyPairGenerator.initialize(keySize) - return keyPairGenerator.generateKeyPair() - } - - companion object { - val sha256: MessageDigest = MessageDigest.getInstance("SHA-256") - const val keySize = 2048 - const val pemSize = 64 - } -} - -fun PublicKey.toPem(): String { - return "-----BEGIN PUBLIC KEY-----\n" + - Base64.getEncoder().encodeToString(encoded).chunked(UserAuthService.pemSize).joinToString("\n") + - "\n-----END PUBLIC KEY-----\n" -} - -fun PrivateKey.toPem(): String { - return "-----BEGIN PRIVATE KEY-----\n" + - Base64.getEncoder().encodeToString(encoded).chunked(UserAuthService.pemSize).joinToString("\n") + - "\n-----END PRIVATE KEY-----\n" + suspend fun verifyAccount(username: String, password: String): Boolean } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt new file mode 100644 index 00000000..0c234430 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt @@ -0,0 +1,53 @@ +package dev.usbharu.hideout.service.user + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.query.UserQueryService +import io.ktor.util.* +import org.koin.core.annotation.Single +import java.security.* +import java.util.* + +@Single +class UserAuthServiceImpl( + val userQueryService: UserQueryService +) : UserAuthService { + + override fun hash(password: String): String { + val digest = sha256.digest(password.toByteArray(Charsets.UTF_8)) + return hex(digest) + } + + override suspend fun usernameAlreadyUse(username: String): Boolean { + userQueryService.findByName(username) + return true + } + + override suspend fun verifyAccount(username: String, password: String): Boolean { + val userEntity = userQueryService.findByNameAndDomain(username, Config.configData.domain) + return userEntity.password == hash(password) + } + + override suspend fun generateKeyPair(): KeyPair { + val keyPairGenerator = KeyPairGenerator.getInstance("RSA") + keyPairGenerator.initialize(keySize) + return keyPairGenerator.generateKeyPair() + } + + companion object { + val sha256: MessageDigest = MessageDigest.getInstance("SHA-256") + const val keySize = 2048 + const val pemSize = 64 + } +} + +fun PublicKey.toPem(): String { + return "-----BEGIN PUBLIC KEY-----\n" + + Base64.getEncoder().encodeToString(encoded).chunked(UserAuthServiceImpl.pemSize).joinToString("\n") + + "\n-----END PUBLIC KEY-----\n" +} + +fun PrivateKey.toPem(): String { + return "-----BEGIN PRIVATE KEY-----\n" + + Base64.getEncoder().encodeToString(encoded).chunked(UserAuthServiceImpl.pemSize).joinToString("\n") + + "\n-----END PRIVATE KEY-----\n" +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt index febc8ac6..a141fa24 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt @@ -1,97 +1,34 @@ package dev.usbharu.hideout.service.user -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.query.FollowerQueryService -import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IUserRepository -import dev.usbharu.hideout.service.ap.APSendFollowService -import org.koin.core.annotation.Single -import java.time.Instant -@Single -class UserService( - private val userRepository: IUserRepository, - private val userAuthService: IUserAuthService, - private val apSendFollowService: APSendFollowService, - private val userQueryService: UserQueryService, - private val followerQueryService: FollowerQueryService -) : - IUserService { +@Suppress("TooManyFunctions") +interface UserService { - override suspend fun usernameAlreadyUse(username: String): Boolean { - val findByNameAndDomain = userQueryService.findByNameAndDomain(username, Config.configData.domain) - return findByNameAndDomain != null - } + suspend fun usernameAlreadyUse(username: String): Boolean - override suspend fun createLocalUser(user: UserCreateDto): User { - val nextId = userRepository.nextId() - val hashedPassword = userAuthService.hash(user.password) - val keyPair = userAuthService.generateKeyPair() - val userEntity = User( - id = nextId, - name = user.name, - domain = Config.configData.domain, - screenName = user.screenName, - description = user.description, - password = hashedPassword, - inbox = "${Config.configData.url}/users/${user.name}/inbox", - outbox = "${Config.configData.url}/users/${user.name}/outbox", - url = "${Config.configData.url}/users/${user.name}", - publicKey = keyPair.public.toPem(), - privateKey = keyPair.private.toPem(), - createdAt = Instant.now() - ) - return userRepository.save(userEntity) - } + suspend fun createLocalUser(user: UserCreateDto): User - override suspend fun createRemoteUser(user: RemoteUserCreateDto): User { - val nextId = userRepository.nextId() - val userEntity = User( - id = nextId, - name = user.name, - domain = user.domain, - screenName = user.screenName, - description = user.description, - inbox = user.inbox, - outbox = user.outbox, - url = user.url, - publicKey = user.publicKey, - createdAt = Instant.now() - ) - return userRepository.save(userEntity) - } + suspend fun createRemoteUser(user: RemoteUserCreateDto): User - // TODO APのフォロー処理を作る - override suspend fun followRequest(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.") - return if (user.domain == Config.configData.domain) { - follow(id, followerId) - true - } else { - if (userRepository.findFollowRequestsById(id, followerId)) { - // do-nothing - } else { - apSendFollowService.sendFollow(SendFollowDto(follower, user)) - } - false - } - } + /** + * フォローリクエストを送信する + * + * @param id + * @param followerId + * @return リクエストが成功したか + */ + suspend fun followRequest(id: Long, followerId: Long): Boolean - override suspend fun follow(id: Long, followerId: Long) { - followerQueryService.appendFollower(id, followerId) - if (userRepository.findFollowRequestsById(id, followerId)) { - userRepository.deleteFollowRequest(id, followerId) - } - } + /** + * フォローする + * + * @param id + * @param followerId + */ + suspend fun follow(id: Long, followerId: Long) - override suspend fun unfollow(id: Long, followerId: Long): Boolean { - followerQueryService.removeFollower(id, followerId) - return false - } + suspend fun unfollow(id: Long, followerId: Long): Boolean } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserServiceImpl.kt new file mode 100644 index 00000000..6558d770 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserServiceImpl.kt @@ -0,0 +1,97 @@ +package dev.usbharu.hideout.service.user + +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.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService +import dev.usbharu.hideout.repository.UserRepository +import dev.usbharu.hideout.service.ap.APSendFollowService +import org.koin.core.annotation.Single +import java.time.Instant + +@Single +class UserServiceImpl( + private val userRepository: UserRepository, + private val userAuthService: UserAuthService, + private val apSendFollowService: APSendFollowService, + private val userQueryService: UserQueryService, + private val followerQueryService: FollowerQueryService +) : + UserService { + + override suspend fun usernameAlreadyUse(username: String): Boolean { + val findByNameAndDomain = userQueryService.findByNameAndDomain(username, Config.configData.domain) + return findByNameAndDomain != null + } + + override suspend fun createLocalUser(user: UserCreateDto): User { + val nextId = userRepository.nextId() + val hashedPassword = userAuthService.hash(user.password) + val keyPair = userAuthService.generateKeyPair() + val userEntity = User( + id = nextId, + name = user.name, + domain = Config.configData.domain, + screenName = user.screenName, + description = user.description, + password = hashedPassword, + inbox = "${Config.configData.url}/users/${user.name}/inbox", + outbox = "${Config.configData.url}/users/${user.name}/outbox", + url = "${Config.configData.url}/users/${user.name}", + publicKey = keyPair.public.toPem(), + privateKey = keyPair.private.toPem(), + createdAt = Instant.now() + ) + return userRepository.save(userEntity) + } + + override suspend fun createRemoteUser(user: RemoteUserCreateDto): User { + val nextId = userRepository.nextId() + val userEntity = User( + id = nextId, + name = user.name, + domain = user.domain, + screenName = user.screenName, + description = user.description, + inbox = user.inbox, + outbox = user.outbox, + url = user.url, + publicKey = user.publicKey, + createdAt = Instant.now() + ) + return userRepository.save(userEntity) + } + + // TODO APのフォロー処理を作る + override suspend fun followRequest(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.") + return if (user.domain == Config.configData.domain) { + follow(id, followerId) + true + } else { + if (userRepository.findFollowRequestsById(id, followerId)) { + // do-nothing + } else { + apSendFollowService.sendFollow(SendFollowDto(follower, user)) + } + false + } + } + + override suspend fun follow(id: Long, followerId: Long) { + followerQueryService.appendFollower(id, followerId) + if (userRepository.findFollowRequestsById(id, followerId)) { + userRepository.deleteFollowRequest(id, followerId) + } + } + + override suspend fun unfollow(id: Long, followerId: Long): Boolean { + followerQueryService.removeFollower(id, followerId) + return false + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/plugins/SecurityKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/plugins/SecurityKtTest.kt index ff3e89f9..0d91ce6f 100644 --- a/src/test/kotlin/dev/usbharu/hideout/plugins/SecurityKtTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/plugins/SecurityKtTest.kt @@ -18,9 +18,9 @@ import dev.usbharu.hideout.exception.InvalidUsernameOrPasswordException import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.routing.api.internal.v1.auth import dev.usbharu.hideout.service.api.UserAuthApiService -import dev.usbharu.hideout.service.auth.IJwtService -import dev.usbharu.hideout.service.core.IMetaService -import dev.usbharu.hideout.service.user.IUserAuthService +import dev.usbharu.hideout.service.auth.JwtService +import dev.usbharu.hideout.service.core.MetaService +import dev.usbharu.hideout.service.user.UserAuthService import dev.usbharu.hideout.util.Base64Util import dev.usbharu.hideout.util.JsonWebKeyUtil import io.ktor.client.request.* @@ -51,7 +51,7 @@ class SecurityKtTest { val userAuthService = mock { onBlocking { login(eq("testUser"), eq("password")) } doReturn jwtToken } - val metaService = mock() + val metaService = mock() val userQueryService = mock { onBlocking { findByNameAndDomain(eq("testUser"), eq("example.com")) } doReturn User( id = 1L, @@ -93,12 +93,12 @@ class SecurityKtTest { config = ApplicationConfig("empty.conf") } Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper()) - mock { + mock { onBlocking { verifyAccount(anyString(), anyString()) }.doReturn(false) } - val metaService = mock() + val metaService = mock() mock() - mock() + mock() val jwkProvider = mock() val userAuthApiService = mock { onBlocking { login(anyString(), anyString()) } doThrow InvalidUsernameOrPasswordException() @@ -126,7 +126,7 @@ class SecurityKtTest { config = ApplicationConfig("empty.conf") } Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper()) - val metaService = mock() + val metaService = mock() val jwkProvider = mock() val userAuthApiService = mock { onBlocking { login(anyString(), eq("InvalidPassword")) } doThrow InvalidUsernameOrPasswordException() @@ -247,7 +247,7 @@ class SecurityKtTest { .withClaim("uid", 123456L) .withExpiresAt(now.plus(30, ChronoUnit.MINUTES)) .sign(Algorithm.RSA256(rsaPublicKey, keyPair.private as RSAPrivateKey)) - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() }.doReturn( Jwt( kid, @@ -308,7 +308,7 @@ class SecurityKtTest { .withClaim("uid", 123345L) .withExpiresAt(now.minus(30, ChronoUnit.MINUTES)) .sign(Algorithm.RSA256(rsaPublicKey, keyPair.private as RSAPrivateKey)) - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() }.doReturn( Jwt( kid, @@ -367,7 +367,7 @@ class SecurityKtTest { .withClaim("uid", 12345L) .withExpiresAt(now.plus(30, ChronoUnit.MINUTES)) .sign(Algorithm.RSA256(rsaPublicKey, keyPair.private as RSAPrivateKey)) - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() }.doReturn( Jwt( kid, @@ -426,7 +426,7 @@ class SecurityKtTest { .withClaim("uid", null as Long?) .withExpiresAt(now.plus(30, ChronoUnit.MINUTES)) .sign(Algorithm.RSA256(rsaPublicKey, keyPair.private as RSAPrivateKey)) - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() }.doReturn( Jwt( kid, @@ -484,7 +484,7 @@ class SecurityKtTest { .withKeyId(kid.toString()) .withExpiresAt(now.plus(30, ChronoUnit.MINUTES)) .sign(Algorithm.RSA256(rsaPublicKey, keyPair.private as RSAPrivateKey)) - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() }.doReturn( Jwt( kid, diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt index 29f220bf..75a2c335 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt @@ -6,7 +6,7 @@ import dev.usbharu.hideout.plugins.configureStatusPages import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.APUserService import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.client.request.* import io.ktor.http.* import io.ktor.server.config.* @@ -47,7 +47,7 @@ class InboxRoutingKtTest { val apService = mock { on { parseActivity(any()) } doThrow JsonParseException() } - mock() + mock() mock() application { configureStatusPages() @@ -88,7 +88,7 @@ class InboxRoutingKtTest { val apService = mock { on { parseActivity(any()) } doThrow JsonParseException() } - mock() + mock() mock() application { configureStatusPages() diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsTest.kt index 4a8fb0e8..81f9e66c 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsTest.kt @@ -10,7 +10,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.plugins.TOKEN_AUTH import dev.usbharu.hideout.plugins.configureSecurity import dev.usbharu.hideout.plugins.configureSerialization -import dev.usbharu.hideout.service.api.IPostApiService +import dev.usbharu.hideout.service.api.PostApiService import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* @@ -60,7 +60,7 @@ class PostsTest { url = "https://example.com/posts/2" ) ) - val postService = mock { + val postService = mock { onBlocking { getAll( since = anyOrNull(), @@ -135,7 +135,7 @@ class PostsTest { ) ) - val postService = mock { + val postService = mock { onBlocking { getAll( since = anyOrNull(), @@ -191,7 +191,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/1" ) - val postService = mock { + val postService = mock { onBlocking { getById(any(), anyOrNull()) } doReturn post } application { @@ -230,7 +230,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/1" ) - val postService = mock { + val postService = mock { onBlocking { getById(any(), isNotNull()) } doReturn post } val claim = mock { @@ -273,7 +273,7 @@ class PostsTest { val payload = mock { on { getClaim(eq("uid")) } doReturn claim } - val postService = mock { + val postService = mock { onBlocking { createPost(any(), any()) } doAnswer { val argument = it.getArgument(0) val userId = it.getArgument(1) @@ -360,7 +360,7 @@ class PostsTest { url = "https://example.com/posts/2" ) ) - val postService = mock { + val postService = mock { onBlocking { getByUser( nameOrId = any(), @@ -421,7 +421,7 @@ class PostsTest { url = "https://example.com/posts/2" ) ) - val postService = mock { + val postService = mock { onBlocking { getByUser( nameOrId = eq("test1"), @@ -482,7 +482,7 @@ class PostsTest { url = "https://example.com/posts/2" ) ) - val postService = mock { + val postService = mock { onBlocking { getByUser( nameOrId = eq("test1@example.com"), @@ -543,7 +543,7 @@ class PostsTest { url = "https://example.com/posts/2" ) ) - val postService = mock { + val postService = mock { onBlocking { getByUser( nameOrId = eq("@test1@example.com"), @@ -593,7 +593,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/2" ) - val postService = mock { + val postService = mock { onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post } application { @@ -633,7 +633,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/2" ) - val postService = mock { + val postService = mock { onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post } application { @@ -673,7 +673,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/2" ) - val postService = mock { + val postService = mock { onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post } application { @@ -713,7 +713,7 @@ class PostsTest { createdAt = Instant.now().toEpochMilli(), url = "https://example.com/posts/2" ) - val postService = mock { + val postService = mock { onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post } application { diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt index a29245df..18f70364 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt @@ -10,8 +10,8 @@ import dev.usbharu.hideout.domain.model.hideout.form.UserCreate import dev.usbharu.hideout.plugins.TOKEN_AUTH import dev.usbharu.hideout.plugins.configureSecurity import dev.usbharu.hideout.plugins.configureSerialization -import dev.usbharu.hideout.service.api.IUserApiService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.api.UserApiService +import dev.usbharu.hideout.service.user.UserService import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* @@ -53,7 +53,7 @@ class UsersTest { Instant.now().toEpochMilli() ), ) - val userService = mock { + val userService = mock { onBlocking { findAll(anyOrNull(), anyOrNull()) } doReturn users } application { @@ -77,7 +77,7 @@ class UsersTest { config = ApplicationConfig("empty.conf") } val userCreateDto = UserCreate("test", "XXXXXXX") - val userService = mock { + val userService = mock { onBlocking { usernameAlreadyUse(any()) } doReturn false onBlocking { createLocalUser(any()) } doReturn User( id = 12345, @@ -122,7 +122,7 @@ class UsersTest { config = ApplicationConfig("empty.conf") } val userCreateDto = UserCreate("test", "XXXXXXX") - val userService = mock { + val userService = mock { onBlocking { usernameAlreadyUse(any()) } doReturn true } application { @@ -157,7 +157,7 @@ class UsersTest { "https://example.com/test", Instant.now().toEpochMilli() ) - val userApiService = mock { + val userApiService = mock { onBlocking { findByAcct(any()) } doReturn userResponse } application { @@ -190,7 +190,7 @@ class UsersTest { "https://example.com/test", Instant.now().toEpochMilli() ) - val userApiService = mock { + val userApiService = mock { onBlocking { findById(any()) } doReturn userResponse } application { @@ -223,7 +223,7 @@ class UsersTest { "https://example.com/test", Instant.now().toEpochMilli() ) - val userApiService = mock { + val userApiService = mock { onBlocking { findByAcct(any()) } doReturn userResponse } application { @@ -256,7 +256,7 @@ class UsersTest { "https://example.com/test", Instant.now().toEpochMilli() ) - val userApiService = mock { + val userApiService = mock { onBlocking { findByAcct(any()) } doReturn userResponse } application { @@ -301,7 +301,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowersByAcct(any()) } doReturn followers } application { @@ -346,7 +346,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowersByAcct(any()) } doReturn followers } application { @@ -391,7 +391,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowers(any()) } doReturn followers } application { @@ -423,7 +423,7 @@ class UsersTest { on { getClaim(eq("uid")) } doReturn claim } - val userApiService = mock { + val userApiService = mock { onBlocking { findByAcct(any()) } doReturn UserResponse( "1235", "follower1", @@ -434,7 +434,7 @@ class UsersTest { Instant.now().toEpochMilli() ) } - val userService = mock { + val userService = mock { onBlocking { followRequest(eq(1235), eq(1234)) } doReturn true } application { @@ -473,7 +473,7 @@ class UsersTest { on { getClaim(eq("uid")) } doReturn claim } - val userApiService = mock { + val userApiService = mock { onBlocking { findByAcct(any()) } doReturn UserResponse( "1235", "follower1", @@ -484,7 +484,7 @@ class UsersTest { Instant.now().toEpochMilli() ) } - val userService = mock { + val userService = mock { onBlocking { followRequest(eq(1235), eq(1234)) } doReturn false } application { @@ -523,7 +523,7 @@ class UsersTest { on { getClaim(eq("uid")) } doReturn claim } - val userApiService = mock { + val userApiService = mock { onBlocking { findById(any()) } doReturn UserResponse( "1235", "follower1", @@ -534,7 +534,7 @@ class UsersTest { Instant.now().toEpochMilli() ) } - val userService = mock { + val userService = mock { onBlocking { followRequest(eq(1235), eq(1234)) } doReturn false } application { @@ -586,7 +586,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowingsByAcct(any()) } doReturn followers } application { @@ -631,7 +631,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowingsByAcct(any()) } doReturn followers } application { @@ -676,7 +676,7 @@ class UsersTest { Instant.now().toEpochMilli() ) ) - val userApiService = mock { + val userApiService = mock { onBlocking { findFollowings(any()) } doReturn followers } application { diff --git a/src/test/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowServiceImplTest.kt index 6e5fbe30..7b7f2c21 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowServiceImplTest.kt @@ -11,7 +11,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.job.JobQueueParentService -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.service.user.UserService import io.ktor.client.* import io.ktor.client.engine.mock.* import kjob.core.dsl.ScheduleContext @@ -128,7 +128,7 @@ class APReceiveFollowServiceImplTest { ) } - val userService = mock { + val userService = mock { onBlocking { followRequest(any(), any()) } doReturn false } val activityPubFollowService = diff --git a/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt index e4f37934..44fc2844 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt @@ -14,8 +14,8 @@ import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken import dev.usbharu.hideout.exception.InvalidRefreshTokenException import dev.usbharu.hideout.query.JwtRefreshTokenQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IJwtRefreshTokenRepository -import dev.usbharu.hideout.service.core.IMetaService +import dev.usbharu.hideout.repository.JwtRefreshTokenRepository +import dev.usbharu.hideout.service.core.MetaService import dev.usbharu.hideout.util.Base64Util import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -42,14 +42,14 @@ class JwtServiceImplTest { keyPairGenerator.initialize(2048) val generateKeyPair = keyPairGenerator.generateKeyPair() - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() } doReturn Jwt( kid, Base64Util.encode(generateKeyPair.private.encoded), Base64Util.encode(generateKeyPair.public.encoded) ) } - val refreshTokenRepository = mock { + val refreshTokenRepository = mock { onBlocking { generateId() } doReturn 1L } val jwtService = JwtServiceImpl(metaService, refreshTokenRepository, mock(), mock()) @@ -94,7 +94,7 @@ class JwtServiceImplTest { keyPairGenerator.initialize(2048) val generateKeyPair = keyPairGenerator.generateKeyPair() - val refreshTokenRepository = mock { + val refreshTokenRepository = mock { onBlocking { generateId() } doReturn 2L } @@ -123,7 +123,7 @@ class JwtServiceImplTest { createdAt = Instant.now() ) } - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() } doReturn Jwt( kid, Base64Util.encode(generateKeyPair.private.encoded), @@ -160,7 +160,7 @@ class JwtServiceImplTest { keyPairGenerator.initialize(2048) val generateKeyPair = keyPairGenerator.generateKeyPair() - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() } doReturn Jwt( kid, Base64Util.encode(generateKeyPair.private.encoded), @@ -187,7 +187,7 @@ class JwtServiceImplTest { keyPairGenerator.initialize(2048) val generateKeyPair = keyPairGenerator.generateKeyPair() - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() } doReturn Jwt( kid, Base64Util.encode(generateKeyPair.private.encoded), @@ -214,7 +214,7 @@ class JwtServiceImplTest { keyPairGenerator.initialize(2048) val generateKeyPair = keyPairGenerator.generateKeyPair() - val metaService = mock { + val metaService = mock { onBlocking { getJwtMeta() } doReturn Jwt( kid, Base64Util.encode(generateKeyPair.private.encoded), diff --git a/src/test/kotlin/dev/usbharu/hideout/service/core/MetaServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/core/MetaServiceImplTest.kt index 02cf9e7e..546dab35 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/core/MetaServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/core/MetaServiceImplTest.kt @@ -5,7 +5,7 @@ package dev.usbharu.hideout.service.core import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta import dev.usbharu.hideout.exception.NotInitException -import dev.usbharu.hideout.repository.IMetaRepository +import dev.usbharu.hideout.repository.MetaRepository import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Test @@ -19,7 +19,7 @@ class MetaServiceImplTest { @Test fun `getMeta メタデータを取得できる`() = runTest { val meta = Meta("1.0.0", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn meta } val metaService = MetaServiceImpl(metaRepository, TestTransaction) @@ -29,7 +29,7 @@ class MetaServiceImplTest { @Test fun `getMeta メタデータが無いときはNotInitExceptionがthrowされる`() = runTest { - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn null } val metaService = MetaServiceImpl(metaRepository, TestTransaction) @@ -39,7 +39,7 @@ class MetaServiceImplTest { @Test fun `updateMeta メタデータを保存できる`() = runTest { val meta = Meta("1.0.1", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) - val metaRepository = mock { + val metaRepository = mock { onBlocking { save(any()) } doReturn Unit } val metaServiceImpl = MetaServiceImpl(metaRepository, TestTransaction) @@ -53,7 +53,7 @@ class MetaServiceImplTest { @Test fun `getJwtMeta Jwtメタデータを取得できる`() = runTest { val meta = Meta("1.0.0", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn meta } val metaService = MetaServiceImpl(metaRepository, TestTransaction) @@ -63,7 +63,7 @@ class MetaServiceImplTest { @Test fun `getJwtMeta メタデータが無いときはNotInitExceptionがthrowされる`() = runTest { - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn null } val metaService = MetaServiceImpl(metaRepository, TestTransaction) diff --git a/src/test/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImplTest.kt index c854754f..e7f87517 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/core/ServerInitialiseServiceImplTest.kt @@ -4,7 +4,7 @@ package dev.usbharu.hideout.service.core import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta -import dev.usbharu.hideout.repository.IMetaRepository +import dev.usbharu.hideout.repository.MetaRepository import dev.usbharu.hideout.util.ServerUtil import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -17,7 +17,7 @@ import kotlin.test.assertEquals class ServerInitialiseServiceImplTest { @Test fun `init メタデータが無いときに初期化を実行する`() = runTest { - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn null onBlocking { save(any()) } doReturn Unit } @@ -30,7 +30,7 @@ class ServerInitialiseServiceImplTest { @Test fun `init メタデータが存在して同じバージョンのときは何もしない`() = runTest { val meta = Meta(ServerUtil.getImplementationVersion(), Jwt(UUID.randomUUID(), "aaafafd", "afafasdf")) - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn meta } val serverInitialiseServiceImpl = ServerInitialiseServiceImpl(metaRepository, TestTransaction) @@ -41,7 +41,7 @@ class ServerInitialiseServiceImplTest { @Test fun `init メタデータが存在して違うバージョンのときはバージョンを変更する`() = runTest { val meta = Meta("1.0.0", Jwt(UUID.randomUUID(), "aaafafd", "afafasdf")) - val metaRepository = mock { + val metaRepository = mock { onBlocking { get() } doReturn meta onBlocking { save(any()) } doReturn Unit } diff --git a/src/test/kotlin/dev/usbharu/hideout/service/user/UserServiceTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/user/UserServiceTest.kt index 4aed672e..e32ca6f1 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/user/UserServiceTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/user/UserServiceTest.kt @@ -6,7 +6,7 @@ import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.ConfigData import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto -import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.repository.UserRepository import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Test @@ -20,15 +20,15 @@ class UserServiceTest { @Test fun `createLocalUser ローカルユーザーを作成できる`() = runTest { Config.configData = ConfigData(domain = "example.com", url = "https://example.com") - val userRepository = mock { + val userRepository = mock { onBlocking { nextId() } doReturn 110001L } val generateKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair() - val userAuthService = mock { + val userAuthService = mock { onBlocking { hash(anyString()) } doReturn "hashedPassword" onBlocking { generateKeyPair() } doReturn generateKeyPair } - val userService = UserService(userRepository, userAuthService, mock(), mock(), mock()) + val userService = UserServiceImpl(userRepository, userAuthService, mock(), mock(), mock()) userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test")) verify(userRepository, times(1)).save(any()) argumentCaptor { @@ -51,10 +51,10 @@ class UserServiceTest { fun `createRemoteUser リモートユーザーを作成できる`() = runTest { Config.configData = ConfigData(domain = "example.com", url = "https://example.com") - val userRepository = mock { + val userRepository = mock { onBlocking { nextId() } doReturn 113345L } - val userService = UserService(userRepository, mock(), mock(), mock(), mock()) + val userService = UserServiceImpl(userRepository, mock(), mock(), mock(), mock()) val user = RemoteUserCreateDto( "test", "example.com",