feat: userにinstanceのidを追加

This commit is contained in:
usbharu 2023-11-18 12:29:20 +09:00
parent 9cc8c77cbd
commit 2dd10890da
5 changed files with 44 additions and 20 deletions

View File

@ -21,18 +21,16 @@ data class User private constructor(
val createdAt: Instant,
val keyId: String,
val followers: String? = null,
val following: String? = null
val following: String? = null,
val instance: Long? = null
) {
override fun toString(): String =
"User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description'," +
" password=$password, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey'," +
" privateKey=$privateKey, createdAt=$createdAt, keyId='$keyId', followers=$followers," +
" following=$following)"
"User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description', password=$password, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey', privateKey=$privateKey, createdAt=$createdAt, keyId='$keyId', followers=$followers, following=$following, instance=$instance)"
@Component
class UserBuilder(private val characterLimit: CharacterLimit, private val applicationConfig: ApplicationConfig) {
private val logger = LoggerFactory.getLogger(UserBuilder::class.java)
private val logger = LoggerFactory.getLogger(UserBuilder::class.java)
@Suppress("LongParameterList", "FunctionMinLength", "LongMethod")
fun of(
id: Long,
@ -49,7 +47,8 @@ data class User private constructor(
createdAt: Instant,
keyId: String,
following: String? = null,
followers: String? = null
followers: String? = null,
instance: Long? = null
): User {
// idは0未満ではいけない
require(id >= 0) { "id must be greater than or equal to 0." }
@ -141,7 +140,8 @@ data class User private constructor(
createdAt = createdAt,
keyId = keyId,
followers = followers,
following = following
following = following,
instance = instance
)
}
}

View File

@ -25,7 +25,8 @@ class UserResultRowMapper(private val userBuilder: User.UserBuilder) : ResultRow
createdAt = Instant.ofEpochMilli((resultRow[Users.createdAt])),
keyId = resultRow[Users.keyId],
followers = resultRow[Users.followers],
following = resultRow[Users.following]
following = resultRow[Users.following],
instance = resultRow[Users.instance]
)
}
}

View File

@ -38,7 +38,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
followers[Users.createdAt],
followers[Users.keyId],
followers[Users.following],
followers[Users.followers]
followers[Users.followers],
followers[Users.instance]
)
.select { Users.id eq id }
.map {
@ -57,7 +58,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]],
following = it[followers[Users.following]]
following = it[followers[Users.following]],
instance = it[followers[Users.instance]]
)
}
}
@ -89,7 +91,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
followers[Users.createdAt],
followers[Users.keyId],
followers[Users.following],
followers[Users.followers]
followers[Users.followers],
followers[Users.instance]
)
.select { Users.name eq name and (Users.domain eq domain) }
.map {
@ -108,7 +111,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]],
following = it[followers[Users.following]]
following = it[followers[Users.following]],
instance = it[followers[Users.instance]]
)
}
}
@ -140,7 +144,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
followers[Users.createdAt],
followers[Users.keyId],
followers[Users.following],
followers[Users.followers]
followers[Users.followers],
followers[Users.instance]
)
.select { followers[Users.id] eq id }
.map {
@ -159,7 +164,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]],
following = it[followers[Users.following]]
following = it[followers[Users.following]],
instance = it[followers[Users.instance]]
)
}
}
@ -191,7 +197,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
followers[Users.createdAt],
followers[Users.keyId],
followers[Users.following],
followers[Users.followers]
followers[Users.followers],
followers[Users.instance]
)
.select { followers[Users.name] eq name and (followers[Users.domain] eq domain) }
.map {
@ -210,7 +217,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]],
following = it[followers[Users.following]]
following = it[followers[Users.following]],
instance = it[followers[Users.instance]]
)
}
}

View File

@ -35,6 +35,7 @@ class UserRepositoryImpl(
it[keyId] = user.keyId
it[following] = user.following
it[followers] = user.followers
it[instance] = user.instance
}
} else {
Users.update({ Users.id eq user.id }) {
@ -52,6 +53,7 @@ class UserRepositoryImpl(
it[keyId] = user.keyId
it[following] = user.following
it[followers] = user.followers
it[instance] = user.instance
}
}
return user
@ -98,6 +100,7 @@ object Users : Table("users") {
val keyId = varchar("key_id", length = 1000)
val following = varchar("following", length = 1000).nullable()
val followers = varchar("followers", length = 1000).nullable()
val instance = long("instance").references(Instance.id).nullable()
override val primaryKey: PrimaryKey = PrimaryKey(id)

View File

@ -10,6 +10,7 @@ import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.follow.SendFollowDto
import dev.usbharu.hideout.core.service.instance.InstanceService
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.time.Instant
@ -51,13 +52,19 @@ class UserServiceImpl(
createdAt = Instant.now(),
following = "$userUrl/following",
followers = "$userUrl/followers",
keyId = "$userUrl#pubkey"
keyId = "$userUrl#pubkey",
instance = null
)
return userRepository.save(userEntity)
}
override suspend fun createRemoteUser(user: RemoteUserCreateDto): User {
instanceService.fetchInstance(user.url, user.sharedInbox)
val instance = try {
instanceService.fetchInstance(user.url, user.sharedInbox)
} catch (e: Exception) {
logger.warn("FAILED to fetch instance. url: {}", user.url, e)
null
}
val nextId = userRepository.nextId()
val userEntity = userBuilder.of(
@ -73,7 +80,8 @@ class UserServiceImpl(
createdAt = Instant.now(),
followers = user.followers,
following = user.following,
keyId = user.keyId
keyId = user.keyId,
instance = instance?.id
)
return try {
userRepository.save(userEntity)
@ -110,4 +118,8 @@ class UserServiceImpl(
followerQueryService.removeFollower(id, followerId)
return false
}
companion object {
private val logger = LoggerFactory.getLogger(UserServiceImpl::class.java)
}
}