mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-07 23:58:40 +01:00
Prevent searching for forbidden terms
This commit is contained in:
parent
1715143b85
commit
153d393bf1
@ -115,4 +115,14 @@ class ExplicitTermFilterTest : InstrumentedTest {
|
||||
fun isValidLastFalse() {
|
||||
explicitTermFilter.isValid("zoo") shouldBe false
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canSearchForFalse() {
|
||||
explicitTermFilter.canSearchFor("zoo") shouldBe false
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canSearchForTrue() {
|
||||
explicitTermFilter.canSearchFor("android") shouldBe true
|
||||
}
|
||||
}
|
@ -23,14 +23,20 @@ class ExplicitTermFilter @Inject constructor(
|
||||
assetReader: AssetReader
|
||||
) {
|
||||
// List of forbidden terms is in file asset forbidden_terms.txt, in lower case
|
||||
private val explicitContentRegex = assetReader.readAssetFile("forbidden_terms.txt")
|
||||
private val explicitTerms = assetReader.readAssetFile("forbidden_terms.txt")
|
||||
.orEmpty()
|
||||
.split("\n")
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
|
||||
private val explicitContentRegex = explicitTerms
|
||||
.joinToString(prefix = ".*\\b(", separator = "|", postfix = ")\\b.*")
|
||||
.toRegex(RegexOption.IGNORE_CASE)
|
||||
|
||||
fun canSearchFor(term: String): Boolean {
|
||||
return term !in explicitTerms && term != "18+"
|
||||
}
|
||||
|
||||
fun isValid(str: String): Boolean {
|
||||
return explicitContentRegex.matches(str.replace("\n", " ")).not()
|
||||
// Special treatment for "18+" since word boundaries does not work here
|
||||
|
@ -161,6 +161,17 @@ class RoomDirectoryViewModel @AssistedInject constructor(
|
||||
}
|
||||
|
||||
private fun load(filter: String, roomDirectoryData: RoomDirectoryData) {
|
||||
if (!showAllRooms && !explicitTermFilter.canSearchFor(filter)) {
|
||||
setState {
|
||||
copy(
|
||||
asyncPublicRoomsRequest = Success(Unit),
|
||||
publicRooms = emptyList(),
|
||||
hasMore = false
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
currentJob = viewModelScope.launch {
|
||||
val data = try {
|
||||
session.getPublicRooms(roomDirectoryData.homeServer,
|
||||
|
Loading…
x
Reference in New Issue
Block a user