refactor: UserEntityを削除

This commit is contained in:
usbharu 2023-04-26 17:08:27 +09:00
parent 50b9e3178c
commit 07cf2c7c9d
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
13 changed files with 159 additions and 152 deletions

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.domain.model
import dev.usbharu.hideout.repository.Users
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table

View File

@ -1,12 +1,6 @@
package dev.usbharu.hideout.domain.model
import org.h2.mvstore.type.LongDataType
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
data class User(
val id:Long,
@ -22,63 +16,3 @@ data class User(
val privateKey:String? = null,
val createdAt:LocalDateTime
)
data class UserEntity(
val id: Long,
val name: String,
val domain: String,
val screenName: String,
val description: String,
val inbox: String,
val outbox: String,
val url: String
) {
constructor(id: Long, user: User) : this(
id,
user.name,
user.domain,
user.screenName,
user.description,
user.inbox,
user.outbox,
user.url
)
}
object Users : Table("users") {
val id = long("id").uniqueIndex()
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)
val createdAt = long("created_at")
override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex(name, domain)
}
}
fun ResultRow.toUser(): User {
return User(
this[Users.id],
this[Users.name],
this[Users.domain],
this[Users.screenName],
this[Users.description],
this[Users.password],
this[Users.inbox],
this[Users.outbox],
this[Users.url],
this[Users.publicKey],
this[Users.privateKey],
LocalDateTime.ofInstant(Instant.ofEpochMilli((this[Users.createdAt])), ZoneId.systemDefault())
)
}

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.domain.model
import dev.usbharu.hideout.repository.Users
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.ReferenceOption

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.domain.model
import dev.usbharu.hideout.repository.Users
import org.jetbrains.exposed.dao.id.LongIdTable
object UsersFollowers : LongIdTable("users_followers") {

View File

@ -1,32 +1,31 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity
interface IUserRepository {
suspend fun create(user: User): UserEntity
suspend fun create(user: User): User
suspend fun findById(id: Long): UserEntity?
suspend fun findById(id: Long): User?
suspend fun findByIds(ids: List<Long>): List<UserEntity>
suspend fun findByIds(ids: List<Long>): List<User>
suspend fun findByName(name: String): UserEntity?
suspend fun findByName(name: String): User?
suspend fun findByNameAndDomains(names: List<Pair<String,String>>): List<UserEntity>
suspend fun findByNameAndDomains(names: List<Pair<String,String>>): List<User>
suspend fun findByUrl(url:String):UserEntity?
suspend fun findByUrl(url:String): User?
suspend fun findByUrls(urls: List<String>): List<UserEntity>
suspend fun findByUrls(urls: List<String>): List<User>
suspend fun update(userEntity: UserEntity)
suspend fun update(userEntity: User)
suspend fun delete(id: Long)
suspend fun findAll(): List<User>
suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long = 0): List<UserEntity>
suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long = 0): List<User>
suspend fun createFollower(id: Long, follower: Long)
suspend fun deleteFollower(id: Long, follower: Long)
suspend fun findFollowersById(id: Long): List<UserEntity>
suspend fun findFollowersById(id: Long): List<User>
}

View File

