-
Notifications
You must be signed in to change notification settings - Fork 13
Add mod status badges + abbreviate query param on mod info
#63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const ONE_THOUSAND: f64 = 1_000.0; | ||
| const ONE_MILLION: f64 = 1_000_000.0; | ||
| const ONE_BILLION: f64 = 1_000_000_000.0; | ||
|
|
||
| pub fn abbreviate_number(n: i32) -> String { | ||
| let n = n as f64; | ||
| if n.abs() >= ONE_BILLION { | ||
| format!("{:.1}B", n / ONE_BILLION) | ||
| } else if n.abs() >= ONE_MILLION { | ||
| format!("{:.1}M", n / ONE_MILLION) | ||
| } else if n.abs() >= ONE_THOUSAND { | ||
| format!("{:.1}K", n / ONE_THOUSAND) | ||
| } else { | ||
| format!("{:.0}", n) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use crate::config::AppData; | ||
| use crate::endpoints::ApiError; | ||
| use actix_web::{HttpResponse, Responder, get, web}; | ||
| use serde::Deserialize; | ||
| use utoipa::{IntoParams, ToSchema}; | ||
|
|
||
| use std::fs; | ||
| use std::path::Path; | ||
| use urlencoding; | ||
|
|
||
| const LABEL_COLOR: &str = "#0c0811"; | ||
| const STAT_COLOR: &str = "#5f3d84"; | ||
|
|
||
| #[derive(Deserialize, Clone, Copy, PartialEq, Eq, ToSchema)] | ||
| #[serde(rename_all = "snake_case")] | ||
| pub enum StatusBadgeStat { | ||
| Version, | ||
| GdVersion, | ||
| GeodeVersion, | ||
| Downloads, | ||
| } | ||
|
|
||
| #[derive(Deserialize, IntoParams)] | ||
| pub struct StatusBadgeQuery { | ||
| pub stat: StatusBadgeStat, | ||
| } | ||
|
|
||
| #[utoipa::path( | ||
| get, | ||
| path = "/v1/mods/{id}/status_badge", | ||
| tag = "mods", | ||
| params( | ||
| ("id" = String, Path, description = "Mod ID"), | ||
| StatusBadgeQuery | ||
| ), | ||
| responses( | ||
| (status = 302, description = "Redirect to Shields.io badge"), | ||
| (status = 400, description = "Invalid stat or missing parameter"), | ||
| (status = 404, description = "Mod not found") | ||
| ) | ||
| )] | ||
| #[get("/v1/mods/{id}/status_badge")] | ||
| pub async fn status_badge( | ||
| data: web::Data<AppData>, | ||
| id: web::Path<String>, | ||
| query: web::Query<StatusBadgeQuery>, | ||
| ) -> Result<impl Responder, ApiError> { | ||
| let (stat, label, svg_path) = match query.stat { | ||
| StatusBadgeStat::Version => ( | ||
| "payload.versions[0].version", | ||
| "Version", | ||
| "static/mod_version.svg", | ||
| ), | ||
| StatusBadgeStat::GdVersion => ( | ||
| "payload.versions[0].gd.win", | ||
| "Geometry Dash", | ||
| "static/mod_gd_version.svg", | ||
| ), | ||
| StatusBadgeStat::GeodeVersion => ( | ||
| "payload.versions[0].geode", | ||
| "Geode", | ||
| "static/mod_geode_version.svg", | ||
| ), | ||
| StatusBadgeStat::Downloads => ( | ||
| "payload.download_count", | ||
| "Downloads", | ||
| "static/mod_downloads.svg", | ||
| ), | ||
| }; | ||
| let svg = fs::read_to_string(Path::new(svg_path)) | ||
| .map_err(|_| ApiError::BadRequest(format!("Could not read SVG file: {}", svg_path)))?; | ||
| let api_url = format!("{}/v1/mods/{}?abbreviate=true", data.app_url(), id); | ||
| let mod_link = format!("{}/mods/{}", data.front_url(), id); | ||
| let svg_data_url = format!("data:image/svg+xml;utf8,{}", urlencoding::encode(&svg)); | ||
| let shields_url = format!( | ||
| "https://img.shields.io/badge/dynamic/json?url={}&query={}&label={}&labelColor={}&color={}&link={}&style=plastic&logo={}", | ||
| urlencoding::encode(&api_url), | ||
| urlencoding::encode(stat), | ||
| label, | ||
| urlencoding::encode(LABEL_COLOR), | ||
| urlencoding::encode(STAT_COLOR), | ||
| urlencoding::encode(&mod_link), | ||
| urlencoding::encode(&svg_data_url) | ||
| ); | ||
| Ok(HttpResponse::Found() | ||
| .append_header(("Location", shields_url)) | ||
| .finish()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use crate::abbreviate::abbreviate_number; | ||
| use serde::{Serialize, Serializer}; | ||
|
|
||
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | ||
| pub struct DownloadCount { | ||
| count: i32, | ||
| abbreviate: bool, | ||
| } | ||
|
|
||
| impl DownloadCount { | ||
| pub const fn new(count: i32) -> Self { | ||
| Self { | ||
| count, | ||
| abbreviate: false, | ||
| } | ||
| } | ||
|
|
||
| pub fn set_abbreviated(&mut self, abbreviate: bool) { | ||
| self.abbreviate = abbreviate; | ||
| } | ||
| } | ||
|
|
||
| impl From<i32> for DownloadCount { | ||
| fn from(count: i32) -> Self { | ||
| Self::new(count) | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for DownloadCount { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| if self.abbreviate { | ||
| serializer.serialize_str(&abbreviate_number(self.count)) | ||
| } else { | ||
| serializer.serialize_i32(self.count) | ||
| } | ||
| } | ||
| } |
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to serve these statically from the web application?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I have a plan to add app storage (which would have to be served statically by the reverse proxy) in #60, though that PR is WIP. I could backport the storage bit to main. |
Uh oh!
There was an error while loading. Please reload this page.