Compare commits

..

No commits in common. "master" and "v0.1.5_2-alpha" have entirely different histories.

16 changed files with 989 additions and 1863 deletions

8
.gitignore vendored
View File

@ -1,7 +1,5 @@
/target /target
/config **/result
/sounds result/
/.idea result
.DS_Store
.env .env

View File

@ -4,10 +4,9 @@ steps:
event: [push, tag] event: [push, tag]
image: alpine:edge image: alpine:edge
commands: commands:
- apk update && apk upgrade
- apk add --no-cache git nix --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing - apk add --no-cache git nix --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing
- echo "system-features = nixos-test benchmark big-parallel uid-range kvm" > /etc/nix/nix.conf - echo "system-features = nixos-test benchmark big-parallel uid-range kvm" > /etc/nix/nix.conf
- nix build --extra-experimental-features nix-command --extra-experimental-features flakes --max-jobs 16 .#docker - nix build --extra-experimental-features nix-command --extra-experimental-features flakes .#docker
- cp $(nix build --extra-experimental-features nix-command --extra-experimental-features flakes --print-out-paths .#docker) ./memejoin-rs.tar.gz - cp $(nix build --extra-experimental-features nix-command --extra-experimental-features flakes --print-out-paths .#docker) ./memejoin-rs.tar.gz
volumes: volumes:
- ${AGENT_NIX_STORE_PATH}:/nix - ${AGENT_NIX_STORE_PATH}:/nix

118
Cargo.lock generated
View File

@ -62,17 +62,6 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "ahash"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.0.2" version = "1.0.2"
@ -82,12 +71,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "allocator-api2"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -330,7 +313,6 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits 0.2.16", "num-traits 0.2.16",
"serde",
"time 0.1.45", "time 0.1.45",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
@ -493,12 +475,6 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.32" version = "0.8.32"
@ -508,26 +484,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "enum-iterator"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7add3873b5dd076766ee79c8e406ad1a472c385476b9e38849f8eec24f1be689"
dependencies = [
"enum-iterator-derive",
]
[[package]]
name = "enum-iterator-derive"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eecf8589574ce9b895052fa12d69af7a233f99e6107f5cb8dd1044f2a17bfdcb"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.27",
]
[[package]] [[package]]
name = "enum_primitive" name = "enum_primitive"
version = "0.1.1" version = "0.1.1"
@ -558,18 +514,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "2.0.0" version = "2.0.0"
@ -800,19 +744,6 @@ name = "hashbrown"
version = "0.14.0" version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]]
name = "hashlink"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "312f66718a2d7789ffef4f4b7b213138ed9f1eb3aa1d0d82fc99f88fb3ffd26f"
dependencies = [
"hashbrown 0.14.0",
]
[[package]] [[package]]
name = "headers" name = "headers"
@ -994,24 +925,6 @@ version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6" checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
[[package]]
name = "iter_tools"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "531cafdc99b3b3252bb32f5620e61d56b19415efc19900b12d1b2e7483854897"
dependencies = [
"itertools",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@ -1039,17 +952,6 @@ version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "libsqlite3-sys"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.3" version = "0.4.3"
@ -1110,18 +1012,15 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]] [[package]]
name = "memejoin-rs" name = "memejoin-rs"
version = "0.2.2-alpha" version = "0.1.1-alpha"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum", "axum",
"axum-extra", "axum-extra",
"chrono", "chrono",
"dotenv", "dotenv",
"enum-iterator",
"futures", "futures",
"iter_tools",
"reqwest", "reqwest",
"rusqlite",
"serde", "serde",
"serde_json", "serde_json",
"serenity", "serenity",
@ -1617,21 +1516,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rusqlite"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549b9d036d571d42e6e85d1c1425e2ac83491075078ca9a15be021c56b1641f2"
dependencies = [
"bitflags 2.3.3",
"chrono",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "memejoin-rs" name = "memejoin-rs"
version = "0.2.2-alpha" version = "0.1.1-alpha"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -9,11 +9,9 @@ edition = "2021"
async-trait = "0.1.72" async-trait = "0.1.72"
axum = { version = "0.6.9", features = ["headers", "multipart"] } axum = { version = "0.6.9", features = ["headers", "multipart"] }
axum-extra = { version = "0.7.5", features = ["cookie-private", "cookie"] } axum-extra = { version = "0.7.5", features = ["cookie-private", "cookie"] }
chrono = { version = "0.4.23", features = ["serde"] } chrono = "0.4.23"
dotenv = "0.15.0" dotenv = "0.15.0"
enum-iterator = "1.4.1"
futures = "0.3.26" futures = "0.3.26"
iter_tools = "0.1.4"
reqwest = "0.11.14" reqwest = "0.11.14"
serde = "1.0.152" serde = "1.0.152"
serde_json = "1.0.93" serde_json = "1.0.93"
@ -32,12 +30,3 @@ features = ["client", "gateway", "rustls_backend", "model", "cache", "voice"]
[dependencies.songbird] [dependencies.songbird]
version = "0.3.2" version = "0.3.2"
features = [ "builtin-queue", "yt-dlp" ] features = [ "builtin-queue", "yt-dlp" ]
[target.'cfg(unix)'.dependencies]
rusqlite = { version = "0.29.0", features = ["chrono"] }
[target.'cfg(windows)'.dependencies]
rusqlite = { version = "0.29.0", features = ["bundled", "chrono"] }
[lints.clippy]
map_flatten = "allow"

View File

@ -1,15 +1,12 @@
{ {
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": { "locked": {
"lastModified": 1710146030, "lastModified": 1667395993,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -19,15 +16,12 @@
} }
}, },
"flake-utils_2": { "flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": { "locked": {
"lastModified": 1705309234, "lastModified": 1659877975,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -38,11 +32,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1717786204, "lastModified": 1675942811,
"narHash": "sha256-4q0s6m0GUcN7q+Y2DqD27iLvbcd1G50T2lv08kKxkSI=", "narHash": "sha256-/v4Z9mJmADTpXrdIlAjFa1e+gkpIIROR670UVDQFwIw=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "051f920625ab5aabe37c920346e3e69d7d34400e", "rev": "724bfc0892363087709bd3a5a1666296759154b1",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -54,11 +48,11 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1706487304, "lastModified": 1665296151,
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=", "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac", "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -81,11 +75,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1717985971, "lastModified": 1676169013,
"narHash": "sha256-24h/qKp0aeI+Ew13WdRF521kY24PYa5HOvw0mlrABjk=", "narHash": "sha256-mhUWa6TUg6Qjba1OdxPuW1ctCuU4O4lSObVc6UUUE0E=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "abfe5b3126b1b7e9e4daafc1c6478d17f0b584e7", "rev": "ef4cd733dc6b595cab5092f5004a489c5fd80b07",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -93,36 +87,6 @@
"repo": "rust-overlay", "repo": "rust-overlay",
"type": "github" "type": "github"
} }
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
} }
}, },
"root": "root", "root": "root",

View File

