feat: migrate to longer room id

dev
gravel 2 years ago
parent 9866d1dc91
commit b2308e4095
Signed by: gravel
GPG Key ID: C0538F3C906B308F

@ -426,7 +426,7 @@ function hideBadCommunities() {
* Removes a Community by its ID and returns the number of elements removed. * Removes a Community by its ID and returns the number of elements removed.
*/ */
function hideCommunity(communityID) { function hideCommunity(communityID) {
const element = dom.community_row(communityID); const element = dom.community_row(communityID, true);
element?.remove(); element?.remove();
return element ? 1 : 0; return element ? 1 : 0;
} }

@ -87,42 +87,15 @@
$servers = CommunityServer::from_details_array($server_data); $servers = CommunityServer::from_details_array($server_data);
$rooms_all = CommunityServer::enumerate_rooms($servers); $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 = []; $listings = [];
foreach ($listings_raw as $id => $listing_props) { foreach ($listings_raw as $id => $listing_props) {
$rooms = []; $filter = [...$listing_props['rooms'], ...$listing_props['sogs']];
// TODO: Blocklist option $matchees = [];
if (isset($listing_props['rooms'])) { $rooms = CommunityRoom::select_rooms($rooms_all, $filter, $matchees);
foreach ($listing_props['rooms'] as $room_id) {
if ($room_id == '*') { foreach ($filter as $filter_item) {
$rooms = $rooms_all; if (!in_array($filter_item, $matchees)) {
break; log_warning("Could not find $filter_item from listing $id.");
}
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.");
}
} }
} }

@ -184,14 +184,12 @@
return $this->language_flag; return $this->language_flag;
} }
$room_identifier = $this->get_room_identifier(); foreach ($languages as $key => $flag) {
$server_pubkey = $this->server->get_pubkey(); if ($this->matched_by_identifier($key)) {
return $flag;
if (isset($languages[$room_identifier])) { }
return $languages[$room_identifier];
} elseif (isset($languages[$server_pubkey])) {
return $languages[$server_pubkey];
} }
return ""; return "";
} }
@ -397,12 +395,12 @@
/** /**
* Return a globally unique room identifier. * 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 { function get_room_identifier(): string {
$token = $this->token; $token = $this->token;
$pubkey_4 = substr($this->server->get_pubkey(), 0, 4); $server_id = $this->server->get_server_id();
return "$token+$pubkey_4"; return "$token+$server_id";
} }
/** /**
@ -468,16 +466,60 @@
return true; 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. * Check whether the given list matches the current Community or its parent server.
* @param string[] $filter * @param string[] $filter
* Array of unique room identifiers, server pubkeys and/or server hostnames. * 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. * @return bool True if the array matches the Community, false otherwise.
*/ */
public function matched_by_list(array $filter): bool { public function matched_by_list(array $filter, string &$matchee): bool {
return in_array($this->get_room_identifier(), $filter) || foreach ($filter as $filter_item) {
in_array($this->server->get_pubkey(), $filter) || if ($this->matched_by_identifier($filter_item)) {
in_array($this->server->get_hostname(), $filter); $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;
} }
/** /**

@ -132,7 +132,9 @@
$room_token = url_get_token($join_url); $room_token = url_get_token($join_url);
$pubkey = url_get_pubkey($join_url); $pubkey = url_get_pubkey($join_url);
$pubkey_4 = substr($pubkey, 0, 4); $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";
} }
/** /**

Loading…
Cancel
Save