@ -6,6 +6,9 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
class UserRepository(private val database: Database) : IUserRepository {
init {
@ -17,25 +20,14 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
private fun ResultRow.toUserEntity(): UserEntity {
return UserEntity(
this[Users.id],
this[Users.name],
this[Users.domain],
this[Users.screenName],
this[Users.description],
this[Users.inbox],
this[Users.outbox],
this[Users.url],
)
}
private fun ResultRow.toUserEntity(): User = toUser()
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun create(user: User): UserEntity {
override suspend fun create(user: User): User {
return query {
UserEntity(Users.insert {
Users.insert {
it[name] = user.name
it[domain] = user.domain
it[screenName] = user.screenName
@ -43,7 +35,8 @@ class UserRepository(private val database: Database) : IUserRepository {
it[inbox] = user.inbox
it[outbox] = user.outbox
it[url] = user.url
}[Users.id], user)
}
return@query user
}
}
@ -56,7 +49,7 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findById(id: Long): UserEntity? {
override suspend fun findById(id: Long): User? {
return query {
Users.select { Users.id eq id }.map {
it.toUserEntity()
@ -64,7 +57,7 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findByIds(ids: List<Long>): List<UserEntity> {
override suspend fun findByIds(ids: List<Long>): List<User> {
return query {
Users.select { Users.id inList ids }.map {
it.toUserEntity()
@ -72,7 +65,7 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findByName(name: String): UserEntity? {
override suspend fun findByName(name: String): User? {
return query {
Users.select { Users.name eq name }.map {
it.toUserEntity()
@ -80,7 +73,7 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<UserEntity> {
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<User> {
return query {
val selectAll = Users.selectAll()
names.forEach { (name, domain) ->
@ -90,19 +83,19 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findByUrl(url: String): UserEntity? {
override suspend fun findByUrl(url: String): User? {
return query {
Users.select { Users.url eq url }.singleOrNull()?.toUserEntity()
}
}
override suspend fun findByUrls(urls: List<String>): List<UserEntity> {
override suspend fun findByUrls(urls: List<String>): List<User> {
return query {
Users.select { Users.url inList urls }.map { it.toUserEntity() }
}
}
override suspend fun findFollowersById(id: Long): List<UserEntity> {
override suspend fun findFollowersById(id: Long): List<User> {
return query {
val followers = Users.alias("FOLLOWERS")
Users.innerJoin(
@ -127,22 +120,26 @@ class UserRepository(private val database: Database) : IUserRepository {
)
.select { Users.id eq id }
.map {
UserEntity(
User(
id = it[followers[Users.id]],
name = it[followers[Users.name]],
domain = it[followers[Users.domain]],
screenName = it[followers[Users.screenName]],
description = it[followers[Users.description]],
password = it[followers[Users.password]],
inbox = it[followers[Users.inbox]],
outbox = it[followers[Users.outbox]],
url = it[followers[Users.url]],
publicKey = it[followers[Users.publicKey]],
privateKey = it[followers[Users.privateKey]],
createdAt = LocalDateTime.ofInstant(Instant.ofEpochMilli(it[followers[Users.createdAt]]), ZoneId.systemDefault())
)
}
}
}
override suspend fun update(userEntity: UserEntity) {
override suspend fun update(userEntity: User) {
return query {
Users.update({ Users.id eq userEntity.id }) {
it[name] = userEntity.name
@ -174,9 +171,46 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<UserEntity> {
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<User> {
return query {
Users.selectAll().limit(limit, offset).map { it.toUserEntity() }
}
}
}
object Users : Table("users") {
val id = long("id").uniqueIndex()
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)
val createdAt = long("created_at")
override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex(name, domain)
}
}
fun ResultRow.toUser(): User {
return User(
this[Users.id],
this[Users.name],
this[Users.domain],
this[Users.screenName],
this[Users.description],
this[Users.password],
this[Users.inbox],
this[Users.outbox],
this[Users.url],
this[Users.publicKey],
this[Users.privateKey],
LocalDateTime.ofInstant(Instant.ofEpochMilli((this[Users.createdAt])), ZoneId.systemDefault())
)
}

View File

@ -4,7 +4,6 @@ import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository

View File

@ -1,7 +1,6 @@
package dev.usbharu.hideout.service.impl
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserRepository
import java.lang.Integer.min
@ -9,7 +8,7 @@ import java.lang.Integer.min
class UserService(private val userRepository: IUserRepository) {
private val maxLimit = 100
suspend fun findAll(limit: Int? = maxLimit, offset: Long? = 0): List<UserEntity> {
suspend fun findAll(limit: Int? = maxLimit, offset: Long? = 0): List<User> {
return userRepository.findAllByLimitAndByOffset(
min(limit ?: maxLimit, maxLimit),
@ -17,36 +16,36 @@ class UserService(private val userRepository: IUserRepository) {
)
}
suspend fun findById(id: Long): UserEntity {
suspend fun findById(id: Long): User {
return userRepository.findById(id) ?: throw UserNotFoundException("$id was not found.")
}
suspend fun findByIds(ids: List<Long>): List<UserEntity> {
suspend fun findByIds(ids: List<Long>): List<User> {
return userRepository.findByIds(ids)
}
suspend fun findByName(name: String): UserEntity {
suspend fun findByName(name: String): User {
return userRepository.findByName(name)
?: throw UserNotFoundException("$name was not found.")
}
suspend fun findByNameAndDomains(names: List<Pair<String,String>>): List<UserEntity> {
suspend fun findByNameAndDomains(names: List<Pair<String,String>>): List<User> {
return userRepository.findByNameAndDomains(names)
}
suspend fun findByUrl(url: String): UserEntity {
suspend fun findByUrl(url: String): User {
return userRepository.findByUrl(url) ?: throw UserNotFoundException("$url was not found.")
}
suspend fun findByUrls(urls: List<String>): List<UserEntity> {
suspend fun findByUrls(urls: List<String>): List<User> {
return userRepository.findByUrls(urls)
}
suspend fun create(user: User): UserEntity {
suspend fun create(user: User): User {
return userRepository.create(user)
}
suspend fun findFollowersById(id: Long): List<UserEntity> {
suspend fun findFollowersById(id: Long): List<User> {
return userRepository.findFollowersById(id)
}

View File

@ -1,10 +1,9 @@
package dev.usbharu.hideout.plugins
import dev.usbharu.hideout.domain.model.ap.JsonLd
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.ap.JsonLd
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.impl.UserAuthService
@ -17,41 +16,55 @@ import org.junit.jupiter.api.Test
import java.security.KeyPairGenerator
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.time.LocalDateTime
class ActivityPubKtTest {
@Test
fun HttpSignTest(): Unit = runBlocking {
val ktorKeyMap = KtorKeyMap(UserAuthService(object : IUserRepository {
override suspend fun create(user: User): UserEntity {
override suspend fun create(user: User): User {
TODO("Not yet implemented")
}
override suspend fun findById(id: Long): UserEntity? {
override suspend fun findById(id: Long): User? {
TODO("Not yet implemented")
}
override suspend fun findByIds(ids: List<Long>): List<UserEntity> {
override suspend fun findByIds(ids: List<Long>): List<User> {
TODO("Not yet implemented")
}
override suspend fun findByName(name: String): UserEntity? {
return UserEntity(1, "test", "localhost", "test", "","","","")
override suspend fun findByName(name: String): User? {
return User(
1,
"test",
"localhost",
"test",
"",
"",
"",
"",
"",
"",
null,
LocalDateTime.now()
)
}
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<UserEntity> {
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<User> {
TODO("Not yet implemented")
}
override suspend fun findByUrl(url: String): UserEntity? {
override suspend fun findByUrl(url: String): User? {
TODO("Not yet implemented")
}
override suspend fun findByUrls(urls: List<String>): List<UserEntity> {
override suspend fun findByUrls(urls: List<String>): List<User> {
TODO("Not yet implemented")
}
override suspend fun update(userEntity: UserEntity) {
override suspend fun update(userEntity: User) {
TODO("Not yet implemented")
}
@ -63,7 +76,7 @@ class ActivityPubKtTest {
TODO("Not yet implemented")
}
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<UserEntity> {
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<User> {
TODO("Not yet implemented")
}
@ -75,7 +88,7 @@ class ActivityPubKtTest {
TODO("Not yet implemented")
}
override suspend fun findFollowersById(id: Long): List<UserEntity> {
override suspend fun findFollowersById(id: Long): List<User> {
TODO("Not yet implemented")
}

View File

@ -3,7 +3,6 @@ package dev.usbharu.hideout.plugins
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.impl.UserAuthService
@ -12,41 +11,54 @@ import org.junit.jupiter.api.Test
import java.security.KeyPairGenerator
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.time.LocalDateTime
class KtorKeyMapTest {
@Test
fun getPrivateKey() {
val ktorKeyMap = KtorKeyMap(UserAuthService(object : IUserRepository {
override suspend fun create(user: User): UserEntity {
override suspend fun create(user: User): User {
TODO("Not yet implemented")
}
override suspend fun findById(id: Long): UserEntity? {
override suspend fun findById(id: Long): User? {
TODO("Not yet implemented")
}
override suspend fun findByIds(ids: List<Long>): List<UserEntity> {
override suspend fun findByIds(ids: List<Long>): List<User> {
TODO("Not yet implemented")
}
override suspend fun findByName(name: String): UserEntity? {
return UserEntity(1, "test", "localhost", "test", "","","","")
override suspend fun findByName(name: String): User? {
return User(
1,
"test",
"localhost",
"test",
"",
"",
"",
"",
"",
"",
createdAt = LocalDateTime.now()
)
}
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<UserEntity> {
override suspend fun findByNameAndDomains(names: List<Pair<String, String>>): List<User> {
TODO("Not yet implemented")
}
override suspend fun findByUrl(url: String): UserEntity? {
override suspend fun findByUrl(url: String): User? {
TODO("Not yet implemented")
}
override suspend fun findByUrls(urls: List<String>): List<UserEntity> {
override suspend fun findByUrls(urls: List<String>): List<User> {
TODO("Not yet implemented")
}
override suspend fun update(userEntity: UserEntity) {
override suspend fun update(userEntity: User) {
TODO("Not yet implemented")
}
@ -58,7 +70,7 @@ class KtorKeyMapTest {
TODO("Not yet implemented")
}
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<UserEntity> {
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<User> {
TODO("Not yet implemented")
}
@ -70,7 +82,7 @@ class KtorKeyMapTest {
TODO("Not yet implemented")
}
override suspend fun findFollowersById(id: Long): List<UserEntity> {
override suspend fun findFollowersById(id: Long): List<User> {
TODO("Not yet implemented")
}

View File

@ -3,7 +3,6 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.Users
import dev.usbharu.hideout.domain.model.UsersFollowers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest

View File

@ -6,7 +6,7 @@ package dev.usbharu.hideout.service.activitypub
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.ap.*
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.service.impl.UserService
@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyString
import org.mockito.kotlin.*
import utils.JsonObjectMapper
import java.time.LocalDateTime
class ActivityPubFollowServiceImplTest {
@Test
@ -88,7 +89,7 @@ class ActivityPubFollowServiceImplTest {
}
val userService = mock<UserService> {
onBlocking { findByUrls(any()) } doReturn listOf(
UserEntity(
User(
id = 1L,
name = "test",
domain = "example.com",
@ -96,9 +97,11 @@ class ActivityPubFollowServiceImplTest {
description = "This user is test user.",
inbox = "https://example.com/inbox",
outbox = "https://example.com/outbox",
url = "https://example.com"
url = "https://example.com",
publicKey = "",
createdAt = LocalDateTime.now()
),
UserEntity(
User(
id = 2L,
name = "follower",
domain = "follower.example.com",
@ -106,7 +109,9 @@ class ActivityPubFollowServiceImplTest {
description = "This user is test follower user.",
inbox = "https://follower.example.com/inbox",
outbox = "https://follower.example.com/outbox",
url = "https://follower.example.com"
url = "https://follower.example.com",
publicKey = "",
createdAt = LocalDateTime.now()
)
)
onBlocking { addFollowers(any(), any()) } doReturn Unit

View File

@ -6,7 +6,7 @@ package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData
import dev.usbharu.hideout.domain.model.PostEntity
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
import dev.usbharu.hideout.service.impl.UserService
import dev.usbharu.hideout.service.job.JobQueueParentService
@ -20,13 +20,14 @@ import org.junit.jupiter.api.Test
import org.mockito.Mockito.eq
import org.mockito.kotlin.*
import utils.JsonObjectMapper
import java.time.LocalDateTime
import kotlin.test.assertEquals
class ActivityPubNoteServiceImplTest {
@Test
fun `createPost 新しい投稿`() = runTest {
val followers = listOf<UserEntity>(
UserEntity(
val followers = listOf<User>(
User(
2L,
"follower",
"follower.example.com",
@ -34,9 +35,12 @@ class ActivityPubNoteServiceImplTest {
"test follower user",
"https://follower.example.com/inbox",
"https://follower.example.com/outbox",
"https://follower.example.com"
"https://follower.example.com",
"",
publicKey = "",
createdAt = LocalDateTime.now()
),
UserEntity(
User(
3L,
"follower2",
"follower2.example.com",
@ -44,11 +48,14 @@ class ActivityPubNoteServiceImplTest {
"test follower2 user",
"https://follower2.example.com/inbox",
"https://follower2.example.com/outbox",
"https:.//follower2.example.com"
"https://follower2.example.com",
"",
publicKey = "",
createdAt = LocalDateTime.now()
)
)
val userService = mock<UserService> {
onBlocking { findById(eq(1L)) } doReturn UserEntity(
onBlocking { findById(eq(1L)) } doReturn User(
1L,
"test",
"example.com",
@ -56,7 +63,10 @@ class ActivityPubNoteServiceImplTest {
"test user",
"https://example.com/inbox",
"https://example.com/outbox",
"https:.//example.com"
"https:.//example.com",
"",
publicKey = "",
createdAt = LocalDateTime.now()
)
onBlocking { findFollowersById(eq(1L)) } doReturn followers
}