mirror of https://github.com/usbharu/Hideout.git
Merge remote-tracking branch 'origin/ddd' into ddd
This commit is contained in:
commit
74e10672bd
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
enum class FilterAction {
|
||||||
|
warn,
|
||||||
|
hide
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
enum class FilterContext {
|
||||||
|
home,
|
||||||
|
notifications,
|
||||||
|
public,
|
||||||
|
thread,
|
||||||
|
account
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class FilterId(val id: Long)
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class FilterKeywordId(val id: Long)
|
|
@ -0,0 +1,4 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class FilterKeywordKeyword(val keyword: String)
|
|
@ -0,0 +1,7 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
enum class FilterMode {
|
||||||
|
WHOLE_WORD,
|
||||||
|
REGEX,
|
||||||
|
NONE
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class FilterName(val name: String)
|
|
@ -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)
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.filter
|
||||||
|
|
||||||
|
class FilterResult(val filter: Filter, val matchedKeyword: String) {
|
||||||
|
}
|
|
@ -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>)
|
|
@ -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) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue