fix 色々直した

This commit is contained in:
usbharu 2023-10-11 14:58:46 +09:00
parent 03498d05fa
commit 30cd1274ac
22 changed files with 74 additions and 35 deletions

View File

@ -113,10 +113,6 @@ dependencies {
compileOnly("io.swagger.core.v3:swagger-annotations:2.2.6") compileOnly("io.swagger.core.v3:swagger-annotations:2.2.6")
implementation("io.swagger.core.v3:swagger-models:2.2.6") implementation("io.swagger.core.v3:swagger-models:2.2.6")
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version") implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
implementation("org.jetbrains.exposed:spring-transaction:$exposed_version")
implementation("org.springframework.data:spring-data-commons")
implementation("org.springframework.boot:spring-boot-starter-jdbc")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
testImplementation("org.springframework.boot:spring-boot-test-autoconfigure") testImplementation("org.springframework.boot:spring-boot-test-autoconfigure")
testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

View File

@ -3,9 +3,11 @@ package dev.usbharu.hideout
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching
@SpringBootApplication @SpringBootApplication
@ConfigurationPropertiesScan @ConfigurationPropertiesScan
@EnableCaching
class SpringApplication class SpringApplication
@Suppress("SpreadOperator") @Suppress("SpreadOperator")

View File

@ -20,7 +20,8 @@ class HttpClientConfig {
} }
install(Logging) { install(Logging) {
logger = Logger.DEFAULT logger = Logger.DEFAULT
level = LogLevel.ALL level = LogLevel.INFO
} }
expectSuccess = true expectSuccess = true
} }

View File

@ -2,6 +2,7 @@ package dev.usbharu.hideout.controller
import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.APService
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
@ -11,7 +12,12 @@ import org.springframework.web.bind.annotation.RestController
class InboxControllerImpl(private val apService: APService) : InboxController { class InboxControllerImpl(private val apService: APService) : InboxController {
override fun inbox(@RequestBody string: String): ResponseEntity<Unit> = runBlocking { override fun inbox(@RequestBody string: String): ResponseEntity<Unit> = runBlocking {
val parseActivity = apService.parseActivity(string) val parseActivity = apService.parseActivity(string)
LOGGER.info("INBOX Processing Activity Type: {}", parseActivity)
apService.processActivity(string, parseActivity) apService.processActivity(string, parseActivity)
ResponseEntity(HttpStatus.ACCEPTED) ResponseEntity(HttpStatus.ACCEPTED)
} }
companion object {
val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java)
}
} }

View File

