From ee958cd3b90feef4ba4f08c1d53ef3b9ee8e06fe Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 22 Dec 2023 09:52:12 +0000 Subject: [PATCH] Fix tag fetching order --- php/servers/servers-rooms.php | 45 +++++++++++++++++++++-------------- php/servers/sources.php | 4 +++- php/servers/tags.php | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/php/servers/servers-rooms.php b/php/servers/servers-rooms.php index 6f2e99c..c511329 100644 --- a/php/servers/servers-rooms.php +++ b/php/servers/servers-rooms.php @@ -112,7 +112,7 @@ // Custom properties /** * @var string[] $string_tags - * String tags originally applied to room. + * String tags from external sources originally applied to room. */ private array $string_tags = []; @@ -120,9 +120,11 @@ * @var CommunityTag[] $tags * Tag-based information from multiple sources. * + * Not valid in fetching phase. + * * Custom attribute. */ - private array $tags = []; + private ?array $tags = null; /** * @var string $language_flag; @@ -157,10 +159,16 @@ if ($suppress_processing) return; $this->language_flag = $details['language_flag'] ?? $this->get_language_flag(); - $this->extract_tags_from_description(); - $this->tags = isset($details['tags']) - ? CommunityTag::from_details_array($details['tags']) - : $this->get_derived_tags(); + if (!isset($details['tags'])) { + // Fetching phase. + // String tags are added, object tags are on-demand only. + $this->tags = null; + $this->extract_tags_from_description(); + } else { + // Post fetching phase. + // Fetched & derived object tags are loaded and used. + $this->tags = CommunityTag::from_details_array($details['tags']); + } } /** @@ -240,6 +248,7 @@ public function jsonSerialize(): array { $details = get_object_vars($this); unset($details['server']); + $details['tags'] = $this->get_room_tags(); return $details; } @@ -418,21 +427,14 @@ * @param string[] $tags */ public function add_tags(array $tags) { - $new_string_tags = []; foreach ($tags as $tag) { if (str_contains($this->token, "mathnodes")) { log_debug("new tag: '$tag'"); } if (strlen(trim($tag)) == 0) continue; - $this->string_tags[] = $tag; if ($this->parse_language_tag($tag)) continue; - $new_string_tags[] = $tag; + $this->string_tags[] = $tag; } - $new_tags = CommunityTag::from_user_tags($new_string_tags); - $this->tags = CommunityTag::dedupe_tags([ - ...$this->tags, - ...$new_tags - ]); } /** @@ -637,6 +639,15 @@ return $this->string_tags; } + /** + * @return CommunityTag[] + */ + private function get_user_tags(): array { + return CommunityTag::dedupe_tags( + CommunityTag::from_user_tags($this->string_tags), + ); + } + /** * Return the derived tags associated with this room. * @return CommunityTag[] Array of tags. @@ -649,9 +660,7 @@ */ $derived_tags = []; - $USERS_PER_STAFF_WARNING = CommunityRoom::USERS_PER_STAFF_WARNING; - - if (in_array($this->get_room_identifier(), $ROOMS_USED_BY_PROJECT)) { + if ($this->matched_by_list($ROOMS_USED_BY_PROJECT)) { $derived_tags[] = ReservedTags::used_by_project(); } @@ -696,7 +705,7 @@ * @return CommunityTag[] Array of tags. */ function get_room_tags(): array { - return $this->tags; + return [...$this->get_derived_tags(), ...$this->get_user_tags()]; } } diff --git a/php/servers/sources.php b/php/servers/sources.php index 0195b81..e1f356c 100644 --- a/php/servers/sources.php +++ b/php/servers/sources.php @@ -106,7 +106,9 @@ $this->tags[$room_id] = explode(',', $tag_string); if ($nsfw) { - $this->tags[$room_id] = array_unique(["nsfw", ...$this->tags[$room_id]]); + $this->tags[$room_id] = array_values(array_unique([ + "nsfw", ...$this->tags[$room_id] + ])); } } diff --git a/php/servers/tags.php b/php/servers/tags.php index c4204c9..16fb985 100644 --- a/php/servers/tags.php +++ b/php/servers/tags.php @@ -249,7 +249,7 @@ private const HIGHLIGHTED_TAGS = ["new", "we're here", "pinned"]; - private const REDUNDANT_TAGS = ["session"]; + private const REDUNDANT_TAGS = []; public const NSFW_KEYWORDS = ["nsfw", "porn", "erotic", "18+", "sex"]; /**