From c0c8eec36bdb9240fb42a44499f89a2ab8d8ede0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9lanie=20Chauvel?= Date: Tue, 4 Jan 2022 19:28:49 +0100 Subject: [PATCH] filter messages by also looking at media descriptions (#2285) --- .../tusky/network/FilterModel.kt | 9 ++++- .../com/keylesspalace/tusky/FilterTest.kt | 34 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/network/FilterModel.kt b/app/src/main/java/com/keylesspalace/tusky/network/FilterModel.kt index d4c7464f1..062191ad0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/FilterModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/FilterModel.kt @@ -29,9 +29,16 @@ class FilterModel @Inject constructor() { } val spoilerText = status.actionableStatus.spoilerText + val attachmentsDescriptions = status.attachments + .mapNotNull { it.description } + return ( matcher.reset(status.actionableStatus.content).find() || - spoilerText.isNotEmpty() && matcher.reset(spoilerText).find() + (spoilerText.isNotEmpty() && matcher.reset(spoilerText).find()) || + ( + attachmentsDescriptions.isNotEmpty() && + matcher.reset(attachmentsDescriptions.joinToString("\n")).find() + ) ) } diff --git a/app/src/test/java/com/keylesspalace/tusky/FilterTest.kt b/app/src/test/java/com/keylesspalace/tusky/FilterTest.kt index a8a5aa682..a48ff1219 100644 --- a/app/src/test/java/com/keylesspalace/tusky/FilterTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/FilterTest.kt @@ -2,6 +2,7 @@ package com.keylesspalace.tusky import android.text.SpannedString import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.keylesspalace.tusky.entity.Attachment import com.keylesspalace.tusky.entity.Filter import com.keylesspalace.tusky.entity.Poll import com.keylesspalace.tusky.entity.PollOption @@ -14,6 +15,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.Config +import java.util.ArrayList import java.util.Date @Config(sdk = [28]) @@ -125,6 +127,19 @@ class FilterTest { ) } + @Test + fun shouldFilter_whenMediaDescriptionDoesMatch() { + assertTrue( + filterModel.shouldFilterStatus( + mockStatus( + content = "should not be filtered", + spoilerText = "should not be filtered", + attachmentsDescriptions = listOf("should not be filtered", "badWord"), + ) + ) + ) + } + @Test fun shouldFilterPartialWord_whenWholeWordFilterContainsNonAlphanumericCharacters() { assertTrue( @@ -137,7 +152,8 @@ class FilterTest { private fun mockStatus( content: String = "", spoilerText: String = "", - pollOptions: List? = null + pollOptions: List? = null, + attachmentsDescriptions: List? = null ): Status { return Status( id = "123", @@ -157,7 +173,21 @@ class FilterTest { sensitive = false, spoilerText = spoilerText, visibility = Status.Visibility.PUBLIC, - attachments = arrayListOf(), + attachments = if (attachmentsDescriptions != null) { + ArrayList( + attachmentsDescriptions.map { + Attachment( + id = "1234", + url = "", + previewUrl = null, + meta = null, + type = Attachment.Type.IMAGE, + description = it, + blurhash = null + ) + } + ) + } else arrayListOf(), mentions = listOf(), application = null, pinned = false,