From 70a1a17e372746a275a3d83b3965d57ea10be009 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 10 Jun 2024 00:12:01 +0900 Subject: [PATCH 1/2] wip --- .../core/domain/model/filter/Filter.kt | 51 +++++++++++++++++++ .../core/domain/model/filter/FilterAction.kt | 6 +++ .../core/domain/model/filter/FilterContext.kt | 9 ++++ .../core/domain/model/filter/FilterId.kt | 4 ++ .../core/domain/model/filter/FilterKeyword.kt | 17 +++++++ .../domain/model/filter/FilterKeywordId.kt | 4 ++ .../model/filter/FilterKeywordKeyword.kt | 4 ++ .../core/domain/model/filter/FilterMode.kt | 7 +++ .../core/domain/model/filter/FilterName.kt | 4 ++ .../domain/model/filter/FilterRepository.kt | 6 +++ .../core/domain/model/filter/FilterResult.kt | 4 ++ .../core/domain/model/filter/FilteredPost.kt | 5 ++ .../service/filter/FilterDomainService.kt | 22 ++++++++ 13 files changed, 143 insertions(+) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterAction.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterContext.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordId.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordKeyword.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterMode.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterRepository.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt 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 new file mode 100644 index 00000000..f1ecdd1a --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt @@ -0,0 +1,51 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import dev.usbharu.hideout.core.domain.model.filter.Filter.Companion.Action.SET_KEYWORDS +import dev.usbharu.hideout.core.domain.model.filter.FilterMode.* +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId + +class Filter( + val id: FilterId, + val userDetailId: UserDetailId, + var name: FilterName, + val filterContext: List, + val filterAction: FilterAction, + filterKeywords: Set +) { + var filterKeywords = filterKeywords + private set + + fun setFilterKeywords(filterKeywords: Set, user: UserDetail) { + require(isAllow(user, SET_KEYWORDS, this)) + this.filterKeywords = filterKeywords + } + + fun compileFilter(): Regex { + val words = mutableListOf() + val wholeWords = mutableListOf() + val regexes = mutableListOf() + + for (filterKeyword in filterKeywords) { + when (filterKeyword.mode) { + WHOLE_WORD -> wholeWords.add(filterKeyword.keyword.keyword) + REGEX -> regexes.add(filterKeyword.keyword.keyword) + NONE -> words.add(filterKeyword.keyword.keyword) + } + } + + return Regex("") + } + + companion object { + fun isAllow(user: UserDetail, action: Action, resource: Filter): Boolean { + return when (action) { + SET_KEYWORDS -> resource.userDetailId == user.id + } + } + + enum class Action { + SET_KEYWORDS + } + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterAction.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterAction.kt new file mode 100644 index 00000000..ac3e8b88 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterAction.kt @@ -0,0 +1,6 @@ +package dev.usbharu.hideout.core.domain.model.filter + +enum class FilterAction { + warn, + hide +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterContext.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterContext.kt new file mode 100644 index 00000000..418f9573 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterContext.kt @@ -0,0 +1,9 @@ +package dev.usbharu.hideout.core.domain.model.filter + +enum class FilterContext { + home, + notifications, + public, + thread, + account +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt new file mode 100644 index 00000000..ce42a4c4 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt @@ -0,0 +1,4 @@ +package dev.usbharu.hideout.core.domain.model.filter + +@JvmInline +value class FilterId(val id: Long) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt new file mode 100644 index 00000000..5565c8e9 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt @@ -0,0 +1,17 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import dev.usbharu.hideout.core.domain.model.filter.FilterMode.* + +class FilterKeyword( + val id: FilterKeywordId, + var keyword: FilterKeywordKeyword, + val mode: FilterMode +) { + fun match(string: String): Boolean { + when (mode) { + WHOLE_WORD -> TODO() + REGEX -> TODO() + NONE -> TODO() + } + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordId.kt new file mode 100644 index 00000000..4f1b2df5 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordId.kt @@ -0,0 +1,4 @@ +package dev.usbharu.hideout.core.domain.model.filter + +@JvmInline +value class FilterKeywordId(val id: Long) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordKeyword.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordKeyword.kt new file mode 100644 index 00000000..89e959b3 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeywordKeyword.kt @@ -0,0 +1,4 @@ +package dev.usbharu.hideout.core.domain.model.filter + +@JvmInline +value class FilterKeywordKeyword(val keyword: String) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterMode.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterMode.kt new file mode 100644 index 00000000..57e38fb7 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterMode.kt @@ -0,0 +1,7 @@ +package dev.usbharu.hideout.core.domain.model.filter + +enum class FilterMode { + WHOLE_WORD, + REGEX, + NONE +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt new file mode 100644 index 00000000..09e424de --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt @@ -0,0 +1,4 @@ +package dev.usbharu.hideout.core.domain.model.filter + +@JvmInline +value class FilterName(val name: String) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterRepository.kt new file mode 100644 index 00000000..17a4cacc --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterRepository.kt @@ -0,0 +1,6 @@ +package dev.usbharu.hideout.core.domain.model.filter + +interface FilterRepository { + suspend fun save(filter: Filter): Filter + suspend fun delete(filter: Filter) +} \ No newline at end of file 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 new file mode 100644 index 00000000..8466d9b4 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt @@ -0,0 +1,4 @@ +package dev.usbharu.hideout.core.domain.model.filter + +class FilterResult(val filter: Filter, val matchedKeyword: FilterKeywordKeyword) { +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt new file mode 100644 index 00000000..4081c651 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt @@ -0,0 +1,5 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import dev.usbharu.hideout.core.domain.model.post.Post + +class FilteredPost(val post: Post, val filterResults: List) \ 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 new file mode 100644 index 00000000..47812ae6 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainService.kt @@ -0,0 +1,22 @@ +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.FilteredPost +import dev.usbharu.hideout.core.domain.model.post.Post + +interface IFilterDomainService { + fun apply(post: Post, context: FilterContext, filters: List): FilteredPost + fun applyAll(postList: List, context: FilterContext, filters: List): List +} + +class FilterDomainService : IFilterDomainService { + override fun apply(post: Post, context: FilterContext, filters: List): FilteredPost { + filters.filter { it.filterContext.contains(context) } + } + + override fun applyAll(postList: List, context: FilterContext, filters: List): List { + TODO("Not yet implemented") + } + +} \ No newline at end of file From 57d3091b18c21f7e5221bfeb5f0d02742296e120 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 10 Jun 2024 14:37:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E3=83=95=E3=82=A3=E3=83=AB?= =?UTF-8?q?=E3=82=BF=E3=83=BC=E3=81=AE=E5=AE=9F=E8=A3=85=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/filter/Filter.kt | 4 +- .../core/domain/model/filter/FilterResult.kt | 2 +- .../service/filter/FilterDomainService.kt | 51 ++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) 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