diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPost.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPost.kt index b5a6740b..16f1092e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPost.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPost.kt @@ -19,9 +19,9 @@ package dev.usbharu.hideout.core.application.post import dev.usbharu.hideout.core.domain.model.post.Visibility data class RegisterLocalPost( - val actorId: Long, + val userDetailId: Long, val content: String, - val overview: String, + val overview: String?, val visibility: Visibility, val repostId: Long?, val replyId: Long?, diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPostApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPostApplicationService.kt index 3b6dcd8c..8b81c8de 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPostApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/RegisterLocalPostApplicationService.kt @@ -25,6 +25,7 @@ import dev.usbharu.hideout.core.domain.model.media.MediaId import dev.usbharu.hideout.core.domain.model.post.PostId import dev.usbharu.hideout.core.domain.model.post.PostOverview import dev.usbharu.hideout.core.domain.model.post.PostRepository +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository import dev.usbharu.hideout.core.infrastructure.factory.PostFactoryImpl import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -35,6 +36,7 @@ class RegisterLocalPostApplicationService( private val postFactory: PostFactoryImpl, private val actorRepository: ActorRepository, private val postRepository: PostRepository, + private val userDetailRepository: UserDetailRepository, transaction: Transaction, ) : AbstractApplicationService(transaction, Companion.logger) { @@ -43,10 +45,12 @@ class RegisterLocalPostApplicationService( } override suspend fun internalExecute(command: RegisterLocalPost, executor: CommandExecutor) { - val actorId = ActorId(command.actorId) + val actorId = (userDetailRepository.findById(command.userDetailId) + ?: throw IllegalStateException("actor not found")).actorId + val post = postFactory.createLocal(actorId, actorRepository.findById(actorId)!!.name, - PostOverview(command.overview), + command.overview?.let { PostOverview(it) }, command.content, command.visibility, command.repostId?.let { PostId(it) }, diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostFactoryImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostFactoryImpl.kt index ab31daf1..1c4acd6c 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostFactoryImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostFactoryImpl.kt @@ -38,7 +38,7 @@ class PostFactoryImpl( suspend fun createLocal( actorId: ActorId, actorName: ActorName, - overview: PostOverview, + overview: PostOverview?, content: String, visibility: Visibility, repostId: PostId?, diff --git a/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql b/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql index 3f76c59b..b85bd31d 100644 --- a/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql +++ b/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql @@ -41,21 +41,21 @@ create table if not exists actors url varchar(1000) not null unique, public_key varchar(10000) not null, private_key varchar(10000) null, - created_at timestamp not null, + created_at timestamp not null, key_id varchar(1000) not null, "following" varchar(1000) null, followers varchar(1000) null, - "instance" bigint not null, + "instance" bigint not null, locked boolean not null, - following_count int null, - followers_count int null, + following_count int null, + followers_count int null, posts_count int not null, last_post_at timestamp null default null, - last_update_at timestamp not null, - suspend boolean not null, - move_to bigint null default null, - emojis varchar(3000) not null default '', - deleted boolean not null default false, + last_update_at timestamp not null, + suspend boolean not null, + move_to bigint null default null, + emojis varchar(3000) not null default '', + deleted boolean not null default false, unique ("name", "domain"), constraint fk_actors_instance__id foreign key ("instance") references instance (id) on delete restrict on update restrict, constraint fk_actors_actors__move_to foreign key ("move_to") references actors (id) on delete restrict on update restrict @@ -75,7 +75,7 @@ create table if not exists user_details actor_id bigint not null unique, password varchar(255) not null, auto_accept_followee_follow_request boolean not null, - last_migration timestamp null default null, + last_migration timestamp null default null, constraint fk_user_details_actor_id__id foreign key (actor_id) references actors (id) on delete restrict on update restrict ); diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringStatusApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringStatusApi.kt index 3783b068..e1885640 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringStatusApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringStatusApi.kt @@ -16,14 +16,22 @@ package dev.usbharu.hideout.mastodon.interfaces.api +import dev.usbharu.hideout.core.application.post.RegisterLocalPost +import dev.usbharu.hideout.core.application.post.RegisterLocalPostApplicationService +import dev.usbharu.hideout.core.domain.model.post.Visibility +import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.Oauth2CommandExecutorFactory import dev.usbharu.hideout.mastodon.interfaces.api.generated.StatusApi import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.Status import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.StatusesRequest +import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.StatusesRequest.Visibility.* import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @Controller -class SpringStatusApi : StatusApi { +class SpringStatusApi( + private val oauth2CommandExecutorFactory: Oauth2CommandExecutorFactory, + private val registerLocalPostApplicationService: RegisterLocalPostApplicationService, +) : StatusApi { override suspend fun apiV1StatusesIdEmojiReactionsEmojiDelete(id: String, emoji: String): ResponseEntity { return super.apiV1StatusesIdEmojiReactionsEmojiDelete(id, emoji) } @@ -37,6 +45,26 @@ class SpringStatusApi : StatusApi { } override suspend fun apiV1StatusesPost(statusesRequest: StatusesRequest): ResponseEntity { - return super.apiV1StatusesPost(statusesRequest) + val executor = oauth2CommandExecutorFactory.getCommandExecutor() + registerLocalPostApplicationService.execute( + RegisterLocalPost( + userDetailId = executor.userDetailId, + content = statusesRequest.status.orEmpty(), + overview = statusesRequest.spoilerText, + visibility = when (statusesRequest.visibility) { + public -> Visibility.PUBLIC + unlisted -> Visibility.UNLISTED + private -> Visibility.FOLLOWERS + direct -> Visibility.DIRECT + null -> Visibility.PUBLIC + }, + repostId = null, + replyId = statusesRequest.inReplyToId?.toLong(), + sensitive = statusesRequest.sensitive == true, + mediaIds = statusesRequest.mediaIds.orEmpty().map { it.toLong() } + ), + executor + ) + return ResponseEntity.ok().build() } } \ No newline at end of file