@ -2,6 +2,71 @@
// of redundant values in the main file, especially those that could
// change in the foreseeable future.
class _RoomInfo {
static ROOMS _ENDPOINT = '/servers.json' ;
static rooms = { } ;
static servers = { } ;
static async fetchRooms ( ) {
const response = await fetch ( this . ROOMS _ENDPOINT ) ;
const servers = await response . json ( ) ;
for ( const server of servers ) {
for ( const room of server . rooms ) {
const identifier = ` ${ room . token } + ${ server . pubkey . slice ( 0 , 4 ) } ` ;
this . rooms [ identifier ] = { ... room , server _key : server . pubkey } ;
}
delete server . rooms ;
this . servers [ server . pubkey ] = server ;
}
}
/ * *
* @ param { string } identifier
* /
static assertRoomExists ( identifier ) {
if ( ! ( identifier in this . rooms ) ) {
throw new Error ( ` No such room: ${ identifier } ` ) ;
}
}
/ * *
* @ param { string } identifier
* @ returns { CommunityRoom }
* /
static getRoom ( identifier ) {
this . assertRoomExists ( identifier ) ;
return this . rooms [ identifier ] ;
}
/ * *
* @ param { string } identifier
* @ returns { CommunityServer }
* /
static getRoomServer ( identifier ) {
this . assertRoomExists ( identifier ) ;
return this . servers [ this . rooms [ identifier ] . server _key ] ;
}
}
export class RoomInfo {
static async fetchRooms ( ) {
return _RoomInfo . fetchRooms ( ) ;
}
/ * *
* @ param { string } identifier
* @ returns { { type : string , text : string , description : string } [ ] }
* /
static getRoomTags ( identifier ) {
return _RoomInfo . getRoom ( identifier ) . tags ;
}
static getRoomStaff ( identifier ) {
const room = _RoomInfo . getRoom ( identifier ) ;
const { admins = [ ] , moderators = [ ] } = room ;
return [ ... new Set ( [ ... admins , ... moderators ] ) ] ;
}
}
export const dom = {
/** @return {HTMLTableElement | null} */
tbl _communities : ( ) => document . getElementById ( "tbl_communities" ) ,
@ -10,6 +75,7 @@ export const dom = {
community _row : ( communityID ) => document . querySelector ( ` .room-row[ ${ ATTRIBUTES . ROW . IDENTIFIER } =" ${ communityID } "] ` ) ,
row _info : ( row ) => {
const dateCreated = new Date ( row . getAttribute ( ATTRIBUTES . ROW . DATE _CREATED ) * 1000 ) ;
const identifier = row . getAttribute ( ATTRIBUTES . ROW . IDENTIFIER ) ;
/** @type {string[]} */
return {
language _flag : row . querySelector ( '.td_language' ) . textContent . trim ( ) ,
@ -18,12 +84,11 @@ export const dom = {
users : parseFloat ( row . querySelector ( '.td_users' ) . textContent . trim ( ) ) ,
preview _link : row . querySelector ( '.td_preview a[href]' ) . getAttribute ( 'href' ) ,
join _link : row . querySelector ( '.td_join_url a[href]' ) . getAttribute ( 'href' ) ,
identifier : row . getAttribute ( ATTRIBUTES . ROW . IDENTIFIER ) ,
identifier ,
hostname : row . getAttribute ( ATTRIBUTES . ROW . HOSTNAME ) ,
public _key : row . getAttribute ( ATTRIBUTES . ROW . PUBLIC _KEY ) ,
staff : row . getAttribute ( ATTRIBUTES . ROW . STAFF _DATA ) ,
/** @type {{type: string, text: string, description: string}[]} */
tags : JSON . parse ( row . getAttribute ( ATTRIBUTES . ROW . TAGS ) ) ,
staff : RoomInfo . getRoomStaff ( identifier ) ,
tags : RoomInfo . getRoomTags ( identifier ) ,
icon : row . getAttribute ( ATTRIBUTES . ROW . ROOM _ICON ) ,
has _icon : row . getAttribute ( ATTRIBUTES . ROW . ROOM _ICON ) . trim ( ) != "" ,
icon _safety : row . getAttribute ( ATTRIBUTES . ROW . ROOM _ICON _SAFETY ) ,