Filter out rooms with "nsfw" in room name or room topic (enabled by default)

This commit is contained in:
Benoit Marty 2021-03-08 13:53:04 +01:00
parent 043ddcae2c
commit 85b1edc6d2
4 changed files with 48 additions and 6 deletions

View File

@ -28,6 +28,7 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.settings.VectorPreferences
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
@ -41,12 +42,13 @@ import org.matrix.android.sdk.api.session.room.model.thirdparty.RoomDirectoryDat
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.rx.rx
import timber.log.Timber
import java.util.Locale
private const val PUBLIC_ROOMS_LIMIT = 20
class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: PublicRoomsViewState,
private val session: Session)
: VectorViewModel<PublicRoomsViewState, RoomDirectoryAction, RoomDirectoryViewEvents>(initialState) {
class RoomDirectoryViewModel @AssistedInject constructor(
@Assisted initialState: PublicRoomsViewState,
vectorPreferences: VectorPreferences,
private val session: Session
) : VectorViewModel<PublicRoomsViewState, RoomDirectoryAction, RoomDirectoryViewEvents>(initialState) {
@AssistedFactory
interface Factory {
@ -54,6 +56,12 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState:
}
companion object : MvRxViewModelFactory<RoomDirectoryViewModel, PublicRoomsViewState> {
private const val PUBLIC_ROOMS_LIMIT = 20
// List of forbidden terms, in lower case
private val explicitContentTerms = listOf(
"nsfw"
)
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: PublicRoomsViewState): RoomDirectoryViewModel? {
@ -62,6 +70,8 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState:
}
}
private val showAllRooms = vectorPreferences.showAllPublicRooms()
private var since: String? = null
private var currentJob: Job? = null
@ -189,11 +199,21 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState:
since = data.nextBatch
// Filter
val newPublicRooms = data.chunk.orEmpty()
.filter {
showAllRooms
|| "${it.name} ${it.topic}".toLowerCase(Locale.ROOT)
.let { str ->
explicitContentTerms.all { term -> term !in str }
}
}
setState {
copy(
asyncPublicRoomsRequest = Success(Unit),
// It's ok to append at the end of the list, so I use publicRooms.size()
publicRooms = publicRooms.appendAt(data.chunk!!, publicRooms.size)
publicRooms = publicRooms.appendAt(newPublicRooms, publicRooms.size)
// Rageshake #8206 tells that we can have several times the same room
.distinctBy { it.roomId },
hasMore = since != null

View File

@ -100,6 +100,9 @@ class VectorPreferences @Inject constructor(private val context: Context) {
private const val SETTINGS_ENABLE_CHAT_EFFECTS = "SETTINGS_ENABLE_CHAT_EFFECTS"
private const val SETTINGS_SHOW_EMOJI_KEYBOARD = "SETTINGS_SHOW_EMOJI_KEYBOARD"
// Room directory
private const val SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS = "SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS"
// Help
private const val SETTINGS_SHOULD_SHOW_HELP_ON_ROOM_LIST_KEY = "SETTINGS_SHOULD_SHOW_HELP_ON_ROOM_LIST_KEY"
@ -399,6 +402,13 @@ class VectorPreferences @Inject constructor(private val context: Context) {
return defaultPrefs.getBoolean(SETTINGS_ENABLE_SEND_VOICE_FEATURE_PREFERENCE_KEY, false)
}
/**
* Show all rooms in room directory
*/
fun showAllPublicRooms(): Boolean {
return defaultPrefs.getBoolean(SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS, false)
}
/**
* Tells which compression level to use by default
*

View File

@ -497,6 +497,10 @@
<item quantity="other">%d users</item>
</plurals>
<string name="settings_category_room_directory">Room directory</string>
<string name="settings_room_directory_show_all_rooms">Show all rooms in the room directory</string>
<string name="settings_room_directory_show_all_rooms_summary">Show all rooms, including rooms with explicit content.</string>
<!-- Groups fragment -->
<string name="groups_invite_header">Invite</string>
<string name="groups_header">Communities</string>

View File

@ -177,4 +177,12 @@
</im.vector.app.core.preference.VectorPreferenceCategory>
<im.vector.app.core.preference.VectorPreferenceCategory android:title="@string/settings_category_room_directory">
<im.vector.app.core.preference.VectorSwitchPreference
android:key="SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS"
android:summary="@string/settings_room_directory_show_all_rooms_summary"
android:title="@string/settings_room_directory_show_all_rooms" />
</im.vector.app.core.preference.VectorPreferenceCategory>
</androidx.preference.PreferenceScreen>