Skip to content

Commit

Permalink
Merge pull request #7803 from element-hq/stefan/decency
Browse files Browse the repository at this point in the history
Better explicit term room directory search filtering
  • Loading branch information
stefanceriu authored Jun 12, 2024
2 parents 0a0c866 + ae271f2 commit 7e71a84
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 65 deletions.
1 change: 0 additions & 1 deletion Config/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ final class BuildSettings: NSObject {
static let settingsScreenShowChangePassword:Bool = true
static let settingsScreenShowEnableStunServerFallback: Bool = true
static let settingsScreenShowNotificationDecodedContentOption: Bool = true
static let settingsScreenShowNsfwRoomsOption: Bool = true
static let settingsSecurityScreenShowSessions:Bool = true
static let settingsSecurityScreenShowSetupBackup:Bool = true
static let settingsSecurityScreenShowRestoreBackup:Bool = true
Expand Down
9 changes: 1 addition & 8 deletions Riot/Managers/Settings/RiotSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ final class RiotSettings: NSObject {
@UserDefault(key: UserDefaultsKeys.pinRoomsWithUnreadMessagesOnHome, defaultValue: false, storage: defaults)
var pinRoomsWithUnreadMessagesOnHome

/// Indicate to show Not Safe For Work public rooms.
@UserDefault(key: "showNSFWPublicRooms", defaultValue: false, storage: defaults)
var showNSFWPublicRooms

// MARK: User interface

@UserDefault<String?>(key: "userInterfaceTheme", defaultValue: nil, storage: defaults)
Expand Down Expand Up @@ -329,10 +325,7 @@ final class RiotSettings: NSObject {

@UserDefault(key: "settingsScreenShowNotificationDecodedContentOption", defaultValue: BuildSettings.settingsScreenShowNotificationDecodedContentOption, storage: defaults)
var settingsScreenShowNotificationDecodedContentOption

@UserDefault(key: "settingsScreenShowNsfwRoomsOption", defaultValue: BuildSettings.settingsScreenShowNsfwRoomsOption, storage: defaults)
var settingsScreenShowNsfwRoomsOption


@UserDefault(key: "settingsSecurityScreenShowSessions", defaultValue: BuildSettings.settingsSecurityScreenShowSessions, storage: defaults)
var settingsSecurityScreenShowSessions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ - (MXKSessionRecentsDataSource *)addMatrixSession:(MXSession *)mxSession
if (!_publicRoomsDirectoryDataSource)
{
_publicRoomsDirectoryDataSource = [[PublicRoomsDirectoryDataSource alloc] initWithMatrixSession:mxSession];
_publicRoomsDirectoryDataSource.showNSFWRooms = RiotSettings.shared.showNSFWPublicRooms;
_publicRoomsDirectoryDataSource.delegate = self;
}

Expand Down
2 changes: 0 additions & 2 deletions Riot/Modules/GlobalSearch/UnifiedSearchViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ - (void)viewWillAppear:(BOOL)animated

// Reset searches
[recentsDataSource searchWithPatterns:nil];
// TODO: Notify RiotSettings.shared.showNSFWPublicRooms change for iPad as viewWillAppear may not be called
recentsDataSource.publicRoomsDirectoryDataSource.showNSFWRooms = RiotSettings.shared.showNSFWPublicRooms;

[self updateSearch];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
*/
@property (nonatomic) BOOL includeAllNetworks;

/**
Flag to indicate to show Not Safe For Work rooms in the public room list.
*/
@property (nonatomic) BOOL showNSFWRooms;

/**
List public rooms from a third party protocol.
Default is nil.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ @interface PublicRoomsDirectoryDataSource ()
NSString *nextBatch;
}

@property (nonatomic, strong) NSRegularExpression *forbiddenTermsRegex;

@end

@implementation PublicRoomsDirectoryDataSource
Expand All @@ -57,6 +59,15 @@ - (instancetype)init
{
rooms = [NSMutableArray array];
_paginationLimit = 20;

NSString *path = [[NSBundle mainBundle] pathForResource:@"forbidden_terms" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:path encoding: NSUTF8StringEncoding error:nil];
NSArray *forbiddenTerms = [fileContents componentsSeparatedByCharactersInSet: NSCharacterSet.whitespaceAndNewlineCharacterSet];

NSString *pattern = [NSString stringWithFormat:@"\\b(%@)\\b", [forbiddenTerms componentsJoinedByString:@"|"]];
pattern = [pattern stringByAppendingString:@"|(\\b18\\+)"]; // Special case "18+"

_forbiddenTermsRegex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
}
return self;
}
Expand Down Expand Up @@ -155,16 +166,6 @@ - (void)setSearchPattern:(NSString *)searchPattern
}
}

- (void)setShowNSFWRooms:(BOOL)showNSFWRooms
{
if (showNSFWRooms != _showNSFWRooms)
{
_showNSFWRooms = showNSFWRooms;

[self resetPagination];
}
}

- (NSUInteger)roomsCount
{
return rooms.count;
Expand Down Expand Up @@ -254,14 +255,7 @@ - (MXHTTPOperation *)paginate:(void (^)(NSUInteger))complete failure:(void (^)(N

NSArray<MXPublicRoom*> *publicRooms;

if (self.showNSFWRooms)
{
publicRooms = publicRoomsResponse.chunk;
}
else
{
publicRooms = [self filterPublicRooms:publicRoomsResponse.chunk containingKeyword:kNSFWKeyword];
}
publicRooms = [self filterPublicRooms:publicRoomsResponse.chunk];

[self->rooms addObjectsFromArray:publicRooms];
self->nextBatch = publicRoomsResponse.nextBatch;
Expand Down Expand Up @@ -338,15 +332,23 @@ - (void)setState:(MXKDataSourceState)newState
}
}

- (NSArray<MXPublicRoom*>*)filterPublicRooms:(NSArray<MXPublicRoom*>*)publicRooms containingKeyword:(NSString*)keyword
- (NSArray<MXPublicRoom*>*)filterPublicRooms:(NSArray<MXPublicRoom*>*)publicRooms
{
NSMutableArray *filteredRooms = [NSMutableArray new];

for (MXPublicRoom *publicRoom in publicRooms)
{
if (NO == [[publicRoom.name lowercaseString] containsString:keyword]
&& NO == [[publicRoom.topic lowercaseString] containsString:keyword])
{
BOOL shouldAllow = YES;

if (publicRoom.name != nil) {
shouldAllow &= [self.forbiddenTermsRegex numberOfMatchesInString:publicRoom.name options:0 range:NSMakeRange(0, publicRoom.name.length)] == 0;
}

if (publicRoom.topic != nil) {
shouldAllow &= [self.forbiddenTermsRegex numberOfMatchesInString:publicRoom.topic options:0 range:NSMakeRange(0, publicRoom.topic.length)] == 0;
}

if (shouldAllow) {
[filteredRooms addObject:publicRoom];
}
}
Expand Down
28 changes: 2 additions & 26 deletions Riot/Modules/Settings/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ typedef NS_ENUM(NSUInteger, PRESENCE)

typedef NS_ENUM(NSUInteger, ADVANCED)
{
ADVANCED_SHOW_NSFW_ROOMS_INDEX = 0,
ADVANCED_CRASH_REPORT_INDEX,
ADVANCED_CRASH_REPORT_INDEX = 0,
ADVANCED_ENABLE_RAGESHAKE_INDEX,
ADVANCED_MARK_ALL_AS_READ_INDEX,
ADVANCED_CLEAR_CACHE_INDEX,
Expand Down Expand Up @@ -567,11 +566,6 @@ - (void)updateSections
Section *sectionAdvanced = [Section sectionWithTag:SECTION_TAG_ADVANCED];
sectionAdvanced.headerTitle = [VectorL10n settingsAdvanced];

if (RiotSettings.shared.settingsScreenShowNsfwRoomsOption)
{
[sectionAdvanced addRowWithTag:ADVANCED_SHOW_NSFW_ROOMS_INDEX];
}

if (BuildSettings.settingsScreenAllowChangingCrashUsageDataSettings)
{
[sectionAdvanced addRowWithTag:ADVANCED_CRASH_REPORT_INDEX];
Expand Down Expand Up @@ -2372,20 +2366,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
else if (section == SECTION_TAG_ADVANCED)
{
if (row == ADVANCED_SHOW_NSFW_ROOMS_INDEX)
{
MXKTableViewCellWithLabelAndSwitch* labelAndSwitchCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath];

labelAndSwitchCell.mxkLabel.text = [VectorL10n settingsShowNSFWPublicRooms];

labelAndSwitchCell.mxkSwitch.on = RiotSettings.shared.showNSFWPublicRooms;
labelAndSwitchCell.mxkSwitch.onTintColor = ThemeService.shared.theme.tintColor;
labelAndSwitchCell.mxkSwitch.enabled = YES;
[labelAndSwitchCell.mxkSwitch addTarget:self action:@selector(toggleNSFWPublicRoomsFiltering:) forControlEvents:UIControlEventTouchUpInside];

cell = labelAndSwitchCell;
}
else if (row == ADVANCED_CRASH_REPORT_INDEX)
if (row == ADVANCED_CRASH_REPORT_INDEX)
{
MXKTableViewCellWithLabelAndSwitch* sendCrashReportCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath];

Expand Down Expand Up @@ -4082,11 +4063,6 @@ - (void)togglePresenceOfflineMode:(UISwitch *)sender
}
}

- (void)toggleNSFWPublicRoomsFiltering:(UISwitch *)sender
{
RiotSettings.shared.showNSFWPublicRooms = sender.isOn;
}

- (void)toggleEnableRoomMessageBubbles:(UISwitch *)sender
{
RiotSettings.shared.roomScreenEnableMessageBubbles = sender.isOn;
Expand Down
68 changes: 68 additions & 0 deletions Riot/SupportingFiles/forbidden_terms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
anal
bbw
bdsm
beast
bestiality
blowjob
bondage
boobs
clit
cock
cuck
cum
cunt
daddy
dick
dildo
erotic
exhibitionism
faggot
femboy
fisting
flogging
fmf
foursome
futa
gangbang
gore
h3ntai
handjob
hentai
incest
jizz
kink
loli
m4f
masturbate
masturbation
mfm
milf
moresome
naked
neet
nsfw
nude
nudity
orgy
pedo
pegging
penis
petplay
porn
pussy
rape
rimming
sadism
sadomasochism
sexy
shota
spank
squirt
strap-on
threesome
vagina
vibrator
voyeur
watersports
xxx
zoo

0 comments on commit 7e71a84

Please sign in to comment.