This commit is contained in:
usbharu 2024-05-14 10:47:51 +09:00
parent ae43b5e793
commit 8f553d3ecd
10 changed files with 81 additions and 33 deletions

View File

@ -31,18 +31,18 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
open class JsonLd {
@JsonProperty("@context")
@JsonDeserialize(contentUsing = ContextDeserializer::class)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class)
@JsonDeserialize(contentUsing = StringOrObjectDeserializer::class)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, contentUsing = StringORObjectSerializer::class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
var context: List<StringOrObject> = emptyList()
set(value) {
field = value.filter { it.isEmpty() }
field = value.filterNot { it.isEmpty() }
}
@JsonCreator
constructor(context: List<StringOrObject?>?) {
if (context != null) {
this.context = context.filterNotNull().filter { it.isEmpty() }
this.context = context.filterNotNull().filterNot { it.isEmpty() }
} else {
this.context = emptyList()
}
@ -89,17 +89,13 @@ class ContextSerializer : JsonSerializer<List<StringOrObject>>() {
return
}
if (value.size == 1) {
gen?.writeString(value[0])
serializers.findValueSerializer(StringOrObject::class.java).serialize(value[0], gen, serializers)
} else {
gen?.writeStartArray()
value.forEach {
gen?.writeString(it)
serializers.findValueSerializer(StringOrObject::class.java).serialize(it, gen, serializers)
}
gen?.writeEndArray()
}
}
override fun serialize(value: List<StringOrObject>?, gen: JsonGenerator?, serializers: SerializerProvider?) {
}
}

View File

@ -50,14 +50,19 @@ open class StringOrObject {
class StringOrObjectDeserializer : JsonDeserializer<StringOrObject>() {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): StringOrObject {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext): StringOrObject {
val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("")
return if (readTree.isValueNode) {
StringOrObject(readTree.textValue())
} else {
val map: Map<String, String> = ctxt.readTreeAsValue<Map<String, String>>(
readTree,
ctxt.typeFactory.constructType(object : TypeReference<Map<String, String>>() {})
)
println(readTree.toPrettyString())
val map = p.readValueAs<Map<String, String>>(object : TypeReference<Map<String, String>>() {})
// val map = p.codec.readValue<Map<String, String>>(p,object : TypeReference<Map<String, String>>() {})
println(map)
StringOrObject(map)
}
}
@ -72,8 +77,10 @@ class StringORObjectSerializer : JsonSerializer<StringOrObject>() {
}
if (value.contextString != null) {
gen?.writeString(value.contextString)
println("serialize string")
} else {
serializers.defaultSerializeValue(value.contextObject, gen)
println("serialize string")
}
}
}

View File

@ -17,6 +17,7 @@
package dev.usbharu.hideout.activitypub.interfaces.api.actor
import dev.usbharu.hideout.activitypub.domain.model.Person
import dev.usbharu.hideout.activitypub.domain.model.StringOrObject
import dev.usbharu.hideout.activitypub.service.objects.user.APUserService
import dev.usbharu.hideout.core.domain.exception.resource.UserNotFoundException
import org.springframework.http.HttpStatus
@ -31,7 +32,7 @@ class UserAPControllerImpl(private val apUserService: APUserService) : UserAPCon
} catch (_: UserNotFoundException) {
return ResponseEntity.notFound().build()
}
person.context += listOf("https://www.w3.org/ns/activitystreams")
person.context += listOf(StringOrObject("https://www.w3.org/ns/activitystreams"))
return ResponseEntity(person, HttpStatus.OK)
}
}

View File

@ -17,6 +17,7 @@
package dev.usbharu.hideout.activitypub.service.common
import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.activitypub.domain.model.StringOrObject
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.util.Base64Util
@ -218,8 +219,8 @@ class APRequestServiceImpl(
}
private fun <T : Object> addContextIfNotNull(body: T?) = if (body != null) {
val mutableListOf = mutableListOf<String>()
mutableListOf.add("https://www.w3.org/ns/activitystreams")
val mutableListOf = mutableListOf<StringOrObject>()
mutableListOf.add(StringOrObject("https://www.w3.org/ns/activitystreams"))
mutableListOf.addAll(body.context)
body.context = mutableListOf
objectMapper.writeValueAsString(body)

View File

@ -49,6 +49,7 @@ class ActivityPubConfig {
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_TRAILING_COMMA, true)
.addMixIn(HttpRequest::class.java, HttpRequestMixIn::class.java)
return objectMapper
}

View File

@ -69,7 +69,11 @@ class DeleteSerializeTest {
),
published = "2023-11-02T15:30:34.160Z",
)
expected.context = listOf("https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1", "")
expected.context = listOf(
StringOrObject("https://www.w3.org/ns/activitystreams"),
StringOrObject("https://w3id.org/security/v1"),
StringOrObject("")
)
assertEquals(expected, readValue)
}

View File

