Merge remote-tracking branch 'origin/ddd' into ddd

This commit is contained in:
usbharu 2024-06-13 15:14:15 +09:00
commit 74e10672bd
13 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,53 @@
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<FilterContext>,
val filterAction: FilterAction,
filterKeywords: Set<FilterKeyword>
) {
var filterKeywords = filterKeywords
private set
fun setFilterKeywords(filterKeywords: Set<FilterKeyword>, user: UserDetail) {
require(isAllow(user, SET_KEYWORDS, this))
this.filterKeywords = filterKeywords
}
fun compileFilter(): Regex {
val words = mutableListOf<String>()
val wholeWords = mutableListOf<String>()
val regexes = mutableListOf<String>()
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 (wholeWords + regexes + wholeWords)
.joinToString("|")
.toRegex()
}
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
}
}
}

View File

@ -0,0 +1,6 @@
package dev.usbharu.hideout.core.domain.model.filter
enum class FilterAction {
warn,
hide
}

View File

@ -0,0 +1,9 @@
package dev.usbharu.hideout.core.domain.model.filter
enum class FilterContext {
home,
notifications,
public,
thread,
account
}

View File

@ -0,0 +1,4 @@
package dev.usbharu.hideout.core.domain.model.filter
@JvmInline
value class FilterId(val id: Long)

View File

@ -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()
}
}
}

View File

@ -0,0 +1,4 @@
package dev.usbharu.hideout.core.domain.model.filter
@JvmInline
value class FilterKeywordId(val id: Long)

View File

@ -0,0 +1,4 @@
package dev.usbharu.hideout.core.domain.model.filter
@JvmInline
value class FilterKeywordKeyword(val keyword: String)

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.core.domain.model.filter
enum class FilterMode {
WHOLE_WORD,
REGEX,
NONE
}

View File

@ -0,0 +1,4 @@
package dev.usbharu.hideout.core.domain.model.filter
@JvmInline
value class FilterName(val name: String)

View File

@ -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)
}

View File

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

View File

@ -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<FilterResult>)

View File

@ -0,0 +1,69 @@
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<Filter>): FilteredPost
fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost>
}
@Service
class FilterDomainService : IFilterDomainService {
override fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost {
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> {
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) }
}
}