style: スタイルを修正

This commit is contained in:
usbharu 2023-06-08 22:54:37 +09:00
parent e8965f798a
commit 0cb58002eb
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
17 changed files with 373 additions and 362 deletions

View File

@ -108,12 +108,12 @@ fun Application.parent() {
inject<JwkProvider>().value, inject<JwkProvider>().value,
) )
configureRouting( configureRouting(
httpSignatureVerifyService = inject<HttpSignatureVerifyService>().value, httpSignatureVerifyService = inject<HttpSignatureVerifyService>().value,
activityPubService = inject<ActivityPubService>().value, activityPubService = inject<ActivityPubService>().value,
userService = inject<IUserService>().value, userService = inject<IUserService>().value,
activityPubUserService = inject<ActivityPubUserService>().value, activityPubUserService = inject<ActivityPubUserService>().value,
postService = inject<IPostApiService>().value, postService = inject<IPostApiService>().value,
userApiService = inject<IUserApiService>().value, userApiService = inject<IUserApiService>().value,
) )
} }

View File

@ -5,16 +5,19 @@ open class Emoji : Object {
var icon: Image? = null var icon: Image? = null
protected constructor() : super() protected constructor() : super()
constructor(type: List<String>, constructor(
name: String?, type: List<String>,
actor: String?, name: String?,
id: String?, actor: String?,
updated: String?, id: String?,
icon: Image?) : super( updated: String?,
type = add(type, "Emoji"), icon: Image?
name = name, ) : super(
actor = actor, type = add(type, "Emoji"),
id = id) { name = name,
actor = actor,
id = id
) {
this.updated = updated this.updated = updated
this.icon = icon this.icon = icon
} }
@ -36,6 +39,4 @@ open class Emoji : Object {
} }
override fun toString(): String = "Emoji(updated=$updated, icon=$icon) ${super.toString()}" override fun toString(): String = "Emoji(updated=$updated, icon=$icon) ${super.toString()}"
} }

View File

@ -10,18 +10,20 @@ open class Like : Object {
var tag: List<Object> = emptyList() var tag: List<Object> = emptyList()
protected constructor() : super() protected constructor() : super()
constructor(type: List<String>, constructor(
name: String?, type: List<String>,
actor: String?, name: String?,
id: String?, actor: String?,
`object`: String?, id: String?,
content: String?, `object`: String?,
tag: List<Object> content: String?,
tag: List<Object>
) : super( ) : super(
type = add(type, "Like"), type = add(type, "Like"),
name = name, name = name,
actor = actor, actor = actor,
id = id) { id = id
) {
this.`object` = `object` this.`object` = `object`
this.content = content this.content = content
this.tag = tag this.tag = tag
@ -46,6 +48,4 @@ open class Like : Object {
} }
override fun toString(): String = "Like(`object`=$`object`, content=$content, tag=$tag) ${super.toString()}" override fun toString(): String = "Like(`object`=$`object`, content=$content, tag=$tag) ${super.toString()}"
} }

View File

