1
0
Fork 1

Detect NSFW keywords

pull/38/head
gravel 3 years ago
parent b17ba166c9
commit 98d95efe26
Signed by: gravel
SSH Key Fingerprint: SHA256:p4HP49CCk4YQMkJpWJ09L8peEPQWjERtdCRAFxPfbOY

@ -80,6 +80,10 @@ html:not(.js) .js-only {
background-color: yellow;
}
.room-label-warning {
background-color: pink;
}
header {
display: flex;
direction: row;

@ -269,16 +269,31 @@
/**
* @param string[] $tags
*/
function add_tags(array $tags) {
public function add_tags(array $tags) {
$this->tags = [...$this->tags, ...$tags];
}
private function has_nsfw_keywords(): bool {
// Description not included due to false positives.
$blob =
strtolower($this->name) . " " .
strtolower(join(" ", $this->tags));
foreach (CommunityTag::NSFW_KEYWORDS as $keyword) {
if (str_contains($blob, $keyword)) {
return true;
}
}
return false;
}
/**
* Return the tags associated with this room.
* @return \CommunityTag[] Tags as string array.
*/
function get_room_tags(): array {
$user_tags = CommunityTag::from_user_tags($this->tags);
$user_tags = CommunityTag::from_user_tags($this->tags, remove_redundant: true);
/**
* @var \CommunityTag[] $derived_tags
@ -289,6 +304,10 @@
$derived_tags[] = new CommunityTag("official", TagType::RESERVED_TAG);
}
if ($this->has_nsfw_keywords()) {
$derived_tags[] = new CommunityTag("nsfw", TagType::WARNING_TAG);
}
return [...$derived_tags, ...$user_tags];
}
}

@ -71,9 +71,12 @@
/**
* Returns the user tags given, without any reserved tags.
* @param string[] $tags
* @param bool $remove_redundant Removes duplicate and obvious tags.
* @return \CommunityTag[]
*/
public static function from_user_tags(array $tags): array {
public static function from_user_tags(
array $tags, bool $remove_redundant = false
): array {
$tags_user = array_filter(
$tags,
function($tag) {
@ -81,7 +84,17 @@
}
);
return CommunityTag::from_tag_array($tags_user);
$tags_built = CommunityTag::from_tag_array($tags_user);
if ($remove_redundant) {
$tags_built = CommunityTag::dedupe_tags($tags_built);
$tags_built = array_filter($tags_built, function(\CommunityTag $tag) {
$text = strtolower($tag->text);
return !in_array($text, CommunityTag::REDUNDANT_TAGS);
});
}
return $tags_built;
}
/**
@ -111,8 +124,12 @@
* @var string[] RESERVED_TAGS
* Array of derived tags unavailable for manual tagging.
*/
private const RESERVED_TAGS = ["official"];
private const RESERVED_TAGS = ["official", "nsfw"];
private const REDUNDANT_TAGS = ["session"];
public const NSFW_KEYWORDS = ["nsfw", "porn", "erotic", "18+"];
/**
* Checks whether the given manual tag can be accepted.
*/