@ -8,19 +8,19 @@
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }: outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
tag = "v0.2.2-alpha"; tag = "v0.1.5_2-alpha";
overlays = [ (import rust-overlay) ]; overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system overlays; inherit system overlays;
}; };
yt-dlp = pkgs.yt-dlp.overrideAttrs (oldAttr: rec { yt-dlp = pkgs.yt-dlp.overrideAttrs (oldAttr: rec {
inherit (oldAttr) name; inherit (oldAttr) name;
version = "2024.05.27"; version = "2023.02.17";
src = pkgs.fetchFromGitHub { src = pkgs.fetchFromGitHub {
owner = "yt-dlp"; owner = "yt-dlp";
repo = "yt-dlp"; repo = "yt-dlp";
rev = "${version}"; rev = "${version}";
sha256 = "55zDAMwCJPn5zKrAFw4ogTxxmvjrv4PvhYO7PsHbRo4="; sha256 = "naC74T6aqCLX45wJLmygsMmTMqdqLbfXLjJKIKMRpiI=";
}; };
}); });
local-rust = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain).override { local-rust = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain).override {
@ -35,14 +35,13 @@
pkg-config pkg-config
gcc gcc
openssl openssl
sqlite
pkg-config pkg-config
python3 python3
ffmpeg ffmpeg
cmake cmake
libopus libopus
yt-dlp yt-dlp
] ++ (if pkgs.system == "aarch64-darwin" || pkgs.system == "x86_64-darwin" then [ darwin.apple_sdk.frameworks.Security darwin.apple_sdk.frameworks.SystemConfiguration ] else []); ];
}; };
packages = with pkgs; flake-utils.lib.flattenTree rec { packages = with pkgs; flake-utils.lib.flattenTree rec {
@ -51,16 +50,11 @@
name = "memejoin-rs"; name = "memejoin-rs";
src = self; src = self;
buildInputs = [ openssl.dev ]; buildInputs = [ openssl.dev ];
nativeBuildInputs = [ local-rust pkg-config openssl openssl.dev cmake gcc libopus sqlite ]; nativeBuildInputs = [ local-rust pkg-config openssl openssl.dev cmake gcc libopus ];
cargoLock = { cargoLock = {
lockFile = ./Cargo.lock; lockFile = ./Cargo.lock;
}; };
# lol, why does `buildRustPackage` not work without this?
postPatch = ''
ln -sf ${./Cargo.lock} Cargo.lock
'';
}; };
docker = dockerTools.buildImage { docker = dockerTools.buildImage {
@ -68,7 +62,7 @@
name = "memejoin-rs"; name = "memejoin-rs";
copyToRoot = buildEnv { copyToRoot = buildEnv {
name = "image-root"; name = "image-root";
paths = [ default cacert openssl openssl.dev ffmpeg libopus youtube-dl yt-dlp sqlite ]; paths = [ default cacert openssl openssl.dev ffmpeg libopus youtube-dl yt-dlp ];
}; };
runAsRoot = '' runAsRoot = ''
#!${runtimeShell} #!${runtimeShell}

View File

@ -1,10 +0,0 @@
alias b := build
alias r := run
set dotenv-load
build:
cargo build
run:
cargo run

View File

@ -1 +1 @@
stable nightly

View File

@ -1,10 +1,5 @@
use std::str::FromStr;
use enum_iterator::Sequence;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::routes::Error;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Discord { pub(crate) struct Discord {
pub(crate) access_token: String, pub(crate) access_token: String,
@ -18,7 +13,6 @@ pub(crate) struct Discord {
pub(crate) struct DiscordSecret { pub(crate) struct DiscordSecret {
pub(crate) client_id: String, pub(crate) client_id: String,
pub(crate) client_secret: String, pub(crate) client_secret: String,
pub(crate) bot_token: String,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -27,83 +21,26 @@ pub(crate) struct User {
pub(crate) name: String, pub(crate) name: String,
} }
#[derive(Default, Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) struct AppPermissions(pub(crate) u8);
impl AppPermissions {
pub(crate) fn can(&self, perm: AppPermission) -> bool {
(self.0 & (perm as u8) > 0) || (self.0 & (AppPermission::Admin as u8) > 0)
}
// FIXME: eventually use this
#[allow(dead_code)]
pub(crate) fn add(&mut self, perm: Permission) {
self.0 |= perm as u8;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Sequence)]
#[repr(u8)]
pub(crate) enum AppPermission {
None = 0,
AddGuild = 1,
Admin = 128,
}
impl AppPermission {
pub(crate) fn all() -> u8 {
0xFF
}
}
impl std::fmt::Display for AppPermission {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
AppPermission::None => todo!(),
AppPermission::AddGuild => "Add Guild".to_string(),
AppPermission::Admin => "Admin".to_string(),
}
)
}
}
impl FromStr for AppPermission {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Add Guild" => Ok(Self::AddGuild),
"Admin" => Ok(Self::Admin),
_ => Err(Self::Err::InvalidRequest),
}
}
}
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) struct Permissions(pub(crate) u8); pub(crate) struct Permissions(pub(crate) u8);
impl Default for Permissions {
fn default() -> Permissions {
Permissions(0)
}
}
impl Permissions { impl Permissions {
pub(crate) fn can(&self, perm: Permission) -> bool { pub(crate) fn can(&self, perm: Permission) -> bool {
(self.0 & (perm as u8) > 0) || (self.0 & (Permission::Moderator as u8) > 0) self.0 & (perm as u8) > 0
}
pub(crate) fn add(&mut self, perm: Permission) {
self.0 |= perm as u8;
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Sequence)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub(crate) enum Permission { pub enum Permission {
None = 0, None,
UploadSounds = 1, UploadSounds,
DeleteSounds = 2, DeleteSounds,
Soundboard = 4,
AddChannel = 8,
Moderator = 128,
} }
impl Permission { impl Permission {
@ -111,35 +48,3 @@ impl Permission {
0xFF 0xFF
} }
} }
impl std::fmt::Display for Permission {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Permission::None => todo!(),
Permission::UploadSounds => "Upload Sounds".to_string(),
Permission::DeleteSounds => "Delete Sounds".to_string(),
Permission::Soundboard => "Soundboard".to_string(),
Permission::AddChannel => "Add Channel".to_string(),
Permission::Moderator => "Moderator".to_string(),
},
)
}
}
impl FromStr for Permission {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Upload Sounds" => Ok(Self::UploadSounds),
"Delete Sounds" => Ok(Self::DeleteSounds),
"Soundboard" => Ok(Self::Soundboard),
"Add Channel" => Ok(Self::AddChannel),
"Moderator" => Ok(Self::Moderator),
_ => Err(Self::Err::InvalidRequest),
}
}
}

View File

