mirror of https://github.com/usbharu/Hideout.git
feat: LocalDateTimeからInstantに変更
This commit is contained in:
parent
d417245d92
commit
cb36a388b1
|
@ -1,18 +1,18 @@
|
||||||
package dev.usbharu.hideout.domain.model
|
package dev.usbharu.hideout.domain.model
|
||||||
|
|
||||||
import java.time.LocalDateTime
|
import java.time.Instant
|
||||||
|
|
||||||
data class User(
|
data class User(
|
||||||
val id:Long,
|
val id: Long,
|
||||||
val name: String,
|
val name: String,
|
||||||
val domain: String,
|
val domain: String,
|
||||||
val screenName: String,
|
val screenName: String,
|
||||||
val description: String,
|
val description: String,
|
||||||
val password:String? = null,
|
val password: String? = null,
|
||||||
val inbox: String,
|
val inbox: String,
|
||||||
val outbox: String,
|
val outbox: String,
|
||||||
val url: String,
|
val url: String,
|
||||||
val publicKey:String,
|
val publicKey: String,
|
||||||
val privateKey:String? = null,
|
val privateKey: String? = null,
|
||||||
val createdAt:LocalDateTime
|
val createdAt: Instant
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
|
||||||
class UserRepository(private val database: Database) : IUserRepository {
|
class UserRepository(private val database: Database) : IUserRepository {
|
||||||
init {
|
init {
|
||||||
|
@ -28,13 +29,18 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
override suspend fun create(user: User): User {
|
override suspend fun create(user: User): User {
|
||||||
return query {
|
return query {
|
||||||
Users.insert {
|
Users.insert {
|
||||||
|
it[id] = user.id
|
||||||
it[name] = user.name
|
it[name] = user.name
|
||||||
it[domain] = user.domain
|
it[domain] = user.domain
|
||||||
it[screenName] = user.screenName
|
it[screenName] = user.screenName
|
||||||
it[description] = user.description
|
it[description] = user.description
|
||||||
|
it[password] = user.password
|
||||||
it[inbox] = user.inbox
|
it[inbox] = user.inbox
|
||||||
it[outbox] = user.outbox
|
it[outbox] = user.outbox
|
||||||
it[url] = user.url
|
it[url] = user.url
|
||||||
|
it[createdAt] = user.createdAt.toEpochMilli()
|
||||||
|
it[publicKey] = user.publicKey
|
||||||
|
it[privateKey] = user.privateKey
|
||||||
}
|
}
|
||||||
return@query user
|
return@query user
|
||||||
}
|
}
|
||||||
|
@ -101,7 +107,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
Users.innerJoin(
|
Users.innerJoin(
|
||||||
otherTable = UsersFollowers,
|
otherTable = UsersFollowers,
|
||||||
onColumn = { Users.id },
|
onColumn = { Users.id },
|
||||||
otherColumn = { UsersFollowers.userId })
|
otherColumn = { userId })
|
||||||
|
|
||||||
.innerJoin(
|
.innerJoin(
|
||||||
otherTable = followers,
|
otherTable = followers,
|
||||||
|
@ -114,9 +120,13 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
followers.get(Users.domain),
|
followers.get(Users.domain),
|
||||||
followers.get(Users.screenName),
|
followers.get(Users.screenName),
|
||||||
followers.get(Users.description),
|
followers.get(Users.description),
|
||||||
|
followers.get(Users.password),
|
||||||
followers.get(Users.inbox),
|
followers.get(Users.inbox),
|
||||||
followers.get(Users.outbox),
|
followers.get(Users.outbox),
|
||||||
followers.get(Users.url)
|
followers.get(Users.url),
|
||||||
|
followers.get(Users.publicKey),
|
||||||
|
followers.get(Users.privateKey),
|
||||||
|
followers.get(Users.createdAt)
|
||||||
)
|
)
|
||||||
.select { Users.id eq id }
|
.select { Users.id eq id }
|
||||||
.map {
|
.map {
|
||||||
|
@ -132,7 +142,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
url = it[followers[Users.url]],
|
url = it[followers[Users.url]],
|
||||||
publicKey = it[followers[Users.publicKey]],
|
publicKey = it[followers[Users.publicKey]],
|
||||||
privateKey = it[followers[Users.privateKey]],
|
privateKey = it[followers[Users.privateKey]],
|
||||||
createdAt = LocalDateTime.ofInstant(Instant.ofEpochMilli(it[followers[Users.createdAt]]), ZoneId.systemDefault())
|
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +199,7 @@ object Users : Table("users") {
|
||||||
val outbox = varchar("outbox", length = 255).uniqueIndex()
|
val outbox = varchar("outbox", length = 255).uniqueIndex()
|
||||||
val url = varchar("url", length = 255).uniqueIndex()
|
val url = varchar("url", length = 255).uniqueIndex()
|
||||||
val publicKey = varchar("public_key", length = 10000)
|
val publicKey = varchar("public_key", length = 10000)
|
||||||
val privateKey = varchar("private_key", length = 10000)
|
val privateKey = varchar("private_key", length = 10000).nullable()
|
||||||
val createdAt = long("created_at")
|
val createdAt = long("created_at")
|
||||||
|
|
||||||
override val primaryKey: PrimaryKey = PrimaryKey(id)
|
override val primaryKey: PrimaryKey = PrimaryKey(id)
|
||||||
|
@ -211,6 +221,6 @@ fun ResultRow.toUser(): User {
|
||||||
this[Users.url],
|
this[Users.url],
|
||||||
this[Users.publicKey],
|
this[Users.publicKey],
|
||||||
this[Users.privateKey],
|
this[Users.privateKey],
|
||||||
LocalDateTime.ofInstant(Instant.ofEpochMilli((this[Users.createdAt])), ZoneId.systemDefault())
|
Instant.ofEpochMilli((this[Users.createdAt]))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
class ActivityPubUserServiceImpl(
|
class ActivityPubUserServiceImpl(
|
||||||
|
@ -99,7 +100,7 @@ class ActivityPubUserServiceImpl(
|
||||||
outbox = person.outbox ?: throw IllegalActivityPubObjectException("outbox is null"),
|
outbox = person.outbox ?: throw IllegalActivityPubObjectException("outbox is null"),
|
||||||
url = url,
|
url = url,
|
||||||
publicKey = person.publicKey?.publicKeyPem ?: throw IllegalActivityPubObjectException("publicKey is null"),
|
publicKey = person.publicKey?.publicKeyPem ?: throw IllegalActivityPubObjectException("publicKey is null"),
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
person
|
person
|
||||||
|
|
|
@ -9,6 +9,7 @@ import io.ktor.util.*
|
||||||
import java.security.*
|
import java.security.*
|
||||||
import java.security.interfaces.RSAPrivateKey
|
import java.security.interfaces.RSAPrivateKey
|
||||||
import java.security.interfaces.RSAPublicKey
|
import java.security.interfaces.RSAPublicKey
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ class UserAuthService(
|
||||||
outbox = "$url/outbox",
|
outbox = "$url/outbox",
|
||||||
url = url,
|
url = url,
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now(),
|
createdAt = Instant.now(),
|
||||||
)
|
)
|
||||||
val createdUser = userRepository.create(registerUser)
|
val createdUser = userRepository.create(registerUser)
|
||||||
|
|
||||||
|
@ -69,13 +70,13 @@ class UserAuthService(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun PublicKey.toPem(): String {
|
fun PublicKey.toPem(): String {
|
||||||
return "-----BEGIN PUBLIC KEY-----\n" +
|
return "-----BEGIN PUBLIC KEY-----\n" +
|
||||||
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
|
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
|
||||||
"\n-----END PUBLIC KEY-----\n"
|
"\n-----END PUBLIC KEY-----\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun PrivateKey.toPem(): String {
|
fun PrivateKey.toPem(): String {
|
||||||
return "-----BEGIN PRIVATE KEY-----" +
|
return "-----BEGIN PRIVATE KEY-----" +
|
||||||
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
|
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
|
||||||
"\n-----END PRIVATE KEY-----\n"
|
"\n-----END PRIVATE KEY-----\n"
|
||||||
|
|
|
@ -3,11 +3,14 @@ package dev.usbharu.hideout.plugins
|
||||||
import dev.usbharu.hideout.domain.model.User
|
import dev.usbharu.hideout.domain.model.User
|
||||||
import dev.usbharu.hideout.domain.model.ap.JsonLd
|
import dev.usbharu.hideout.domain.model.ap.JsonLd
|
||||||
import dev.usbharu.hideout.repository.IUserRepository
|
import dev.usbharu.hideout.repository.IUserRepository
|
||||||
|
import dev.usbharu.hideout.service.impl.toPem
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.engine.mock.*
|
import io.ktor.client.engine.mock.*
|
||||||
import io.ktor.client.plugins.logging.*
|
import io.ktor.client.plugins.logging.*
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.security.KeyPairGenerator
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
class ActivityPubKtTest {
|
class ActivityPubKtTest {
|
||||||
|
@ -27,7 +30,10 @@ class ActivityPubKtTest {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByName(name: String): User? {
|
override suspend fun findByName(name: String): User {
|
||||||
|
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
|
||||||
|
keyPairGenerator.initialize(1024)
|
||||||
|
val generateKeyPair = keyPairGenerator.generateKeyPair()
|
||||||
return User(
|
return User(
|
||||||
1,
|
1,
|
||||||
"test",
|
"test",
|
||||||
|
@ -39,8 +45,8 @@ class ActivityPubKtTest {
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
null,
|
generateKeyPair.private.toPem(),
|
||||||
LocalDateTime.now()
|
Instant.now()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ package dev.usbharu.hideout.plugins
|
||||||
|
|
||||||
import dev.usbharu.hideout.domain.model.User
|
import dev.usbharu.hideout.domain.model.User
|
||||||
import dev.usbharu.hideout.repository.IUserRepository
|
import dev.usbharu.hideout.repository.IUserRepository
|
||||||
|
import dev.usbharu.hideout.service.impl.toPem
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.security.KeyPairGenerator
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
class KtorKeyMapTest {
|
class KtorKeyMapTest {
|
||||||
|
@ -22,7 +25,10 @@ class KtorKeyMapTest {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByName(name: String): User? {
|
override suspend fun findByName(name: String): User {
|
||||||
|
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
|
||||||
|
keyPairGenerator.initialize(1024)
|
||||||
|
val generateKeyPair = keyPairGenerator.generateKeyPair()
|
||||||
return User(
|
return User(
|
||||||
1,
|
1,
|
||||||
"test",
|
"test",
|
||||||
|
@ -34,7 +40,8 @@ class KtorKeyMapTest {
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
createdAt = LocalDateTime.now()
|
generateKeyPair.private.toPem(),
|
||||||
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,10 @@ import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.time.Clock
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneId
|
||||||
|
|
||||||
|
|
||||||
class UserRepositoryTest {
|
class UserRepositoryTest {
|
||||||
|
@ -43,35 +47,47 @@ class UserRepositoryTest {
|
||||||
val userRepository = UserRepository(db)
|
val userRepository = UserRepository(db)
|
||||||
val user = userRepository.create(
|
val user = userRepository.create(
|
||||||
User(
|
User(
|
||||||
"test",
|
id = 0L,
|
||||||
"example.com",
|
name = "test",
|
||||||
"testUser",
|
domain = "example.com",
|
||||||
"This user is test user.",
|
screenName = "testUser",
|
||||||
"https://example.com/inbox",
|
description = "This user is test user.",
|
||||||
"https://example.com/outbox",
|
password = "https://example.com/inbox",
|
||||||
"https://example.com"
|
inbox = "",
|
||||||
|
outbox = "https://example.com/outbox",
|
||||||
|
url = "https://example.com",
|
||||||
|
publicKey = "",
|
||||||
|
createdAt = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val follower = userRepository.create(
|
val follower = userRepository.create(
|
||||||
User(
|
User(
|
||||||
"follower",
|
id = 1L,
|
||||||
"follower.example.com",
|
name = "follower",
|
||||||
"followerUser",
|
domain = "follower.example.com",
|
||||||
"This user is follower user.",
|
screenName = "followerUser",
|
||||||
"https://follower.example.com/inbox",
|
description = "This user is follower user.",
|
||||||
"https://follower.example.com/outbox",
|
password = "",
|
||||||
"https://follower.example.com"
|
inbox = "https://follower.example.com/inbox",
|
||||||
|
outbox = "https://follower.example.com/outbox",
|
||||||
|
url = "https://follower.example.com",
|
||||||
|
publicKey = "",
|
||||||
|
createdAt = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val follower2 = userRepository.create(
|
val follower2 = userRepository.create(
|
||||||
User(
|
User(
|
||||||
"follower2",
|
id = 3L,
|
||||||
"follower2.example.com",
|
name = "follower2",
|
||||||
"followerUser2",
|
domain = "follower2.example.com",
|
||||||
"This user is follower user 2.",
|
screenName = "followerUser2",
|
||||||
"https://follower2.example.com/inbox",
|
description = "This user is follower user 2.",
|
||||||
"https://follower2.example.com/outbox",
|
password = "",
|
||||||
"https://follower2.example.com"
|
inbox = "https://follower2.example.com/inbox",
|
||||||
|
outbox = "https://follower2.example.com/outbox",
|
||||||
|
url = "https://follower2.example.com",
|
||||||
|
publicKey = "",
|
||||||
|
createdAt = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
userRepository.createFollower(user.id, follower.id)
|
userRepository.createFollower(user.id, follower.id)
|
||||||
|
@ -86,25 +102,31 @@ class UserRepositoryTest {
|
||||||
fun `createFollower フォロワー追加`() = runTest {
|
fun `createFollower フォロワー追加`() = runTest {
|
||||||
val userRepository = UserRepository(db)
|
val userRepository = UserRepository(db)
|
||||||
val user = userRepository.create(
|
val user = userRepository.create(
|
||||||
User(
|
User(0L,
|
||||||
"test",
|
"test",
|
||||||
"example.com",
|
"example.com",
|
||||||
"testUser",
|
"testUser",
|
||||||
"This user is test user.",
|
"This user is test user.",
|
||||||
"https://example.com/inbox",
|
"https://example.com/inbox",
|
||||||
|
"",
|
||||||
"https://example.com/outbox",
|
"https://example.com/outbox",
|
||||||
"https://example.com"
|
"https://example.com",
|
||||||
|
publicKey = "",
|
||||||
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val follower = userRepository.create(
|
val follower = userRepository.create(
|
||||||
User(
|
User(1L,
|
||||||
"follower",
|
"follower",
|
||||||
"follower.example.com",
|
"follower.example.com",
|
||||||
"followerUser",
|
"followerUser",
|
||||||
"This user is follower user.",
|
"This user is follower user.",
|
||||||
|
"",
|
||||||
"https://follower.example.com/inbox",
|
"https://follower.example.com/inbox",
|
||||||
"https://follower.example.com/outbox",
|
"https://follower.example.com/outbox",
|
||||||
"https://follower.example.com"
|
"https://follower.example.com",
|
||||||
|
publicKey = "",
|
||||||
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
userRepository.createFollower(user.id, follower.id)
|
userRepository.createFollower(user.id, follower.id)
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test
|
||||||
import org.mockito.ArgumentMatchers.anyString
|
import org.mockito.ArgumentMatchers.anyString
|
||||||
import org.mockito.kotlin.*
|
import org.mockito.kotlin.*
|
||||||
import utils.JsonObjectMapper
|
import utils.JsonObjectMapper
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
class ActivityPubFollowServiceImplTest {
|
class ActivityPubFollowServiceImplTest {
|
||||||
|
@ -99,7 +100,7 @@ class ActivityPubFollowServiceImplTest {
|
||||||
outbox = "https://example.com/outbox",
|
outbox = "https://example.com/outbox",
|
||||||
url = "https://example.com",
|
url = "https://example.com",
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
),
|
),
|
||||||
User(
|
User(
|
||||||
id = 2L,
|
id = 2L,
|
||||||
|
@ -111,7 +112,7 @@ class ActivityPubFollowServiceImplTest {
|
||||||
outbox = "https://follower.example.com/outbox",
|
outbox = "https://follower.example.com/outbox",
|
||||||
url = "https://follower.example.com",
|
url = "https://follower.example.com",
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
onBlocking { addFollowers(any(), any()) } doReturn Unit
|
onBlocking { addFollowers(any(), any()) } doReturn Unit
|
||||||
|
|
|
@ -20,7 +20,7 @@ import org.junit.jupiter.api.Test
|
||||||
import org.mockito.Mockito.eq
|
import org.mockito.Mockito.eq
|
||||||
import org.mockito.kotlin.*
|
import org.mockito.kotlin.*
|
||||||
import utils.JsonObjectMapper
|
import utils.JsonObjectMapper
|
||||||
import java.time.LocalDateTime
|
import java.time.Instant
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
class ActivityPubNoteServiceImplTest {
|
class ActivityPubNoteServiceImplTest {
|
||||||
|
@ -38,7 +38,7 @@ class ActivityPubNoteServiceImplTest {
|
||||||
"https://follower.example.com",
|
"https://follower.example.com",
|
||||||
"",
|
"",
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
),
|
),
|
||||||
User(
|
User(
|
||||||
3L,
|
3L,
|
||||||
|
@ -51,7 +51,7 @@ class ActivityPubNoteServiceImplTest {
|
||||||
"https://follower2.example.com",
|
"https://follower2.example.com",
|
||||||
"",
|
"",
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val userService = mock<UserService> {
|
val userService = mock<UserService> {
|
||||||
|
@ -66,7 +66,7 @@ class ActivityPubNoteServiceImplTest {
|
||||||
"https:.//example.com",
|
"https:.//example.com",
|
||||||
"",
|
"",
|
||||||
publicKey = "",
|
publicKey = "",
|
||||||
createdAt = LocalDateTime.now()
|
createdAt = Instant.now()
|
||||||
)
|
)
|
||||||
onBlocking { findFollowersById(eq(1L)) } doReturn followers
|
onBlocking { findFollowersById(eq(1L)) } doReturn followers
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue