refactor: UserAuthenticationを非推奨に

This commit is contained in:
usbharu 2023-04-26 16:52:05 +09:00
parent 8101354e91
commit 98178ef5ab
5 changed files with 41 additions and 26 deletions

View File

@ -1,16 +1,26 @@
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,
val name: String,
val domain: String,
val screenName: String,
val description: String,
val password:String? = null,
val inbox: String,
val outbox: String,
val url: String
val url: String,
val publicKey:String,
val privateKey:String? = null,
val createdAt:LocalDateTime
)
data class UserEntity(
@ -35,15 +45,21 @@ data class UserEntity(
)
}
object Users : LongIdTable("users") {
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)
}
@ -52,12 +68,17 @@ object Users : LongIdTable("users") {
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.url],
this[Users.publicKey],
this[Users.privateKey],
LocalDateTime.ofInstant(Instant.ofEpochMilli((this[Users.createdAt])), ZoneId.systemDefault())
)
}

View File

@ -3,13 +3,14 @@ package dev.usbharu.hideout.domain.model
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.ReferenceOption
@Deprecated("")
data class UserAuthentication(
val userId: Long,
val hash: String?,
val publicKey: String,
val privateKey: String?
)
@Deprecated("")
data class UserAuthenticationEntity(
val id: Long,
val userId: Long,
@ -25,7 +26,7 @@ data class UserAuthenticationEntity(
userAuthentication.privateKey
)
}
@Deprecated("")
object UsersAuthentication : LongIdTable("users_auth") {
val userId = long("user_id").references(Users.id, onUpdate = ReferenceOption.CASCADE)
val hash = varchar("hash", length = 64).nullable()

View File

@ -1,9 +1,6 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.Users
import dev.usbharu.hideout.domain.model.UsersFollowers
import dev.usbharu.hideout.domain.model.*
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
@ -20,21 +17,9 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
private fun ResultRow.toUser(): User {
return User(
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(): UserEntity {
return UserEntity(
this[Users.id].value,
this[Users.id],
this[Users.name],
this[Users.domain],
this[Users.screenName],
@ -58,7 +43,7 @@ class UserRepository(private val database: Database) : IUserRepository {
it[inbox] = user.inbox
it[outbox] = user.outbox
it[url] = user.url
}[Users.id].value, user)
}[Users.id], user)
}
}
@ -143,7 +128,7 @@ class UserRepository(private val database: Database) : IUserRepository {
.select { Users.id eq id }
.map {
UserEntity(
id = it[followers[Users.id]].value,
id = it[followers[Users.id]],
name = it[followers[Users.name]],
domain = it[followers[Users.domain]],
screenName = it[followers[Users.screenName]],

View File

@ -17,6 +17,7 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import org.slf4j.LoggerFactory
import java.time.LocalDateTime
class ActivityPubUserServiceImpl(
private val userService: UserService,
@ -91,6 +92,7 @@ class ActivityPubUserServiceImpl(
val person = Config.configData.objectMapper.readValue<Person>(httpResponse.bodyAsText())
val userEntity = userService.create(
User(
id = 0L,
name = person.preferredUsername
?: throw IllegalActivityPubObjectException("preferredUsername is null"),
domain = url.substringAfter(":").substringBeforeLast("/"),
@ -98,7 +100,9 @@ class ActivityPubUserServiceImpl(
description = person.summary ?: "",
inbox = person.inbox ?: throw IllegalActivityPubObjectException("inbox is null"),
outbox = person.outbox ?: throw IllegalActivityPubObjectException("outbox is null"),
url = url
url = url,
publicKey = "",
createdAt = LocalDateTime.now()
)
)
userAuthService.createAccount(

View File

@ -13,6 +13,7 @@ import io.ktor.util.*
import java.security.*
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.time.LocalDateTime
import java.util.*
class UserAuthService(
@ -34,13 +35,16 @@ class UserAuthService(
override suspend fun registerAccount(username: String, hash: String) {
val url = "${Config.configData.url}/users/$username"
val registerUser = User(
id = 0L,
name = username,
domain = Config.configData.domain,
screenName = username,
description = "",
inbox = "$url/inbox",
outbox = "$url/outbox",
url = url
url = url,
publicKey = "",
createdAt = LocalDateTime.now(),
)
val createdUser = userRepository.create(registerUser)