diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/MongoGenerateTimelineService.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/MongoGenerateTimelineService.kt index 098e17ca..0a677430 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/MongoGenerateTimelineService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/MongoGenerateTimelineService.kt @@ -4,6 +4,7 @@ import dev.usbharu.hideout.domain.mastodon.model.generated.Status import dev.usbharu.hideout.domain.model.hideout.entity.Timeline import dev.usbharu.hideout.query.mastodon.StatusQueryService import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty +import org.springframework.data.domain.Sort import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.query.Criteria import org.springframework.data.mongodb.core.query.Query @@ -26,6 +27,7 @@ class MongoGenerateTimelineService( limit: Int ): List { val query = Query() + if (forUserId != null) { val criteria = Criteria.where("userId").`is`(forUserId) query.addCriteria(criteria) @@ -43,7 +45,10 @@ class MongoGenerateTimelineService( query.addCriteria(criteria) } - val timelines = mongoTemplate.find(query.limit(limit), Timeline::class.java) + query.limit(limit) + query.with(Sort.by(Sort.Direction.DESC, "createdAt")) + + val timelines = mongoTemplate.find(query, Timeline::class.java) return statusQueryService.findByPostIds(timelines.flatMap { setOfNotNull(it.postId, it.replyId, it.repostId) }) }