diff --git a/Riot/Modules/PublicRoomList/DataSources/PublicRoomsDirectoryDataSource.m b/Riot/Modules/PublicRoomList/DataSources/PublicRoomsDirectoryDataSource.m index 257f68d2e..5ae737549 100644 --- a/Riot/Modules/PublicRoomList/DataSources/PublicRoomsDirectoryDataSource.m +++ b/Riot/Modules/PublicRoomList/DataSources/PublicRoomsDirectoryDataSource.m @@ -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 *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*)filterPublicRooms:(NSArray*)publicRooms containingKeyword:(NSString*)keyword +- (NSArray*)filterPublicRooms:(NSArray*)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]; } } diff --git a/Riot/SupportingFiles/forbidden_terms.txt b/Riot/SupportingFiles/forbidden_terms.txt new file mode 100644 index 000000000..ff22aa40f --- /dev/null +++ b/Riot/SupportingFiles/forbidden_terms.txt @@ -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 \ No newline at end of file