diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/user/User.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/user/User.kt index bf021757..9666df3e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/user/User.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/user/User.kt @@ -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 ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/UserResultRowMapper.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/UserResultRowMapper.kt index c55a352a..bad247f3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/UserResultRowMapper.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/UserResultRowMapper.kt @@ -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] ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/FollowerQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/FollowerQueryServiceImpl.kt index 3034c0cb..4c62003e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/FollowerQueryServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/FollowerQueryServiceImpl.kt @@ -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]] ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt index 6d45a5e7..5cd94ccf 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt @@ -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) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt index 3d486f45..6ee4a53c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt @@ -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) + } }