mirror of https://github.com/usbharu/Hideout.git
Merge pull request #62 from usbharu/feature/refactor-exposed
Feature/refactor exposed
This commit is contained in:
commit
401f5e103c
|
@ -111,6 +111,8 @@ dependencies {
|
|||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
|
||||
implementation("org.springframework.security:spring-security-oauth2-jose")
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
|
||||
implementation("org.jetbrains.exposed:exposed-spring-boot-starter:0.44.0")
|
||||
|
||||
|
||||
implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")
|
||||
|
||||
|
|
|
@ -157,3 +157,6 @@ potential-bugs:
|
|||
|
||||
ElseCaseInsteadOfExhaustiveWhen:
|
||||
active: true
|
||||
|
||||
HasPlatformType:
|
||||
active: false
|
||||
|
|
|
@ -2,7 +2,7 @@ ktor_version=2.3.0
|
|||
kotlin_version=1.8.21
|
||||
logback_version=1.4.6
|
||||
kotlin.code.style=official
|
||||
exposed_version=0.41.1
|
||||
exposed_version=0.44.0
|
||||
h2_version=2.1.214
|
||||
koin_version=3.4.3
|
||||
org.gradle.parallel=true
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package dev.usbharu.hideout.config
|
||||
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import java.net.URL
|
||||
|
||||
|
@ -15,16 +13,6 @@ class SpringConfig {
|
|||
|
||||
@Autowired
|
||||
lateinit var config: ApplicationConfig
|
||||
|
||||
@Bean
|
||||
fun database(): Database {
|
||||
return Database.connect(
|
||||
url = dbConfig.url,
|
||||
driver = dbConfig.driver,
|
||||
user = dbConfig.user,
|
||||
password = dbConfig.password
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ConfigurationProperties("hideout")
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package dev.usbharu.hideout.config
|
||||
|
||||
import org.jetbrains.exposed.spring.SpringTransactionManager
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement
|
||||
import org.springframework.transaction.annotation.TransactionManagementConfigurer
|
||||
import javax.sql.DataSource
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
class SpringTransactionConfig(val datasource: DataSource) : TransactionManagementConfigurer {
|
||||
@Bean
|
||||
override fun annotationDrivenTransactionManager(): PlatformTransactionManager =
|
||||
SpringTransactionManager(datasource)
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package dev.usbharu.hideout.query
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
|
||||
import dev.usbharu.hideout.exception.FailedToGetResourcesException
|
||||
import dev.usbharu.hideout.repository.JwtRefreshTokens
|
||||
import dev.usbharu.hideout.repository.toJwtRefreshToken
|
||||
import dev.usbharu.hideout.util.singleOr
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.deleteAll
|
||||
import org.jetbrains.exposed.sql.deleteWhere
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
class JwtRefreshTokenQueryServiceImpl : JwtRefreshTokenQueryService {
|
||||
override suspend fun findById(id: Long): JwtRefreshToken =
|
||||
JwtRefreshTokens.select { JwtRefreshTokens.id.eq(id) }
|
||||
.singleOr { FailedToGetResourcesException("id: $id is a duplicate or does not exist.", it) }
|
||||
.toJwtRefreshToken()
|
||||
|
||||
override suspend fun findByToken(token: String): JwtRefreshToken =
|
||||
JwtRefreshTokens.select { JwtRefreshTokens.refreshToken.eq(token) }
|
||||
.singleOr { FailedToGetResourcesException("token: $token is a duplicate or does not exist.", it) }
|
||||
.toJwtRefreshToken()
|
||||
|
||||
override suspend fun findByUserId(userId: Long): JwtRefreshToken =
|
||||
JwtRefreshTokens.select { JwtRefreshTokens.userId.eq(userId) }
|
||||
.singleOr { FailedToGetResourcesException("userId: $userId is a duplicate or does not exist.", it) }
|
||||
.toJwtRefreshToken()
|
||||
|
||||
override suspend fun deleteById(id: Long) {
|
||||
JwtRefreshTokens.deleteWhere { JwtRefreshTokens.id eq id }
|
||||
}
|
||||
|
||||
override suspend fun deleteByToken(token: String) {
|
||||
JwtRefreshTokens.deleteWhere { refreshToken eq token }
|
||||
}
|
||||
|
||||
override suspend fun deleteByUserId(userId: Long) {
|
||||
JwtRefreshTokens.deleteWhere { JwtRefreshTokens.userId eq userId }
|
||||
}
|
||||
|
||||
override suspend fun deleteAll() {
|
||||
JwtRefreshTokens.deleteAll()
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package dev.usbharu.hideout.repository
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface JwtRefreshTokenRepository {
|
||||
suspend fun generateId(): Long
|
||||
|
||||
suspend fun save(token: JwtRefreshToken)
|
||||
|
||||
suspend fun findById(id: Long): JwtRefreshToken?
|
||||
|
||||
suspend fun delete(token: JwtRefreshToken)
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
package dev.usbharu.hideout.repository
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.time.Instant
|
||||
|
||||
@Repository
|
||||
class JwtRefreshTokenRepositoryImpl(
|
||||
private val database: Database,
|
||||
private val idGenerateService: IdGenerateService
|
||||
) :
|
||||
JwtRefreshTokenRepository {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(JwtRefreshTokens)
|
||||
SchemaUtils.createMissingTablesAndColumns(JwtRefreshTokens)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun generateId(): Long = idGenerateService.generateId()
|
||||
|
||||
override suspend fun save(token: JwtRefreshToken) {
|
||||
if (JwtRefreshTokens.select { JwtRefreshTokens.id.eq(token.id) }.empty()) {
|
||||
JwtRefreshTokens.insert {
|
||||
it[id] = token.id
|
||||
it[userId] = token.userId
|
||||
it[refreshToken] = token.refreshToken
|
||||
it[createdAt] = token.createdAt.toEpochMilli()
|
||||
it[expiresAt] = token.expiresAt.toEpochMilli()
|
||||
}
|
||||
} else {
|
||||
JwtRefreshTokens.update({ JwtRefreshTokens.id eq token.id }) {
|
||||
it[userId] = token.userId
|
||||
it[refreshToken] = token.refreshToken
|
||||
it[createdAt] = token.createdAt.toEpochMilli()
|
||||
it[expiresAt] = token.expiresAt.toEpochMilli()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun findById(id: Long): JwtRefreshToken? =
|
||||
JwtRefreshTokens.select { JwtRefreshTokens.id.eq(id) }.singleOrNull()?.toJwtRefreshToken()
|
||||
|
||||
override suspend fun delete(token: JwtRefreshToken) {
|
||||
JwtRefreshTokens.deleteWhere { id eq token.id }
|
||||
}
|
||||
}
|
||||
|
||||
fun ResultRow.toJwtRefreshToken(): JwtRefreshToken {
|
||||
return JwtRefreshToken(
|
||||
this[JwtRefreshTokens.id],
|
||||
this[JwtRefreshTokens.userId],
|
||||
this[JwtRefreshTokens.refreshToken],
|
||||
Instant.ofEpochMilli(this[JwtRefreshTokens.createdAt]),
|
||||
Instant.ofEpochMilli(this[JwtRefreshTokens.expiresAt])
|
||||
)
|
||||
}
|
||||
|
||||
object JwtRefreshTokens : Table("jwt_refresh_tokens") {
|
||||
val id = long("id")
|
||||
val userId = long("user_id")
|
||||
val refreshToken = varchar("refresh_token", 1000)
|
||||
val createdAt = long("created_at")
|
||||
val expiresAt = long("expires_at")
|
||||
override val primaryKey = PrimaryKey(id)
|
||||
}
|
|
@ -1,20 +1,15 @@
|
|||
package dev.usbharu.hideout.repository
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Jwt
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.util.*
|
||||
|
||||
@Repository
|
||||
class MetaRepositoryImpl(private val database: Database) : MetaRepository {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(Meta)
|
||||
SchemaUtils.createMissingTablesAndColumns(Meta)
|
||||
}
|
||||
}
|
||||
class MetaRepositoryImpl : MetaRepository {
|
||||
|
||||
override suspend fun save(meta: dev.usbharu.hideout.domain.model.hideout.entity.Meta) {
|
||||
if (Meta.select { Meta.id eq 1 }.empty()) {
|
||||
|
|
|
@ -13,7 +13,6 @@ class MongoTimelineRepositoryWrapper(
|
|||
private val idGenerateService: IdGenerateService
|
||||
) :
|
||||
TimelineRepository {
|
||||
override suspend fun generateId(): Long = idGenerateService.generateId()
|
||||
|
||||
override suspend fun save(timeline: Timeline): Timeline {
|
||||
return withContext(Dispatchers.IO) {
|
||||
|
|
|
@ -6,7 +6,6 @@ import org.springframework.stereotype.Repository
|
|||
@Suppress("LongParameterList")
|
||||
@Repository
|
||||
interface PostRepository {
|
||||
suspend fun generateId(): Long
|
||||
suspend fun save(post: Post): Post
|
||||
suspend fun delete(id: Long)
|
||||
suspend fun findById(id: Long): Post
|
||||
|
|
|
@ -6,20 +6,10 @@ import dev.usbharu.hideout.exception.FailedToGetResourcesException
|
|||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : PostRepository {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(Posts)
|
||||
SchemaUtils.createMissingTablesAndColumns(Posts)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun generateId(): Long = idGenerateService.generateId()
|
||||
class PostRepositoryImpl(private val idGenerateService: IdGenerateService) : PostRepository {
|
||||
|
||||
override suspend fun save(post: Post): Post {
|
||||
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
|
||||
|
|
|
@ -5,7 +5,6 @@ import org.springframework.stereotype.Repository
|
|||
|
||||
@Repository
|
||||
interface ReactionRepository {
|
||||
suspend fun generateId(): Long
|
||||
suspend fun save(reaction: Reaction): Reaction
|
||||
suspend fun delete(reaction: Reaction): Reaction
|
||||
}
|
||||
|
|
|
@ -5,24 +5,13 @@ 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.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
class ReactionRepositoryImpl(
|
||||
private val database: Database,
|
||||
private val idGenerateService: IdGenerateService
|
||||
) : ReactionRepository {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(Reactions)
|
||||
SchemaUtils.createMissingTablesAndColumns(Reactions)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun generateId(): Long = idGenerateService.generateId()
|
||||
|
||||
override suspend fun save(reaction: Reaction): Reaction {
|
||||
if (Reactions.select { Reactions.id eq reaction.id }.empty()) {
|
||||
Reactions.insert {
|
||||
|
|
|
@ -10,7 +10,6 @@ import dev.usbharu.hideout.service.auth.ExposedOAuth2AuthorizationService
|
|||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.javatime.CurrentTimestamp
|
||||
import org.jetbrains.exposed.sql.javatime.timestamp
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.security.jackson2.SecurityJackson2Modules
|
||||
import org.springframework.security.oauth2.core.AuthorizationGrantType
|
||||
|
@ -27,14 +26,7 @@ import java.time.Instant
|
|||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient as SpringRegisteredClient
|
||||
|
||||
@Repository
|
||||
class RegisteredClientRepositoryImpl(private val database: Database) : RegisteredClientRepository {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(RegisteredClient)
|
||||
SchemaUtils.createMissingTablesAndColumns(RegisteredClient)
|
||||
}
|
||||
}
|
||||
class RegisteredClientRepositoryImpl : RegisteredClientRepository {
|
||||
|
||||
override fun save(registeredClient: SpringRegisteredClient?) {
|
||||
requireNotNull(registeredClient)
|
||||
|
|
|
@ -3,7 +3,6 @@ package dev.usbharu.hideout.repository
|
|||
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
|
||||
|
||||
interface TimelineRepository {
|
||||
suspend fun generateId(): Long
|
||||
suspend fun save(timeline: Timeline): Timeline
|
||||
suspend fun saveAll(timelines: List<Timeline>): List<Timeline>
|
||||
suspend fun findByUserId(id: Long): List<Timeline>
|
||||
|
|
|
@ -6,23 +6,12 @@ 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.springframework.stereotype.Repository
|
||||
import java.time.Instant
|
||||
|
||||
@Repository
|
||||
class UserRepositoryImpl(private val database: Database, private val idGenerateService: IdGenerateService) :
|
||||
class UserRepositoryImpl(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()
|
||||
|
|
|
@ -16,6 +16,7 @@ import dev.usbharu.hideout.query.FollowerQueryService
|
|||
import dev.usbharu.hideout.query.PostQueryService
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.repository.PostRepository
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.post.PostCreateInterceptor
|
||||
import dev.usbharu.hideout.service.post.PostService
|
||||
|
@ -49,7 +50,8 @@ class APNoteServiceImpl(
|
|||
private val postQueryService: PostQueryService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val applicationConfig: ApplicationConfig,
|
||||
private val postService: PostService
|
||||
private val postService: PostService,
|
||||
private val idGenerateService: IdGenerateService
|
||||
|
||||
) : APNoteService, PostCreateInterceptor {
|
||||
|
||||
|
@ -171,7 +173,7 @@ class APNoteServiceImpl(
|
|||
|
||||
postService.createRemote(
|
||||
Post.of(
|
||||
id = postRepository.generateId(),
|
||||
id = idGenerateService.generateId(),
|
||||
userId = person.second.id,
|
||||
overview = null,
|
||||
text = note.content.orEmpty(),
|
||||
|
|
|
@ -4,7 +4,6 @@ import dev.usbharu.hideout.service.core.Transaction
|
|||
import kotlinx.coroutines.runBlocking
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService
|
||||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository
|
||||
|
@ -15,17 +14,9 @@ import org.springframework.security.oauth2.server.authorization.OAuth2Authorizat
|
|||
class ExposedOAuth2AuthorizationConsentService(
|
||||
private val registeredClientRepository: RegisteredClientRepository,
|
||||
private val transaction: Transaction,
|
||||
private val database: Database
|
||||
) :
|
||||
OAuth2AuthorizationConsentService {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(OAuth2AuthorizationConsent)
|
||||
SchemaUtils.createMissingTablesAndColumns(OAuth2AuthorizationConsent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun save(authorizationConsent: AuthorizationConsent?) = runBlocking {
|
||||
requireNotNull(authorizationConsent)
|
||||
transaction.transaction {
|
||||
|
|
|
@ -10,7 +10,6 @@ import kotlinx.coroutines.runBlocking
|
|||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.javatime.timestamp
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.security.jackson2.CoreJackson2Module
|
||||
import org.springframework.security.jackson2.SecurityJackson2Modules
|
||||
import org.springframework.security.oauth2.core.*
|
||||
|
@ -29,17 +28,9 @@ import org.springframework.stereotype.Service
|
|||
class ExposedOAuth2AuthorizationService(
|
||||
private val registeredClientRepository: RegisteredClientRepository,
|
||||
private val transaction: Transaction,
|
||||
private val database: Database
|
||||
) :
|
||||
OAuth2AuthorizationService {
|
||||
|
||||
init {
|
||||
transaction(database) {
|
||||
SchemaUtils.create(Authorization)
|
||||
SchemaUtils.createMissingTablesAndColumns(Authorization)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongMethod", "CyclomaticComplexMethod")
|
||||
override fun save(authorization: OAuth2Authorization?): Unit = runBlocking {
|
||||
requireNotNull(authorization)
|
||||
|
|
|
@ -10,8 +10,6 @@ import org.springframework.stereotype.Service
|
|||
@Service
|
||||
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "nosql")
|
||||
class KjobMongoJobQueueParentService : JobQueueParentService {
|
||||
override fun init(jobDefines: List<Job>) = Unit
|
||||
|
||||
private val kjob = kjob(Mongo) {
|
||||
connectionString = "mongodb://localhost"
|
||||
databaseName = "kjob"
|
||||
|
@ -21,6 +19,8 @@ class KjobMongoJobQueueParentService : JobQueueParentService {
|
|||
isWorker = false
|
||||
}.start()
|
||||
|
||||
override fun init(jobDefines: List<Job>) = Unit
|
||||
|
||||
override suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit) {
|
||||
kjob.schedule(job, block)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
|||
import dev.usbharu.hideout.exception.UserNotFoundException
|
||||
import dev.usbharu.hideout.repository.PostRepository
|
||||
import dev.usbharu.hideout.repository.UserRepository
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
|
@ -13,7 +14,8 @@ import java.util.*
|
|||
class PostServiceImpl(
|
||||
private val postRepository: PostRepository,
|
||||
private val userRepository: UserRepository,
|
||||
private val timelineService: TimelineService
|
||||
private val timelineService: TimelineService,
|
||||
private val idGenerateService: IdGenerateService
|
||||
) : PostService {
|
||||
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
||||
|
||||
|
@ -36,7 +38,7 @@ class PostServiceImpl(
|
|||
|
||||
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
|
||||
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
|
||||
val id = postRepository.generateId()
|
||||
val id = idGenerateService.generateId()
|
||||
val createPost = Post.of(
|
||||
id = id,
|
||||
userId = post.userId,
|
||||
|
|
|
@ -6,13 +6,15 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
|
|||
import dev.usbharu.hideout.query.FollowerQueryService
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.repository.TimelineRepository
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class TimelineService(
|
||||
private val followerQueryService: FollowerQueryService,
|
||||
private val userQueryService: UserQueryService,
|
||||
private val timelineRepository: TimelineRepository
|
||||
private val timelineRepository: TimelineRepository,
|
||||
private val idGenerateService: IdGenerateService
|
||||
) {
|
||||
suspend fun publishTimeline(post: Post, isLocal: Boolean) {
|
||||
val findFollowersById = followerQueryService.findFollowersById(post.userId).toMutableList()
|
||||
|
@ -23,7 +25,7 @@ class TimelineService(
|
|||
}
|
||||
val timelines = findFollowersById.map {
|
||||
Timeline(
|
||||
id = timelineRepository.generateId(),
|
||||
id = idGenerateService.generateId(),
|
||||
userId = it.id,
|
||||
timelineId = 0,
|
||||
postId = post.id,
|
||||
|
@ -39,7 +41,7 @@ class TimelineService(
|
|||
if (post.visibility == Visibility.PUBLIC) {
|
||||
timelines.add(
|
||||
Timeline(
|
||||
id = timelineRepository.generateId(),
|
||||
id = idGenerateService.generateId(),
|
||||
userId = 0,
|
||||
timelineId = 0,
|
||||
postId = post.id,
|
||||
|
|
|
@ -4,18 +4,20 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
|
|||
import dev.usbharu.hideout.query.ReactionQueryService
|
||||
import dev.usbharu.hideout.repository.ReactionRepository
|
||||
import dev.usbharu.hideout.service.ap.APReactionService
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class ReactionServiceImpl(
|
||||
private val reactionRepository: ReactionRepository,
|
||||
private val apReactionService: APReactionService,
|
||||
private val reactionQueryService: ReactionQueryService
|
||||
private val reactionQueryService: ReactionQueryService,
|
||||
private val idGenerateService: IdGenerateService
|
||||
) : ReactionService {
|
||||
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
|
||||
if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) {
|
||||
reactionRepository.save(
|
||||
Reaction(reactionRepository.generateId(), 0, postId, userId)
|
||||
Reaction(idGenerateService.generateId(), 0, postId, userId)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +27,7 @@ class ReactionServiceImpl(
|
|||
// delete
|
||||
reactionQueryService.deleteByPostIdAndUserId(postId, userId)
|
||||
} else {
|
||||
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
|
||||
val reaction = Reaction(idGenerateService.generateId(), 0, postId, userId)
|
||||
reactionRepository.save(reaction)
|
||||
apReactionService.reaction(reaction)
|
||||
}
|
||||
|
|
|
@ -10,6 +10,4 @@ interface UserAuthService {
|
|||
suspend fun usernameAlreadyUse(username: String): Boolean
|
||||
|
||||
suspend fun generateKeyPair(): KeyPair
|
||||
|
||||
suspend fun verifyAccount(username: String, password: String): Boolean
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package dev.usbharu.hideout.service.user
|
||||
|
||||
import dev.usbharu.hideout.config.ApplicationConfig
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
||||
import org.springframework.stereotype.Service
|
||||
|
@ -9,8 +8,7 @@ import java.util.*
|
|||
|
||||
@Service
|
||||
class UserAuthServiceImpl(
|
||||
val userQueryService: UserQueryService,
|
||||
private val applicationConfig: ApplicationConfig
|
||||
val userQueryService: UserQueryService
|
||||
) : UserAuthService {
|
||||
|
||||
override fun hash(password: String): String = BCryptPasswordEncoder().encode(password)
|
||||
|
@ -20,11 +18,6 @@ class UserAuthServiceImpl(
|
|||
return true
|
||||
}
|
||||
|
||||
override suspend fun verifyAccount(username: String, password: String): Boolean {
|
||||
val userEntity = userQueryService.findByNameAndDomain(username, applicationConfig.url.host)
|
||||
return userEntity.password == hash(password)
|
||||
}
|
||||
|
||||
override suspend fun generateKeyPair(): KeyPair {
|
||||
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
|
||||
keyPairGenerator.initialize(keySize)
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
hideout:
|
||||
url: "https://test-hideout.usbharu.dev"
|
||||
database:
|
||||
url: "jdbc:h2:./test-dev2;MODE=POSTGRESQL"
|
||||
driver: "org.h2.Driver"
|
||||
user: ""
|
||||
password: ""
|
||||
job-queue:
|
||||
type: "nosql"
|
||||
security:
|
||||
|
@ -33,8 +28,9 @@ spring:
|
|||
h2:
|
||||
console:
|
||||
enabled: true
|
||||
exposed:
|
||||
generate-ddl: true
|
||||
server:
|
||||
|
||||
tomcat:
|
||||
basedir: tomcat
|
||||
accesslog:
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalCoroutinesApi::class)
|
||||
|
||||
package dev.usbharu.hideout.repository
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.time.temporal.ChronoUnit
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class JwtRefreshTokenRepositoryImplTest {
|
||||
|
||||
lateinit var db: Database
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
db = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
|
||||
transaction(db) {
|
||||
SchemaUtils.create(JwtRefreshTokens)
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
transaction(db) {
|
||||
SchemaUtils.drop(JwtRefreshTokens)
|
||||
}
|
||||
TransactionManager.closeAndUnregister(db)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save 存在しない場合はinsertする`() = runTest {
|
||||
val repository = JwtRefreshTokenRepositoryImpl(
|
||||
db,
|
||||
object : IdGenerateService {
|
||||
override suspend fun generateId(): Long {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
)
|
||||
val now = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
|
||||
val expiresAt = now.plus(10, ChronoUnit.MINUTES)
|
||||
|
||||
val expect = JwtRefreshToken(1L, 2L, "refreshToken", now, expiresAt)
|
||||
newSuspendedTransaction {
|
||||
repository.save(expect)
|
||||
val actual = repository.findById(1L)
|
||||
assertEquals(expect, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save 存在する場合はupdateする`() = runTest {
|
||||
val repository = JwtRefreshTokenRepositoryImpl(
|
||||
db,
|
||||
object : IdGenerateService {
|
||||
override suspend fun generateId(): Long {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
)
|
||||
newSuspendedTransaction {
|
||||
JwtRefreshTokens.insert {
|
||||
it[id] = 1L
|
||||
it[userId] = 2L
|
||||
it[refreshToken] = "refreshToken1"
|
||||
it[createdAt] = Instant.now().toEpochMilli()
|
||||
it[expiresAt] = Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli()
|
||||
}
|
||||
repository.save(
|
||||
JwtRefreshToken(
|
||||
id = 1L,
|
||||
userId = 2L,
|
||||
refreshToken = "refreshToken2",
|
||||
createdAt = Instant.now(),
|
||||
expiresAt = Instant.now().plus(10, ChronoUnit.MINUTES)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
transaction {
|
||||
val toJwtRefreshToken = JwtRefreshTokens.select { JwtRefreshTokens.id.eq(1L) }.single().toJwtRefreshToken()
|
||||
assertEquals("refreshToken2", toJwtRefreshToken.refreshToken)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -89,7 +89,8 @@ class APNoteServiceImplTest {
|
|||
postQueryService = mock(),
|
||||
objectMapper = objectMapper,
|
||||
applicationConfig = testApplicationConfig,
|
||||
postService = mock()
|
||||
postService = mock(),
|
||||
idGenerateService = mock()
|
||||
)
|
||||
val postEntity = Post.of(
|
||||
1L,
|
||||
|
@ -123,7 +124,8 @@ class APNoteServiceImplTest {
|
|||
postQueryService = mock(),
|
||||
objectMapper = objectMapper,
|
||||
applicationConfig = testApplicationConfig,
|
||||
postService = mock()
|
||||
postService = mock(),
|
||||
idGenerateService = mock()
|
||||
)
|
||||
activityPubNoteService.createNoteJob(
|
||||
JobProps(
|
||||
|
|
Loading…
Reference in New Issue