feat: read 'icon' & 'stickied' from local config

dev
gravel 9 months ago
parent 4c9251e976
commit 1ad99d30a8
No known key found for this signature in database
GPG Key ID: CA95FFF4E0123903

@ -6,6 +6,7 @@
require_once 'servers/known-servers.php';
require_once 'assets/room-icons.php';
require_once 'utils/read-config.php';
/**
* Fetch the icon of the given Community server and return its relative path.
@ -14,21 +15,17 @@
* @return string Relative path or null if icon is absent.
*/
function server_icon(CommunityServer $server, string $size): ?string {
global $SERVER_ICON_MAPPING;
$hostname = $server->get_hostname();
$pubkey = $server->get_pubkey();
if (isset($SERVER_ICON_MAPPING[$hostname])) {
$room_token = $SERVER_ICON_MAPPING[$hostname];
} else if (isset($SERVER_ICON_MAPPING[$pubkey])) {
$room_token = $SERVER_ICON_MAPPING[$pubkey];
} else {
return "";
}
$room_token = LocalConfig::get_instance()->get_server_icon_room($server);
if (!$room_token) return "";
$room = $server->get_room_by_token($room_token);
if (!$room) {
$hostname = $server->get_hostname();
log_warning("Room $room_token on $hostname does not exist, cannot be used as icon.");
return "";
}
return room_icon($room, $size);
}
?>

@ -23,96 +23,11 @@
];
/**
* @var string[] $NSFW_INCLUDE
* Hostnames or rooms considered to host adult content.
*/
$NSFW_INCLUDE = [
"gaohuangse.work",
"46.101.253.18",
"womanbodybeauty+13f6",
"88.212.53.198:4080",
"aiunlimited+fc30",
"AlexMed+e093",
"gore+e5e0",
"internet+70d0",
"k9training+fdcb",
"dogmen+fdcb",
"RU-STEROID+e093",
"thestart+e4b1",
"deutschclub+e4b1",
"cocaine+e4b1",
"chigua+4567",
"A4hanguo+4567",
"adult.oddch.at",
"55e43f8010a820cf3a4ed50bff1916bd5f166148f2c8b4a9feb1088bc86729",
"naturist+a3d5",
"meme+c975",
"trader+c975",
"public-chinese+c975",
"brasil+ad34abde",
"cryptochinese+0e0d253d",
"uadigitaltalks+e529b085",
"uaonlinetalks+e529b085",
"program-magic+194f9085",
"main+bf06303a",
"offtopic+e56f81b8",
];
/**
* @var string[] $NSFW_EXCLUDE
* Hostnames or rooms considered not to host adult content.
*/
$NSFW_EXCLUDE = [
];
/**
* @var string[] $TESTING_INCLUDE
* Rooms intended for testing and not for a general userbase.
*/
$TESTING_INCLUDE = [
"fishing+8e2e",
"test+118d",
"test+13f6",
"test+fe93",
"xyz+7908",
"testingroom+22fd",
"TOKEN_fortest1+4567",
"test2+ed339954",
];
/**
* @var array<string,string> $SERVER_ICON_MAPPING
* Associative array specifies which room token provides a server-wide icon.
*/
$SERVER_ICON_MAPPING = [
"open.getsession.org" => "session",
"sog.caliban.org" => "privacy",
"64dcb99cf35d803452106a63ed4d55e3869d4c966d3e762c365b784c08007333" => "midnightmadness",
"a3d56b901a39ba1bb4f6f8ab9dc9bb9343135a99f7d3d0ddc15d9ae2ddcefb55" => "esperanto",
"sogs.cosmicnation.co" => "cosmicnation",
"chat.privacycountry.org:8888" => "pcf",
"oddch.at" => "icon",
"adult.oddch.at" => "icon",
"ai.oddch.at" => "icon",
"talk.oddch.at" => "icon",
"sogs.hloth.dev" => "sessionjs",
];
/**
* @var string[] $ROOMS_USED_BY_PROJECT
* Community rooms encouraged for use in discussing https://sessioncommmunities.online/.
*/
$ROOMS_USED_BY_PROJECT = [
"webdev+118d",
];
/**
* @var string[] $STICKIED_ROOMS
* Community rooms anchored to the top of the list.
*/
$STICKIED_ROOMS = [
"reports+21317dc3",
"sogops+118d"
];
?>

