From b2308e409596a7c4f57f4abb4085c6424de7546a Mon Sep 17 00:00:00 2001 From: gravel Date: Wed, 20 Dec 2023 19:40:37 +0000 Subject: [PATCH] feat: migrate to longer room id --- output/main.js | 2 +- php/generate-listings.php | 41 ++++---------------- php/servers/servers-rooms.php | 70 ++++++++++++++++++++++++++++------- php/utils/utils.php | 4 +- 4 files changed, 67 insertions(+), 50 deletions(-) diff --git a/output/main.js b/output/main.js index ef7dd7d..a517845 100644 --- a/output/main.js +++ b/output/main.js @@ -426,7 +426,7 @@ function hideBadCommunities() { * Removes a Community by its ID and returns the number of elements removed. */ function hideCommunity(communityID) { - const element = dom.community_row(communityID); + const element = dom.community_row(communityID, true); element?.remove(); return element ? 1 : 0; } diff --git a/php/generate-listings.php b/php/generate-listings.php index c608883..a505a9f 100644 --- a/php/generate-listings.php +++ b/php/generate-listings.php @@ -87,42 +87,15 @@ $servers = CommunityServer::from_details_array($server_data); $rooms_all = CommunityServer::enumerate_rooms($servers); - $rooms_by_id = []; - foreach ($rooms_all as $room) { - $rooms_by_id[$room->get_room_identifier()] = $room; - } - - $sogs_by_pubkey = []; - foreach ($servers as $server) { - $sogs_by_pubkey[$server->get_pubkey()] = $server; - } - $listings = []; foreach ($listings_raw as $id => $listing_props) { - $rooms = []; - // TODO: Blocklist option - if (isset($listing_props['rooms'])) { - foreach ($listing_props['rooms'] as $room_id) { - if ($room_id == '*') { - $rooms = $rooms_all; - break; - } - if (isset($rooms_by_id[$room_id])) { - $rooms[] = $rooms_by_id[$room_id]; - } else { - log_warning("Could not find room $room_id from listing $id."); - } - } - } - if (isset($listing_props['sogs'])) { - foreach ($listing_props['sogs'] as $public_key) { - if (isset($sogs_by_pubkey[$public_key])) { - /** @var CommunityServer $sogs */ - $sogs = $sogs_by_pubkey[$public_key]; - array_push($rooms, ...$sogs->rooms); - } else { - log_warning("Could not find sogs $public_key from listing $id."); - } + $filter = [...$listing_props['rooms'], ...$listing_props['sogs']]; + $matchees = []; + $rooms = CommunityRoom::select_rooms($rooms_all, $filter, $matchees); + + foreach ($filter as $filter_item) { + if (!in_array($filter_item, $matchees)) { + log_warning("Could not find $filter_item from listing $id."); } } diff --git a/php/servers/servers-rooms.php b/php/servers/servers-rooms.php index 7712c55..3d0eb7a 100644 --- a/php/servers/servers-rooms.php +++ b/php/servers/servers-rooms.php @@ -184,14 +184,12 @@ return $this->language_flag; } - $room_identifier = $this->get_room_identifier(); - $server_pubkey = $this->server->get_pubkey(); - - if (isset($languages[$room_identifier])) { - return $languages[$room_identifier]; - } elseif (isset($languages[$server_pubkey])) { - return $languages[$server_pubkey]; + foreach ($languages as $key => $flag) { + if ($this->matched_by_identifier($key)) { + return $flag; + } } + return ""; } @@ -397,12 +395,12 @@ /** * Return a globally unique room identifier. - * @return string String in the form `token+pubkey[:4]`. + * @return string String in the form `token+hex[8]`. */ function get_room_identifier(): string { $token = $this->token; - $pubkey_4 = substr($this->server->get_pubkey(), 0, 4); - return "$token+$pubkey_4"; + $server_id = $this->server->get_server_id(); + return "$token+$server_id"; } /** @@ -468,16 +466,60 @@ return true; } + /** + * Check whether the given identifier matches the current Community or its parent server. + * @param string $identifier Server pubkey, server ID, server hostname or room ID prefix. + * @return bool True if the string matches the Community, false otherwise. + */ + public function matched_by_identifier(string $identifier): bool { + if ($identifier == "*" || + $identifier == $this->server->get_pubkey() || + $identifier == $this->server->get_hostname() || + $identifier == $this->server->get_server_id()) { + return true; + } + + // Legacy identifier check + return ( + str_starts_with($this->get_room_identifier(), $identifier) && + str_contains($identifier, "+") + ); + } + /** * Check whether the given list matches the current Community or its parent server. * @param string[] $filter * Array of unique room identifiers, server pubkeys and/or server hostnames. + * @param string $matchee String matching room. Output parameter. * @return bool True if the array matches the Community, false otherwise. */ - public function matched_by_list(array $filter): bool { - return in_array($this->get_room_identifier(), $filter) || - in_array($this->server->get_pubkey(), $filter) || - in_array($this->server->get_hostname(), $filter); + public function matched_by_list(array $filter, string &$matchee): bool { + foreach ($filter as $filter_item) { + if ($this->matched_by_identifier($filter_item)) { + $matchee = $filter_item; + return true; + } + } + + return false; + } + + /** + * @param CommunityRoom[] $rooms + * @param string[] $filter + * @param string[] $matchees output parameter + * @return CommunityRoom[] + */ + public static function select_rooms(array $rooms, array|string $filter, array &$matchees): array { + $_matchees = []; + $rooms = array_values(array_filter($rooms, function(CommunityRoom $room) use ($filter, $_matchees) { + $matchee = null; + $success = $room->matched_by_list($filter, $matchee); + if ($matchee != null) $_matchees[] = $matchee; + return $success; + })); + $matchees = $_matchees; + return $rooms; } /** diff --git a/php/utils/utils.php b/php/utils/utils.php index 6921609..088a45a 100644 --- a/php/utils/utils.php +++ b/php/utils/utils.php @@ -132,7 +132,9 @@ $room_token = url_get_token($join_url); $pubkey = url_get_pubkey($join_url); $pubkey_4 = substr($pubkey, 0, 4); - return "$room_token+$pubkey_4"; + $base_url = url_get_base($join_url, include_scheme: false); + $base_url_hash_4 = substr(md5($base_url), 0, 4); + return "$room_token+$pubkey_4$base_url_hash_4"; } /**