feat: タイムラインが表示されない問題を修正

This commit is contained in:
usbharu 2023-10-11 16:30:32 +09:00
parent 7fac732150
commit 7cc3094084
4 changed files with 40 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.config package dev.usbharu.hideout.config
import com.fasterxml.jackson.annotation.JsonInclude
import com.nimbusds.jose.jwk.JWKSet import com.nimbusds.jose.jwk.JWKSet
import com.nimbusds.jose.jwk.RSAKey import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.jose.jwk.source.ImmutableJWKSet import com.nimbusds.jose.jwk.source.ImmutableJWKSet
@ -8,12 +9,16 @@ import com.nimbusds.jose.proc.SecurityContext
import dev.usbharu.hideout.domain.model.UserDetailsImpl import dev.usbharu.hideout.domain.model.UserDetailsImpl
import dev.usbharu.hideout.util.RsaUtil import dev.usbharu.hideout.util.RsaUtil
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
import org.springframework.boot.autoconfigure.security.servlet.PathRequest import org.springframework.boot.autoconfigure.security.servlet.PathRequest
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.core.annotation.Order import org.springframework.core.annotation.Order
import org.springframework.http.HttpMethod import org.springframework.http.HttpMethod
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.security.config.Customizer import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
@ -35,6 +40,7 @@ import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey import java.security.interfaces.RSAPublicKey
import java.util.* import java.util.*
@EnableWebSecurity(debug = false) @EnableWebSecurity(debug = false)
@Configuration @Configuration
class SecurityConfig { class SecurityConfig {
@ -155,6 +161,21 @@ class SecurityConfig {
} }
} }
} }
@Bean
@Primary
fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer {
return Jackson2ObjectMapperBuilderCustomizer {
it.serializationInclusion(JsonInclude.Include.ALWAYS).serializers()
}
}
@Bean
fun mappingJackson2HttpMessageConverter(): MappingJackson2HttpMessageConverter {
val builder = Jackson2ObjectMapperBuilder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
return MappingJackson2HttpMessageConverter(builder.build())
}
} }
@ConfigurationProperties("hideout.security.jwt") @ConfigurationProperties("hideout.security.jwt")

View File

@ -50,16 +50,16 @@ class StatusQueryServiceImpl : StatusQueryService {
@Suppress("FunctionMaxLength") @Suppress("FunctionMaxLength")
private suspend fun findByPostIdsWithMediaAttachments(ids: List<Long>): List<Status> { private suspend fun findByPostIdsWithMediaAttachments(ids: List<Long>): List<Status> {
val pairs = Posts val pairs = Posts
.innerJoin(PostsMedia, onColumn = { Posts.id }, otherColumn = { PostsMedia.postId }) .leftJoin(PostsMedia)
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) .leftJoin(Users)
.innerJoin(Media, onColumn = { PostsMedia.mediaId }, otherColumn = { id }) .leftJoin(Media)
.select { Posts.id inList ids } .select { Posts.id inList ids }
.groupBy { it[Posts.id] } .groupBy { it[Posts.id] }
.map { it.value } .map { it.value }
.map { .map {
toStatus(it.first()).copy( toStatus(it.first()).copy(
mediaAttachments = it.map { mediaAttachments = it.mapNotNull {
it.toMedia().let { it.toMediaOrNull()?.let {
MediaAttachment( MediaAttachment(
id = it.id.toString(), id = it.id.toString(),
type = when (it.type) { type = when (it.type) {
@ -132,7 +132,7 @@ private fun toStatus(it: ResultRow) = Status(
favouritesCount = 0, favouritesCount = 0,
repliesCount = 0, repliesCount = 0,
url = it[Posts.apId], url = it[Posts.apId],
inReplyToId = it[Posts.replyId].toString(), inReplyToId = it[Posts.replyId]?.toString(),
inReplyToAccountId = null, inReplyToAccountId = null,
language = null, language = null,
text = it[Posts.text], text = it[Posts.text],

View File

@ -69,6 +69,18 @@ fun ResultRow.toMedia(): EntityMedia {
) )
} }
fun ResultRow.toMediaOrNull(): EntityMedia? {
return EntityMedia(
id = this.getOrNull(Media.id) ?: return null,
name = this.getOrNull(Media.name) ?: return null,
url = this.getOrNull(Media.url) ?: return null,
remoteUrl = this[Media.remoteUrl],
thumbnailUrl = this[Media.thumbnailUrl],
type = FileType.values().first { it.ordinal == this.getOrNull(Media.type) },
blurHash = this[Media.blurhash],
)
}
object Media : Table("media") { object Media : Table("media") {
val id = long("id") val id = long("id")
val name = varchar("name", 255) val name = varchar("name", 255)

View File

@ -16,6 +16,7 @@ spring:
jackson: jackson:
serialization: serialization:
WRITE_DATES_AS_TIMESTAMPS: false WRITE_DATES_AS_TIMESTAMPS: false
default-property-inclusion: always
datasource: datasource:
driver-class-name: org.h2.Driver driver-class-name: org.h2.Driver
url: "jdbc:h2:./test-dev2;MODE=POSTGRESQL" url: "jdbc:h2:./test-dev2;MODE=POSTGRESQL"