Merge branch 'develop' into feature/remote-media

This commit is contained in:
usbharu 2023-11-02 20:01:17 +09:00 committed by GitHub
commit 8eead0d115
8 changed files with 30 additions and 11 deletions

View File

@ -132,6 +132,7 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.springframework.security:spring-security-oauth2-jose")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
implementation("org.jetbrains.exposed:exposed-spring-boot-starter:0.44.0")
implementation("io.trbl:blurhash:1.0.0")
implementation("software.amazon.awssdk:s3:2.20.157")

View File

@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository
@Repository
interface PostRepository {
suspend fun generateId(): Long
suspend fun save(post: Post): Post
suspend fun save(post: Post): Boolean
suspend fun delete(id: Long)
suspend fun findById(id: Long): Post
}

View File

@ -2,9 +2,11 @@ package dev.usbharu.hideout.core.domain.model.timeline
import dev.usbharu.hideout.core.domain.model.post.Visibility
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.mapping.Document
@Document
@CompoundIndex(def = "{'userId':1,'timelineId':1,'postId':1}", unique = true)
data class Timeline(
@Id
val id: Long,

View File

@ -17,7 +17,7 @@ class PostRepositoryImpl(
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(post: Post): Post {
override suspend fun save(post: Post): Boolean {
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
if (singleOrNull == null) {
Posts.insert {
@ -63,7 +63,7 @@ class PostRepositoryImpl(
"Faild to insert"
}
return post
return singleOrNull == null
}
override suspend fun findById(id: Long): Post =

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.infrastructure.kjobmongodb
import com.mongodb.reactivestreams.client.MongoClient
import dev.usbharu.hideout.core.service.job.JobQueueWorkerService
import kjob.core.dsl.JobRegisterContext
import kjob.core.dsl.KJobFunctions
@ -12,10 +13,10 @@ import kjob.core.dsl.JobContextWithProps as JCWP
@Service
@ConditionalOnProperty(name = ["hideout.use-mongodb"], havingValue = "true", matchIfMissing = false)
class KJobMongoJobQueueWorkerService : JobQueueWorkerService {
class KJobMongoJobQueueWorkerService(private val mongoClient: MongoClient) : JobQueueWorkerService, AutoCloseable {
val kjob by lazy {
kjob(Mongo) {
connectionString = "mongodb://localhost"
client = mongoClient
nonBlockingMaxJobs = 10
blockingMaxJobs = 10
jobExecutionPeriodInSeconds = 1
@ -29,4 +30,8 @@ class KJobMongoJobQueueWorkerService : JobQueueWorkerService {
kjob.register(job.first, job.second)
}
}
override fun close() {
kjob.shutdown()
}
}

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.infrastructure.kjobmongodb
import com.mongodb.reactivestreams.client.MongoClient
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import kjob.core.Job
import kjob.core.dsl.ScheduleContext
@ -10,9 +11,9 @@ import org.springframework.stereotype.Service
@Service
@ConditionalOnProperty(name = ["hideout.use-mongodb"], havingValue = "true", matchIfMissing = false)
class KjobMongoJobQueueParentService : JobQueueParentService {
class KjobMongoJobQueueParentService(private val mongoClient: MongoClient) : JobQueueParentService, AutoCloseable {
private val kjob = kjob(Mongo) {
connectionString = "mongodb://localhost"
client = mongoClient
databaseName = "kjob"
jobCollection = "kjob-jobs"
lockCollection = "kjob-locks"
@ -25,4 +26,8 @@ class KjobMongoJobQueueParentService : JobQueueParentService {
override suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit) {
kjob.schedule(job, block)
}
override fun close() {
kjob.shutdown()
}
}

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.service.post
import com.mongodb.DuplicateKeyException
import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService
import dev.usbharu.hideout.core.domain.exception.UserNotFoundException
import dev.usbharu.hideout.core.domain.model.post.Post
@ -38,13 +39,17 @@ class PostServiceImpl(
}
private suspend fun internalCreate(post: Post, isLocal: Boolean): Post {
val save = try {
postRepository.save(post)
return try {
if (postRepository.save(post)) {
try {
timelineService.publishTimeline(post, isLocal)
} catch (_: DuplicateKeyException) {
}
}
post
} catch (_: ExposedSQLException) {
postQueryService.findByApId(post.apId)
}
timelineService.publishTimeline(save, isLocal)
return save
}
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {

View File

@ -24,6 +24,7 @@ spring:
password: ""
data:
mongodb:
auto-index-creation: true
host: localhost
port: 27017
database: hideout