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 createdAt: Instant,
val keyId: String, val keyId: String,
val followers: String? = null, val followers: String? = null,
val following: String? = null val following: String? = null,
val instance: Long? = null
) { ) {
override fun toString(): String = override fun toString(): String =
"User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description'," + "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)"
" password=$password, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey'," +
" privateKey=$privateKey, createdAt=$createdAt, keyId='$keyId', followers=$followers," +
" following=$following)"
@Component @Component
class UserBuilder(private val characterLimit: CharacterLimit, private val applicationConfig: ApplicationConfig) { 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") @Suppress("LongParameterList", "FunctionMinLength", "LongMethod")
fun of( fun of(
id: Long, id: Long,
@ -49,7 +47,8 @@ data class User private constructor(
createdAt: Instant, createdAt: Instant,
keyId: String, keyId: String,
following: String? = null, following: String? = null,
followers: String? = null followers: String? = null,
instance: Long? = null
): User { ): User {
// idは0未満ではいけない // idは0未満ではいけない
require(id >= 0) { "id must be greater than or equal to 0." } require(id >= 0) { "id must be greater than or equal to 0." }
@ -141,7 +140,8 @@ data class User private constructor(
createdAt = createdAt, createdAt = createdAt,
keyId = keyId, keyId = keyId,
followers = followers, 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])), createdAt = Instant.ofEpochMilli((resultRow[Users.createdAt])),
keyId = resultRow[Users.keyId], keyId = resultRow[Users.keyId],
followers = resultRow[Users.followers], 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.createdAt],
followers[Users.keyId], followers[Users.keyId],
followers[Users.following], followers[Users.following],
followers[Users.followers] followers[Users.followers],
followers[Users.instance]
) )
.select { Users.id eq id } .select { Users.id eq id }
.map { .map {
@ -57,7 +58,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]), createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]], keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]], 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.createdAt],
followers[Users.keyId], followers[Users.keyId],
followers[Users.following], followers[Users.following],
followers[Users.followers] followers[Users.followers],
followers[Users.instance]
) )
.select { Users.name eq name and (Users.domain eq domain) } .select { Users.name eq name and (Users.domain eq domain) }
.map { .map {
@ -108,7 +111,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]), createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]], keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]], 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.createdAt],
followers[Users.keyId], followers[Users.keyId],
followers[Users.following], followers[Users.following],
followers[Users.followers] followers[Users.followers],
followers[Users.instance]
) )
.select { followers[Users.id] eq id } .select { followers[Users.id] eq id }
.map { .map {
@ -159,7 +164,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]), createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]], keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]], 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.createdAt],
followers[Users.keyId], followers[Users.keyId],
followers[Users.following], followers[Users.following],
followers[Users.followers] followers[Users.followers],
followers[Users.instance]
) )
.select { followers[Users.name] eq name and (followers[Users.domain] eq domain) } .select { followers[Users.name] eq name and (followers[Users.domain] eq domain) }
.map { .map {
@ -210,7 +217,8 @@ class FollowerQueryServiceImpl(private val userBuilder: User.UserBuilder) : Foll
createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]), createdAt = Instant.ofEpochMilli(it[followers[Users.createdAt]]),
keyId = it[followers[Users.keyId]], keyId = it[followers[Users.keyId]],
followers = it[followers[Users.followers]], 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[keyId] = user.keyId
it[following] = user.following it[following] = user.following
it[followers] = user.followers it[followers] = user.followers
it[instance] = user.instance
} }
} else { } else {
Users.update({ Users.id eq user.id }) { Users.update({ Users.id eq user.id }) {
@ -52,6 +53,7 @@ class UserRepositoryImpl(
it[keyId] = user.keyId it[keyId] = user.keyId
it[following] = user.following it[following] = user.following
it[followers] = user.followers it[followers] = user.followers
it[instance] = user.instance
} }
} }
return user return user
@ -98,6 +100,7 @@ object Users : Table("users") {
val keyId = varchar("key_id", length = 1000) val keyId = varchar("key_id", length = 1000)
val following = varchar("following", length = 1000).nullable() val following = varchar("following", length = 1000).nullable()
val followers = varchar("followers", length = 1000).nullable() val followers = varchar("followers", length = 1000).nullable()
val instance = long("instance").references(Instance.id).nullable()
override val primaryKey: PrimaryKey = PrimaryKey(id) 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.follow.SendFollowDto
import dev.usbharu.hideout.core.service.instance.InstanceService import dev.usbharu.hideout.core.service.instance.InstanceService
import org.jetbrains.exposed.exceptions.ExposedSQLException import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Instant import java.time.Instant
@ -51,13 +52,19 @@ class UserServiceImpl(
createdAt = Instant.now(), createdAt = Instant.now(),
following = "$userUrl/following", following = "$userUrl/following",
followers = "$userUrl/followers", followers = "$userUrl/followers",
keyId = "$userUrl#pubkey" keyId = "$userUrl#pubkey",
instance = null
) )
return userRepository.save(userEntity) return userRepository.save(userEntity)
} }
override suspend fun createRemoteUser(user: RemoteUserCreateDto): User { 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 nextId = userRepository.nextId()
val userEntity = userBuilder.of( val userEntity = userBuilder.of(
@ -73,7 +80,8 @@ class UserServiceImpl(
createdAt = Instant.now(), createdAt = Instant.now(),
followers = user.followers, followers = user.followers,
following = user.following, following = user.following,
keyId = user.keyId keyId = user.keyId,
instance = instance?.id
) )
return try { return try {
userRepository.save(userEntity) userRepository.save(userEntity)
@ -110,4 +118,8 @@ class UserServiceImpl(
followerQueryService.removeFollower(id, followerId) followerQueryService.removeFollower(id, followerId)
return false return false
} }
companion object {
private val logger = LoggerFactory.getLogger(UserServiceImpl::class.java)
}
} }