From c0d02a99902a6b0ac35ba7b026898704e577f5b6 Mon Sep 17 00:00:00 2001 From: easrng Date: Mon, 14 Feb 2022 13:47:05 -0500 Subject: [PATCH 1/3] add emoji downloader script --- .gitignore | 1 + scripts/get_emojis.sh | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100755 scripts/get_emojis.sh diff --git a/.gitignore b/.gitignore index ea8c4bf..4f5b1e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/frontend/emojis/* diff --git a/scripts/get_emojis.sh b/scripts/get_emojis.sh new file mode 100755 index 0000000..c68e332 --- /dev/null +++ b/scripts/get_emojis.sh @@ -0,0 +1,23 @@ +#!/bin/sh +set -eu + +# Get guild ID +if [ ! "$1" ];then + echo "You need to provide a Discord Guild ID." + exit 1 +else + guild="$1" +fi + +# Get emoji folder +emojiFolder="$(readlink -f "$(dirname $0)/../frontend/emojis/")" + +# Get Discord token +printf "Token: " 1>&2 +trap 'stty echo' INT EXIT +stty -echo +read token +printf "\n" 1>&2 +stty echo + +curl "https://discord.com/api/v9/guilds/${guild}/emojis" -H "Authorization: $token" | jq --raw-output 'map("curl '"'"'https://cdn.discordapp.com/emojis/" + .id + ".png?size=48&quality=lossless'"'"' -o '"'${emojiFolder}/"'" + .name + ".png'"'"'") | join("\n")' | sh From 1f78f03b681b05b9bafd0d9e4ef8b076ea0f4302 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Tue, 15 Feb 2022 23:15:06 +0000 Subject: [PATCH 2/3] Add an emoji list endpoint --- Cargo.toml | 2 +- src/main.rs | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a1f13ad..8a57317 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,6 @@ once_cell = "1.8.0" serde = { version = "1.0.130", features = ["derive"] } serde_json = "1.0.68" tokio = { version = "1.12.0", features = ["full"] } -tokio-stream = "0.1.7" +tokio-stream = { version = "0.1.7", features = ["fs"] } uuid = { version = "0.8.2", features = ["v4"] } warp = "0.3.1" diff --git a/src/main.rs b/src/main.rs index 77a0eec..cda5b41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,9 @@ use warb::{hyper::StatusCode, Filter, Reply}; use warp as warb; // i think it's funny mod events; +mod utils; mod viewer_connection; mod watch_session; -mod utils; use serde::Deserialize; @@ -30,6 +30,22 @@ struct SubscribeQuery { colour: String, } +async fn get_emoji_list() -> Result { + use tokio_stream::{wrappers::ReadDirStream, StreamExt}; + + let dir = tokio::fs::read_dir("frontend/emojis") + .await + .expect("Couldn't read emojis directory!"); + + let files = ReadDirStream::new(dir) + .filter_map(|r| r.ok()) + .map(|e| e.file_name().to_string_lossy().to_string()) + .collect::>() + .await; + + Ok(warb::reply::json(&files)) +} + #[tokio::main] async fn main() { let start_session_route = warb::path!("start_session") @@ -46,6 +62,8 @@ async fn main() { warb::reply::json(&json!({ "id": session_uuid.to_string(), "session": session_view })) }); + let get_emoji_route = warb::path!("emojis").and_then(get_emoji_list); + enum RequestedSession { Session(Uuid, WatchSession), Error(warb::reply::WithStatus), @@ -96,6 +114,7 @@ async fn main() { let routes = start_session_route .or(get_status_route) .or(ws_subscribe_route) + .or(get_emoji_route) .or(warb::path::end().and(warb::fs::file("frontend/index.html"))) .or(warb::fs::dir("frontend")); From 24f5560d8d34de8fe4f46e14d0b85559c9432a4b Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Tue, 15 Feb 2022 23:22:24 +0000 Subject: [PATCH 3/3] Fix clippy warnings & run cargo fmt --- src/viewer_connection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viewer_connection.rs b/src/viewer_connection.rs index fb62b02..ea38161 100644 --- a/src/viewer_connection.rs +++ b/src/viewer_connection.rs @@ -51,7 +51,7 @@ pub async fn ws_subscribe(session_uuid: Uuid, nickname: String, colour: String, }); let mut colour = colour; - if !colour.len() == 6 || !colour.chars().all(|x| x.is_ascii_hexdigit()) { + if colour.len() != 6 || !colour.chars().all(|x| x.is_ascii_hexdigit()) { colour = String::from("7ed0ff"); } let nickname = truncate_str(&nickname, 50).to_string(); @@ -92,7 +92,7 @@ pub async fn ws_subscribe(session_uuid: Uuid, nickname: String, colour: String, let event: WatchEventData = match event { WatchEventData::SetTime { from: _, to } => WatchEventData::SetTime { from: Some(session.get_time_ms()), - to: to, + to, }, _ => event, };