@ -31,7 +31,7 @@ class JsonLdSerializeTest {
val readValue = objectMapper.readValue<JsonLd>(json)
assertEquals(JsonLd(listOf("https://example.com")), readValue)
assertEquals(JsonLd(listOf(StringOrObject("https://example.com"))), readValue)
}
@Test
@ -43,7 +43,14 @@ class JsonLdSerializeTest {
val readValue = objectMapper.readValue<JsonLd>(json)
assertEquals(JsonLd(listOf("https://example.com", "https://www.w3.org/ns/activitystreams")), readValue)
assertEquals(
JsonLd(
listOf(
StringOrObject("https://example.com"),
StringOrObject("https://www.w3.org/ns/activitystreams")
)
), readValue
)
}
@Test
@ -67,7 +74,14 @@ class JsonLdSerializeTest {
val readValue = objectMapper.readValue<JsonLd>(json)
assertEquals(JsonLd(listOf("https://example.com", "https://www.w3.org/ns/activitystreams")), readValue)
assertEquals(
JsonLd(
listOf(
StringOrObject("https://example.com"),
StringOrObject("https://www.w3.org/ns/activitystreams")
)
), readValue
)
}
@Test
@ -79,7 +93,7 @@ class JsonLdSerializeTest {
val readValue = objectMapper.readValue<JsonLd>(json)
assertEquals(JsonLd(emptyList()), readValue)
assertEquals(JsonLd(listOf(StringOrObject(mapOf("hoge" to "fuga")))), readValue)
}
@Test
@ -91,7 +105,15 @@ class JsonLdSerializeTest {
val readValue = objectMapper.readValue<JsonLd>(json)
assertEquals(JsonLd(listOf("https://example.com", "https://www.w3.org/ns/activitystreams")), readValue)
assertEquals(
JsonLd(
listOf(
StringOrObject("https://example.com"),
StringOrObject(mapOf("hoge" to "fuga")),
StringOrObject("https://www.w3.org/ns/activitystreams")
)
), readValue
)
}
@Test
@ -130,7 +152,7 @@ class JsonLdSerializeTest {
@Test
fun contextが文字列のとき文字列としてシリアライズされる() {
val jsonLd = JsonLd(listOf("https://example.com"))
val jsonLd = JsonLd(listOf(StringOrObject("https://example.com")))
val objectMapper = ActivityPubConfig().objectMapper()
@ -141,7 +163,12 @@ class JsonLdSerializeTest {
@Test
fun contextが文字列の配列のとき配列としてシリアライズされる() {
val jsonLd = JsonLd(listOf("https://example.com", "https://www.w3.org/ns/activitystreams"))
val jsonLd = JsonLd(
listOf(
StringOrObject("https://example.com"),
StringOrObject("https://www.w3.org/ns/activitystreams")
)
)
val objectMapper = ActivityPubConfig().objectMapper()

View File

@ -183,8 +183,8 @@ class NoteSerializeTest {
)
expected.context = listOf(
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1"
StringOrObject("https://www.w3.org/ns/activitystreams"),
StringOrObject("https://w3id.org/security/v1")
)
val note = objectMapper.readValue<Note>(json)

View File

@ -72,7 +72,12 @@ class RejectTest {
actor = "https://test-hideout.usbharu.dev/users/test-user2",
id = "https://misskey.usbharu.dev/follows/9mxh6mawru/97ws8y3rj6"
)
).apply { context = listOf("https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1") }
).apply {
context = listOf(
StringOrObject("https://www.w3.org/ns/activitystreams"),
StringOrObject("https://w3id.org/security/v1")
)
}
assertThat(reject).isEqualTo(expected)
}
@ -88,7 +93,12 @@ class RejectTest {
apObject = "https://misskey.usbharu.dev/users/97ws8y3rj6",
actor = "https://test-hideout.usbharu.dev/users/test-user2"
)
).apply { context = listOf("https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1") }
).apply {
context = listOf(
StringOrObject("https://www.w3.org/ns/activitystreams"),
StringOrObject("https://w3id.org/security/v1")
)
}
val objectMapper = ActivityPubConfig().objectMapper()

View File

@ -18,6 +18,7 @@ package dev.usbharu.hideout.activitypub.service.common
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.activitypub.domain.model.Follow
import dev.usbharu.hideout.activitypub.domain.model.StringOrObject
import dev.usbharu.hideout.util.Base64Util
import dev.usbharu.httpsignature.common.HttpHeaders
import dev.usbharu.httpsignature.common.HttpMethod
@ -173,7 +174,7 @@ class APRequestServiceImplTest {
val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine {
val readValue = objectMapper.readValue<Follow>(it.body.toByteArray())
assertThat(readValue.context).contains("https://www.w3.org/ns/activitystreams")
assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams"))
respondOk("{}")
}), objectMapper, mock(), dateTimeFormatter)
@ -205,7 +206,7 @@ class APRequestServiceImplTest {
val src = it.body.toByteArray()
val readValue = objectMapper.readValue<Follow>(src)
assertThat(readValue.context).contains("https://www.w3.org/ns/activitystreams")
assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams"))
val map = it.headers.toMap()
assertThat(map).containsKey("Date")
@ -238,7 +239,7 @@ class APRequestServiceImplTest {
val src = it.body.toByteArray()
val readValue = objectMapper.readValue<Follow>(src)
assertThat(readValue.context).contains("https://www.w3.org/ns/activitystreams")
assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams"))
val map = it.headers.toMap()
assertThat(map).containsKey("Date")
@ -279,7 +280,7 @@ class APRequestServiceImplTest {
val src = it.body.toByteArray()
val readValue = objectMapper.readValue<Follow>(src)
assertThat(readValue.context).contains("https://www.w3.org/ns/activitystreams")
assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams"))
val map = it.headers.toMap()
assertThat(map).containsKey("Date")
@ -340,7 +341,7 @@ class APRequestServiceImplTest {
val src = it.body.toByteArray()
val readValue = objectMapper.readValue<Follow>(src)
assertThat(readValue.context).contains("https://www.w3.org/ns/activitystreams")
assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams"))
respondOk(src.decodeToString())
}), objectMapper, mock(), dateTimeFormatter)