@ -18,12 +18,12 @@ import io.ktor.server.routing.*
@Suppress("LongParameterList") @Suppress("LongParameterList")
fun Application.configureRouting( fun Application.configureRouting(
httpSignatureVerifyService: HttpSignatureVerifyService, httpSignatureVerifyService: HttpSignatureVerifyService,
activityPubService: ActivityPubService, activityPubService: ActivityPubService,
userService: IUserService, userService: IUserService,
activityPubUserService: ActivityPubUserService, activityPubUserService: ActivityPubUserService,
postService: IPostApiService, postService: IPostApiService,
userApiService: IUserApiService userApiService: IUserApiService
) { ) {
install(AutoHeadResponse) install(AutoHeadResponse)
routing { routing {

View File

@ -11,33 +11,33 @@ interface IPostRepository {
suspend fun findByUrl(url: String): Post? suspend fun findByUrl(url: String): Post?
suspend fun delete(id: Long) suspend fun delete(id: Long)
suspend fun findAll( suspend fun findAll(
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> ): List<Post>
suspend fun findByUserNameAndDomain( suspend fun findByUserNameAndDomain(
username: String, username: String,
s: String, s: String,
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> ): List<Post>
suspend fun findByUserId( suspend fun findByUserId(
idOrNull: Long, idOrNull: Long,
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> ): List<Post>
suspend fun findByApId(id: String): Post? suspend fun findByApId(id: String): Post?

View File

@ -81,37 +81,37 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
} }
override suspend fun findAll( override suspend fun findAll(
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<Post> {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun findByUserNameAndDomain( override suspend fun findByUserNameAndDomain(
username: String, username: String,
s: String, s: String,
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<Post> {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun findByUserId( override suspend fun findByUserId(
idOrNull: Long, idOrNull: Long,
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<Post> {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
@ -140,16 +140,16 @@ object Posts : Table() {
fun ResultRow.toPost(): Post { fun ResultRow.toPost(): Post {
return Post( return Post(
id = this[Posts.id], id = this[Posts.id],
userId = this[Posts.userId], userId = this[Posts.userId],
overview = this[Posts.overview], overview = this[Posts.overview],
text = this[Posts.text], text = this[Posts.text],
createdAt = this[Posts.createdAt], createdAt = this[Posts.createdAt],
visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] }, visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] },
url = this[Posts.url], url = this[Posts.url],
repostId = this[Posts.repostId], repostId = this[Posts.repostId],
replyId = this[Posts.replyId], replyId = this[Posts.replyId],
sensitive = this[Posts.sensitive], sensitive = this[Posts.sensitive],
apId = this[Posts.apId] apId = this[Posts.apId]
) )
} }

View File

@ -10,7 +10,10 @@ import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single import org.koin.core.annotation.Single
@Single @Single
class ReactionRepositoryImpl(private val database: Database, private val idGenerateService: IdGenerateService) : ReactionRepository { class ReactionRepositoryImpl(
private val database: Database,
private val idGenerateService: IdGenerateService
) : ReactionRepository {
init { init {
transaction(database) { transaction(database) {
@ -19,10 +22,9 @@ class ReactionRepositoryImpl(private val database: Database, private val idGener
} }
} }
@Suppress("InjectDispatcher") @Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T = suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() } newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun generateId(): Long = idGenerateService.generateId() override suspend fun generateId(): Long = idGenerateService.generateId()
@ -40,7 +42,6 @@ class ReactionRepositoryImpl(private val database: Database, private val idGener
it[emojiId] = reaction.emojiId it[emojiId] = reaction.emojiId
it[postId] = reaction.postId it[postId] = reaction.postId
it[userId] = reaction.userId it[userId] = reaction.userId
} }
} }
} }
@ -49,17 +50,21 @@ class ReactionRepositoryImpl(private val database: Database, private val idGener
override suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean { override suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean {
return query { return query {
Reactions.select { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and(Reactions.emojiId.eq(emojiId)) }.empty().not() Reactions.select {
Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and(
Reactions.emojiId.eq(emojiId)
)
}.empty().not()
} }
} }
} }
fun ResultRow.toReaction(): Reaction { fun ResultRow.toReaction(): Reaction {
return Reaction( return Reaction(
this[Reactions.id].value, this[Reactions.id].value,
this[Reactions.emojiId], this[Reactions.emojiId],
this[Reactions.postId], this[Reactions.postId],
this[Reactions.userId] this[Reactions.userId]
) )
} }

View File

@ -51,14 +51,14 @@ fun Route.posts(postApiService: IPostApiService) {
get { get {
val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong() val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong()
val targetUserName = call.parameters["name"] val targetUserName = call.parameters["name"]
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.") ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
val posts = postApiService.getByUser(targetUserName, userId = userId) val posts = postApiService.getByUser(targetUserName, userId = userId)
call.respond(posts) call.respond(posts)
} }
get("/{id}") { get("/{id}") {
val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong() val userId = call.principal<JWTPrincipal>()?.payload?.getClaim("uid")?.asLong()
val id = call.parameters["id"]?.toLong() val id = call.parameters["id"]?.toLong()
?: throw ParameterNotExistException("Parameter(name='postsId' does not exist.") ?: throw ParameterNotExistException("Parameter(name='postsId' does not exist.")
val post = postApiService.getById(id, userId) val post = postApiService.getById(id, userId)
call.respond(post) call.respond(post)
} }

View File

@ -43,9 +43,9 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
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()))
@ -93,9 +93,9 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) {
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()))

View File

@ -11,13 +11,14 @@ import dev.usbharu.hideout.service.user.IUserService
import io.ktor.http.* import io.ktor.http.*
import org.koin.core.annotation.Single import org.koin.core.annotation.Single
@Single @Single
class ActivityPubLikeServiceImpl(private val reactionService: IReactionService, class ActivityPubLikeServiceImpl(
private val activityPubUserService: ActivityPubUserService, private val reactionService: IReactionService,
private val userService: IUserService, private val activityPubUserService: ActivityPubUserService,
private val postService: IPostRepository, private val userService: IUserService,
private val activityPubNoteService: ActivityPubNoteService) : ActivityPubLikeService { private val postService: IPostRepository,
private val activityPubNoteService: ActivityPubNoteService
) : ActivityPubLikeService {
override suspend fun receiveLike(like: Like): ActivityPubResponse { override suspend fun receiveLike(like: Like): ActivityPubResponse {
val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null") val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null")
val content = like.content ?: throw IllegalActivityPubObjectException("content is null") val content = like.content ?: throw IllegalActivityPubObjectException("content is null")
@ -25,11 +26,13 @@ class ActivityPubLikeServiceImpl(private val reactionService: IReactionService,
val person = activityPubUserService.fetchPerson(actor) val person = activityPubUserService.fetchPerson(actor)
activityPubNoteService.fetchNote(like.`object`!!) activityPubNoteService.fetchNote(like.`object`!!)
val user = userService.findByUrl(person.url val user = userService.findByUrl(
?: throw IllegalActivityPubObjectException("actor is not found")) person.url
?: throw IllegalActivityPubObjectException("actor is not found")
)
val post = postService.findByUrl(like.`object`!!) val post = postService.findByUrl(like.`object`!!)
?: throw PostNotFoundException("${like.`object`} was not found") ?: throw PostNotFoundException("${like.`object`} was not found")
reactionService.receiveReaction(content, actor.substringAfter("://").substringBefore("/"), user.id, post.id) reactionService.receiveReaction(content, actor.substringAfter("://").substringBefore("/"), user.id, post.id)
return ActivityPubStringResponse(HttpStatusCode.OK, "") return ActivityPubStringResponse(HttpStatusCode.OK, "")

View File

@ -22,11 +22,11 @@ import java.time.Instant
@Single @Single
class ActivityPubNoteServiceImpl( class ActivityPubNoteServiceImpl(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val jobQueueParentService: JobQueueParentService, private val jobQueueParentService: JobQueueParentService,
private val userService: IUserService, private val userService: IUserService,
private val postRepository: IPostRepository, private val postRepository: IPostRepository,
private val activityPubUserService: ActivityPubUserService private val activityPubUserService: ActivityPubUserService
) : ActivityPubNoteService { ) : ActivityPubNoteService {
private val logger = LoggerFactory.getLogger(this::class.java) private val logger = LoggerFactory.getLogger(this::class.java)
@ -48,24 +48,24 @@ class ActivityPubNoteServiceImpl(
val actor = props[DeliverPostJob.actor] val actor = props[DeliverPostJob.actor]
val postEntity = Config.configData.objectMapper.readValue<Post>(props[DeliverPostJob.post]) val postEntity = Config.configData.objectMapper.readValue<Post>(props[DeliverPostJob.post])
val note = Note( val note = Note(
name = "Note", name = "Note",
id = postEntity.url, id = postEntity.url,
attributedTo = actor, attributedTo = actor,
content = postEntity.text, content = postEntity.text,
published = Instant.ofEpochMilli(postEntity.createdAt).toString(), published = Instant.ofEpochMilli(postEntity.createdAt).toString(),
to = listOf(public, actor + "/follower") to = listOf(public, actor + "/follower")
) )
val inbox = props[DeliverPostJob.inbox] val inbox = props[DeliverPostJob.inbox]
logger.debug("createNoteJob: actor={}, note={}, inbox={}", actor, postEntity, inbox) logger.debug("createNoteJob: actor={}, note={}, inbox={}", actor, postEntity, inbox)
httpClient.postAp( httpClient.postAp(
urlString = inbox, urlString = inbox,
username = "$actor#pubkey", username = "$actor#pubkey",
jsonLd = Create( jsonLd = Create(
name = "Create Note", name = "Create Note",
`object` = note, `object` = note,
actor = note.attributedTo, actor = note.attributedTo,
id = "${Config.configData.url}/create/note/${postEntity.id}" id = "${Config.configData.url}/create/note/${postEntity.id}"
) )
) )
} }
@ -75,7 +75,8 @@ class ActivityPubNoteServiceImpl(
return postToNote(post) return postToNote(post)
} }
val response = httpClient.getAp( val response = httpClient.getAp(
url, targetActor?.let { "$targetActor#pubkey" } url,
targetActor?.let { "$targetActor#pubkey" }
) )
val note = Config.configData.objectMapper.readValue<Note>(response.bodyAsText()) val note = Config.configData.objectMapper.readValue<Note>(response.bodyAsText())
return note(note, targetActor, url) return note(note, targetActor, url)
@ -85,44 +86,44 @@ class ActivityPubNoteServiceImpl(
val user = userService.findById(post.userId) val user = userService.findById(post.userId)
val reply = post.replyId?.let { postRepository.findOneById(it) } val reply = post.replyId?.let { postRepository.findOneById(it) }
return Note( return Note(
name = "Post", name = "Post",
id = post.apId, id = post.apId,
attributedTo = user.url, attributedTo = user.url,
content = post.text, content = post.text,
published = Instant.ofEpochMilli(post.createdAt).toString(), published = Instant.ofEpochMilli(post.createdAt).toString(),
to = listOf(public, user.url + "/follower"), to = listOf(public, user.url + "/follower"),
sensitive = post.sensitive, sensitive = post.sensitive,
cc = listOf(public, user.url + "/follower"), cc = listOf(public, user.url + "/follower"),
inReplyTo = reply?.url inReplyTo = reply?.url
) )
} }
private suspend fun ActivityPubNoteServiceImpl.note( private suspend fun ActivityPubNoteServiceImpl.note(
note: Note, note: Note,
targetActor: String?, targetActor: String?,
url: String url: String
): Note { ): Note {
val findByApId = postRepository.findByApId(url) val findByApId = postRepository.findByApId(url)
if (findByApId != null) { if (findByApId != null) {
return postToNote(findByApId) return postToNote(findByApId)
} }
val person = activityPubUserService.fetchPerson( val person = activityPubUserService.fetchPerson(
note.attributedTo ?: throw IllegalActivityPubObjectException("note.attributedTo is null"), note.attributedTo ?: throw IllegalActivityPubObjectException("note.attributedTo is null"),
targetActor targetActor
) )
val user = val user =
userService.findByUrl(person.url ?: throw IllegalActivityPubObjectException("person.url is null")) userService.findByUrl(person.url ?: throw IllegalActivityPubObjectException("person.url is null"))
val visibility = val visibility =
if (note.to.contains(public) && note.cc.contains(public)) { if (note.to.contains(public) && note.cc.contains(public)) {
Visibility.PUBLIC Visibility.PUBLIC
} else if (note.to.find { it.endsWith("/followers") } != null && note.cc.contains(public)) { } else if (note.to.find { it.endsWith("/followers") } != null && note.cc.contains(public)) {
Visibility.UNLISTED Visibility.UNLISTED
} else if (note.to.find { it.endsWith("/followers") } != null) { } else if (note.to.find { it.endsWith("/followers") } != null) {
Visibility.FOLLOWERS Visibility.FOLLOWERS
} else { } else {
Visibility.DIRECT Visibility.DIRECT
} }
val reply = note.inReplyTo?.let { val reply = note.inReplyTo?.let {
fetchNote(it, targetActor) fetchNote(it, targetActor)
@ -130,25 +131,25 @@ class ActivityPubNoteServiceImpl(
} }
postRepository.save( postRepository.save(
Post( Post(
id = postRepository.generateId(), id = postRepository.generateId(),
userId = user.id, userId = user.id,
overview = null, overview = null,
text = note.content.orEmpty(), text = note.content.orEmpty(),
createdAt = Instant.parse(note.published).toEpochMilli(), createdAt = Instant.parse(note.published).toEpochMilli(),
visibility = visibility, visibility = visibility,
url = note.id ?: url, url = note.id ?: url,
repostId = null, repostId = null,
replyId = reply?.id, replyId = reply?.id,
sensitive = note.sensitive, sensitive = note.sensitive,
apId = note.id ?: url, apId = note.id ?: url,
) )
) )
return note return note
} }
override suspend fun fetchNote(note: Note, targetActor: String?): Note = override suspend fun fetchNote(note: Note, targetActor: String?): Note =
note(note, targetActor, note.id ?: throw IllegalArgumentException("note.id is null")) note(note, targetActor, note.id ?: throw IllegalArgumentException("note.id is null"))
companion object { companion object {
const val public: String = "https://www.w3.org/ns/activitystreams#Public" const val public: String = "https://www.w3.org/ns/activitystreams#Public"

View File

@ -17,12 +17,12 @@ import org.slf4j.LoggerFactory
@Single @Single
class ActivityPubServiceImpl( class ActivityPubServiceImpl(
private val activityPubReceiveFollowService: ActivityPubReceiveFollowService, private val activityPubReceiveFollowService: ActivityPubReceiveFollowService,
private val activityPubNoteService: ActivityPubNoteService, private val activityPubNoteService: ActivityPubNoteService,
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
) : ActivityPubService { ) : ActivityPubService {
val logger: Logger = LoggerFactory.getLogger(this::class.java) val logger: Logger = LoggerFactory.getLogger(this::class.java)

View File

@ -3,25 +3,26 @@ package dev.usbharu.hideout.service.api
import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.Post
import java.time.Instant import java.time.Instant
@Suppress("LongParameterList")
interface IPostApiService { interface IPostApiService {
suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post
suspend fun getById(id: Long, userId: Long?): Post suspend fun getById(id: Long, userId: Long?): Post
suspend fun getAll( suspend fun getAll(
since: Instant? = null, since: Instant? = null,
until: Instant? = null, until: Instant? = null,
minId: Long? = null, minId: Long? = null,
maxId: Long? = null, maxId: Long? = null,
limit: Int? = null, limit: Int? = null,
userId: Long? = null userId: Long? = null
): List<Post> ): List<Post>
suspend fun getByUser( suspend fun getByUser(
nameOrId: String, nameOrId: String,
since: Instant? = null, since: Instant? = null,
until: Instant? = null, until: Instant? = null,
minId: Long? = null, minId: Long? = null,
maxId: Long? = null, maxId: Long? = null,
limit: Int? = null, limit: Int? = null,
userId: Long? = null userId: Long? = null
): List<Post> ): List<Post>
} }

View File

@ -13,58 +13,58 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost
@Single @Single
class PostApiServiceImpl( class PostApiServiceImpl(
private val postService: IPostService, private val postService: IPostService,
private val postRepository: IPostRepository private val postRepository: IPostRepository
) : IPostApiService { ) : IPostApiService {
override suspend fun createPost(postForm: FormPost, userId: Long): Post { override suspend fun createPost(postForm: FormPost, userId: Long): Post {
return postService.createLocal( return postService.createLocal(
PostCreateDto( PostCreateDto(
text = postForm.text, text = postForm.text,
overview = postForm.overview, overview = postForm.overview,
visibility = postForm.visibility, visibility = postForm.visibility,
repostId = postForm.repostId, repostId = postForm.repostId,
repolyId = postForm.replyId, repolyId = postForm.replyId,
userId = userId userId = userId
) )
) )
} }
override suspend fun getById(id: Long, userId: Long?): Post { override suspend fun getById(id: Long, userId: Long?): Post {
return postRepository.findOneById(id, userId) return postRepository.findOneById(id, userId)
?: throw PostNotFoundException("$id was not found or is not authorized.") ?: throw PostNotFoundException("$id was not found or is not authorized.")
} }
override suspend fun getAll( override suspend fun getAll(
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> = postRepository.findAll(since, until, minId, maxId, limit, userId) ): List<Post> = postRepository.findAll(since, until, minId, maxId, limit, userId)
override suspend fun getByUser( override suspend fun getByUser(
nameOrId: String, nameOrId: String,
since: Instant?, since: Instant?,
until: Instant?, until: Instant?,
minId: Long?, minId: Long?,
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<Post> {
val idOrNull = nameOrId.toLongOrNull() val idOrNull = nameOrId.toLongOrNull()
return if (idOrNull == null) { return if (idOrNull == null) {
val acct = AcctUtil.parse(nameOrId) val acct = AcctUtil.parse(nameOrId)
postRepository.findByUserNameAndDomain( postRepository.findByUserNameAndDomain(
acct.username, username = acct.username,
acct.domain s = acct.domain
?: Config.configData.domain, ?: Config.configData.domain,
since, since = since,
until, until = until,
minId, minId = minId,
maxId, maxId = maxId,
limit, limit = limit,
userId userId = userId
) )
} else { } else {
postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId) postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId)

View File

@ -11,23 +11,23 @@ import java.time.Instant
@Single @Single
class PostServiceImpl( class PostServiceImpl(
private val postRepository: IPostRepository, private val postRepository: IPostRepository,
private val userRepository: IUserRepository, private val userRepository: IUserRepository,
private val activityPubNoteService: ActivityPubNoteService private val activityPubNoteService: ActivityPubNoteService
) : IPostService { ) : IPostService {
override suspend fun createLocal(post: PostCreateDto): Post { override suspend fun createLocal(post: PostCreateDto): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
val id = postRepository.generateId() val id = postRepository.generateId()
val createPost = Post( val createPost = Post(
id = id, id = id,
userId = post.userId, userId = post.userId,
overview = post.overview, overview = post.overview,
text = post.text, text = post.text,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
visibility = post.visibility, visibility = post.visibility,
url = "${user.url}/posts/$id", url = "${user.url}/posts/$id",
repostId = null, repostId = null,
replyId = null replyId = null
) )
activityPubNoteService.createNote(createPost) activityPubNoteService.createNote(createPost)
return internalCreate(createPost) return internalCreate(createPost)

View File

@ -9,7 +9,7 @@ class ReactionServiceImpl(private val reactionRepository: ReactionRepository) :
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) { override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
if (reactionRepository.reactionAlreadyExist(postId, userId, 0).not()) { if (reactionRepository.reactionAlreadyExist(postId, userId, 0).not()) {
reactionRepository.save( reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId) Reaction(reactionRepository.generateId(), 0, postId, userId)
) )
} }
} }

View File

@ -41,24 +41,24 @@ class PostsTest {
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
), ),
Post( Post(
id = 123456, id = 123456,
userId = 4322, userId = 4322,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getAll( getAll(
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = isNull() userId = isNull()
) )
} doReturn posts } doReturn posts
} }
@ -120,12 +120,12 @@ class PostsTest {
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getAll( getAll(
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = isNotNull() userId = isNotNull()
) )
} doReturn posts } doReturn posts
} }
@ -157,12 +157,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
12345, 12345,
1234, 1234,
text = "aaa", text = "aaa",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(any(), anyOrNull()) } doReturn post onBlocking { getById(any(), anyOrNull()) } doReturn post
@ -188,12 +188,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
12345, 12345,
1234, 1234,
text = "aaa", text = "aaa",
visibility = Visibility.FOLLOWERS, visibility = Visibility.FOLLOWERS,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(any(), isNotNull()) } doReturn post onBlocking { getById(any(), isNotNull()) } doReturn post
@ -243,13 +243,13 @@ class PostsTest {
val argument = it.getArgument<dev.usbharu.hideout.domain.model.hideout.form.Post>(0) val argument = it.getArgument<dev.usbharu.hideout.domain.model.hideout.form.Post>(0)
val userId = it.getArgument<Long>(1) val userId = it.getArgument<Long>(1)
Post( Post(
123L, 123L,
userId, userId,
null, null,
argument.text, argument.text,
Instant.now().toEpochMilli(), Instant.now().toEpochMilli(),
Visibility.PUBLIC, Visibility.PUBLIC,
"https://example.com" "https://example.com"
) )
} }
} }
@ -299,25 +299,25 @@ class PostsTest {
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
), ),
Post( Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getByUser( getByUser(
nameOrId = any(), nameOrId = any(),
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = anyOrNull() userId = anyOrNull()
) )
} doReturn posts } doReturn posts
} }
@ -351,25 +351,25 @@ class PostsTest {
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
), ),
Post( Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getByUser( getByUser(
nameOrId = eq("test1"), nameOrId = eq("test1"),
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = anyOrNull() userId = anyOrNull()
) )
} doReturn posts } doReturn posts
} }
@ -403,25 +403,25 @@ class PostsTest {
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
), ),
Post( Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getByUser( getByUser(
nameOrId = eq("test1@example.com"), nameOrId = eq("test1@example.com"),
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = anyOrNull() userId = anyOrNull()
) )
} doReturn posts } doReturn posts
} }
@ -455,25 +455,25 @@ class PostsTest {
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/1" url = "https://example.com/posts/1"
), ),
Post( Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { onBlocking {
getByUser( getByUser(
nameOrId = eq("@test1@example.com"), nameOrId = eq("@test1@example.com"),
since = anyOrNull(), since = anyOrNull(),
until = anyOrNull(), until = anyOrNull(),
minId = anyOrNull(), minId = anyOrNull(),
maxId = anyOrNull(), maxId = anyOrNull(),
limit = anyOrNull(), limit = anyOrNull(),
userId = anyOrNull() userId = anyOrNull()
) )
} doReturn posts } doReturn posts
} }
@ -499,12 +499,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post
@ -531,12 +531,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post
@ -563,12 +563,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post
@ -595,12 +595,12 @@ class PostsTest {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
val post = Post( val post = Post(
id = 123456, id = 123456,
userId = 1, userId = 1,
text = "test2", text = "test2",
visibility = Visibility.PUBLIC, visibility = Visibility.PUBLIC,
createdAt = Instant.now().toEpochMilli(), createdAt = Instant.now().toEpochMilli(),
url = "https://example.com/posts/2" url = "https://example.com/posts/2"
) )
val postService = mock<IPostApiService> { val postService = mock<IPostApiService> {
onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post onBlocking { getById(eq(12345L), anyOrNull()) } doReturn post