mirror of https://github.com/usbharu/Hideout.git
feat: リアクション情報の配送の実装を作成
This commit is contained in:
parent
88f8b405e3
commit
e340d68dc0
|
@ -8,6 +8,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import dev.usbharu.hideout.config.Config
|
import dev.usbharu.hideout.config.Config
|
||||||
import dev.usbharu.hideout.config.ConfigData
|
import dev.usbharu.hideout.config.ConfigData
|
||||||
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
||||||
|
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||||
import dev.usbharu.hideout.plugins.*
|
import dev.usbharu.hideout.plugins.*
|
||||||
import dev.usbharu.hideout.repository.IUserRepository
|
import dev.usbharu.hideout.repository.IUserRepository
|
||||||
|
@ -135,4 +136,10 @@ fun Application.worker() {
|
||||||
activityPubService.processActivity(this, it)
|
activityPubService.processActivity(this, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kJob.register(DeliverReactionJob) {
|
||||||
|
execute {
|
||||||
|
activityPubService.processActivity(this, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,13 +11,13 @@ open class Like : Object {
|
||||||
|
|
||||||
protected constructor() : super()
|
protected constructor() : super()
|
||||||
constructor(
|
constructor(
|
||||||
type: List<String>,
|
type: List<String> = emptyList(),
|
||||||
name: String?,
|
name: String?,
|
||||||
actor: String?,
|
actor: String?,
|
||||||
id: String?,
|
id: String?,
|
||||||
`object`: String?,
|
`object`: String?,
|
||||||
content: String?,
|
content: String?,
|
||||||
tag: List<Object>
|
tag: List<Object> = emptyList()
|
||||||
) : super(
|
) : super(
|
||||||
type = add(type, "Like"),
|
type = add(type, "Like"),
|
||||||
name = name,
|
name = name,
|
||||||
|
|
|
@ -18,7 +18,7 @@ data class User(
|
||||||
) {
|
) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description'," +
|
return "User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description'," +
|
||||||
" password=****, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey'," +
|
" password=****, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey'," +
|
||||||
" privateKey=****, createdAt=$createdAt)"
|
" privateKey=****, createdAt=$createdAt)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,4 +21,5 @@ object DeliverReactionJob : HideoutJob("DeliverReactionJob") {
|
||||||
val postUrl = string("postUrl")
|
val postUrl = string("postUrl")
|
||||||
val actor = string("actor")
|
val actor = string("actor")
|
||||||
val inbox = string("inbox")
|
val inbox = string("inbox")
|
||||||
|
val id = string("id")
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,11 +42,11 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
|
||||||
authenticate(TOKEN_AUTH, optional = true) {
|
authenticate(TOKEN_AUTH, optional = true) {
|
||||||
get {
|
get {
|
||||||
val userParameter = (
|
val userParameter = (
|
||||||
call.parameters["name"]
|
call.parameters["name"]
|
||||||
?: throw ParameterNotExistException(
|
?: throw ParameterNotExistException(
|
||||||
"Parameter(name='userName@domain') does not exist."
|
"Parameter(name='userName@domain') does not exist."
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
if (userParameter.toLongOrNull() != null) {
|
if (userParameter.toLongOrNull() != null) {
|
||||||
return@get call.respond(userApiService.findById(userParameter.toLong()))
|
return@get call.respond(userApiService.findById(userParameter.toLong()))
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,11 +92,11 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
|
||||||
route("/following") {
|
route("/following") {
|
||||||
get {
|
get {
|
||||||
val userParameter = (
|
val userParameter = (
|
||||||
call.parameters["name"]
|
call.parameters["name"]
|
||||||
?: throw ParameterNotExistException(
|
?: throw ParameterNotExistException(
|
||||||
"Parameter(name='userName@domain') does not exist."
|
"Parameter(name='userName@domain') does not exist."
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
if (userParameter.toLongOrNull() != null) {
|
if (userParameter.toLongOrNull() != null) {
|
||||||
return@get call.respond(userApiService.findFollowings(userParameter.toLong()))
|
return@get call.respond(userApiService.findFollowings(userParameter.toLong()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package dev.usbharu.hideout.service.activitypub
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.config.Config
|
||||||
|
import dev.usbharu.hideout.domain.model.ap.Like
|
||||||
|
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
|
||||||
|
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||||
|
import dev.usbharu.hideout.exception.PostNotFoundException
|
||||||
|
import dev.usbharu.hideout.plugins.postAp
|
||||||
|
import dev.usbharu.hideout.repository.IPostRepository
|
||||||
|
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||||
|
import dev.usbharu.hideout.service.user.IUserService
|
||||||
|
import io.ktor.client.*
|
||||||
|
import kjob.core.job.JobProps
|
||||||
|
import org.koin.core.annotation.Single
|
||||||
|
|
||||||
|
@Single
|
||||||
|
class ActivityPubReactionServiceImpl(
|
||||||
|
private val userService: IUserService,
|
||||||
|
private val jobQueueParentService: JobQueueParentService,
|
||||||
|
private val iPostRepository: IPostRepository,
|
||||||
|
private val httpClient: HttpClient
|
||||||
|
) : ActivityPubReactionService {
|
||||||
|
override suspend fun reaction(like: Reaction) {
|
||||||
|
val followers = userService.findFollowersById(like.userId)
|
||||||
|
val user = userService.findById(like.userId)
|
||||||
|
val post =
|
||||||
|
iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.")
|
||||||
|
followers.forEach { follower ->
|
||||||
|
jobQueueParentService.schedule(DeliverReactionJob) {
|
||||||
|
props[it.actor] = user.url
|
||||||
|
props[it.reaction] = "❤"
|
||||||
|
props[it.inbox] = follower.inbox
|
||||||
|
props[it.postUrl] = post.url
|
||||||
|
props[it.id] = post.id.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun reactionJob(props: JobProps<DeliverReactionJob>) {
|
||||||
|
val inbox = props[DeliverReactionJob.inbox]
|
||||||
|
val actor = props[DeliverReactionJob.actor]
|
||||||
|
val postUrl = props[DeliverReactionJob.postUrl]
|
||||||
|
val id = props[DeliverReactionJob.id]
|
||||||
|
val content = props[DeliverReactionJob.reaction]
|
||||||
|
httpClient.postAp(
|
||||||
|
urlString = inbox,
|
||||||
|
username = "$actor#pubkey",
|
||||||
|
jsonLd = Like(
|
||||||
|
name = "Like",
|
||||||
|
actor = actor,
|
||||||
|
`object` = postUrl,
|
||||||
|
id = "${Config.configData.url}/like/note/$id",
|
||||||
|
content = content
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import dev.usbharu.hideout.config.Config.configData
|
||||||
import dev.usbharu.hideout.domain.model.ActivityPubResponse
|
import dev.usbharu.hideout.domain.model.ActivityPubResponse
|
||||||
import dev.usbharu.hideout.domain.model.ap.Follow
|
import dev.usbharu.hideout.domain.model.ap.Follow
|
||||||
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
||||||
|
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||||
import dev.usbharu.hideout.domain.model.job.HideoutJob
|
import dev.usbharu.hideout.domain.model.job.HideoutJob
|
||||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||||
import dev.usbharu.hideout.exception.JsonParseException
|
import dev.usbharu.hideout.exception.JsonParseException
|
||||||
|
@ -22,7 +23,8 @@ class ActivityPubServiceImpl(
|
||||||
private val activityPubUndoService: ActivityPubUndoService,
|
private val activityPubUndoService: ActivityPubUndoService,
|
||||||
private val activityPubAcceptService: ActivityPubAcceptService,
|
private val activityPubAcceptService: ActivityPubAcceptService,
|
||||||
private val activityPubCreateService: ActivityPubCreateService,
|
private val activityPubCreateService: ActivityPubCreateService,
|
||||||
private val activityPubLikeService: ActivityPubLikeService
|
private val activityPubLikeService: ActivityPubLikeService,
|
||||||
|
private val activityPubReactionService: ActivityPubReactionService
|
||||||
) : ActivityPubService {
|
) : ActivityPubService {
|
||||||
|
|
||||||
val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
@ -71,6 +73,7 @@ class ActivityPubServiceImpl(
|
||||||
)
|
)
|
||||||
|
|
||||||
DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
|
DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
|
||||||
|
DeliverReactionJob -> activityPubReactionService.reactionJob(job.props as JobProps<DeliverReactionJob>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ class ReactionServiceImpl(
|
||||||
|
|
||||||
override suspend fun sendReaction(name: String, userId: Long, postId: Long) {
|
override suspend fun sendReaction(name: String, userId: Long, postId: Long) {
|
||||||
if (reactionRepository.reactionAlreadyExist(postId, userId, 0)) {
|
if (reactionRepository.reactionAlreadyExist(postId, userId, 0)) {
|
||||||
//delete
|
// delete
|
||||||
} else {
|
} else {
|
||||||
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
|
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
|
||||||
reactionRepository.save(reaction)
|
reactionRepository.save(reaction)
|
||||||
|
|
Loading…
Reference in New Issue