Prevent searching for forbidden terms

This commit is contained in:
Benoit Marty 2021-04-08 17:28:47 +02:00
parent 1715143b85
commit 153d393bf1
3 changed files with 28 additions and 1 deletions

View file

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

View file

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

View file

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