Add server icons from rooms

dev
gravel 2 years ago
parent 3345e9c9b3
commit a058bf2de1
Signed by: gravel
GPG Key ID: C0538F3C906B308F

@ -349,6 +349,7 @@ a[href^="https:"] .protocol-indicator::after {
color: white;
text-shadow: 0 0 0.25em #000a;
box-shadow: 0 0 0.05em #777;
background-size: cover;
}
.td_server_icon-circle span {

@ -72,4 +72,9 @@
$ICON_BLOCKLIST = [
"gaohuangse.work"
];
$SERVER_ICON_MAPPING = [
"open.getsession.org" => "session",
"sog.caliban.org" => "privacy"
]
?>

@ -0,0 +1,25 @@
<?php
require_once "$PROJECT_ROOT/php/servers/known-servers.php";
require_once "$PROJECT_ROOT/php/utils/room-icons.php";
/**
* Fetch the icon of the given Community server and return its relative path.
* @param \CommunityServer $server
* @param string $size Image dimensions.
* @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();
if (!isset($SERVER_ICON_MAPPING[$hostname])) {
return "";
}
$room_token = $SERVER_ICON_MAPPING[$hostname];
$room = $server->get_room_by_token($room_token);
if (!$room) {
log_warning("Room $room_token on $hostname does not exist, cannot be used as icon.");
return "";
}
return room_icon($room, $size);
}
?>

@ -776,6 +776,22 @@
return $this->pubkey != "";
}
/**
* Returns the room of the given token, or null if one does not exist.
*/
function get_room_by_token(string $token): \CommunityRoom | null {
$candidates = array_filter($this->rooms, function(\CommunityRoom $room) use ($token) {
return $room->token == $token;
});
/** Filter doesn't reindex */
foreach ($candidates as $candidate) {
return $candidate;
}
return null;
}
/**
* Attempts to fetch the current server's room listing.
* Downgrades the server's scheme to HTTP if necessary.

@ -3,6 +3,7 @@
require_once "$PROJECT_ROOT/php/utils/servers-rooms.php";
require_once "$PROJECT_ROOT/php/utils/room-invites.php";
require_once "$PROJECT_ROOT/php/utils/room-icons.php";
require_once "$PROJECT_ROOT/php/utils/server-icons.php";
/**
* @var CommunityRoom[] $rooms
@ -52,6 +53,7 @@
$pubkey = $room->server->get_pubkey();
$icon_hue = hexdec($pubkey[2] . $pubkey[2]);
$icon_color = "hsl($icon_hue, 80%, 50%)";
$server_icon = server_icon($room->server, '64x64');
$hostname = $room->server->get_base_url();
@ -145,9 +147,13 @@
title="Host: <?=$hostname?> (<?=$pubkey?>)"
item="image"
>
<?php if (empty($server_icon)): ?>
<div class="td_server_icon-circle" style="background-color: <?=$icon_color?>">
<span><?=strtoupper($pubkey[0] . $pubkey[1])?></span>
</div>
<?php else: ?>
<div class="td_server_icon-circle" style="background-image: url('<?=$server_icon?>')"></div>
<?php endif; ?>
</td>
<td class="td_join_url">
<div class="join_url_container" data-url="<?=$join_link?>">

Loading…
Cancel
Save