mirror of https://github.com/usbharu/Hideout.git
wip
This commit is contained in:
parent
1245165516
commit
70a1a17e37
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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: FilterKeywordKeyword) {
|
||||||
|
}
|
|
@ -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,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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue