Adopt the EXA forbidden terms list when searching for public rooms

This commit is contained in:
Stefan Ceriu 2024-06-10 14:53:09 +03:00
parent 72d7ca79d7
commit ae271f202f
2 changed files with 92 additions and 5 deletions

View file

@ -46,6 +46,8 @@ static NSString *const kNSFWKeyword = @"nsfw";
NSString *nextBatch;
}
@property (nonatomic, strong) NSRegularExpression *forbiddenTermsRegex;
@end
@implementation PublicRoomsDirectoryDataSource
@ -57,6 +59,15 @@ static NSString *const kNSFWKeyword = @"nsfw";
{
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;
}
@ -244,7 +255,7 @@ static NSString *const kNSFWKeyword = @"nsfw";
NSArray<MXPublicRoom*> *publicRooms;
publicRooms = [self filterPublicRooms:publicRoomsResponse.chunk containingKeyword:kNSFWKeyword];
publicRooms = [self filterPublicRooms:publicRoomsResponse.chunk];
[self->rooms addObjectsFromArray:publicRooms];
self->nextBatch = publicRoomsResponse.nextBatch;
@ -321,15 +332,23 @@ static NSString *const kNSFWKeyword = @"nsfw";
}
}
- (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];
}
}

View file

@ -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