mirror of
https://github.com/element-hq/element-android
synced 2024-10-25 12:17:25 +03:00
Prevent searching for forbidden terms
This commit is contained in:
parent
1715143b85
commit
153d393bf1
3 changed files with 28 additions and 1 deletions
|
@ -115,4 +115,14 @@ class ExplicitTermFilterTest : InstrumentedTest {
|
||||||
fun isValidLastFalse() {
|
fun isValidLastFalse() {
|
||||||
explicitTermFilter.isValid("zoo") shouldBe false
|
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
|
assetReader: AssetReader
|
||||||
) {
|
) {
|
||||||
// List of forbidden terms is in file asset forbidden_terms.txt, in lower case
|
// 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()
|
.orEmpty()
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map { it.trim() }
|
.map { it.trim() }
|
||||||
.filter { it.isNotEmpty() }
|
.filter { it.isNotEmpty() }
|
||||||
|
|
||||||
|
private val explicitContentRegex = explicitTerms
|
||||||
.joinToString(prefix = ".*\\b(", separator = "|", postfix = ")\\b.*")
|
.joinToString(prefix = ".*\\b(", separator = "|", postfix = ")\\b.*")
|
||||||
.toRegex(RegexOption.IGNORE_CASE)
|
.toRegex(RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
|
fun canSearchFor(term: String): Boolean {
|
||||||
|
return term !in explicitTerms && term != "18+"
|
||||||
|
}
|
||||||
|
|
||||||
fun isValid(str: String): Boolean {
|
fun isValid(str: String): Boolean {
|
||||||
return explicitContentRegex.matches(str.replace("\n", " ")).not()
|
return explicitContentRegex.matches(str.replace("\n", " ")).not()
|
||||||
// Special treatment for "18+" since word boundaries does not work here
|
// 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) {
|
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 {
|
currentJob = viewModelScope.launch {
|
||||||
val data = try {
|
val data = try {
|
||||||
session.getPublicRooms(roomDirectoryData.homeServer,
|
session.getPublicRooms(roomDirectoryData.homeServer,
|
||||||
|
|
Loading…
Reference in a new issue