diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt index f1ecdd1a..cf679a1f 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt @@ -34,7 +34,9 @@ class Filter( } } - return Regex("") + return (wholeWords + regexes + wholeWords) + .joinToString("|") + .toRegex() } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt index 8466d9b4..0878c0cd 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt @@ -1,4 +1,4 @@ package dev.usbharu.hideout.core.domain.model.filter -class FilterResult(val filter: Filter, val matchedKeyword: FilterKeywordKeyword) { +class FilterResult(val filter: Filter, val matchedKeyword: String) { } \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt index 47812ae6..3e685747 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt @@ -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.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.post.Post +import org.springframework.stereotype.Service interface IFilterDomainService { fun apply(post: Post, context: FilterContext, filters: List): FilteredPost fun applyAll(postList: List, context: FilterContext, filters: List): List } +@Service class FilterDomainService : IFilterDomainService { override fun apply(post: Post, context: FilterContext, filters: List): 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, context: FilterContext, filters: List): List { - 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) } } } \ No newline at end of file