From e462221dd9ba4fc7136dc1ae980a0cc8a9b3d399 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:21:35 +0000 Subject: [PATCH 01/12] Apply `--verbose` to `make dev` & propagate to SSG --- .phpenv | 6 ++++++ Makefile | 42 +++++++++++++++++++++--------------------- php/fetch-servers.php | 8 -------- php/generate-html.php | 38 +++++++++++++++++++++++--------------- php/utils/logging.php | 20 ++++++++++++++++---- 5 files changed, 66 insertions(+), 48 deletions(-) diff --git a/.phpenv b/.phpenv index 8040b40..a57fb7f 100644 --- a/.phpenv +++ b/.phpenv @@ -9,6 +9,12 @@ include_once "$PROJECT_ROOT/php/utils/logging.php"; + // Read the -v|--verbose option increasing logging verbosity to debug. + $options = getopt("v", ["verbose"]); + if (isset($options["v"]) or isset($options["verbose"])) { + $LOGGING_VERBOSITY = LoggingVerbosity::Debug; + } + // set timeout for file_get_contents() ini_set('default_socket_timeout', 6); // in seconds, default is 60 diff --git a/Makefile b/Makefile index f123daf..76d07b4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -port = 8081 -output = output +PORT ?= 8081 +OUTPUT ?= output +FLAGS ?= +MAKE = make FLAGS=$(FLAGS) # First goal is the default with `make`. @@ -14,49 +16,47 @@ all: fetch html # Fetch room listing. fetch: - /bin/php php/fetch-servers.php - -# Fetch room listing with extra verbosity. -fetch-v: - /bin/php php/fetch-servers.php --verbose + /bin/php php/fetch-servers.php $(FLAGS) # Generate HTML from data. html: - /bin/php php/generate-html.php - -# Last item run in foreground to receive interrupts. + /bin/php php/generate-html.php $(FLAGS) # Serve a local copy which responds to file changes. +dev: FLAGS = --verbose dev: open - make server & - make watchdog + $(MAKE) server & + $(MAKE) watchdog + +# (Last item run in foreground to receive interrupts.) # Serve a local copy on LAN which responds to file changes. +lan-dev: FLAGS = --verbose lan-dev: open - ip addr | fgrep -e ' 192.' -e ' 10.' - make lan-server & - make watchdog + -which ip 1>/dev/null 2>/dev/null && ip addr | fgrep -e ' 192.' -e ' 10.' || true + $(MAKE) lan-server & + $(MAKE) watchdog # Serve a local copy. server: - /bin/php -S localhost:$(port) -t $(output) + /bin/php -S "localhost:$(PORT)" -t "$(OUTPUT)" # Serve a local copy on all interfaces. lan-server: - /bin/php -S 0.0.0.0:$(port) -t $(output) + /bin/php -S "0.0.0.0:$(PORT)" -t "$(OUTPUT)" # Open locally served page in browser. open: - xdg-open http://localhost:$(port) >/dev/null 2>/dev/null & disown + xdg-open "http://localhost:$(PORT)" >/dev/null 2>/dev/null & disown # Update HTML on file change. Doesn't check for new files. watchdog: - find . | entr -n -s "make html" + find . | grep -v ".git" | entr -n -s "$(MAKE) html" # Remove artefacts clean: - -rm -r cache - -rm -r output/*.html + -rm -r cache 2>/dev/null || true + -rm -r output/*.html 2>/dev/null || true # Build everything from scratch and test functionality. test: clean all open server diff --git a/php/fetch-servers.php b/php/fetch-servers.php index b2d31ef..ab42f92 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -21,14 +21,6 @@ * 6. De-dupe servers based on pubkey */ function main() { - global $LOGGING_VERBOSITY; - - // Read the -v|--verbose option increasing logging verbosity to debug. - $options = getopt("v", ["verbose"]); - if (isset($options["v"]) or isset($options["verbose"])) { - $LOGGING_VERBOSITY = LoggingVerbosity::Debug; - } - global $CACHE_ROOT, $ROOMS_FILE, $KNOWN_SERVERS, $KNOWN_PUBKEYS; // Create default directories with conservative permissions. diff --git a/php/generate-html.php b/php/generate-html.php index 318ed6a..638a48b 100644 --- a/php/generate-html.php +++ b/php/generate-html.php @@ -15,26 +15,34 @@ return $files; } - foreach (rglob("$TEMPLATES_ROOT/*.php") as $phppath) { - // Do not render auxiliary PHP files. - if (str_contains("$phppath", "/+") || $phppath[0] == "+") - continue; + function generate_html() { + global $LOGGING_VERBOSITY, $TEMPLATES_ROOT, $DOCUMENT_ROOT; + $flags = LoggingVerbosity::getVerbosityFlags($LOGGING_VERBOSITY)[1]; - $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); - $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); - $docpath = str_replace(".php", ".html", $docpath); + foreach (rglob("$TEMPLATES_ROOT/*.php") as $phppath) { + // Do not render auxiliary PHP files. + if (str_contains("$phppath", "/+") || $phppath[0] == "+") + continue; - // This works? Yes, yes it does. - // We do this to isolate the environment and include-once triggers, - // otherwise we could include the documents in an ob_* wrapper. - // Same as shell_exec, except we don't have to escape quotes. - log_info("Generating output for $relpath."); - $document = `cd "$TEMPLATES_ROOT"; php $phppath`; + $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); + $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); + $docpath = str_replace(".php", ".html", $docpath); - file_put_contents($docpath, $document); + // This works? Yes, yes it does. + // We do this to isolate the environment and include-once triggers, + // otherwise we could include the documents in an ob_* wrapper. + + // Same as shell_exec, except we don't have to escape quotes. + log_info("Generating output for $relpath."); + $document = `cd "$TEMPLATES_ROOT"; php $phppath $flags`; + + file_put_contents($docpath, $document); + } + + log_info("Done generating HTML."); } - log_info("Done generating HTML."); + generate_html(); ?> diff --git a/php/utils/logging.php b/php/utils/logging.php index 3d340aa..a4f6b36 100644 --- a/php/utils/logging.php +++ b/php/utils/logging.php @@ -44,7 +44,7 @@ /** * Returns the color marker for the given logging verbosity. - * @param $verbosity Logging verbosity to used for printing. + * @param int $verbosity Logging verbosity to used for printing. * @return ?string Terminal escape sequence to color foreground text. */ static function getVerbosityColorMarker(int $verbosity): ?string { @@ -56,6 +56,18 @@ default => '' }; } + + /** + * Returns a pair of optíons trigerring the given verbosity. + * @param int $verbosity Logging verbosity to set using flag. + * @return string[] Pair of short and long command-line verbosity flags. + */ + static function getVerbosityFlags(int $verbosity): array { + return match($verbosity) { + LoggingVerbosity::Debug => ["-v", "--verbose"], + default => ['', ''] + }; + } } /** @@ -135,12 +147,12 @@ * Only logs when `$LOGGING_VERBOSITY` is debug and below. * @param string $msg String message to log. */ - function log_value(mixed $value) { - log_debug(var_export($value, true)); + function log_value(mixed $value, int $message_verbosity = LoggingVerbosity::Debug) { + _log_message(var_export($value, true), $message_verbosity); } /** - * @var $LOGGING_VERBOSITY + * @var int $LOGGING_VERBOSITY * Global setting. * Controls how detailed the displayed logs are. */ From 0cb25ffaff5e9ad383f72a195abd2f9d5b12ad16 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:25:13 +0000 Subject: [PATCH 02/12] Re-normalize active user counts --- php/utils/servers-rooms.php | 23 +++++++++++++++++++++++ sites/+components/tbl_communities.php | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index fe287e8..89ac54d 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -2,6 +2,7 @@ include_once "$PROJECT_ROOT/languages/language_flags.php"; + $WEEK_SECONDS = 604800; /** * Representation of Session Community room. */ @@ -199,6 +200,28 @@ $pubkey_4 = substr($this->server->pubkey, 0, 4); return "$token+$pubkey_4"; } + + /** + * Returns an estimate for the weekly active users of a CommunityRoom. + * @return int Active user count normalized by tracked timespan of active users. + */ + function get_weekly_active_users(): int { + global $WEEK_SECONDS; + + $active_users = $this->active_users; + + if ($active_users == null) { + return 0; + } + + $active_users_cutoff = $this->active_users_cutoff; + + if ($active_users_cutoff == null) { + return $active_users; + } + + return floor($active_users * $WEEK_SECONDS / $active_users_cutoff); + } } /** diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index c03841e..48c1c2f 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -57,7 +57,7 @@ $language = html_sanitize($room->language_flag); $name = html_sanitize($room->name); $desc = html_sanitize($room->description); - $users = html_sanitize($room->active_users); + $users = html_sanitize($room->get_weekly_active_users()); $preview_link = html_sanitize($room->get_preview_url()); $join_link = html_sanitize($room->get_join_url()); $pubkey = html_sanitize($pubkey); From 1f13ca1ad9728b7cfdb69d92ebb0c2512cee8dee Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:26:19 +0000 Subject: [PATCH 03/12] Pre-sort by name and host --- php/utils/servers-rooms.php | 28 +++++++++++++++++++++++++++- sites/index.php | 4 ++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index 89ac54d..8c003dd 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -1,5 +1,4 @@ $key, + $b->$key + ); + }); + } + + /** + * Sorts Community rooms in-place by their server's public key. + * @param \CommunityRoom[] $rooms Rooms to sort by server pubkey. + */ + public static function sort_rooms_by_pubkey(array &$rooms) { + usort($rooms, function(\CommunityRoom $a, \CommunityRoom $b) { + return strcmp( + $a->server->get_pubkey(), + $b->server->get_pubkey() + ); + }); + } + /** * Returns array of staff Session IDs. * @return string[] diff --git a/sites/index.php b/sites/index.php index cb205a1..50befa1 100644 --- a/sites/index.php +++ b/sites/index.php @@ -16,6 +16,10 @@ // List all rooms from the cached servers. $rooms = CommunityServer::enumerate_rooms($servers); + // Sort rooms by name and then host. + CommunityRoom::sort_rooms_str($rooms, 'name'); + CommunityRoom::sort_rooms_by_pubkey($rooms); + // Set the last-updated timestamp // to the time the server data file was last modified. $timestamp = filemtime($ROOMS_FILE); From cc4140aa75bdad86feccad42457a93cf8fb9eb71 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:27:01 +0000 Subject: [PATCH 04/12] Helpful table tooltips & fix sortable check --- sites/+components/tbl_communities.php | 42 ++++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index 48c1c2f..8ea6846 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -9,15 +9,15 @@ // Once handlers are attached in JS, this check ceases to be useful. function column_sortable($id) { // Join URL contents are not guaranteed to have visible text. - return $id != "qr" && $id != "preview" && $id != "join_url"; + return $id != "qr_code" && $id != "preview" && $id != "join_url"; } function sort_onclick($colno) { global $TABLE_COLUMNS; $column = $TABLE_COLUMNS[$colno]; $name = isset($column['name_long']) ? $column['name_long'] : $column['name']; - if (!column_sortable($column['id'])) return " title='Column: $name'"; - return " title='Click to sort by $name'"; + if (!column_sortable($column['id'])) return " title='$name'"; + return " title='Click to sort by $name'."; } // Note: Changing the names displayed requires updating @@ -30,9 +30,9 @@ ['id' => "description", 'name' => "About", 'name_long' => "Description"], ['id' => "users", 'name' => "#", 'name_long' => "Weekly Active Users"], ['id' => "preview", 'name' => "Preview"], - ['id' => "qr_code", 'name' => "QR"], + ['id' => "qr_code", 'name' => "QR", 'name_long' => "QR Code (for use in-app)"], ['id' => "server_icon", 'name' => "Host", 'name_long' => "Server host"], - ['id' => "join_url", 'name' => "URL", 'name_long' => "In-app Join URL"], + ['id' => "join_url", 'name' => "URL", 'name_long' => "Join URL (for use in-app)"], ]; ?> @@ -70,15 +70,24 @@ data-hostname="" > - - - - name?> + + + + - - + + @@ -89,11 +98,12 @@ class="qr-code-icon" src="qrcode-solid.svg" alt="Pictogram of a QR code" + title="Click here to view the QR Code for ''" >
@@ -104,8 +114,12 @@ From 1e71e524ef3ccede8e951908a1c5942310b90210 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:27:12 +0000 Subject: [PATCH 05/12] Re-apply bottom padding on footer --- output/styles2.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output/styles2.css b/output/styles2.css index 84786a3..7c70e7b 100644 --- a/output/styles2.css +++ b/output/styles2.css @@ -330,7 +330,7 @@ footer { max-width: 100%; text-align: center; - padding-top: var(--body-margin); + padding-bottom: var(--body-margin); padding-inline: var(--body-margin); } From be29fc5bc50fd09d5a32addc0abde84fa4956959 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:28:24 +0000 Subject: [PATCH 06/12] Whitespace --- php/utils/servers-rooms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index 8c003dd..de08dca 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -269,7 +269,7 @@ /** * @var bool $merge_error - * + * * Flag specifying whether the server is invalidated as a result of merging. */ private bool $merge_error = false; From 27ba952a30502a1a8d5ff711b23de29e9b4dd300 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 11:32:08 +0000 Subject: [PATCH 07/12] Apply `--verbose` to tests --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 76d07b4..3b1591f 100644 --- a/Makefile +++ b/Makefile @@ -59,9 +59,11 @@ clean: -rm -r output/*.html 2>/dev/null || true # Build everything from scratch and test functionality. +test: FLAGS = --verbose test: clean all open server # Build everything from scratch and test functionality on LAN. +test: FLAGS = --verbose test-lan: clean all open lan-server # -- Aliases -- From 121633debc49719b089f1acbce657e85838bf2a7 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 13:37:27 +0000 Subject: [PATCH 08/12] Revert "Re-normalize active user counts" This reverts commit 3b47818f6e6fe79f3df829b4f09d91ff3dc76b00. --- php/utils/servers-rooms.php | 23 ----------------------- sites/+components/tbl_communities.php | 2 +- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index de08dca..1c1226e 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -1,7 +1,6 @@ server->pubkey, 0, 4); return "$token+$pubkey_4"; } - - /** - * Returns an estimate for the weekly active users of a CommunityRoom. - * @return int Active user count normalized by tracked timespan of active users. - */ - function get_weekly_active_users(): int { - global $WEEK_SECONDS; - - $active_users = $this->active_users; - - if ($active_users == null) { - return 0; - } - - $active_users_cutoff = $this->active_users_cutoff; - - if ($active_users_cutoff == null) { - return $active_users; - } - - return floor($active_users * $WEEK_SECONDS / $active_users_cutoff); - } } /** diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index 8ea6846..d49d139 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -57,7 +57,7 @@ $language = html_sanitize($room->language_flag); $name = html_sanitize($room->name); $desc = html_sanitize($room->description); - $users = html_sanitize($room->get_weekly_active_users()); + $users = html_sanitize($room->active_users); $preview_link = html_sanitize($room->get_preview_url()); $join_link = html_sanitize($room->get_join_url()); $pubkey = html_sanitize($pubkey); From 22d1d017bead6d1a9a04bc287197f587e4aaf704 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 14:01:12 +0000 Subject: [PATCH 09/12] Reflect cutoff period for user counts in tooltip --- php/utils/servers-rooms.php | 31 +++++++++++++++++++++++++++ sites/+components/tbl_communities.php | 5 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index 1c1226e..67e33db 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -1,6 +1,11 @@ created; } + /** + * Formats the period over which active users are counted as a duration string. + * @return string Active user cutoff period for this room, expressed in days. + */ + function format_user_cutoff_period(): ?string { + global $WEEK_SECONDS, $DAY_SECONDS, $HOUR_SECONDS, $MINUTE_SECONDS; + + $active_users_cutoff = $this->active_users_cutoff; + + if ($active_users_cutoff >= $WEEK_SECONDS) { + return floor($active_users_cutoff / $WEEK_SECONDS) . ' week(s)'; + } + if ($active_users_cutoff >= $DAY_SECONDS) { + return floor($active_users_cutoff / $DAY_SECONDS) . ' day(s)'; + } + if ($active_users_cutoff >= $HOUR_SECONDS) { + return floor($active_users_cutoff / $HOUR_SECONDS) . ' hour(s)'; + } + if ($active_users_cutoff >= $MINUTE_SECONDS) { + return floor($active_users_cutoff / $MINUTE_SECONDS) . 'minute(s)'; + } + + return floor($active_users_cutoff) . 's'; + + } + /** * Return the browser preview URL for this room. */ diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index d49d139..59b3401 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -28,7 +28,7 @@ ['id' => "language", 'name' => "L", 'name_long' => "Language"], ['id' => "name", 'name' => "Name"], ['id' => "description", 'name' => "About", 'name_long' => "Description"], - ['id' => "users", 'name' => "#", 'name_long' => "Weekly Active Users"], + ['id' => "users", 'name' => "#", 'name_long' => "Active Users"], ['id' => "preview", 'name' => "Preview"], ['id' => "qr_code", 'name' => "QR", 'name_long' => "QR Code (for use in-app)"], ['id' => "server_icon", 'name' => "Host", 'name_long' => "Server host"], @@ -58,6 +58,7 @@ $name = html_sanitize($room->name); $desc = html_sanitize($room->description); $users = html_sanitize($room->active_users); + $users_cutoff = html_sanitize($room->format_user_cutoff_period()); $preview_link = html_sanitize($room->get_preview_url()); $join_link = html_sanitize($room->get_join_url()); $pubkey = html_sanitize($pubkey); @@ -86,7 +87,7 @@ > From 4770e3ffeaf167634723958fcb940097d44305ae Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 14:03:19 +0000 Subject: [PATCH 10/12] Add tooltip to preview button --- sites/+components/tbl_communities.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index 59b3401..62590da 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -90,7 +90,12 @@ title="'' has had active users in the last ." > - + From 44ef7d848ec650fab9d313682d7b10d1a60a2006 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 14:20:57 +0000 Subject: [PATCH 11/12] Clarify when Communities are hidden --- output/styles2.css | 1 - sites/index.php | 12 +++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/output/styles2.css b/output/styles2.css index 7c70e7b..caf0aaa 100644 --- a/output/styles2.css +++ b/output/styles2.css @@ -58,7 +58,6 @@ html.js .noscript, .hidden { } html:not(.js) .js-only { - /* Dead style */ display: none; } diff --git a/sites/index.php b/sites/index.php index 50befa1..1221d6c 100644 --- a/sites/index.php +++ b/sites/index.php @@ -96,9 +96,15 @@ target="_blank" >various sources.
- We make an attempt to hide communities containing - objectionable or illegal content, but - you should still proceed with caution. + + We make an attempt to hide communities containing + objectionable or illegal content, but + you should still proceed with caution. + + + Proceed with caution when joining unofficial communities. + As JavaScript is disabled, no communities are filtered from the list. +

This site works fine without JavaScript. From af53a5f7a3dd739f48f8b6e68d2e83a206b04c06 Mon Sep 17 00:00:00 2001 From: mdPlusPlus Date: Tue, 18 Apr 2023 23:20:29 +0200 Subject: [PATCH 12/12] Fix test-lan: in Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3b1591f..614a366 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ test: FLAGS = --verbose test: clean all open server # Build everything from scratch and test functionality on LAN. -test: FLAGS = --verbose +test-lan: FLAGS = --verbose test-lan: clean all open lan-server # -- Aliases --