From ae43b5e793ebff5e41194d871b16f3af26b1b82f Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 09:32:35 +0900 Subject: [PATCH 1/9] wip --- .../activitypub/domain/model/JsonLd.kt | 20 +++-- .../domain/model/StringOrObject.kt | 79 +++++++++++++++++++ 2 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt index 0e1a3d79..ca6a1371 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt @@ -34,15 +34,15 @@ open class JsonLd { @JsonDeserialize(contentUsing = ContextDeserializer::class) @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class) @JsonInclude(JsonInclude.Include.NON_EMPTY) - var context: List = emptyList() + var context: List = emptyList() set(value) { - field = value.filterNotNull().filter { it.isNotBlank() } + field = value.filter { it.isEmpty() } } @JsonCreator - constructor(context: List?) { + constructor(context: List?) { if (context != null) { - this.context = context.filterNotNull().filter { it.isNotBlank() } + this.context = context.filterNotNull().filter { it.isEmpty() } } else { this.context = emptyList() } @@ -76,14 +76,14 @@ class ContextDeserializer : JsonDeserializer() { } } -class ContextSerializer : JsonSerializer>() { +class ContextSerializer : JsonSerializer>() { @Deprecated("Deprecated in Java") - override fun isEmpty(value: List?): Boolean = value.isNullOrEmpty() + override fun isEmpty(value: List?): Boolean = value.isNullOrEmpty() - override fun isEmpty(provider: SerializerProvider?, value: List?): Boolean = value.isNullOrEmpty() + override fun isEmpty(provider: SerializerProvider?, value: List?): Boolean = value.isNullOrEmpty() - override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider) { + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider) { if (value.isNullOrEmpty()) { serializers.defaultSerializeNull(gen) return @@ -98,4 +98,8 @@ class ContextSerializer : JsonSerializer>() { gen?.writeEndArray() } } + + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider?) { + + } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt new file mode 100644 index 00000000..b0773b5a --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt @@ -0,0 +1,79 @@ +package dev.usbharu.hideout.activitypub.domain.model + +import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.* + +open class StringOrObject { + var contextString: String? = null + var contextObject: Map? = null + + @JsonCreator + protected constructor() + + constructor(string: String) : this() { + contextString = string + } + + constructor(contextObject: Map) : this() { + this.contextObject = contextObject + } + + fun isEmpty(): Boolean = contextString.isNullOrEmpty() and contextObject.isNullOrEmpty() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as StringOrObject + + if (contextString != other.contextString) return false + if (contextObject != other.contextObject) return false + + return true + } + + override fun hashCode(): Int { + var result = contextString?.hashCode() ?: 0 + result = 31 * result + (contextObject?.hashCode() ?: 0) + return result + } + + override fun toString(): String { + return "StringOrObject(contextString=$contextString, contextObject=$contextObject)" + } + + +} + + +class StringOrObjectDeserializer : JsonDeserializer() { + 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 = p.readValueAs>(object : TypeReference>() {}) + + StringOrObject(map) + } + } + +} + +class StringORObjectSerializer : JsonSerializer() { + override fun serialize(value: StringOrObject?, gen: JsonGenerator?, serializers: SerializerProvider) { + if (value == null) { + serializers.defaultSerializeNull(gen) + return + } + if (value.contextString != null) { + gen?.writeString(value.contextString) + } else { + serializers.defaultSerializeValue(value.contextObject, gen) + } + } +} From 8f553d3ecd27d87c6ecfd0e0c184a53f63476739 Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 10:47:51 +0900 Subject: [PATCH 2/9] wip --- .../activitypub/domain/model/JsonLd.kt | 16 +++----- .../domain/model/StringOrObject.kt | 13 ++++-- .../api/actor/UserAPControllerImpl.kt | 3 +- .../service/common/APRequestServiceImpl.kt | 5 ++- .../application/config/ActivityPubConfig.kt | 1 + .../domain/model/DeleteSerializeTest.kt | 6 ++- .../domain/model/JsonLdSerializeTest.kt | 41 +++++++++++++++---- .../domain/model/NoteSerializeTest.kt | 4 +- .../activitypub/domain/model/RejectTest.kt | 14 ++++++- .../common/APRequestServiceImplTest.kt | 11 ++--- 10 files changed, 81 insertions(+), 33 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt index ca6a1371..8f963f6e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt @@ -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 = emptyList() set(value) { - field = value.filter { it.isEmpty() } + field = value.filterNot { it.isEmpty() } } @JsonCreator constructor(context: List?) { 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>() { 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?, gen: JsonGenerator?, serializers: SerializerProvider?) { - - } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt index b0773b5a..ac146b09 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt @@ -50,14 +50,19 @@ open class StringOrObject { class StringOrObjectDeserializer : JsonDeserializer() { - 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 = ctxt.readTreeAsValue>( + readTree, + ctxt.typeFactory.constructType(object : TypeReference>() {}) + ) + println(readTree.toPrettyString()) - val map = p.readValueAs>(object : TypeReference>() {}) - +// val map = p.codec.readValue>(p,object : TypeReference>() {}) + println(map) StringOrObject(map) } } @@ -72,8 +77,10 @@ class StringORObjectSerializer : JsonSerializer() { } if (value.contextString != null) { gen?.writeString(value.contextString) + println("serialize string") } else { serializers.defaultSerializeValue(value.contextObject, gen) + println("serialize string") } } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt index c1e8a9b9..18378e42 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt @@ -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) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt index 9f19ebde..ab464293 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt @@ -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 addContextIfNotNull(body: T?) = if (body != null) { - val mutableListOf = mutableListOf() - mutableListOf.add("https://www.w3.org/ns/activitystreams") + val mutableListOf = mutableListOf() + mutableListOf.add(StringOrObject("https://www.w3.org/ns/activitystreams")) mutableListOf.addAll(body.context) body.context = mutableListOf objectMapper.writeValueAsString(body) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt index f46a67ce..f68c32cf 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt @@ -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 } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt index d301f079..78aa4e29 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt @@ -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) } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt index 445b68ba..3056cc17 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt @@ -31,7 +31,7 @@ class JsonLdSerializeTest { val readValue = objectMapper.readValue(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(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(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(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(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() diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt index 7546c606..74059932 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt @@ -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(json) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt index 94c006ba..b059bd77 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt @@ -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() diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt index 84bf3a7e..8b02f9b4 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt @@ -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(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(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(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(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(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) From fb029098b77f8f1ad7236bb79774dbbe1bdd6197 Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 10:54:18 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat:=20@context=E3=81=AB=E3=82=AA=E3=83=96?= =?UTF-8?q?=E3=82=B8=E3=82=A7=E3=82=AF=E3=83=88=E3=81=8C=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=84=E3=81=A6=E3=82=82=E3=82=B7=E3=83=AA?= =?UTF-8?q?=E3=82=A2=E3=83=A9=E3=82=A4=E3=82=BA=E3=83=BB=E3=83=87=E3=82=B7?= =?UTF-8?q?=E3=83=AA=E3=82=A2=E3=83=A9=E3=82=A4=E3=82=BA=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt | 2 +- .../hideout/activitypub/domain/model/StringOrObject.kt | 4 +++- .../hideout/application/config/ActivityPubConfig.kt | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt index 8f963f6e..88a8e48d 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt @@ -32,7 +32,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize open class JsonLd { @JsonProperty("@context") @JsonDeserialize(contentUsing = StringOrObjectDeserializer::class) - @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, contentUsing = StringORObjectSerializer::class) + @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class) @JsonInclude(JsonInclude.Include.NON_EMPTY) var context: List = emptyList() set(value) { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt index ac146b09..2011fae8 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt @@ -54,7 +54,7 @@ class StringOrObjectDeserializer : JsonDeserializer() { val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("") return if (readTree.isValueNode) { StringOrObject(readTree.textValue()) - } else { + } else if (readTree.isObject) { val map: Map = ctxt.readTreeAsValue>( readTree, ctxt.typeFactory.constructType(object : TypeReference>() {}) @@ -64,6 +64,8 @@ class StringOrObjectDeserializer : JsonDeserializer() { // val map = p.codec.readValue>(p,object : TypeReference>() {}) println(map) StringOrObject(map) + } else { + StringOrObject("") } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt index f68c32cf..e77dd1d7 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/ActivityPubConfig.kt @@ -22,7 +22,10 @@ import com.fasterxml.jackson.annotation.Nulls import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import dev.usbharu.hideout.activitypub.domain.model.StringORObjectSerializer +import dev.usbharu.hideout.activitypub.domain.model.StringOrObject import dev.usbharu.hideout.core.infrastructure.httpsignature.HttpRequestMixIn import dev.usbharu.httpsignature.common.HttpRequest import dev.usbharu.httpsignature.sign.HttpSignatureSigner @@ -39,7 +42,11 @@ class ActivityPubConfig { @Bean @Qualifier("activitypub") fun objectMapper(): ObjectMapper { + + val module = SimpleModule().addSerializer(StringOrObject::class.java, StringORObjectSerializer()) + val objectMapper = jacksonObjectMapper() + .registerModules(module) .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) From 4ee71416d93a58fe2d0ff14136277b89b0619623 Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 10:58:15 +0900 Subject: [PATCH 4/9] =?UTF-8?q?style:=20=E4=B8=8D=E8=A6=81=E3=81=AAprintln?= =?UTF-8?q?=E3=82=92=E6=B6=88=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/activitypub/domain/model/StringOrObject.kt | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt index 2011fae8..b419bf79 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt @@ -55,14 +55,10 @@ class StringOrObjectDeserializer : JsonDeserializer() { return if (readTree.isValueNode) { StringOrObject(readTree.textValue()) } else if (readTree.isObject) { - val map: Map = ctxt.readTreeAsValue>( + val map: Map = ctxt.readTreeAsValue( readTree, ctxt.typeFactory.constructType(object : TypeReference>() {}) ) - println(readTree.toPrettyString()) - -// val map = p.codec.readValue>(p,object : TypeReference>() {}) - println(map) StringOrObject(map) } else { StringOrObject("") @@ -79,10 +75,8 @@ class StringORObjectSerializer : JsonSerializer() { } if (value.contextString != null) { gen?.writeString(value.contextString) - println("serialize string") } else { serializers.defaultSerializeValue(value.contextObject, gen) - println("serialize string") } } } From 822c57c02bdff6ba02b98295f5fb76ba8083cd8d Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 13:27:44 +0900 Subject: [PATCH 5/9] =?UTF-8?q?test:=20JsonLD=E3=81=AE=E3=82=B7=E3=83=AA?= =?UTF-8?q?=E3=82=A2=E3=83=A9=E3=82=A4=E3=82=BA=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/JsonLdSerializeTest.kt | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt index 3056cc17..9c9530cc 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLdSerializeTest.kt @@ -176,4 +176,74 @@ class JsonLdSerializeTest { assertEquals("""{"@context":["https://example.com","https://www.w3.org/ns/activitystreams"]}""", actual) } + + @Test + fun contextがオブジェクトのときシリアライズできる() { + val jsonLd = JsonLd( + listOf( + StringOrObject(mapOf("hoge" to "fuga")) + ) + ) + + val objectMapper = ActivityPubConfig().objectMapper() + + val actual = objectMapper.writeValueAsString(jsonLd) + + assertEquals("""{"@context":{"hoge":"fuga"}}""", actual) + + } + + @Test + fun contextが複数のオブジェクトのときシリアライズできる() { + val jsonLd = JsonLd( + listOf( + StringOrObject(mapOf("hoge" to "fuga")), + StringOrObject(mapOf("foo" to "bar")) + ) + ) + + val objectMapper = ActivityPubConfig().objectMapper() + + val actual = objectMapper.writeValueAsString(jsonLd) + + assertEquals("""{"@context":[{"hoge":"fuga"},{"foo":"bar"}]}""", actual) + } + + @Test + fun contextが複数のオブジェクトのときデシリアライズできる() { + //language=JSON + val json = """{"@context":["https://example.com",{"hoge": "fuga"},{"foo": "bar"}]}""" + + val objectMapper = ActivityPubConfig().objectMapper() + + val readValue = objectMapper.readValue(json) + + assertEquals( + JsonLd( + listOf( + StringOrObject("https://example.com"), + StringOrObject(mapOf("hoge" to "fuga")), + StringOrObject(mapOf("foo" to "bar")) + ) + ), readValue + ) + } + + @Test + fun contextがオブジェクトのときデシリアライズできる() { + //language=JSON + val json = """{"@context":{"hoge": "fuga"}}""" + + val objectMapper = ActivityPubConfig().objectMapper() + + val readValue = objectMapper.readValue(json) + + assertEquals( + JsonLd( + listOf( + StringOrObject(mapOf("hoge" to "fuga")) + ) + ), readValue + ) + } } From 19458e9adc5a91c7c0a876141f928911cb3cd1c6 Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 15:11:02 +0900 Subject: [PATCH 6/9] wip --- .../interfaces/api/actor/UserAPControllerImpl.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt index 18378e42..232f2a05 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt @@ -32,7 +32,11 @@ class UserAPControllerImpl(private val apUserService: APUserService) : UserAPCon } catch (_: UserNotFoundException) { return ResponseEntity.notFound().build() } - person.context += listOf(StringOrObject("https://www.w3.org/ns/activitystreams")) + person.context += listOf( + StringOrObject("https://www.w3.org/ns/activitystreams"), + StringOrObject("https://w3id.org/security/v1"), + StringOrObject(mapOf("manuallyApprovesFollowers" to "as:manuallyApprovesFollowers")) + ) return ResponseEntity(person, HttpStatus.OK) } } From f25e9df896d2f3c83e37186515f3fc326eb5668e Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 16:06:59 +0900 Subject: [PATCH 7/9] wip --- .../interfaces/api/actor/UserAPControllerImpl.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt index 232f2a05..c6615720 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt @@ -35,7 +35,13 @@ class UserAPControllerImpl(private val apUserService: APUserService) : UserAPCon person.context += listOf( StringOrObject("https://www.w3.org/ns/activitystreams"), StringOrObject("https://w3id.org/security/v1"), - StringOrObject(mapOf("manuallyApprovesFollowers" to "as:manuallyApprovesFollowers")) + StringOrObject( + mapOf( + "manuallyApprovesFollowers" to "as:manuallyApprovesFollowers", + "sensitive" to "as:sensitive", + "Hashtag" to "as:Hashtag" + ) + ) ) return ResponseEntity(person, HttpStatus.OK) } From 301c07c38ecef5db5b9d1753acb481f2e116c19f Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 14 May 2024 22:52:23 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat:=20JSON-LD=E3=81=A8=E3=81=97=E3=81=A6?= =?UTF-8?q?=E6=AD=A3=E3=81=97=E3=81=84JSON=E3=82=92=E8=BF=94=E3=81=99?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/activitypub/domain/Constant.kt | 41 +++++++++++++++++++ .../api/actor/UserAPControllerImpl.kt | 14 +------ .../service/common/APRequestServiceImpl.kt | 17 ++++---- .../application/config/SecurityConfig.kt | 8 +++- 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/Constant.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/Constant.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/Constant.kt new file mode 100644 index 00000000..451e24d9 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/Constant.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.hideout.activitypub.domain + +import dev.usbharu.hideout.activitypub.domain.model.StringOrObject + +object Constant { + val context = listOf( + StringOrObject("https://www.w3.org/ns/activitystreams"), + StringOrObject("https://w3id.org/security/v1"), + StringOrObject( + mapOf( + "manuallyApprovesFollowers" to "as:manuallyApprovesFollowers", + "sensitive" to "as:sensitive", + "Hashtag" to "as:Hashtag", + "quoteUrl" to "as:quoteUrl", + "toot" to "http://joinmastodon.org/ns#", + "Emoji" to "toot:Emoji", + "featured" to "toot:featured", + "discoverable" to "toot:discoverable", + "schema" to "http://schema.org#", + "PropertyValue" to "schema:PropertyValue", + "value" to "schema:value", + ) + ) + ) +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt index c6615720..73b9b8a5 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImpl.kt @@ -16,8 +16,8 @@ package dev.usbharu.hideout.activitypub.interfaces.api.actor +import dev.usbharu.hideout.activitypub.domain.Constant 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 @@ -32,17 +32,7 @@ class UserAPControllerImpl(private val apUserService: APUserService) : UserAPCon } catch (_: UserNotFoundException) { return ResponseEntity.notFound().build() } - person.context += listOf( - StringOrObject("https://www.w3.org/ns/activitystreams"), - StringOrObject("https://w3id.org/security/v1"), - StringOrObject( - mapOf( - "manuallyApprovesFollowers" to "as:manuallyApprovesFollowers", - "sensitive" to "as:sensitive", - "Hashtag" to "as:Hashtag" - ) - ) - ) + person.context += Constant.context return ResponseEntity(person, HttpStatus.OK) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt index ab464293..4b463532 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt @@ -17,6 +17,7 @@ package dev.usbharu.hideout.activitypub.service.common import com.fasterxml.jackson.databind.ObjectMapper +import dev.usbharu.hideout.activitypub.domain.Constant 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 @@ -75,7 +76,7 @@ class APRequestServiceImpl( date: String, u: URL, signer: Actor, - url: String + url: String, ): HttpResponse { val headers = headers { append("Accept", Activity) @@ -118,7 +119,7 @@ class APRequestServiceImpl( url: String, body: T?, signer: Actor?, - responseClass: Class + responseClass: Class, ): R { val bodyAsText = apPost(url, body, signer) return objectMapper.readValue(bodyAsText, responseClass) @@ -168,7 +169,7 @@ class APRequestServiceImpl( url: String, date: String?, digest: String, - requestBody: String? + requestBody: String?, ) = httpClient.post(url) { accept(Activity) header("Date", date) @@ -184,7 +185,7 @@ class APRequestServiceImpl( u: URL, digest: String, signer: Actor, - requestBody: String? + requestBody: String?, ): HttpResponse { val headers = headers { append("Accept", Activity) @@ -219,10 +220,10 @@ class APRequestServiceImpl( } private fun addContextIfNotNull(body: T?) = if (body != null) { - val mutableListOf = mutableListOf() - mutableListOf.add(StringOrObject("https://www.w3.org/ns/activitystreams")) - mutableListOf.addAll(body.context) - body.context = mutableListOf + val context = mutableListOf() + context.addAll(Constant.context) + context.addAll(body.context) + body.context = context objectMapper.writeValueAsString(body) } else { null diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt index b4f6fb05..889133e7 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -17,11 +17,14 @@ package dev.usbharu.hideout.application.config import com.fasterxml.jackson.annotation.JsonInclude +import com.fasterxml.jackson.databind.module.SimpleModule import com.nimbusds.jose.jwk.JWKSet import com.nimbusds.jose.jwk.RSAKey import com.nimbusds.jose.jwk.source.ImmutableJWKSet import com.nimbusds.jose.jwk.source.JWKSource import com.nimbusds.jose.proc.SecurityContext +import dev.usbharu.hideout.activitypub.domain.model.StringORObjectSerializer +import dev.usbharu.hideout.activitypub.domain.model.StringOrObject import dev.usbharu.hideout.application.external.Transaction import dev.usbharu.hideout.application.infrastructure.springframework.RoleHierarchyAuthorizationManagerFactory import dev.usbharu.hideout.core.domain.model.actor.ActorRepository @@ -295,13 +298,16 @@ class SecurityConfig { @Primary fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer { return Jackson2ObjectMapperBuilderCustomizer { - it.serializationInclusion(JsonInclude.Include.ALWAYS).serializers() + it.serializationInclusion(JsonInclude.Include.ALWAYS) + .modulesToInstall(SimpleModule().addSerializer(StringOrObject::class.java, StringORObjectSerializer())) + .serializers() } } @Bean fun mappingJackson2HttpMessageConverter(): MappingJackson2HttpMessageConverter { val builder = Jackson2ObjectMapperBuilder().serializationInclusion(JsonInclude.Include.NON_NULL) + builder.modulesToInstall(SimpleModule().addSerializer(StringOrObject::class.java, StringORObjectSerializer())) return MappingJackson2HttpMessageConverter(builder.build()) } From 4151cda7b6277e9dbd160c21b37d563f9cc59a91 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 16 May 2024 15:23:29 +0900 Subject: [PATCH 9/9] =?UTF-8?q?test:=20=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/DeleteSerializeTest.kt | 16 ++------- .../domain/model/NoteSerializeTest.kt | 16 ++------- .../activitypub/domain/model/RejectTest.kt | 16 ++------- .../api/actor/ActorAPControllerImplTest.kt | 7 +++- .../common/APRequestServiceImplTest.kt | 33 ++++++++++--------- 5 files changed, 32 insertions(+), 56 deletions(-) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt index 78aa4e29..6f964232 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/DeleteSerializeTest.kt @@ -17,6 +17,7 @@ package dev.usbharu.hideout.activitypub.domain.model import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.Constant import dev.usbharu.hideout.application.config.ActivityPubConfig import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Assertions.assertEquals @@ -37,14 +38,7 @@ class DeleteSerializeTest { "discoverable" : "toot:discoverable", "schema" : "http://schema.org#", "PropertyValue" : "schema:PropertyValue", - "value" : "schema:value", - "misskey" : "https://misskey-hub.net/ns#", - "_misskey_content" : "misskey:_misskey_content", - "_misskey_quote" : "misskey:_misskey_quote", - "_misskey_reaction" : "misskey:_misskey_reaction", - "_misskey_votes" : "misskey:_misskey_votes", - "isCat" : "misskey:isCat", - "vcard" : "http://www.w3.org/2006/vcard/ns#" + "value" : "schema:value" } ], "type" : "Delete", "actor" : "https://misskey.usbharu.dev/users/97ws8y3rj6", @@ -69,11 +63,7 @@ class DeleteSerializeTest { ), published = "2023-11-02T15:30:34.160Z", ) - expected.context = listOf( - StringOrObject("https://www.w3.org/ns/activitystreams"), - StringOrObject("https://w3id.org/security/v1"), - StringOrObject("") - ) + expected.context = Constant.context assertEquals(expected, readValue) } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt index 74059932..8c4b3f9d 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/NoteSerializeTest.kt @@ -17,6 +17,7 @@ package dev.usbharu.hideout.activitypub.domain.model import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.Constant import dev.usbharu.hideout.activitypub.service.objects.note.APNoteServiceImpl.Companion.public import dev.usbharu.hideout.application.config.ActivityPubConfig import org.assertj.core.api.Assertions.assertThat @@ -108,15 +109,7 @@ class NoteSerializeTest { "discoverable": "toot:discoverable", "schema": "http://schema.org#", "PropertyValue": "schema:PropertyValue", - "value": "schema:value", - "misskey": "https://misskey-hub.net/ns#", - "_misskey_content": "misskey:_misskey_content", - "_misskey_quote": "misskey:_misskey_quote", - "_misskey_reaction": "misskey:_misskey_reaction", - "_misskey_votes": "misskey:_misskey_votes", - "_misskey_summary": "misskey:_misskey_summary", - "isCat": "misskey:isCat", - "vcard": "http://www.w3.org/2006/vcard/ns#" + "value": "schema:value" } ], "id": "https://misskey.usbharu.dev/notes/9nj1omt1rn", @@ -182,10 +175,7 @@ class NoteSerializeTest { ) ) - expected.context = listOf( - StringOrObject("https://www.w3.org/ns/activitystreams"), - StringOrObject("https://w3id.org/security/v1") - ) + expected.context = Constant.context val note = objectMapper.readValue(json) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt index b059bd77..65e26aac 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/RejectTest.kt @@ -17,6 +17,7 @@ package dev.usbharu.hideout.activitypub.domain.model import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.Constant import dev.usbharu.hideout.application.config.ActivityPubConfig import org.assertj.core.api.Assertions.assertThat import org.intellij.lang.annotations.Language @@ -38,15 +39,7 @@ class RejectTest { "discoverable" : "toot:discoverable", "schema" : "http://schema.org#", "PropertyValue" : "schema:PropertyValue", - "value" : "schema:value", - "misskey" : "https://misskey-hub.net/ns#", - "_misskey_content" : "misskey:_misskey_content", - "_misskey_quote" : "misskey:_misskey_quote", - "_misskey_reaction" : "misskey:_misskey_reaction", - "_misskey_votes" : "misskey:_misskey_votes", - "_misskey_summary" : "misskey:_misskey_summary", - "isCat" : "misskey:isCat", - "vcard" : "http://www.w3.org/2006/vcard/ns#" + "value" : "schema:value" } ], "type" : "Reject", "actor" : "https://misskey.usbharu.dev/users/97ws8y3rj6", @@ -73,10 +66,7 @@ class RejectTest { id = "https://misskey.usbharu.dev/follows/9mxh6mawru/97ws8y3rj6" ) ).apply { - context = listOf( - StringOrObject("https://www.w3.org/ns/activitystreams"), - StringOrObject("https://w3id.org/security/v1") - ) + context = Constant.context } assertThat(reject).isEqualTo(expected) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/ActorAPControllerImplTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/ActorAPControllerImplTest.kt index 656cdf6d..76239bdd 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/ActorAPControllerImplTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/ActorAPControllerImplTest.kt @@ -33,6 +33,7 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.doThrow import org.mockito.kotlin.eq import org.mockito.kotlin.whenever +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.get import org.springframework.test.web.servlet.post @@ -51,7 +52,10 @@ class ActorAPControllerImplTest { @BeforeEach fun setUp() { - mockMvc = MockMvcBuilders.standaloneSetup(userAPControllerImpl).build() + mockMvc = MockMvcBuilders + .standaloneSetup(userAPControllerImpl) + .setMessageConverters(MappingJackson2HttpMessageConverter(ActivityPubConfig().objectMapper())) + .build() } @Test @@ -85,6 +89,7 @@ class ActorAPControllerImplTest { mockMvc .get("/users/hoge") .asyncDispatch() + .andDo { print() } .andExpect { status { isOk() } } .andExpect { content { this.json(objectMapper.writeValueAsString(person)) } } } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt index 8b02f9b4..e8243f53 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImplTest.kt @@ -17,8 +17,10 @@ package dev.usbharu.hideout.activitypub.service.common import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.Constant import dev.usbharu.hideout.activitypub.domain.model.Follow import dev.usbharu.hideout.activitypub.domain.model.StringOrObject +import dev.usbharu.hideout.application.config.ActivityPubConfig import dev.usbharu.hideout.util.Base64Util import dev.usbharu.httpsignature.common.HttpHeaders import dev.usbharu.httpsignature.common.HttpMethod @@ -36,7 +38,6 @@ import org.mockito.kotlin.any import org.mockito.kotlin.doReturn import org.mockito.kotlin.eq import org.mockito.kotlin.mock -import utils.JsonObjectMapper.objectMapper import utils.UserBuilder import java.net.URL import java.security.MessageDigest @@ -58,7 +59,7 @@ class APRequestServiceImplTest { } respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""") }), - objectMapper, + ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter ) @@ -83,7 +84,7 @@ class APRequestServiceImplTest { } respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""") }), - objectMapper, + ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter ) @@ -123,7 +124,7 @@ class APRequestServiceImplTest { } respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""") }), - objectMapper, + ActivityPubConfig().objectMapper(), httpSignatureSigner, dateTimeFormatter ) @@ -172,12 +173,12 @@ class APRequestServiceImplTest { fun `apPost bodyがnullでないときcontextにactivitystreamのURLを追加する`() = runTest { val dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine { - val readValue = objectMapper.readValue(it.body.toByteArray()) + val readValue = ActivityPubConfig().objectMapper().readValue(it.body.toByteArray()) - assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams")) + assertThat(readValue.context).containsAll(Constant.context) respondOk("{}") - }), objectMapper, mock(), dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter) val body = Follow( apObject = "https://example.com", @@ -194,7 +195,7 @@ class APRequestServiceImplTest { assertEquals(0, it.body.toByteArray().size) respondOk("{}") - }), objectMapper, mock(), dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter) apRequestServiceImpl.apPost("https://example.com", null, null) } @@ -204,7 +205,7 @@ class APRequestServiceImplTest { val dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine { val src = it.body.toByteArray() - val readValue = objectMapper.readValue(src) + val readValue = ActivityPubConfig().objectMapper().readValue(src) assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams")) @@ -223,7 +224,7 @@ class APRequestServiceImplTest { assertEquals(digest, it.headers["Digest"].orEmpty().split("256=").last()) respondOk("{}") - }), objectMapper, mock(), dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter) val body = Follow( apObject = "https://example.com", @@ -237,7 +238,7 @@ class APRequestServiceImplTest { val dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine { val src = it.body.toByteArray() - val readValue = objectMapper.readValue(src) + val readValue = ActivityPubConfig().objectMapper().readValue(src) assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams")) @@ -253,7 +254,7 @@ class APRequestServiceImplTest { assertEquals(digest, it.headers["Digest"].orEmpty().split("256=").last()) respondOk("{}") - }), objectMapper, mock(), dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter) val body = Follow( apObject = "https://example.com", @@ -278,7 +279,7 @@ class APRequestServiceImplTest { } val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine { val src = it.body.toByteArray() - val readValue = objectMapper.readValue(src) + val readValue = ActivityPubConfig().objectMapper().readValue(src) assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams")) @@ -294,7 +295,7 @@ class APRequestServiceImplTest { assertEquals(digest, it.headers["Digest"].orEmpty().split("256=").last()) respondOk("{}") - }), objectMapper, httpSignatureSigner, dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), httpSignatureSigner, dateTimeFormatter) val body = Follow( apObject = "https://example.com", @@ -339,12 +340,12 @@ class APRequestServiceImplTest { val dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) val apRequestServiceImpl = APRequestServiceImpl(HttpClient(MockEngine { val src = it.body.toByteArray() - val readValue = objectMapper.readValue(src) + val readValue = ActivityPubConfig().objectMapper().readValue(src) assertThat(readValue.context).contains(StringOrObject("https://www.w3.org/ns/activitystreams")) respondOk(src.decodeToString()) - }), objectMapper, mock(), dateTimeFormatter) + }), ActivityPubConfig().objectMapper(), mock(), dateTimeFormatter) val body = Follow( apObject = "https://example.com",