@ -1,578 +0,0 @@
use std::path::Path;
use chrono::NaiveDateTime;
use rusqlite::{Connection, OptionalExtension, Result};
use serde::{Deserialize, Serialize};
use tracing::warn;
use crate::auth;
pub struct Database {
conn: Connection,
}
impl Database {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
conn: Connection::open(path)?,
})
}
pub(crate) fn init(&self) -> Result<()> {
self.conn.execute_batch(include_str!("schema.sql"))?;
Ok(())
}
pub(crate) fn get_guild_users(&self, guild_id: u64) -> Result<Vec<String>> {
let mut query = self.conn.prepare(
"
SELECT
username
FROM UserGuild
WHERE guild_id = :guild_id
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
let users = query
.query_map(&[(":guild_id", &guild_id.to_string())], |row| row.get(0))?
.collect::<Result<Vec<String>>>()?;
Ok(users)
}
pub fn get_guild(&self, guild_id: u64) -> Result<String> {
let mut query = self.conn.prepare(
"
SELECT
Guild.name
FROM Guild
WHERE Guild.id = :guild_id
",
)?;
let guild_name =
query.query_row(&[(":guild_id", &guild_id.to_string())], |row| row.get(0))?;
Ok(guild_name)
}
pub(crate) fn get_guilds(&self) -> Result<Vec<Guild>> {
let mut query = self.conn.prepare(
"
SELECT
id, name, sound_delay
FROM Guild
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let guilds = query
.query_map([], |row| {
Ok(Guild {
id: row.get(0)?,
name: row.get(1)?,
sound_delay: row.get(2)?,
})
})?
.into_iter()
.collect::<Result<Vec<Guild>>>();
guilds
}
pub(crate) fn get_user_count(&self) -> Result<i64> {
self.conn.query_row(
"
SELECT
COUNT(username)
FROM User
",
[],
|row| row.get(0),
)
}
pub(crate) fn get_user_from_api_key(&self, api_key: &str) -> Result<User> {
self.conn.query_row(
"
SELECT
username AS name, api_key, api_key_expires_at, discord_token, discord_token_expires_at
FROM User
WHERE api_key = ?1
",
[api_key],
|row| {
Ok(User {
name: row.get(0)?,
api_key: row.get(1)?,
api_key_expires_at: row.get(2)?,
discord_token: row.get(3)?,
discord_token_expires_at: row.get(4)?,
})
},
)
}
pub(crate) fn get_user(&self, username: &str) -> Result<Option<User>> {
self.conn
.query_row(
"
SELECT
username AS name, api_key, api_key_expires_at, discord_token, discord_token_expires_at
FROM User
WHERE name = ?1
",
[username],
|row| {
Ok(User {
name: row.get(0)?,
api_key: row.get(1)?,
api_key_expires_at: row.get(2)?,
discord_token: row.get(3)?,
discord_token_expires_at: row.get(4)?,
})
},
)
.optional()
}
pub fn get_user_guilds(&self, username: &str) -> Result<Vec<Guild>> {
let mut query = self.conn.prepare(
"
SELECT
id, name, sound_delay
FROM Guild
LEFT JOIN UserGuild ON UserGuild.guild_id = Guild.id
WHERE UserGuild.username = :username
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let guilds = query
.query_map(&[(":username", username)], |row| {
Ok(Guild {
id: row.get(0)?,
name: row.get(1)?,
sound_delay: row.get(2)?,
})
})?
.into_iter()
.collect::<Result<Vec<Guild>>>();
guilds
}
pub fn get_guild_intros(&self, guild_id: u64) -> Result<Vec<Intro>> {
let mut query = self.conn.prepare(
"
SELECT
Intro.id,
Intro.name,
Intro.filename
FROM Intro
WHERE
Intro.guild_id = :guild_id
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let intros = query
.query_map(
&[
// :vomit:
(":guild_id", &guild_id.to_string()),
],
|row| {
Ok(Intro {
id: row.get(0)?,
name: row.get(1)?,
filename: row.get(2)?,
})
},
)?
.into_iter()
.collect::<Result<Vec<Intro>>>();
intros
}
pub fn get_all_user_intros(&self, guild_id: u64) -> Result<Vec<UserIntro>> {
let mut query = self.conn.prepare(
"
SELECT
Intro.id,
Intro.name,
Intro.filename,
UI.channel_name,
UI.username
FROM Intro
LEFT JOIN UserIntro UI ON UI.intro_id = Intro.id
WHERE
UI.guild_id = :guild_id
ORDER BY UI.username DESC, UI.channel_name DESC, UI.intro_id;
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let intros = query
.query_map(
&[
// :vomit:
(":guild_id", &guild_id.to_string()),
],
|row| {
Ok(UserIntro {
intro: Intro {
id: row.get(0)?,
name: row.get(1)?,
filename: row.get(2)?,
},
channel_name: row.get(3)?,
username: row.get(4)?,
})
},
)?
.into_iter()
.collect::<Result<Vec<UserIntro>>>();
intros
}
pub(crate) fn get_all_user_permissions(
&self,
guild_id: u64,
) -> Result<Vec<(String, auth::Permissions)>> {
let mut query = self.conn.prepare(
"
SELECT
username,
permissions
FROM UserPermission
WHERE
guild_id = :guild_id
",
)?;
let permissions = query
.query_map(
&[
// :vomit:
(":guild_id", &guild_id.to_string()),
],
|row| Ok((row.get(0)?, auth::Permissions(row.get(1)?))),
)?
.collect::<Result<Vec<(String, auth::Permissions)>>>()?;
Ok(permissions)
}
pub(crate) fn get_user_permissions(
&self,
username: &str,
guild_id: u64,
) -> Result<auth::Permissions> {
self.conn.query_row(
"
SELECT
permissions
FROM UserPermission
WHERE
username = ?1
AND guild_id = ?2
",
[username, &guild_id.to_string()],
|row| Ok(auth::Permissions(row.get(0)?)),
)
}
pub(crate) fn get_user_app_permissions(&self, username: &str) -> Result<auth::AppPermissions> {
self.conn.query_row(
"
SELECT
permissions
FROM UserAppPermission
WHERE
username = ?1
",
[username],
|row| Ok(auth::AppPermissions(row.get(0)?)),
)
}
pub(crate) fn get_guild_channels(&self, guild_id: u64) -> Result<Vec<String>> {
let mut query = self.conn.prepare(
"
SELECT
Channel.name
FROM Channel
WHERE
Channel.guild_id = :guild_id
ORDER BY Channel.name DESC
",
)?;
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let intros = query
.query_map(
&[
// :vomit:
(":guild_id", &guild_id.to_string()),
],
|row| row.get(0),
)?
.into_iter()
.collect::<Result<Vec<String>>>();
intros
}
pub(crate) fn get_user_channel_intros(
&self,
username: &str,
guild_id: u64,
channel_name: &str,
) -> Result<Vec<Intro>> {
let all_user_intros = self.get_all_user_intros(guild_id)?.into_iter();
let intros = all_user_intros
.filter(|intro| intro.username == username && intro.channel_name == channel_name)
.map(|intro| intro.intro)
.collect();
Ok(intros)
}
pub fn insert_guild(&self, guild_id: &u64, name: &str, sound_delay: u32) -> Result<()> {
let affected = self.conn.execute(
"INSERT INTO
Guild (id, name, sound_delay)
VALUES (?1, ?2, ?3)",
[
guild_id.to_string(),
name.to_string(),
sound_delay.to_string(),
],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert guild");
}
Ok(())
}
pub fn insert_guild_channel(&self, guild_id: &u64, name: &str) -> Result<()> {
let affected = self.conn.execute(
"INSERT INTO
Channel (name, guild_id)
VALUES (?1, ?2)",
[name.to_string(), guild_id.to_string()],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert channel");
}
Ok(())
}
pub fn insert_user(
&self,
username: &str,
api_key: &str,
api_key_expires_at: NaiveDateTime,
discord_token: &str,
discord_token_expires_at: NaiveDateTime,
) -> Result<()> {
let affected = self.conn.execute(
"INSERT INTO
User (username, api_key, api_key_expires_at, discord_token, discord_token_expires_at)
VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT(username) DO UPDATE SET api_key = ?2, api_key_expires_at = ?3, discord_token = ?4, discord_token_expires_at = ?5",
[
username,
api_key,
&api_key_expires_at.to_string(),
discord_token,
&discord_token_expires_at.to_string(),
],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert new user");
}
Ok(())
}
pub fn insert_intro(
&self,
name: &str,
volume: i32,
guild_id: u64,
filename: &str,
) -> Result<()> {
let affected = self.conn.execute(
"INSERT INTO
Intro (name, volume, guild_id, filename)
VALUES (?1, ?2, ?3, ?4)",
[name, &volume.to_string(), &guild_id.to_string(), filename],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert intro");
}
Ok(())
}
pub fn insert_user_guild(&self, username: &str, guild_id: u64) -> Result<()> {
let affected = self.conn.execute(
"INSERT OR IGNORE INTO UserGuild (username, guild_id) VALUES (?1, ?2)",
[username, &guild_id.to_string()],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert user guild");
}
Ok(())
}
pub fn insert_user_intro(
&self,
username: &str,
guild_id: u64,
channel_name: &str,
intro_id: i32,
) -> Result<()> {
let affected = self.conn.execute(
"INSERT INTO UserIntro (username, guild_id, channel_name, intro_id) VALUES (?1, ?2, ?3, ?4)",
[
username,
&guild_id.to_string(),
channel_name,
&intro_id.to_string(),
],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert user intro");
}
Ok(())
}
pub(crate) fn insert_user_permission(
&self,
username: &str,
guild_id: u64,
permissions: auth::Permissions,
) -> Result<()> {
let affected = self.conn.execute(
"
INSERT INTO
UserPermission (username, guild_id, permissions)
VALUES (?1, ?2, ?3)
ON CONFLICT(username, guild_id) DO UPDATE SET permissions = ?3",
[username, &guild_id.to_string(), &permissions.0.to_string()],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert user permissions");
}
Ok(())
}
pub(crate) fn insert_user_app_permission(
&self,
username: &str,
permissions: auth::AppPermissions,
) -> Result<()> {
let affected = self.conn.execute(
"
INSERT INTO
UserAppPermission (username, permissions)
VALUES (?1, ?2)
ON CONFLICT(username) DO UPDATE SET permissions = ?2",
[username, &permissions.0.to_string()],
)?;
if affected < 1 {
warn!("no rows affected when attempting to insert user app permissions");
}
Ok(())
}
pub fn delete_user_intro(
&self,
username: &str,
guild_id: u64,
channel_name: &str,
intro_id: i32,
) -> Result<()> {
let affected = self.conn.execute(
"DELETE FROM
UserIntro
WHERE
username = ?1
AND guild_id = ?2
AND channel_name = ?3
AND intro_id = ?4",
[
username,
&guild_id.to_string(),
channel_name,
&intro_id.to_string(),
],
)?;
if affected < 1 {
warn!("no rows affected when attempting to delete user intro");
}
Ok(())
}
}
pub struct Guild {
pub id: u64,
pub name: String,
pub sound_delay: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub name: String,
pub api_key: String,
pub api_key_expires_at: NaiveDateTime,
pub discord_token: String,
pub discord_token_expires_at: NaiveDateTime,
}
pub struct Intro {
pub id: i32,
pub name: String,
pub filename: String,
}
pub struct UserIntro {
pub intro: Intro,
pub channel_name: String,
pub username: String,
}

View File

@ -1,93 +0,0 @@
BEGIN;
create table if not exists User
(
username TEXT not null
constraint User_pk
primary key,
api_key TEXT not null,
api_key_expires_at DATETIME not null,
discord_token TEXT not null,
discord_token_expires_at DATETIME not null
);
create table if not exists Intro
(
id integer not null
constraint Intro_pk
primary key autoincrement,
name TEXT not null,
volume integer not null,
guild_id integer not null
constraint Intro_Guild_guild_id_fk
references Guild ("id"),
filename TEXT not null
);
create table if not exists Guild
(
id integer not null
primary key,
name TEXT not null,
sound_delay integer not null
);
create table if not exists Channel
(
name TEXT
primary key,
guild_id integer
constraint Channel_Guild_id_fk
references Guild (id)
);
create table if not exists UserGuild
(
username TEXT not null
constraint UserGuild_User_username_fk
references User,
guild_id integer not null
constraint UserGuild_Guild_id_fk
references Guild (id),
primary key ("username", "guild_id")
);
create table if not exists UserIntro
(
username text not null
constraint UserIntro_User_username_fk
references User,
intro_id integer not null
constraint UserIntro_Intro_id_fk
references Intro,
guild_id integer not null
constraint UserIntro_Guild_guild_id_fk
references Guild ("id"),
channel_name text not null
constraint UserIntro_Channel_channel_name_fk
references Channel ("name"),
primary key ("username", "intro_id", "guild_id", "channel_name")
);
create table if not exists UserPermission
(
username TEXT not null
constraint UserPermission_User_username_fk
references User,
guild_id integer not null
constraint User_Guild_guild_id_fk
references Guild ("id"),
permissions integer not null,
primary key ("username", "guild_id")
);
create table if not exists UserAppPermission
(
username TEXT not null
constraint UserPermission_User_username_fk
references User,
permissions integer not null,
primary key ("username")
);
COMMIT;

View File

@ -1,5 +1,3 @@
#![allow(dead_code)]
use std::collections::HashMap; use std::collections::HashMap;
pub trait Build { pub trait Build {
@ -193,7 +191,7 @@ impl Build for HtmxBuilder {
} }
} }
if self.tag != Tag::JustText && self.tag != Tag::Empty { if self.tag != Tag::JustText && self.tag != Tag::Empty {
string.push('>'); string.push_str(">");
} }
} }
@ -231,23 +229,23 @@ impl HtmxBuilder {
self self
} }
pub fn hx_get(self, uri: &str) -> Self { pub fn hx_get(mut self, uri: &str) -> Self {
self.attribute("hx-get", uri) self.attribute("hx-get", uri)
} }
pub fn hx_post(self, uri: &str) -> Self { pub fn hx_post(mut self, uri: &str) -> Self {
self.attribute("hx-post", uri) self.attribute("hx-post", uri)
} }
pub fn hx_swap(self, swap_method: SwapMethod) -> Self { pub fn hx_swap(mut self, swap_method: SwapMethod) -> Self {
self.attribute("hx-swap", swap_method.as_str()) self.attribute("hx-swap", swap_method.as_str())
} }
pub fn hx_trigger(self, trigger: &str) -> Self { pub fn hx_trigger(mut self, trigger: &str) -> Self {
self.attribute("hx-trigger", trigger) self.attribute("hx-trigger", trigger)
} }
pub fn hx_target(self, target: &str) -> Self { pub fn hx_target(mut self, target: &str) -> Self {
self.attribute("hx-target", target) self.attribute("hx-target", target)
} }

