From 70a1a17e372746a275a3d83b3965d57ea10be009 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 10 Jun 2024 00:12:01 +0900 Subject: [PATCH] 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