Filter NSFW content in room directory (#10196)
* add SpotlightSearch.showNsfwPublicRooms setting * use setting in publicroomsearch * add nsfw keyword filter and setting for room directory * unit tests * remove assertions
This commit is contained in:
parent
03e4b96037
commit
168f6df7c8
5 changed files with 86 additions and 2 deletions
|
@ -25,6 +25,7 @@ import SdkConfig from "../SdkConfig";
|
|||
import SettingsStore from "../settings/SettingsStore";
|
||||
import { Protocols } from "../utils/DirectoryUtils";
|
||||
import { useLatestResult } from "./useLatestResult";
|
||||
import { useSettingValue } from "./useSettings";
|
||||
|
||||
export const ALL_ROOMS = "ALL_ROOMS";
|
||||
const LAST_SERVER_KEY = "mx_last_room_directory_server";
|
||||
|
@ -38,6 +39,10 @@ export interface IPublicRoomsOpts {
|
|||
|
||||
let thirdParty: Protocols;
|
||||
|
||||
const NSFW_KEYWORD = "nsfw";
|
||||
const cheapNsfwFilter = (room: IPublicRoomsChunkRoom): boolean =>
|
||||
!room.name?.toLocaleLowerCase().includes(NSFW_KEYWORD) && !room.topic?.toLocaleLowerCase().includes(NSFW_KEYWORD);
|
||||
|
||||
export const usePublicRoomDirectory = (): {
|
||||
ready: boolean;
|
||||
loading: boolean;
|
||||
|
@ -58,6 +63,8 @@ export const usePublicRoomDirectory = (): {
|
|||
|
||||
const [updateQuery, updateResult] = useLatestResult<IRoomDirectoryOptions, IPublicRoomsChunkRoom[]>(setPublicRooms);
|
||||
|
||||
const showNsfwPublicRooms = useSettingValue<boolean>("SpotlightSearch.showNsfwPublicRooms");
|
||||
|
||||
async function initProtocols(): Promise<void> {
|
||||
if (!MatrixClientPeg.get()) {
|
||||
// We may not have a client yet when invoked from welcome page
|
||||
|
@ -108,7 +115,7 @@ export const usePublicRoomDirectory = (): {
|
|||
try {
|
||||
setLoading(true);
|
||||
const { chunk } = await MatrixClientPeg.get().publicRooms(opts);
|
||||
updateResult(opts, chunk);
|
||||
updateResult(opts, showNsfwPublicRooms ? chunk : chunk.filter(cheapNsfwFilter));
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error("Could not fetch public rooms for params", opts, e);
|
||||
|
@ -118,7 +125,7 @@ export const usePublicRoomDirectory = (): {
|
|||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[config, updateQuery, updateResult],
|
||||
[config, updateQuery, updateResult, showNsfwPublicRooms],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue