diff --git a/output/styles2.css b/output/styles2.css index 2ab3d3cf..9a271d67 100644 --- a/output/styles2.css +++ b/output/styles2.css @@ -80,6 +80,10 @@ html:not(.js) .js-only { background-color: yellow; } +.room-label-warning { + background-color: pink; +} + header { display: flex; direction: row; diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index 04b586e6..f79ac4a3 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -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]; } } diff --git a/php/utils/tags.php b/php/utils/tags.php index 8ac34de4..b7ea3366 100644 --- a/php/utils/tags.php +++ b/php/utils/tags.php @@ -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. */