This commit is contained in:
usbharu 2024-06-10 00:12:01 +09:00
parent 1245165516
commit 70a1a17e37
13 changed files with 143 additions and 0 deletions

View File

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

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: FilterKeywordKeyword) {
}

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,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<Filter>): FilteredPost
fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost>
}
class FilterDomainService : IFilterDomainService {
override fun apply(post: Post, context: FilterContext, filters: List<Filter>): FilteredPost {
filters.filter { it.filterContext.contains(context) }
}
override fun applyAll(postList: List<Post>, context: FilterContext, filters: List<Filter>): List<FilteredPost> {
TODO("Not yet implemented")
}
}