refactor: POJOの変更を反映

This commit is contained in:
usbharu 2023-11-28 11:58:30 +09:00
parent 7a34b11147
commit 2e1cee4e1a
24 changed files with 56 additions and 123 deletions

View File

@ -4,18 +4,16 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.activitypub.domain.model.objects.ObjectDeserializer
open class Delete : Object, HasId, HasActor, HasName {
open class Delete : Object, HasId, HasActor {
@JsonDeserialize(using = ObjectDeserializer::class)
@Suppress("VariableNaming")
var `object`: Object? = null
var published: String? = null
override val actor: String
override val id: String
override val name: String
constructor(
type: List<String> = emptyList(),
name: String = "Delete",
actor: String,
id: String,
`object`: Object,
@ -23,7 +21,6 @@ open class Delete : Object, HasId, HasActor, HasName {
) : super(add(type, "Delete")) {
this.`object` = `object`
this.published = published
this.name = name
this.actor = actor
this.id = id
}
@ -39,7 +36,6 @@ open class Delete : Object, HasId, HasActor, HasName {
if (published != other.published) return false
if (actor != other.actor) return false
if (id != other.id) return false
if (name != other.name) return false
return true
}
@ -50,10 +46,9 @@ open class Delete : Object, HasId, HasActor, HasName {
result = 31 * result + (published?.hashCode() ?: 0)
result = 31 * result + actor.hashCode()
result = 31 * result + id.hashCode()
result = 31 * result + name.hashCode()
return result
}
override fun toString(): String =
"Delete(`object`=$`object`, published=$published, actor='$actor', id='$id', name='$name') ${super.toString()}"
"Delete(`object`=$`object`, published=$published, actor='$actor', id='$id') ${super.toString()}"
}

View File

@ -2,24 +2,18 @@ package dev.usbharu.hideout.activitypub.domain.model
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
open class Document : Object, HasName {
open class Document(
type: List<String> = emptyList(),
override val name: String = "",
mediaType: String,
url: String
) : Object(
type = add(type, "Document")
),
HasName {
var mediaType: String? = null
var url: String? = null
override val name: String
constructor(
type: List<String> = emptyList(),
name: String,
mediaType: String,
url: String
) : super(
type = add(type, "Document")
) {
this.mediaType = mediaType
this.url = url
this.name = name
}
var mediaType: String? = mediaType
var url: String? = url
override fun equals(other: Any?): Boolean {
if (this === other) return true

View File

@ -2,24 +2,17 @@ package dev.usbharu.hideout.activitypub.domain.model
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
open class Emoji : Object {
var updated: String? = null
var icon: Image? = null
protected constructor() : super()
constructor(
type: List<String>,
name: String?,
actor: String?,
id: String?,
updated: String?,
icon: Image?
) : super(
type = add(type, "Emoji")
) {
this.updated = updated
this.icon = icon
}
open class Emoji(
type: List<String>,
override val name: String,
override val id: String,
var updated: String?,
var icon: Image?
) : Object(
type = add(type, "Emoji")
),
HasName,
HasId {
override fun equals(other: Any?): Boolean {
if (this === other) return true

View File

@ -3,7 +3,6 @@ package dev.usbharu.hideout.activitypub.domain.model
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.activitypub.domain.model.objects.ObjectDeserializer
import java.time.Instant
open class Undo : Object, HasId, HasActor {
@ -19,10 +18,10 @@ open class Undo : Object, HasId, HasActor {
actor: String,
id: String,
`object`: Object,
published: Instant
published: String
) : super(add(type, "Undo")) {
this.`object` = `object`
this.published = published.toString()
this.published = published
this.id = id
this.actor = actor
}

View File

@ -15,9 +15,6 @@ class ObjectDeserializer : JsonDeserializer<Object>() {
if (treeNode.isValueNode) {
return ObjectValue(
emptyList(),
null,
null,
null,
treeNode.asText()
)
} else if (treeNode.isObject) {

View File

@ -3,30 +3,25 @@ package dev.usbharu.hideout.activitypub.domain.model.objects
import com.fasterxml.jackson.annotation.JsonCreator
@Suppress("VariableNaming")
open class ObjectValue : Object {
lateinit var `object`: String
@JsonCreator
constructor(type: List<String>) : super(
type
) {
this.`object` = `object`
}
open class ObjectValue @JsonCreator constructor(type: List<String>, var `object`: String) : Object(
type
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ObjectValue) return false
if (javaClass != other?.javaClass) return false
if (!super.equals(other)) return false
other as ObjectValue
return `object` == other.`object`
}
override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + (`object`?.hashCode() ?: 0)
result = 31 * result + `object`.hashCode()
return result
}
override fun toString(): String = "ObjectValue(`object`=$`object`) ${super.toString()}"
override fun toString(): String = "ObjectValue(`object`='$`object`') ${super.toString()}"
}

View File

@ -64,7 +64,6 @@ class NoteQueryServiceImpl(private val postRepository: PostRepository, private v
this[Users.followers]
)
return Note(
name = "Post",
id = this[Posts.apId],
attributedTo = this[Users.url],
content = this[Posts.text],

View File

@ -2,6 +2,7 @@ package dev.usbharu.hideout.activitypub.service.activity.delete
import dev.usbharu.hideout.activitypub.domain.exception.IllegalActivityPubObjectException
import dev.usbharu.hideout.activitypub.domain.model.Delete
import dev.usbharu.hideout.activitypub.domain.model.HasId
import dev.usbharu.hideout.activitypub.service.common.AbstractActivityPubProcessor
import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
@ -17,7 +18,11 @@ class APDeleteProcessor(
) :
AbstractActivityPubProcessor<Delete>(transaction) {
override suspend fun internalProcess(activity: ActivityPubProcessContext<Delete>) {
val deleteId = activity.activity.`object`?.id ?: throw IllegalActivityPubObjectException("object.id is null")
val value = activity.activity.`object`
if (value !is HasId) {
throw IllegalActivityPubObjectException("object hasn't id")
}
val deleteId = value.id
val post = try {
postQueryService.findByApId(deleteId)

View File

@ -15,7 +15,6 @@ class APSendFollowServiceImpl(
) : APSendFollowService {
override suspend fun sendFollow(sendFollowDto: SendFollowDto) {
val follow = Follow(
name = "Follow",
`object` = sendFollowDto.followTargetUserId.url,
actor = sendFollowDto.userId.url
)

View File

@ -21,7 +21,6 @@ class ApReactionJobProcessor(
apRequestService.apPost(
param.inbox,
Like(
name = "Like",
actor = param.actor,
`object` = param.postUrl,
id = "${applicationConfig.url}/liek/note/${param.id}",

View File

@ -28,11 +28,10 @@ class ApRemoveReactionJobProcessor(
apRequestService.apPost(
param.inbox,
Undo(
name = "Undo Reaction",
actor = param.actor,
`object` = like,
id = "${applicationConfig.url}/undo/like/${param.id}",
published = Instant.now()
published = Instant.now().toString()
),
signer
)

View File

@ -57,13 +57,11 @@ class APUserServiceImpl(
url = userUrl,
icon = Image(
type = emptyList(),
name = "$userUrl/icon.png",
mediaType = "image/png",
url = "$userUrl/icon.png"
),
publicKey = Key(
type = emptyList(),
name = "Public Key",
id = userEntity.keyId,
owner = userUrl,
publicKeyPem = userEntity.publicKey
@ -127,13 +125,11 @@ class APUserServiceImpl(
url = id,
icon = Image(
type = emptyList(),
name = "$id/icon.png",
mediaType = "image/png",
url = "$id/icon.png"
),
publicKey = Key(
type = emptyList(),
name = "Public Key",
id = userEntity.keyId,
owner = id,
publicKeyPem = userEntity.publicKey

View File

@ -25,6 +25,7 @@ class ActivityPubConfig {
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY))
.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY))
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)

View File

@ -46,7 +46,6 @@ class DeleteSerializeTest {
val readValue = objectMapper.readValue<Delete>(json)
val expected = Delete(
name = null,
actor = "https://misskey.usbharu.dev/users/97ws8y3rj6",
id = "https://misskey.usbharu.dev/4b5b6ed5-9269-45f3-8403-cba1e74b4b69",
`object` = Tombstone(
@ -61,7 +60,6 @@ class DeleteSerializeTest {
@Test
fun シリアライズできる() {
val delete = Delete(
name = null,
actor = "https://misskey.usbharu.dev/users/97ws8y3rj6",
id = "https://misskey.usbharu.dev/4b5b6ed5-9269-45f3-8403-cba1e74b4b69",
`object` = Tombstone(
@ -75,7 +73,7 @@ class DeleteSerializeTest {
val actual = objectMapper.writeValueAsString(delete)
val expected =
"""{"type":"Delete","actor":"https://misskey.usbharu.dev/users/97ws8y3rj6","id":"https://misskey.usbharu.dev/4b5b6ed5-9269-45f3-8403-cba1e74b4b69","object":{"type":"Tombstone","name":"Tombstone","id":"https://misskey.usbharu.dev/notes/9lkwqnwqk9"},"published":"2023-11-02T15:30:34.160Z"}"""
"""{"type":"Delete","actor":"https://misskey.usbharu.dev/users/97ws8y3rj6","id":"https://misskey.usbharu.dev/4b5b6ed5-9269-45f3-8403-cba1e74b4b69","object":{"type":"Tombstone","id":"https://misskey.usbharu.dev/notes/9lkwqnwqk9"},"published":"2023-11-02T15:30:34.160Z"}"""
assertEquals(expected, actual)
}
}

View File

@ -10,7 +10,6 @@ class NoteSerializeTest {
@Test
fun Noteのシリアライズができる() {
val note = Note(
name = "Note",
id = "https://example.com",
attributedTo = "https://example.com/actor",
content = "Hello",
@ -22,7 +21,7 @@ class NoteSerializeTest {
val writeValueAsString = objectMapper.writeValueAsString(note)
assertEquals(
"{\"type\":\"Note\",\"name\":\"Note\",\"id\":\"https://example.com\",\"attributedTo\":\"https://example.com/actor\",\"content\":\"Hello\",\"published\":\"2023-05-20T10:28:17.308Z\",\"sensitive\":false}",
"""{"type":"Note","id":"https://example.com","attributedTo":"https://example.com/actor","content":"Hello","published":"2023-05-20T10:28:17.308Z","sensitive":false}""",
writeValueAsString
)
}
@ -65,7 +64,6 @@ class NoteSerializeTest {
val readValue = objectMapper.readValue<Note>(json)
val note = Note(
name = "",
id = "https://misskey.usbharu.dev/notes/9f2i9cm88e",
type = listOf("Note"),
attributedTo = "https://misskey.usbharu.dev/users/97ws8y3rj6",
@ -77,7 +75,6 @@ class NoteSerializeTest {
inReplyTo = "https://calckey.jp/notes/9f2i7ymf1d",
attachment = emptyList()
)
note.name = null
assertEquals(note, readValue)
}
}

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.activitypub.domain.model
import dev.usbharu.hideout.application.config.ActivityPubConfig
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test
import utils.JsonObjectMapper
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
@ -12,18 +12,16 @@ class UndoTest {
fun Undoのシリアライズができる() {
val undo = Undo(
emptyList(),
"Undo Follow",
"https://follower.example.com/",
"https://follower.example.com/undo/1",
Follow(
emptyList(),
null,
"https://follower.example.com/users/",
actor = "https://follower.exaple.com/users/1"
),
Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
Instant.now(Clock.tickMillis(ZoneId.systemDefault())).toString()
)
val writeValueAsString = JsonObjectMapper.objectMapper.writeValueAsString(undo)
val writeValueAsString = ActivityPubConfig().objectMapper().writeValueAsString(undo)
println(writeValueAsString)
}
@ -70,7 +68,7 @@ class UndoTest {
""".trimIndent()
val undo = JsonObjectMapper.objectMapper.readValue(json, Undo::class.java)
val undo = ActivityPubConfig().objectMapper().readValue(json, Undo::class.java)
println(undo)
}
}

View File

@ -16,10 +16,7 @@ class ObjectSerializeTest {
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
listOf("Object"),
null,
null,
null
listOf("Object")
)
assertEquals(expected, readValue)
}
@ -34,10 +31,7 @@ class ObjectSerializeTest {
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
listOf("Hoge", "Object"),
null,
null,
null
listOf("Hoge", "Object")
)
assertEquals(expected, readValue)
@ -53,10 +47,7 @@ class ObjectSerializeTest {
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
emptyList(),
null,
null,
null
emptyList()
)
assertEquals(expected, readValue)

View File

@ -49,12 +49,10 @@ class UserAPControllerImplTest {
outbox = "https://example.com/users/hoge/outbox",
url = "https://example.com/users/hoge",
icon = Image(
name = "icon",
mediaType = "image/jpeg",
url = "https://example.com/users/hoge/icon.jpg"
),
publicKey = Key(
name = "Public Key",
id = "https://example.com/users/hoge#pubkey",
owner = "https://example.com/users/hoge",
publicKeyPem = "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----",

View File

@ -53,7 +53,6 @@ class NoteApControllerImplTest {
fun `postAP 匿名で取得できる`() = runTest {
SecurityContextHolder.clearContext()
val note = Note(
name = "Note",
id = "https://example.com/users/hoge/posts/1234",
attributedTo = "https://example.com/users/hoge",
content = "Hello",
@ -90,7 +89,6 @@ class NoteApControllerImplTest {
@Test
fun `postAP 認証に成功している場合userIdがnullでない`() = runTest {
val note = Note(
name = "Note",
id = "https://example.com/users/hoge/posts/1234",
attributedTo = "https://example.com/users/hoge",
content = "Hello",

View File

@ -52,7 +52,6 @@ class ApSendCreateServiceImplTest {
val post = PostBuilder.of()
val user = UserBuilder.localUserOf(id = post.userId)
val note = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,

View File

@ -24,7 +24,6 @@ class APSendFollowServiceImplTest {
apSendFollowServiceImpl.sendFollow(sendFollowDto)
val value = Follow(
name = "Follow",
`object` = sendFollowDto.followTargetUserId.url,
actor = sendFollowDto.userId.url
)

View File

@ -39,7 +39,7 @@ class APRequestServiceImplTest {
assertDoesNotThrow {
dateTimeFormatter.parse(it.headers["Date"])
}
respond("{}")
respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""")
}),
objectMapper,
mock(),
@ -47,7 +47,6 @@ class APRequestServiceImplTest {
)
val responseClass = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -65,7 +64,7 @@ class APRequestServiceImplTest {
assertDoesNotThrow {
dateTimeFormatter.parse(it.headers["Date"])
}
respond("{}")
respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""")
}),
objectMapper,
mock(),
@ -73,7 +72,6 @@ class APRequestServiceImplTest {
)
val responseClass = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -106,7 +104,7 @@ class APRequestServiceImplTest {
assertDoesNotThrow {
dateTimeFormatter.parse(it.headers["Date"])
}
respond("{}")
respond("""{"type":"Follow","object": "https://example.com","actor": "https://example.com"}""")
}),
objectMapper,
httpSignatureSigner,
@ -114,7 +112,6 @@ class APRequestServiceImplTest {
)
val responseClass = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -166,7 +163,6 @@ class APRequestServiceImplTest {
}), objectMapper, mock(), dateTimeFormatter)
val body = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -213,7 +209,6 @@ class APRequestServiceImplTest {
}), objectMapper, mock(), dateTimeFormatter)
val body = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -244,7 +239,6 @@ class APRequestServiceImplTest {
}), objectMapper, mock(), dateTimeFormatter)
val body = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -286,7 +280,6 @@ class APRequestServiceImplTest {
}), objectMapper, httpSignatureSigner, dateTimeFormatter)
val body = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)
@ -337,7 +330,6 @@ class APRequestServiceImplTest {
}), objectMapper, mock(), dateTimeFormatter)
val body = Follow(
name = "Follow",
`object` = "https://example.com",
actor = "https://example.com"
)

View File

@ -56,7 +56,6 @@ class APNoteServiceImplTest {
onBlocking { findById(eq(post.userId)) } doReturn user
}
val expected = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,
@ -98,7 +97,6 @@ class APNoteServiceImplTest {
onBlocking { findById(eq(post.userId)) } doReturn user
}
val note = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,
@ -124,13 +122,11 @@ class APNoteServiceImplTest {
url = user.url,
icon = Image(
type = emptyList(),
name = user.url + "/icon.png",
mediaType = "image/png",
url = user.url + "/icon.png"
),
publicKey = Key(
type = emptyList(),
name = "Public Key",
id = user.keyId,
owner = user.url,
publicKeyPem = user.publicKey
@ -177,7 +173,6 @@ class APNoteServiceImplTest {
onBlocking { findById(eq(post.userId)) } doReturn user
}
val note = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,
@ -246,11 +241,11 @@ class APNoteServiceImplTest {
outbox = user.outbox,
url = user.url,
icon = Image(
name = user.url + "/icon.png", mediaType = "image/png", url = user.url + "/icon.png"
mediaType = "image/png",
url = user.url + "/icon.png"
),
publicKey = Key(
type = emptyList(),
name = "Public Key",
id = user.keyId,
owner = user.url,
publicKeyPem = user.publicKey
@ -278,7 +273,6 @@ class APNoteServiceImplTest {
)
val note = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,
@ -311,7 +305,6 @@ class APNoteServiceImplTest {
onBlocking { findById(eq(user.id)) } doReturn user
}
val note = Note(
name = "Post",
id = post.apId,
attributedTo = user.url,
content = post.text,

View File

@ -13,7 +13,6 @@ class ContextSerializerTest {
name = "aaa",
actor = "bbb",
`object` = Follow(
name = "ccc",
`object` = "ddd",
actor = "aaa"
)