@ -387,8 +387,10 @@
* @return CommunityRoom[] Pinned Communities.
*/
public static function get_stickied_rooms(array $rooms, array &$rest = null) {
global $STICKIED_ROOMS;
return CommunityRoom::select_rooms($rooms, $STICKIED_ROOMS, unmatched: $rest);
$config = LocalConfig::get_instance();
return CommunityRoom::select_rooms_predicate($rooms, function (CommunityRoom $room) use ($config) {
return $config->is_stickied_room($room);
}, unmatched: $rest);
}
/**
@ -613,6 +615,20 @@
return $_rooms;
}
/**
* Like select_rooms but uses a predicate to select rooms.
*/
public static function select_rooms_predicate(array $rooms, Callable $predicate, array &$unmatched = null): array {
$_unmatched = [];
$_rooms = [];
foreach ($rooms as $room) {
if ($predicate($room)) $_rooms[] = $room;
else $_unmatched[] = $room;
}
$unmatched = $_unmatched;
return $_rooms;
}
/**
* Checks whether this room belongs to a Session-owned server.
*/
@ -756,7 +772,7 @@
* @return CommunityTag[] Array of tags.
*/
private function get_derived_tags(): array {
global $ROOMS_USED_BY_PROJECT, $TESTING_INCLUDE, $STICKIED_ROOMS;
global $ROOMS_USED_BY_PROJECT;
/**
* @var CommunityTag[] $derived_tags

@ -1,6 +1,7 @@
<?php
require_once 'php/utils/logging.php';
require_once 'php/utils/utils.php';
/**
* @var string $CONFIG_ROOT
@ -23,6 +24,17 @@ function config_requiring_type(
throw new Error("Unexpected configuration option type: $realtype");
}
function remove_trailing_key_slashes(array $array): array {
return $array;
$newarray = array();
foreach ($array as $key => $value) {
if (str_ends_with($key, "/")) {
$newarray[substring($key, 0, -1)] = $value;
} else $newarray[$key] = $value;
}
return $newarray;
}
/**
* Provides custom configuration values.
*/
@ -37,9 +49,10 @@ class LocalConfig {
});
$this->room_overrides = $room_overrides;
$this->room_overrides_computed = array();
$this->known_servers =
$this->known_servers = remove_trailing_key_slashes(
LocalConfig::maybe_parse_ini_file(LocalConfig::KNOWN_SERVERS_CONFIG)
?? array();
?? array()
);
}
private static LocalConfig | null $instance = null;
@ -80,7 +93,7 @@ class LocalConfig {
return new LocalConfig();
}
private function get_room_overrides(CommunityRoom $room) {
private function get_room_overrides(CommunityRoom $room): array {
$room_id = $room->get_room_identifier();
if (isset($this->room_overrides_computed[$room_id])) {
@ -98,6 +111,12 @@ class LocalConfig {
return $this->room_overrides_computed[$room_id] = $room_overrides;
}
private function get_server_overrides(CommunityServer $server): array {
$server_base_url = $server->get_base_url();
return $this->known_servers[$server_base_url] ?? array();
}
public function get_room_staff_count_override(CommunityRoom $room): int | null {
return config_requiring_type(
$this->get_room_overrides($room)['staff_count'],
@ -133,6 +152,10 @@ class LocalConfig {
public function get_known_servers(): array {
return $this->known_servers;
}
public function get_server_icon_room(CommunityServer $server): ?string {
return $this->get_server_overrides($server)['icon'] ?? null;
}
}
class RoomSafety {

@ -204,4 +204,10 @@
function sign(float $num): int {
return ($num > 0) - ($num < 0);
}
function substring(string $string, int $start, int $end): string {
if ($end < 0) $end += strlen($string);
$length = $end - $start;
if ($length < 0) return ""; else return substr($string, $start, $length);
}
?>

Loading…
Cancel
Save