View File

@ -1,25 +1,28 @@
// #![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
// #![feature(proc_macro_hygiene)] #![feature(proc_macro_hygiene)]
// #![feature(async_closure)] #![feature(async_closure)]
mod auth; mod auth;
mod db;
mod htmx; mod htmx;
mod media; mod media;
mod page; mod page;
mod routes; mod routes;
pub mod settings; pub mod settings;
use axum::http::Method; use axum::http::{HeaderValue, Method};
use axum::routing::{get, post}; use axum::routing::{delete, get, post};
use axum::Router; use axum::Router;
use futures::StreamExt;
use settings::ApiState; use settings::ApiState;
use songbird::tracks::TrackQueue;
use std::collections::HashMap;
use std::env; use std::env;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tower_http::cors::{Any, CorsLayer}; use tower_http::cors::{Any, CorsLayer};
use serde::Deserialize;
use serenity::async_trait; use serenity::async_trait;
use serenity::model::prelude::{Channel, ChannelId, GuildId, Member, Ready}; use serenity::model::prelude::{Channel, ChannelId, GuildId, Member, Ready};
use serenity::model::voice::VoiceState; use serenity::model::voice::VoiceState;
@ -28,7 +31,7 @@ use serenity::prelude::*;
use songbird::SerenityInit; use songbird::SerenityInit;
use tracing::*; use tracing::*;
use crate::settings::Settings; use crate::settings::{Intro, Settings};
enum HandlerMessage { enum HandlerMessage {
Ready(Context), Ready(Context),
@ -116,17 +119,16 @@ impl EventHandler for Handler {
} }
} }
fn spawn_api(db: Arc<tokio::sync::Mutex<db::Database>>) { fn spawn_api(settings: Arc<Mutex<Settings>>) {
let secrets = auth::DiscordSecret { let secrets = auth::DiscordSecret {
client_id: env::var("DISCORD_CLIENT_ID").expect("expected DISCORD_CLIENT_ID env var"), client_id: env::var("DISCORD_CLIENT_ID").expect("expected DISCORD_CLIENT_ID env var"),
client_secret: env::var("DISCORD_CLIENT_SECRET") client_secret: env::var("DISCORD_CLIENT_SECRET")
.expect("expected DISCORD_CLIENT_SECRET env var"), .expect("expected DISCORD_CLIENT_SECRET env var"),
bot_token: env::var("DISCORD_TOKEN").expect("expected DISCORD_TOKEN env var"),
}; };
let origin = env::var("APP_ORIGIN").expect("expected APP_ORIGIN"); let origin = env::var("APP_ORIGIN").expect("expected APP_ORIGIN");
let state = ApiState { let state = ApiState {
db, settings,
secrets, secrets,
origin: origin.clone(), origin: origin.clone(),
}; };
@ -137,15 +139,6 @@ fn spawn_api(db: Arc<tokio::sync::Mutex<db::Database>>) {
.route("/index.html", get(page::home)) .route("/index.html", get(page::home))
.route("/login", get(page::login)) .route("/login", get(page::login))
.route("/guild/:guild_id", get(page::guild_dashboard)) .route("/guild/:guild_id", get(page::guild_dashboard))
.route("/guild/:guild_id/setup", get(routes::guild_setup))
.route(
"/guild/:guild_id/add_channel",
post(routes::guild_add_channel),
)
.route(
"/guild/:guild_id/permissions/update",
post(routes::update_guild_permissions),
)
.route("/v2/auth", get(routes::v2_auth)) .route("/v2/auth", get(routes::v2_auth))
.route( .route(
"/v2/intros/add/:guild_id/:channel", "/v2/intros/add/:guild_id/:channel",
@ -161,8 +154,23 @@ fn spawn_api(db: Arc<tokio::sync::Mutex<db::Database>>) {
post(routes::v2_upload_guild_intro), post(routes::v2_upload_guild_intro),
) )
.route("/health", get(routes::health)) .route("/health", get(routes::health))
.route("/me", get(routes::me))
.route("/intros/:guild", get(routes::intros))
.route("/intros/:guild/add", get(routes::add_guild_intro))
.route("/intros/:guild/upload", post(routes::upload_guild_intro))
.route("/intros/:guild/delete", delete(routes::delete_guild_intro))
.route(
"/intros/:guild/:channel/:intro",
post(routes::add_intro_to_user),
)
.route(
"/intros/:guild/:channel/:intro/remove",
post(routes::remove_intro_to_user),
)
.route("/auth", get(routes::auth))
.layer( .layer(
CorsLayer::new() CorsLayer::new()
// TODO: move this to env variable
.allow_origin([origin.parse().unwrap()]) .allow_origin([origin.parse().unwrap()])
.allow_headers(Any) .allow_headers(Any)
.allow_methods([Method::GET, Method::POST, Method::DELETE]), .allow_methods([Method::GET, Method::POST, Method::DELETE]),
@ -177,7 +185,7 @@ fn spawn_api(db: Arc<tokio::sync::Mutex<db::Database>>) {
}); });
} }
async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) { async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
let token = env::var("DISCORD_TOKEN").expect("expected DISCORD_TOKEN env var"); let token = env::var("DISCORD_TOKEN").expect("expected DISCORD_TOKEN env var");
let songbird = songbird::Songbird::serenity(); let songbird = songbird::Songbird::serenity();
@ -207,19 +215,12 @@ async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) {
match msg { match msg {
HandlerMessage::Ready(ctx) => { HandlerMessage::Ready(ctx) => {
info!("Got Ready message"); info!("Got Ready message");
let settings = settings.lock().await;
let songbird = songbird::get(&ctx).await.expect("no songbird instance"); let songbird = songbird::get(&ctx).await.expect("no songbird instance");
let guilds = match db.lock().await.get_guilds() { for guild_id in settings.guilds.keys() {
Ok(guilds) => guilds, let handler_lock = songbird.get_or_insert(GuildId(*guild_id));
Err(err) => {
error!(?err, "failed to get guild on bot ready");
continue;
}
};
for guild in guilds {
let handler_lock = songbird.get_or_insert(GuildId(guild.id));
let mut handler = handler_lock.lock().await; let mut handler = handler_lock.lock().await;
@ -227,7 +228,7 @@ async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) {
songbird::Event::Track(songbird::TrackEvent::End), songbird::Event::Track(songbird::TrackEvent::End),
TrackEventHandler { TrackEventHandler {
tx: tx.clone(), tx: tx.clone(),
guild_id: GuildId(guild.id), guild_id: GuildId(*guild_id),
}, },
); );
} }
@ -250,6 +251,7 @@ async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) {
HandlerMessage::PlaySound(ctx, member, channel_id) => { HandlerMessage::PlaySound(ctx, member, channel_id) => {
info!("Got PlaySound message"); info!("Got PlaySound message");
let settings = settings.lock().await;
let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx.cache) let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx.cache)
else { else {
@ -257,35 +259,60 @@ async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) {
continue; continue;
}; };
let intros = match db.lock().await.get_user_channel_intros( let Some(guild_settings) = settings.guilds.get(channel.guild_id.as_u64())
&member.user.name, else {
channel.guild_id.0, error!("couldn't get guild from id: {}", channel.guild_id.as_u64());
channel.name(), continue;
) { };
Ok(intros) => intros, let Some(channel_settings) = guild_settings.channels.get(channel.name()) else {
Err(err) => { error!(
error!( "couldn't get channel_settings from name: {}",
?err, channel.name()
"failed to get user channel intros when playing sound through bot" );
); continue;
continue; };
} let Some(user) = channel_settings.users.get(&member.user.name) else {
error!(
"couldn't get user settings from name: {}",
&member.user.name
);
continue;
}; };
// TODO: randomly choose a intro to play // TODO: randomly choose a intro to play
let Some(intro) = intros.first() else { let Some(intro) = user.intros.first() else {
error!("couldn't get user intro, none exist"); error!("couldn't get user intro, none exist");
continue; continue;
}; };
let source = match songbird::ffmpeg(format!("sounds/{}", &intro.filename)).await let source = match guild_settings.intros.get(&intro.index) {
{ Some(Intro::Online(intro)) => match songbird::ytdl(&intro.url).await {
Ok(source) => source, Ok(source) => source,
Err(err) => { Err(err) => {
error!("Error starting youtube source from {}: {err:?}", intro.url);
continue;
}
},
Some(Intro::File(intro)) => {
match songbird::ffmpeg(format!("sounds/{}", &intro.filename)).await {
Ok(source) => source,
Err(err) => {
error!(
"Error starting file source from {}: {err:?}",
intro.filename
);
continue;
}
}
}
None => {
error!( error!(
"Error starting file source from {}: {err:?}", "Failed to find intro for user {} on guild {} in channel {}, IntroIndex: {}",
intro.filename member.user.name,
); channel.guild_id.as_u64(),
channel.name(),
intro.index
);
continue; continue;
} }
}; };
@ -319,29 +346,23 @@ async fn main() -> std::io::Result<()> {
&std::fs::read_to_string("config/settings.json").expect("no config/settings.json"), &std::fs::read_to_string("config/settings.json").expect("no config/settings.json"),
) )
.expect("error parsing settings file"); .expect("error parsing settings file");
info!("{settings:?}");
let (run_api, run_bot) = (settings.run_api, settings.run_bot); let (run_api, run_bot) = (settings.run_api, settings.run_bot);
let db = Arc::new(tokio::sync::Mutex::new(
db::Database::new("./config/db.sqlite").expect("couldn't open sqlite db"),
));
{ info!("{settings:?}");
// attempt to initialize the database with the schema
let db = db.lock().await;
db.init().expect("couldn't init db");
}
let settings = Arc::new(Mutex::new(settings));
if run_api { if run_api {
spawn_api(db.clone()); spawn_api(settings.clone());
} }
if run_bot { if run_bot {
spawn_bot(db).await; spawn_bot(settings.clone()).await;
} }
info!("spawned background tasks"); info!("spawned background tasks");
let _ = tokio::signal::ctrl_c().await; let _ = tokio::signal::ctrl_c().await;
settings.lock().await.save()?;
info!("Received Ctrl-C, shuttdown down."); info!("Received Ctrl-C, shuttdown down.");
Ok(()) Ok(())

View File

@ -1,14 +1,12 @@
use crate::{ use crate::{
auth, auth::{self, User},
db::{self, User},
htmx::{Build, HtmxBuilder, Tag}, htmx::{Build, HtmxBuilder, Tag},
settings::ApiState, settings::{ApiState, GuildSettings, Intro, IntroFriendlyName},
}; };
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
response::{Html, Redirect}, response::{Html, Redirect},
}; };
use iter_tools::Itertools;
use tracing::error; use tracing::error;
fn page_header(title: &str) -> HtmxBuilder { fn page_header(title: &str) -> HtmxBuilder {
@ -20,7 +18,7 @@ fn page_header(title: &str) -> HtmxBuilder {
) )
// Not currently using // Not currently using
// .script("https://unpkg.com/hyperscript.org@0.9.9", None) // .script("https://unpkg.com/hyperscript.org@0.9.9", None)
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css") .style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
}) })
} }
@ -29,82 +27,19 @@ pub(crate) async fn home(
user: Option<User>, user: Option<User>,
) -> Result<Html<String>, Redirect> { ) -> Result<Html<String>, Redirect> {
if let Some(user) = user { if let Some(user) = user {
let db = state.db.lock().await; let settings = state.settings.lock().await;
let needs_setup = db let guild = settings
.get_guilds() .guilds
.map_err(|err| { .iter()
error!(?err, "failed to get user guilds"); .filter(|(_, guild_settings)| guild_settings.users.contains_key(&user.name));
// TODO: change this to returning a error to the client
Redirect::to(&format!("{}/error", state.origin))
})?
.is_empty();
let user_guilds = db.get_user_guilds(&user.name).map_err(|err| {
error!(?err, "failed to get user guilds");
// TODO: change this to returning a error to the client
Redirect::to(&format!("{}/login", state.origin))
})?;
let user_app_permissions = db.get_user_app_permissions(&user.name).unwrap_or_default();
let can_add_guild = user_app_permissions.can(auth::AppPermission::AddGuild);
let client = reqwest::Client::new();
let discord_guilds: Vec<crate::routes::DiscordUserGuild> = if can_add_guild {
client
.get("https://discord.com/api/v10/users/@me/guilds")
.bearer_auth(&user.discord_token)
.send()
.await
.map_err(|err| {
error!(?err, "failed to get guilds");
// TODO: change this to returning a error to the client
Redirect::to(&format!("{}/error", state.origin))
})?
.json()
.await
.map_err(|err| {
error!(?err, "failed to parse json");
// TODO: change this to returning a error to the client
Redirect::to(&format!("{}/error", state.origin))
})?
} else {
vec![]
}
.into_iter()
// lol, why does this need to have an explicit type annotation
.filter(|discord_guild: &crate::routes::DiscordUserGuild| {
!user_guilds
.iter()
.any(|user_guild| discord_guild.id == user_guild.id)
})
.collect();
let guild_list = if needs_setup {
HtmxBuilder::new(Tag::Empty).builder(Tag::Div, |b| {
b.attribute("class", "container")
.builder_text(Tag::Header2, "Select a Guild to setup")
.push_builder(setup_guild_list(&state.origin, &discord_guilds))
})
} else {
HtmxBuilder::new(Tag::Empty).builder(Tag::Div, |b| {
b.attribute("class", "container")
.builder_text(Tag::Header2, "Choose a Guild")
.push_builder(guild_list(&state.origin, user_guilds.iter()))
})
};
Ok(Html( Ok(Html(
page_header("MemeJoin - Home") page_header("MemeJoin - Home")
.builder(Tag::Div, |b| { .builder(Tag::Div, |b| {
let mut b = b.push_builder(guild_list); b.attribute("class", "container")
.builder_text(Tag::Header2, "Choose a Guild")
if !needs_setup && can_add_guild && !discord_guilds.is_empty() { .push_builder(guild_list(&state.origin, guild))
b = b
.attribute("class", "container")
.builder_text(Tag::Header2, "Add a Guild")
.push_builder(setup_guild_list(&state.origin, &discord_guilds));
}
b
}) })
.build(), .build(),
)) ))
@ -113,28 +48,26 @@ pub(crate) async fn home(
} }
} }
fn setup_guild_list(origin: &str, user_guilds: &[crate::routes::DiscordUserGuild]) -> HtmxBuilder { fn guild_list<'a>(
origin: &str,
guilds: impl Iterator<Item = (&'a u64, &'a GuildSettings)>,
) -> HtmxBuilder {
HtmxBuilder::new(Tag::Empty).ul(|b| { HtmxBuilder::new(Tag::Empty).ul(|b| {
let mut b = b; let mut b = b;
for guild in user_guilds { let mut in_any_guilds = false;
for (guild_id, guild_settings) in guilds {
in_any_guilds = true;
b = b.li(|b| { b = b.li(|b| {
b.link( b.link(
&guild.name, &guild_settings.name,
// TODO: url encode the name &format!("{}/guild/{}", origin, guild_id),
&format!("{}/guild/{}/setup?name={}", origin, guild.id, guild.name),
) )
}); });
} }
b if !in_any_guilds {
}) b = b.builder_text(Tag::Header4, "Looks like you aren't in any guilds");
}
fn guild_list<'a>(origin: &str, guilds: impl Iterator<Item = &'a db::Guild>) -> HtmxBuilder {
HtmxBuilder::new(Tag::Empty).ul(|b| {
let mut b = b;
for guild in guilds {
b = b.li(|b| b.link(&guild.name, &format!("{}/guild/{}", origin, guild.id)));
} }
b b
@ -142,26 +75,24 @@ fn guild_list<'a>(origin: &str, guilds: impl Iterator<Item = &'a db::Guild>) ->
} }
fn intro_list<'a>( fn intro_list<'a>(
intros: impl Iterator<Item = &'a db::Intro>, intros: impl Iterator<Item = (&'a String, &'a Intro)>,
label: &str, label: &str,
post: &str, post: &str,
) -> HtmxBuilder { ) -> HtmxBuilder {
HtmxBuilder::new(Tag::Empty).form(|b| { HtmxBuilder::new(Tag::Empty).form(|b| {
b.attribute("class", "container") b.attribute("class", "container")
.hx_post(post) .hx_post(post)
.hx_target("closest #channel-intro-selector")
.attribute("hx-encoding", "multipart/form-data") .attribute("hx-encoding", "multipart/form-data")
.builder(Tag::FieldSet, |b| { .builder(Tag::FieldSet, |b| {
let mut b = b let mut b = b
.attribute("class", "container") .attribute("class", "container")
.attribute("style", "height: 256px; overflow: auto"); .attribute("style", "max-height: 50%; overflow-y: scroll");
for intro in intros { for intro in intros {
b = b.builder(Tag::Label, |b| { b = b.builder(Tag::Label, |b| {
b.builder(Tag::Input, |b| { b.builder(Tag::Input, |b| {
b.attribute("type", "checkbox") b.attribute("type", "checkbox").attribute("name", &intro.0)
.attribute("name", &intro.id.to_string())
}) })
.builder_text(Tag::Paragraph, &intro.name) .builder_text(Tag::Paragraph, intro.1.friendly_name())
}); });
} }
@ -176,53 +107,19 @@ pub(crate) async fn guild_dashboard(
user: User, user: User,
Path(guild_id): Path<u64>, Path(guild_id): Path<u64>,
) -> Result<Html<String>, Redirect> { ) -> Result<Html<String>, Redirect> {
let (guild_name, guild_intros, guild_channels, all_user_intros, user_permissions) = { let settings = state.settings.lock().await;
let db = state.db.lock().await;
let guild_name = db.get_guild(guild_id).map_err(|err| { let Some(guild) = settings.guilds.get(&guild_id) else {
error!(?err, %guild_id, "couldn't get guild"); error!(%guild_id, "no such guild");
// TODO: change to actual error return Err(Redirect::to(&format!("{}/", state.origin)));
Redirect::to(&format!("{}/login", state.origin)) };
})?; let Some(guild_user) = guild.users.get(&user.name) else {
error!(%guild_id, %user.name, "no user in guild");
let guild_intros = db.get_guild_intros(guild_id).map_err(|err| { return Err(Redirect::to(&format!("{}/", state.origin)));
error!(?err, %guild_id, "couldn't get guild intros");
// TODO: change to actual error
Redirect::to(&format!("{}/login", state.origin))
})?;
let guild_channels = db.get_guild_channels(guild_id).map_err(|err| {
error!(?err, %guild_id, "couldn't get guild channels");
// TODO: change to actual error
Redirect::to(&format!("{}/login", state.origin))
})?;
let all_user_intros = db.get_all_user_intros(guild_id).map_err(|err| {
error!(?err, %guild_id, "couldn't get user intros");
// TODO: change to actual error
Redirect::to(&format!("{}/login", state.origin))
})?;
let user_permissions = db
.get_user_permissions(&user.name, guild_id)
.unwrap_or_default();
(
guild_name,
guild_intros,
guild_channels,
all_user_intros,
user_permissions,
)
}; };
let can_upload = user_permissions.can(auth::Permission::UploadSounds); let can_upload = guild_user.permissions.can(auth::Permission::UploadSounds);
let can_add_channel = user_permissions.can(auth::Permission::AddChannel); let is_moderator = guild_user.permissions.can(auth::Permission::DeleteSounds);
let is_moderator = user_permissions.can(auth::Permission::Moderator);
let mod_dashboard =
moderator_dashboard(&state, &state.secrets.bot_token, guild_id, user_permissions).await;
let user_intros = all_user_intros
.iter()
.filter(|intro| intro.username == user.name)
.group_by(|intro| &intro.channel_name);
Ok(Html( Ok(Html(
HtmxBuilder::new(Tag::Html) HtmxBuilder::new(Tag::Html)
@ -231,16 +128,17 @@ pub(crate) async fn guild_dashboard(
b.builder(Tag::HeaderGroup, |b| { b.builder(Tag::HeaderGroup, |b| {
b.attribute("class", "container") b.attribute("class", "container")
.builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros")) .builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros"))
.builder_text(Tag::Header6, &format!("{} - {}", user.name, guild_name)) .builder_text(Tag::Header6, &user.name)
}) })
}) })
.builder(Tag::Empty, |b| { .builder(Tag::Empty, |b| {
let mut b = if is_moderator || can_add_channel { let mut b = if is_moderator {
b.builder(Tag::Div, |b| { b.builder(Tag::Div, |b| {
b.attribute("class", "container") b.attribute("class", "container")
.builder(Tag::Article, |b| { .builder(Tag::Article, |b| {
b.builder_text(Tag::Header, "Server Settings") b.builder_text(Tag::Header, "Wow, you're a moderator")
.push_builder(mod_dashboard) .push_builder(moderator_dashboard(&state))
.builder_text(Tag::Footer, "End of super cool mod section")
}) })
}) })
} else { } else {
@ -270,37 +168,53 @@ pub(crate) async fn guild_dashboard(
.builder(Tag::Article, |b| { .builder(Tag::Article, |b| {
let mut b = b.builder_text(Tag::Header, "Guild Intros"); let mut b = b.builder_text(Tag::Header, "Guild Intros");
let mut user_intros = user_intros.into_iter().peekable(); for (channel_name, channel_settings) in &guild.channels {
if let Some(channel_user) = channel_settings.users.get(&user.name) {
for guild_channel_name in &guild_channels { let current_intros =
// Get user intros for this channel channel_user.intros.iter().filter_map(|intro_index| {
let intros = user_intros Some((
.peeking_take_while(|(channel_name, _)| { &intro_index.index,
channel_name == &guild_channel_name guild.intros.get(&intro_index.index)?,
}) ))
.map(|(_, intros)| intros.map(|intro| &intro.intro)) });
.flatten(); let available_intros =
guild.intros.iter().filter_map(|intro| {
b = b.builder(Tag::Details, |b| { if !channel_user
let mut b = b; .intros
if guild_channels.len() < 2 { .iter()
b = b.attribute("open", ""); .any(|intro_index| intro.0 == &intro_index.index)
} {
b.builder_text(Tag::Summary, guild_channel_name).builder( Some((intro.0, intro.1))
Tag::Div, } else {
|b| { None
b.attribute("id", "channel-intro-selector") }
.attribute("style", "display: flex; align-items: flex-end; max-height: 50%; overflow: hidden;") });
.push_builder(channel_intro_selector( b = b.builder(Tag::Article, |b| {
&state.origin, b.builder_text(Tag::Header, channel_name).builder(
guild_id, Tag::Div,
guild_channel_name, |b| {
intros, b.builder_text(Tag::Strong, "Your Current Intros")
guild_intros.iter(), .push_builder(intro_list(
)) current_intros,
}, "Remove Intro",
) &format!(
}); "{}/v2/intros/remove/{}/{}",
state.origin, guild_id, channel_name
),
))
.builder_text(Tag::Strong, "Select Intros")
.push_builder(intro_list(
available_intros,
"Add Intro",
&format!(
"{}/v2/intros/add/{}/{}",
state.origin, guild_id, channel_name
),
))
},
)
});
}
} }
b b
@ -311,34 +225,6 @@ pub(crate) async fn guild_dashboard(
)) ))
} }
pub fn channel_intro_selector<'a>(
origin: &str,
guild_id: u64,
channel_name: &String,
intros: impl Iterator<Item = &'a db::Intro>,
guild_intros: impl Iterator<Item = &'a db::Intro>,
) -> HtmxBuilder {
HtmxBuilder::new(Tag::Empty)
.builder(Tag::Div, |b| {
b.attribute("style", "display: flex; flex-direction: column; justify-content: space-between; align-items: center; width: 100%; height: 100%; padding: 16px;")
.builder_text(Tag::Strong, "Your Current Intros")
.push_builder(intro_list(
intros,
"Remove Intro",
&format!("{}/v2/intros/remove/{}/{}", origin, guild_id, &channel_name),
))
})
.builder(Tag::Div, |b| {
b.attribute("style", "display: flex; flex-direction: column; justify-content: space-between; align-items: center; width: 100%; height: 100%; padding: 16px;")
.builder_text(Tag::Strong, "Select Intros")
.push_builder(intro_list(
guild_intros,
"Add Intro",
&format!("{}/v2/intros/add/{}/{}", origin, guild_id, channel_name),
))
})
}
fn upload_form(origin: &str, guild_id: u64) -> HtmxBuilder { fn upload_form(origin: &str, guild_id: u64) -> HtmxBuilder {
HtmxBuilder::new(Tag::Empty).form(|b| { HtmxBuilder::new(Tag::Empty).form(|b| {
b.attribute("class", "container") b.attribute("class", "container")
@ -346,14 +232,16 @@ fn upload_form(origin: &str, guild_id: u64) -> HtmxBuilder {
.attribute("hx-encoding", "multipart/form-data") .attribute("hx-encoding", "multipart/form-data")
.builder(Tag::FieldSet, |b| { .builder(Tag::FieldSet, |b| {
b.attribute("class", "container") b.attribute("class", "container")
.attribute("role", "group")
.input(|b| b.attribute("type", "file").attribute("name", "file"))
.input(|b| { .input(|b| {
b.attribute("name", "name") b.attribute("name", "name")
.attribute("placeholder", "enter intro title") .attribute("placeholder", "enter intro title")
}) })
.button(|b| b.attribute("type", "submit").text("Upload")) .label(|b| {
b.text("Choose File")
.input(|b| b.attribute("type", "file").attribute("name", "file"))
})
}) })
.button(|b| b.attribute("type", "submit").text("Upload"))
}) })
} }
@ -363,212 +251,34 @@ fn ytdl_form(origin: &str, guild_id: u64) -> HtmxBuilder {
.hx_get(&format!("{}/v2/intros/{}/add", origin, guild_id)) .hx_get(&format!("{}/v2/intros/{}/add", origin, guild_id))
.builder(Tag::FieldSet, |b| { .builder(Tag::FieldSet, |b| {
b.attribute("class", "container") b.attribute("class", "container")
.attribute("role", "group") .label(|b| {
.input(|b| { b.text("Video Url").input(|b| {
b.attribute("placeholder", "enter video url") b.attribute("placeholder", "enter video url")
.attribute("name", "url") .attribute("name", "url")
})
}) })
.input(|b| { .label(|b| {
b.attribute("placeholder", "enter intro title") b.text("Intro Title").input(|b| {
.attribute("name", "name") b.attribute("placeholder", "enter intro title")
.attribute("name", "name")
})
}) })
.button(|b| b.attribute("type", "submit").text("Upload"))
}) })
.button(|b| b.attribute("type", "submit").text("Upload"))
}) })
} }
async fn moderator_dashboard( fn moderator_dashboard(state: &ApiState) -> HtmxBuilder {
state: &ApiState, HtmxBuilder::new(Tag::Empty).link("Go back to old UI", &format!("{}/old", state.origin))
bot_token: &str,
guild_id: u64,
user_permissions: auth::Permissions,
) -> HtmxBuilder {
let permissions_editor = permissions_editor(state, guild_id).await;
let channel_editor = channel_editor(state, bot_token, guild_id).await;
let mut b = HtmxBuilder::new(Tag::Empty);
if user_permissions.can(auth::Permission::Moderator) {
b = b.push_builder(permissions_editor);
}
if user_permissions.can(auth::Permission::AddChannel) {
b = b.push_builder(channel_editor);
}
b
} }
async fn channel_editor(state: &ApiState, bot_token: &str, guild_id: u64) -> HtmxBuilder { pub(crate) async fn login(State(state): State<ApiState>) -> Html<String> {
let db = state.db.lock().await; let authorize_uri = format!("https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}/v2/auth&response_type=code&scope=guilds.members.read%20guilds%20identify", state.secrets.client_id, state.origin);
let added_guild_channels = db.get_guild_channels(guild_id).unwrap_or_default();
let mut got_channels = true; Html(
let client = reqwest::Client::new(); HtmxBuilder::new(Tag::Html)
let channels: Vec<String> = { .push_builder(page_header("MemeJoin - Login"))
match client .link("Login", &authorize_uri)
.get(format!( .build(),
"https://discord.com/api/v10/guilds/{}/channels", )
guild_id
))
.header("Authorization", format!("Bot {}", bot_token))
.send()
.await
{
Ok(resp) => match resp.json::<Vec<crate::routes::DiscordChannel>>().await {
Ok(channels) => channels
.into_iter()
.filter(|channel| channel.ty == crate::routes::ChannelType::GuildVoice as u32)
.filter_map(|channel| channel.name)
.filter(|name| !added_guild_channels.contains(name))
.collect(),
Err(err) => {
error!(?err, "failed to parse json");
got_channels = false;
vec![]
}
},
Err(err) => {
error!(?err, "failed to get channels");
got_channels = false;
vec![]
}
}
};
if got_channels && !channels.is_empty() {
HtmxBuilder::new(Tag::Details)
.builder_text(Tag::Summary, "Add Channels")
.form(|b| {
b.attribute("class", "container")
.hx_post(&format!("{}/guild/{}/add_channel", state.origin, guild_id))
.attribute("hx-encoding", "multipart/form-data")
.builder(Tag::FieldSet, |b| {
let mut b = b
.attribute("class", "container")
.attribute("style", "max-height: 50%; overflow-y: scroll");
for channel_name in channels {
b = b.builder(Tag::Label, |b| {
b.builder(Tag::Input, |b| {
b.attribute("type", "checkbox")
.attribute("name", &channel_name.to_string())
})
.builder_text(Tag::Paragraph, &channel_name)
});
}
b
})
.button(|b| b.attribute("type", "submit").text("Add Channel"))
})
} else if channels.is_empty() {
HtmxBuilder::new(Tag::Empty)
} else {
HtmxBuilder::new(Tag::Empty).text("Failed to get channels")
}
}
async fn permissions_editor(state: &ApiState, guild_id: u64) -> HtmxBuilder {
let db = state.db.lock().await;
let user_permissions = db.get_all_user_permissions(guild_id).unwrap_or_default();
HtmxBuilder::new(Tag::Details)
.builder_text(Tag::Summary, "Permissions")
.form(|b| {
b.hx_post(&format!(
"{}/guild/{}/permissions/update",
state.origin, guild_id
))
.attribute("hx-encoding", "multipart/form-data")
.builder(Tag::Table, |b| {
let mut b = b.attribute("role", "grid").builder(Tag::TableHead, |b| {
let mut b = b.builder_text(Tag::TableHeader, "User");
for perm in enum_iterator::all::<auth::Permission>() {
if perm == auth::Permission::Moderator || perm == auth::Permission::None {
continue;
}
b = b.builder_text(Tag::TableHeader, &perm.to_string());
}
b
});
for permission in user_permissions {
b = b.builder(Tag::TableRow, |b| {
let mut b = b.builder_text(Tag::TableData, permission.0.as_str());
for perm in enum_iterator::all::<auth::Permission>() {
if perm == auth::Permission::Moderator || perm == auth::Permission::None
{
continue;
}
b = b.builder(Tag::TableData, |b| {
b.builder(Tag::Input, |b| {
let mut b = b
.attribute("type", "checkbox")
.attribute("name", &format!("{}#{}", permission.0, perm));
if permission.1.can(auth::Permission::Moderator) {
b = b.flag("disabled");
}
if permission.1.can(perm) {
return b.flag("checked");
}
b
})
});
}
b
});
}
b
})
.button(|b| b.attribute("type", "submit").text("Update Permissions"))
})
}
pub(crate) async fn login(
State(state): State<ApiState>,
user: Option<User>,
) -> Result<Html<String>, Redirect> {
if user.is_some() {
Err(Redirect::to(&format!("{}/", state.origin)))
} else {
let authorize_uri = format!("https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}/v2/auth&response_type=code&scope=guilds.members.read+guilds+identify", state.secrets.client_id, state.origin);
Ok(Html(
HtmxBuilder::new(Tag::Html)
.push_builder(page_header("MemeJoin - Dashboard"))
.builder(Tag::Nav, |b| {
b.builder(Tag::HeaderGroup, |b| {
b.attribute("class", "container")
.builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros"))
.builder_text(Tag::Header6, "salad")
})
})
.builder(Tag::Main, |b| {
b.attribute("class", "container").builder(Tag::Anchor, |b| {
b.attribute("role", "button")
.text("Login with Discord")
.attribute("href", &authorize_uri)
})
})
.build(),
))
}
//Html(
// HtmxBuilder::new(Tag::Html)
// .push_builder(page_header("MemeJoin - Login"))
// .link("Login", &authorize_uri)
// .build(),
//)
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,52 +1,41 @@
use std::sync::Arc; use std::{collections::HashMap, sync::Arc};
use crate::{ use crate::auth;
auth,
db::{self, Database},
};
use axum::{async_trait, extract::FromRequestParts, http::request::Parts, response::Redirect}; use axum::{async_trait, extract::FromRequestParts, http::request::Parts, response::Redirect};
use axum_extra::extract::CookieJar; use axum_extra::extract::CookieJar;
use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use tracing::error; use tracing::trace;
use uuid::Uuid;
type UserToken = String;
// TODO: make this is wrapped type so cloning isn't happening // TODO: make this is wrapped type so cloning isn't happening
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct ApiState { pub(crate) struct ApiState {
pub db: Arc<tokio::sync::Mutex<Database>>, pub settings: Arc<tokio::sync::Mutex<Settings>>,
pub secrets: auth::DiscordSecret, pub secrets: auth::DiscordSecret,
pub origin: String, pub origin: String,
} }
#[async_trait] #[async_trait]
impl FromRequestParts<ApiState> for db::User { impl FromRequestParts<ApiState> for crate::auth::User {
type Rejection = Redirect; type Rejection = Redirect;
async fn from_request_parts( async fn from_request_parts(
Parts { headers, .. }: &mut Parts, Parts { headers, .. }: &mut Parts,
state: &ApiState, state: &ApiState,
) -> Result<Self, Self::Rejection> { ) -> Result<Self, Self::Rejection> {
let jar = CookieJar::from_headers(headers); let jar = CookieJar::from_headers(&headers);
if let Some(token) = jar.get("access_token") { if let Some(token) = jar.get("access_token") {
match state.db.lock().await.get_user_from_api_key(token.value()) { match state.settings.lock().await.auth_users.get(token.value()) {
Ok(user) => { // :vomit:
let now = Utc::now().naive_utc(); Some(user) => Ok(user.clone()),
if user.api_key_expires_at < now || user.discord_token_expires_at < now { None => Err(Redirect::to("/login")),
Err(Redirect::to(&format!("{}/login", state.origin)))
} else {
Ok(user)
}
}
Err(err) => {
error!(?err, "failed to authenticate user");
Err(Redirect::to(&format!("{}/login", state.origin)))
}
} }
} else { } else {
Err(Redirect::to(&format!("{}/login", state.origin))) Err(Redirect::to("/login"))
} }
} }
} }
@ -58,7 +47,116 @@ pub(crate) struct Settings {
pub(crate) run_api: bool, pub(crate) run_api: bool,
#[serde(default)] #[serde(default)]
pub(crate) run_bot: bool, pub(crate) run_bot: bool,
pub(crate) guilds: HashMap<u64, GuildSettings>,
#[serde(default)]
pub(crate) auth_users: HashMap<UserToken, auth::User>,
} }
impl TypeMapKey for Settings { impl TypeMapKey for Settings {
type Value = Arc<Settings>; type Value = Arc<Settings>;
} }
impl Settings {
pub(crate) fn save(&self) -> Result<(), std::io::Error> {
trace!("attempting to save config");
let serialized = serde_json::to_string_pretty(&self)?;
std::fs::copy(
"./config/settings.json",
format!(
"./config/{}-settings.json.old",
chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S")
),
)?;
trace!("created copy of original settings");
std::fs::write("./config/settings.json", serialized)?;
trace!("saved settings to disk");
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GuildSettings {
pub(crate) name: String,
pub(crate) sound_delay: u64,
#[serde(default)]
pub(crate) channels: HashMap<String, ChannelSettings>,
#[serde(default)]
pub(crate) intros: HashMap<String, Intro>,
#[serde(default)]
pub(crate) users: HashMap<String, GuildUser>,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GuildUser {
pub(crate) permissions: auth::Permissions,
}
pub(crate) trait IntroFriendlyName {
fn friendly_name(&self) -> &str;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) enum Intro {
File(FileIntro),
Online(OnlineIntro),
}
impl IntroFriendlyName for Intro {
fn friendly_name(&self) -> &str {
match self {
Self::File(intro) => intro.friendly_name(),
Self::Online(intro) => intro.friendly_name(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct FileIntro {
pub(crate) filename: String,
pub(crate) friendly_name: String,
}
impl IntroFriendlyName for FileIntro {
fn friendly_name(&self) -> &str {
&self.friendly_name
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct OnlineIntro {
pub(crate) url: String,
pub(crate) friendly_name: String,
}
impl IntroFriendlyName for OnlineIntro {
fn friendly_name(&self) -> &str {
&self.friendly_name
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ChannelSettings {
#[serde(alias = "enterUsers")]
pub(crate) users: HashMap<String, UserSettings>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct IntroIndex {
pub(crate) index: String,
pub(crate) volume: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct UserSettings {
pub(crate) intros: Vec<IntroIndex>,
}