feat: フィルターの実装を追加

This commit is contained in:
usbharu 2024-06-10 14:37:16 +09:00
parent 70a1a17e37
commit 57d3091b18
3 changed files with 53 additions and 4 deletions

View File

@ -34,7 +34,9 @@ class Filter(
} }
} }
return Regex("") return (wholeWords + regexes + wholeWords)
.joinToString("|")
.toRegex()
} }
companion object { companion object {

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.core.domain.model.filter package dev.usbharu.hideout.core.domain.model.filter
class FilterResult(val filter: Filter, val matchedKeyword: FilterKeywordKeyword) { class FilterResult(val filter: Filter, val matchedKeyword: String) {
} }

View File

@ -2,21 +2,68 @@ package dev.usbharu.hideout.core.domain.service.filter
import dev.usbharu.hideout.core.domain.model.filter.Filter import dev.usbharu.hideout.core.domain.model.filter.Filter
import dev.usbharu.hideout.core.domain.model.filter.FilterContext import dev.usbharu.hideout.core.domain.model.filter.FilterContext
import dev.usbharu.hideout.core.domain.model.filter.FilterResult
import dev.usbharu.hideout.core.domain.model.filter.FilteredPost import dev.usbharu.hideout.core.domain.model.filter.FilteredPost
import dev.usbharu.hideout.core.domain.model.post.Post import dev.usbharu.hideout.core.domain.model.post.Post
import org.springframework.stereotype.Service
interface IFilterDomainService { interface IFilterDomainService {
fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost
fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost> fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost>
} }
@Service
class FilterDomainService : IFilterDomainService { class FilterDomainService : IFilterDomainService {
override fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost { override fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost {
filters.filter { it.filterContext.contains(context) } val filterResults = filters
.filter { it.filterContext.contains(context) }
.flatMap { filter ->
val regex = filter.compileFilter()
post
.overview
?.overview
?.let { it1 -> regex.findAll(it1) }
.orEmpty()
.toList()
.map { FilterResult(filter, it.value) }
.plus(
post
.text
.let { regex.findAll(it) }
.toList()
.map { FilterResult(filter, it.value) }
)
}
return FilteredPost(post, filterResults)
} }
override fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost> { override fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost> {
TODO("Not yet implemented") return filters
.filter { it.filterContext.contains(context) }
.map { it to it.compileFilter() }
.flatMap { compiledFilter ->
postList
.map { post ->
val filterResults = post
.overview
?.overview
?.let { overview -> compiledFilter.second.findAll(overview) }
.orEmpty()
.toList()
.map { FilterResult(compiledFilter.first, it.value) }
.plus(
post
.text
.let { compiledFilter.second.findAll(it) }
.toList()
.map { FilterResult(compiledFilter.first, it.value) }
)
post to filterResults
}
}
.map { FilteredPost(it.first, it.second) }
} }
} }