From e1e53829593e246eb23b07e8c978619f5d33ed48 Mon Sep 17 00:00:00 2001 From: FireMaskterK <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 11 Nov 2021 11:35:50 +0000 Subject: [PATCH] Add registered users badge. --- src/main/java/me/kavin/piped/ServerLauncher.java | 8 ++++++++ .../java/me/kavin/piped/utils/ResponseHelper.java | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index af95d49..01b62eb 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -4,6 +4,7 @@ import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.http.HttpHeaders.AUTHORIZATION; import static io.activej.http.HttpHeaders.CACHE_CONTROL; import static io.activej.http.HttpHeaders.CONTENT_TYPE; +import static io.activej.http.HttpHeaders.LOCATION; import static io.activej.http.HttpMethod.GET; import static io.activej.http.HttpMethod.POST; @@ -272,6 +273,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e, request.getPath()); } + })).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> { + try { + return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect()) + .withHeader(CACHE_CONTROL, "public, max-age=30"); + } catch (Exception e) { + return getErrorResponse(e, request.getPath()); + } })); return new CustomServletDecorator(router); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index cbb44bb..d8dfb23 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -967,6 +967,17 @@ public class ResponseHelper { } + public static final String registeredBadgeRedirect() { + + Session s = DatabaseSessionFactory.createSession(); + + long registered = ((Long) s.createQuery("select count(*) from User").uniqueResult()).longValue(); + + s.close(); + + return String.format("https://img.shields.io/badge/Registered%%20Users-%s-blue", String.valueOf(registered)); + } + private static final String getLBRYId(String videoId) throws IOException, InterruptedException { return new JSONObject(Constants.h2client.send(HttpRequest .newBuilder(URI.create("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId)))