@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestParam
@Controller @Controller
class MastodonAppsApiController(private val appApiService: AppApiService) : AppApi { class MastodonAppsApiController(private val appApiService: AppApiService) : AppApi {
override fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity<Application> = runBlocking { override fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity<Application> = runBlocking {
println(appsRequest)
ResponseEntity( ResponseEntity(
appApiService.createApp(appsRequest), appApiService.createApp(appsRequest),
HttpStatus.OK HttpStatus.OK

View File

@ -51,7 +51,6 @@ class UserDetailsDeserializer : JsonDeserializer<UserDetailsImpl>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): UserDetailsImpl { override fun deserialize(p: JsonParser, ctxt: DeserializationContext): UserDetailsImpl {
val mapper = p.codec as ObjectMapper val mapper = p.codec as ObjectMapper
val jsonNode: JsonNode = mapper.readTree(p) val jsonNode: JsonNode = mapper.readTree(p)
println(jsonNode)
val authorities: Set<GrantedAuthority> = mapper.convertValue( val authorities: Set<GrantedAuthority> = mapper.convertValue(
jsonNode["authorities"], jsonNode["authorities"],
SIMPLE_GRANTED_AUTHORITY_SET SIMPLE_GRANTED_AUTHORITY_SET

View File

@ -54,7 +54,6 @@ open class Object : JsonLd {
class TypeSerializer : JsonSerializer<List<String>>() { class TypeSerializer : JsonSerializer<List<String>>() {
override fun serialize(value: List<String>?, gen: JsonGenerator?, serializers: SerializerProvider?) { override fun serialize(value: List<String>?, gen: JsonGenerator?, serializers: SerializerProvider?) {
println(value)
if (value?.size == 1) { if (value?.size == 1) {
gen?.writeString(value[0]) gen?.writeString(value[0])
} else { } else {

View File

@ -34,7 +34,6 @@ class ObjectDeserializer : JsonDeserializer<Object>() {
return when (activityType) { return when (activityType) {
ExtendedActivityVocabulary.Follow -> { ExtendedActivityVocabulary.Follow -> {
val readValue = p.codec.treeToValue(treeNode, Follow::class.java) val readValue = p.codec.treeToValue(treeNode, Follow::class.java)
println(readValue)
readValue readValue
} }

View File

@ -1,7 +1,9 @@
package dev.usbharu.hideout.domain.model.hideout.entity package dev.usbharu.hideout.domain.model.hideout.entity
import org.springframework.data.annotation.Id import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
@Document
data class Timeline( data class Timeline(
@Id @Id
val id: Long, val id: Long,

View File

@ -67,11 +67,9 @@ val httpSignaturePlugin: ClientPlugin<HttpSignaturePluginConfig> = createClientP
request.header("Date", format.format(Date())) request.header("Date", format.format(Date()))
request.header("Host", request.url.host) request.header("Host", request.url.host)
println(request.bodyType)
println(request.bodyType?.type)
if (request.bodyType?.type == String::class) { if (request.bodyType?.type == String::class) {
println(body as String) body as String
println("Digest !!")
// UserAuthService.sha256.reset() // UserAuthService.sha256.reset()
val digest = val digest =
Base64.getEncoder().encodeToString(UserAuthServiceImpl.sha256.digest(body.toByteArray(Charsets.UTF_8))) Base64.getEncoder().encodeToString(UserAuthServiceImpl.sha256.digest(body.toByteArray(Charsets.UTF_8)))

View File

@ -6,25 +6,24 @@ import dev.usbharu.hideout.repository.Posts
import dev.usbharu.hideout.repository.PostsMedia import dev.usbharu.hideout.repository.PostsMedia
import dev.usbharu.hideout.repository.toPost import dev.usbharu.hideout.repository.toPost
import dev.usbharu.hideout.util.singleOr import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.innerJoin
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class PostQueryServiceImpl : PostQueryService { class PostQueryServiceImpl : PostQueryService {
override suspend fun findById(id: Long): Post = override suspend fun findById(id: Long): Post =
Posts.innerJoin(PostsMedia, onColumn = { Posts.id }, otherColumn = { PostsMedia.postId }) Posts.leftJoin(PostsMedia)
.select { Posts.id eq id } .select { Posts.id eq id }
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }.toPost() .singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }.toPost()
override suspend fun findByUrl(url: String): Post = override suspend fun findByUrl(url: String): Post =
Posts.innerJoin(PostsMedia, onColumn = { Posts.id }, otherColumn = { PostsMedia.postId }) Posts.leftJoin(PostsMedia)
.select { Posts.url eq url } .select { Posts.url eq url }
.toPost() .toPost()
.singleOr { FailedToGetResourcesException("url: $url is duplicate or does not exist.", it) } .singleOr { FailedToGetResourcesException("url: $url is duplicate or does not exist.", it) }
override suspend fun findByApId(string: String): Post = override suspend fun findByApId(string: String): Post =
Posts.innerJoin(PostsMedia, onColumn = { Posts.id }, otherColumn = { PostsMedia.postId }) Posts.leftJoin(PostsMedia)
.select { Posts.apId eq string } .select { Posts.apId eq string }
.toPost() .toPost()
.singleOr { FailedToGetResourcesException("apId: $string is duplicate or does not exist.", it) } .singleOr { FailedToGetResourcesException("apId: $string is duplicate or does not exist.", it) }

View File

@ -54,6 +54,12 @@ class PostRepositoryImpl(private val idGenerateService: IdGenerateService) : Pos
it[apId] = post.apId it[apId] = post.apId
} }
} }
assert(Posts.select { Posts.id eq post.id }.singleOrNull() != null) {
"Faild to insert"
}
return post return post
} }
@ -109,5 +115,5 @@ fun ResultRow.toPost(): Post {
fun Query.toPost(): List<Post> { fun Query.toPost(): List<Post> {
return this.groupBy { it[Posts.id] } return this.groupBy { it[Posts.id] }
.map { it.value } .map { it.value }
.map { it.first().toPost().copy(mediaIds = it.map { it[PostsMedia.mediaId] }) } .map { it.first().toPost().copy(mediaIds = it.mapNotNull { it.getOrNull(PostsMedia.mediaId) }) }
} }

View File

@ -14,8 +14,8 @@ class UserRepositoryImpl(private val idGenerateService: IdGenerateService) :
UserRepository { UserRepository {
override suspend fun save(user: User): User { override suspend fun save(user: User): User {
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull() val singleOrNull = Users.select { Users.id eq user.id or (Users.url eq user.url) }.empty()
if (singleOrNull == null) { if (singleOrNull) {
Users.insert { Users.insert {
it[id] = user.id it[id] = user.id
it[name] = user.name it[name] = user.name

View File

@ -26,7 +26,7 @@ class APLikeServiceImpl(
val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null") val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null")
val content = like.content ?: throw IllegalActivityPubObjectException("content is null") val content = like.content ?: throw IllegalActivityPubObjectException("content is null")
like.`object` ?: throw IllegalActivityPubObjectException("object is null") like.`object` ?: throw IllegalActivityPubObjectException("object is null")
transaction.transaction(java.sql.Connection.TRANSACTION_SERIALIZABLE) { transaction.transaction {
val person = apUserService.fetchPersonWithEntity(actor) val person = apUserService.fetchPersonWithEntity(actor)
apNoteService.fetchNote(like.`object` ?: return@transaction) apNoteService.fetchNote(like.`object` ?: return@transaction)

View File

@ -226,8 +226,6 @@ class APServiceImpl(
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) { override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
logger.debug("processActivity: ${hideoutJob.name}") logger.debug("processActivity: ${hideoutJob.name}")
// println(apReceiveFollowService::class.java)
// apReceiveFollowService.receiveFollowJob(job.props as JobProps<ReceiveFollowJob>)
when (hideoutJob) { when (hideoutJob) {
is ReceiveFollowJob -> { is ReceiveFollowJob -> {
apReceiveFollowService.receiveFollowJob( apReceiveFollowService.receiveFollowJob(

View File

@ -39,7 +39,6 @@ class StatsesApiServiceImpl(
statusesRequest: dev.usbharu.hideout.domain.model.mastodon.StatusesRequest, statusesRequest: dev.usbharu.hideout.domain.model.mastodon.StatusesRequest,
userId: Long userId: Long
): Status = transaction.transaction { ): Status = transaction.transaction {
println("Post status media ids " + statusesRequest.media_ids)
val visibility = when (statusesRequest.visibility) { val visibility = when (statusesRequest.visibility) {
StatusesRequest.Visibility.public -> Visibility.PUBLIC StatusesRequest.Visibility.public -> Visibility.PUBLIC
StatusesRequest.Visibility.unlisted -> Visibility.UNLISTED StatusesRequest.Visibility.unlisted -> Visibility.UNLISTED

View File

@ -6,7 +6,7 @@ import org.springframework.stereotype.Service
@Service @Service
class ExposedTransaction : Transaction { class ExposedTransaction : Transaction {
override suspend fun <T> transaction(block: suspend () -> T): T { override suspend fun <T> transaction(block: suspend () -> T): T {
return newSuspendedTransaction(transactionIsolation = java.sql.Connection.TRANSACTION_SERIALIZABLE) { return newSuspendedTransaction {
block() block()
} }
} }

View File

@ -0,0 +1,27 @@
package dev.usbharu.hideout.service.core
import jakarta.servlet.Filter
import jakarta.servlet.FilterChain
import jakarta.servlet.ServletRequest
import jakarta.servlet.ServletResponse
import org.slf4j.MDC
import org.springframework.stereotype.Service
import java.util.*
@Service
class MdcXrequestIdFilter : Filter {
override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain) {
val uuid = UUID.randomUUID()
try {
MDC.put(KEY, uuid.toString())
chain.doFilter(request, response)
} finally {
MDC.remove(KEY)
}
}
companion object {
val KEY = "x-request-id"
}
}

View File

@ -3,6 +3,7 @@ package dev.usbharu.hideout.service.post
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.query.PostQueryService
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@ -13,7 +14,8 @@ import java.util.*
class PostServiceImpl( class PostServiceImpl(
private val postRepository: PostRepository, private val postRepository: PostRepository,
private val userRepository: UserRepository, private val userRepository: UserRepository,
private val timelineService: TimelineService private val timelineService: TimelineService,
private val postQueryService: PostQueryService
) : PostService { ) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>()) private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
@ -30,8 +32,13 @@ class PostServiceImpl(
} }
private suspend fun internalCreate(post: Post, isLocal: Boolean): Post { private suspend fun internalCreate(post: Post, isLocal: Boolean): Post {
timelineService.publishTimeline(post, isLocal) val save = try {
return postRepository.save(post) postRepository.save(post)
} catch (e: Exception) {
postQueryService.findByApId(post.apId)
}
timelineService.publishTimeline(save, isLocal)
return save
} }
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post { private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {

View File

@ -64,7 +64,11 @@ class UserServiceImpl(
publicKey = user.publicKey, publicKey = user.publicKey,
createdAt = Instant.now() createdAt = Instant.now()
) )
return userRepository.save(userEntity) return try {
userRepository.save(userEntity)
} catch (e: Exception) {
userQueryService.findByUrl(user.url)
}
} }
// TODO APのフォロー処理を作る // TODO APのフォロー処理を作る

View File

@ -20,7 +20,6 @@ object HttpUtil {
subType: String, subType: String,
parameter: String parameter: String
): Boolean { ): Boolean {
println("$contentType/$subType $parameter")
if (contentType != "application") { if (contentType != "application") {
return false return false
} }

View File

@ -1,17 +1,16 @@
<configuration> <configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder> <encoder>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{x-request-id}] %logger{36} - %msg%n</pattern>
</encoder> </encoder>
</appender> </appender>
<root level="DEBUG"> <root level="INFO">
<appender-ref ref="STDOUT"/> <appender-ref ref="STDOUT"/>
</root> </root>
<logger name="org.eclipse.jetty" level="INFO"/> <logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/> <logger name="io.netty" level="INFO"/>
<logger name="kjob.core.internal.scheduler.JobServiceImpl" level="INFO"/> <logger name="kjob.core.internal.scheduler.JobServiceImpl" level="INFO"/>
<logger name="Exposed" level="INFO"/> <logger name="Exposed" level="DEBUG"/>
<logger name="io.ktor.server.plugins.contentnegotiation" level="INFO"/> <logger name="io.ktor.server.plugins.contentnegotiation" level="INFO"/>
<logger name="org.springframework.security" level="DEBUG"/> <logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/>
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="DEBUG"/>
</configuration> </configuration>