Compare commits
26 Commits
v0.1.4_4-a
...
master
Author | SHA1 | Date |
---|---|---|
|
85477d3c86 | |
|
f3054d2805 | |
|
daa57cae92 | |
|
132b7b99cc | |
|
2e1d41b2cd | |
|
52d7cc7ded | |
|
9f426407a9 | |
|
5da57545e2 | |
|
ff7e608f9a | |
|
969a97cab7 | |
|
88ba532d1f | |
|
2b3e681a38 | |
|
b449a900fe | |
|
4a9f826369 | |
|
04a0d499e1 | |
|
90eceab11d | |
|
816d68cbe1 | |
|
9a832a5aac | |
|
17ec2185d0 | |
|
25bb24694c | |
|
83a0eadf6d | |
|
06ed48f4cf | |
|
bd3d71ae16 | |
|
a15a1820a8 | |
|
b1be0d0d77 | |
|
9d1d0eaecd |
|
@ -1,5 +1,7 @@
|
|||
/target
|
||||
**/result
|
||||
result/
|
||||
result
|
||||
/config
|
||||
/sounds
|
||||
/.idea
|
||||
.DS_Store
|
||||
|
||||
.env
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
steps:
|
||||
build-docker-image:
|
||||
when:
|
||||
event: push
|
||||
event: [push, tag]
|
||||
image: alpine:edge
|
||||
commands:
|
||||
- apk update && apk upgrade
|
||||
- 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
|
||||
- nix build --extra-experimental-features nix-command --extra-experimental-features flakes .#docker
|
||||
- nix build --extra-experimental-features nix-command --extra-experimental-features flakes --max-jobs 16 .#docker
|
||||
- cp $(nix build --extra-experimental-features nix-command --extra-experimental-features flakes --print-out-paths .#docker) ./memejoin-rs.tar.gz
|
||||
volumes:
|
||||
- ${AGENT_NIX_STORE_PATH}:/nix
|
||||
|
|
|
@ -62,6 +62,17 @@ dependencies = [
|
|||
"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]]
|
||||
name = "aho-corasick"
|
||||
version = "1.0.2"
|
||||
|
@ -71,6 +82,12 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
|
@ -313,6 +330,7 @@ dependencies = [
|
|||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits 0.2.16",
|
||||
"serde",
|
||||
"time 0.1.45",
|
||||
"wasm-bindgen",
|
||||
"winapi",
|
||||
|
@ -475,6 +493,12 @@ version = "0.15.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.32"
|
||||
|
@ -484,6 +508,26 @@ dependencies = [
|
|||
"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]]
|
||||
name = "enum_primitive"
|
||||
version = "0.1.1"
|
||||
|
@ -514,6 +558,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "fastrand"
|
||||
version = "2.0.0"
|
||||
|
@ -744,6 +800,19 @@ name = "hashbrown"
|
|||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "headers"
|
||||
|
@ -925,6 +994,24 @@ version = "2.8.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
|
@ -952,6 +1039,17 @@ version = "0.2.147"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.3"
|
||||
|
@ -1012,15 +1110,18 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
|||
|
||||
[[package]]
|
||||
name = "memejoin-rs"
|
||||
version = "0.1.1-alpha"
|
||||
version = "0.2.2-alpha"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"axum-extra",
|
||||
"chrono",
|
||||
"dotenv",
|
||||
"enum-iterator",
|
||||
"futures",
|
||||
"iter_tools",
|
||||
"reqwest",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serenity",
|
||||
|
@ -1516,6 +1617,21 @@ dependencies = [
|
|||
"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]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
|
|
15
Cargo.toml
15
Cargo.toml
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "memejoin-rs"
|
||||
version = "0.1.1-alpha"
|
||||
version = "0.2.2-alpha"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
@ -9,9 +9,11 @@ edition = "2021"
|
|||
async-trait = "0.1.72"
|
||||
axum = { version = "0.6.9", features = ["headers", "multipart"] }
|
||||
axum-extra = { version = "0.7.5", features = ["cookie-private", "cookie"] }
|
||||
chrono = "0.4.23"
|
||||
chrono = { version = "0.4.23", features = ["serde"] }
|
||||
dotenv = "0.15.0"
|
||||
enum-iterator = "1.4.1"
|
||||
futures = "0.3.26"
|
||||
iter_tools = "0.1.4"
|
||||
reqwest = "0.11.14"
|
||||
serde = "1.0.152"
|
||||
serde_json = "1.0.93"
|
||||
|
@ -30,3 +32,12 @@ features = ["client", "gateway", "rustls_backend", "model", "cache", "voice"]
|
|||
[dependencies.songbird]
|
||||
version = "0.3.2"
|
||||
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"
|
||||
|
|
66
flake.lock
66
flake.lock
|
@ -1,12 +1,15 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1667395993,
|
||||
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||
"lastModified": 1710146030,
|
||||
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -16,12 +19,15 @@
|
|||
}
|
||||
},
|
||||
"flake-utils_2": {
|
||||
"inputs": {
|
||||
"systems": "systems_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1659877975,
|
||||
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||
"lastModified": 1705309234,
|
||||
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -32,11 +38,11 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1675942811,
|
||||
"narHash": "sha256-/v4Z9mJmADTpXrdIlAjFa1e+gkpIIROR670UVDQFwIw=",
|
||||
"lastModified": 1717786204,
|
||||
"narHash": "sha256-4q0s6m0GUcN7q+Y2DqD27iLvbcd1G50T2lv08kKxkSI=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "724bfc0892363087709bd3a5a1666296759154b1",
|
||||
"rev": "051f920625ab5aabe37c920346e3e69d7d34400e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -48,11 +54,11 @@
|
|||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1665296151,
|
||||
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
|
||||
"lastModified": 1706487304,
|
||||
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
|
||||
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -75,11 +81,11 @@
|
|||
"nixpkgs": "nixpkgs_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1676169013,
|
||||
"narHash": "sha256-mhUWa6TUg6Qjba1OdxPuW1ctCuU4O4lSObVc6UUUE0E=",
|
||||
"lastModified": 1717985971,
|
||||
"narHash": "sha256-24h/qKp0aeI+Ew13WdRF521kY24PYa5HOvw0mlrABjk=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "ef4cd733dc6b595cab5092f5004a489c5fd80b07",
|
||||
"rev": "abfe5b3126b1b7e9e4daafc1c6478d17f0b584e7",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -87,6 +93,36 @@
|
|||
"repo": "rust-overlay",
|
||||
"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",
|
||||
|
|
18
flake.nix
18
flake.nix
|
@ -8,19 +8,19 @@
|
|||
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
tag = "v0.1.4_4-alpha";
|
||||
tag = "v0.2.2-alpha";
|
||||
overlays = [ (import rust-overlay) ];
|
||||
pkgs = import nixpkgs {
|
||||
inherit system overlays;
|
||||
};
|
||||
yt-dlp = pkgs.yt-dlp.overrideAttrs (oldAttr: rec {
|
||||
inherit (oldAttr) name;
|
||||
version = "2023.02.17";
|
||||
version = "2024.05.27";
|
||||
src = pkgs.fetchFromGitHub {
|
||||
owner = "yt-dlp";
|
||||
repo = "yt-dlp";
|
||||
rev = "${version}";
|
||||
sha256 = "naC74T6aqCLX45wJLmygsMmTMqdqLbfXLjJKIKMRpiI=";
|
||||
sha256 = "55zDAMwCJPn5zKrAFw4ogTxxmvjrv4PvhYO7PsHbRo4=";
|
||||
};
|
||||
});
|
||||
local-rust = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain).override {
|
||||
|
@ -35,13 +35,14 @@
|
|||
pkg-config
|
||||
gcc
|
||||
openssl
|
||||
sqlite
|
||||
pkg-config
|
||||
python3
|
||||
ffmpeg
|
||||
cmake
|
||||
libopus
|
||||
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 {
|
||||
|
@ -50,11 +51,16 @@
|
|||
name = "memejoin-rs";
|
||||
src = self;
|
||||
buildInputs = [ openssl.dev ];
|
||||
nativeBuildInputs = [ local-rust pkg-config openssl openssl.dev cmake gcc libopus ];
|
||||
nativeBuildInputs = [ local-rust pkg-config openssl openssl.dev cmake gcc libopus sqlite ];
|
||||
|
||||
cargoLock = {
|
||||
lockFile = ./Cargo.lock;
|
||||
};
|
||||
|
||||
# lol, why does `buildRustPackage` not work without this?
|
||||
postPatch = ''
|
||||
ln -sf ${./Cargo.lock} Cargo.lock
|
||||
'';
|
||||
};
|
||||
|
||||
docker = dockerTools.buildImage {
|
||||
|
@ -62,7 +68,7 @@
|
|||
name = "memejoin-rs";
|
||||
copyToRoot = buildEnv {
|
||||
name = "image-root";
|
||||
paths = [ default cacert openssl openssl.dev ffmpeg libopus youtube-dl yt-dlp ];
|
||||
paths = [ default cacert openssl openssl.dev ffmpeg libopus youtube-dl yt-dlp sqlite ];
|
||||
};
|
||||
runAsRoot = ''
|
||||
#!${runtimeShell}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
alias b := build
|
||||
alias r := run
|
||||
|
||||
set dotenv-load
|
||||
|
||||
build:
|
||||
cargo build
|
||||
|
||||
run:
|
||||
cargo run
|
|
@ -1 +1 @@
|
|||
nightly
|
||||
stable
|
||||
|
|
117
src/auth.rs
117
src/auth.rs
|
@ -1,5 +1,10 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use enum_iterator::Sequence;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::routes::Error;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(crate) struct Discord {
|
||||
pub(crate) access_token: String,
|
||||
|
@ -13,6 +18,7 @@ pub(crate) struct Discord {
|
|||
pub(crate) struct DiscordSecret {
|
||||
pub(crate) client_id: String,
|
||||
pub(crate) client_secret: String,
|
||||
pub(crate) bot_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
@ -21,26 +27,83 @@ pub(crate) struct User {
|
|||
pub(crate) name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub(crate) struct Permissions(pub(crate) u8);
|
||||
impl Default for Permissions {
|
||||
fn default() -> Permissions {
|
||||
Permissions(0)
|
||||
#[derive(Default, Debug, Clone, 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);
|
||||
|
||||
impl Permissions {
|
||||
pub(crate) fn can(&self, perm: Permission) -> bool {
|
||||
self.0 & (perm as u8) > 0
|
||||
(self.0 & (perm as u8) > 0) || (self.0 & (Permission::Moderator as u8) > 0)
|
||||
}
|
||||
|
||||
pub(crate) fn add(&mut self, perm: Permission) {
|
||||
self.0 |= perm as u8;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Sequence)]
|
||||
#[repr(u8)]
|
||||
pub enum Permission {
|
||||
None,
|
||||
UploadSounds,
|
||||
DeleteSounds,
|
||||
pub(crate) enum Permission {
|
||||
None = 0,
|
||||
UploadSounds = 1,
|
||||
DeleteSounds = 2,
|
||||
Soundboard = 4,
|
||||
AddChannel = 8,
|
||||
Moderator = 128,
|
||||
}
|
||||
|
||||
impl Permission {
|
||||
|
@ -48,3 +111,35 @@ impl Permission {
|
|||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,578 @@
|
|||
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,
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
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;
|
18
src/htmx.rs
18
src/htmx.rs
|
@ -1,3 +1,5 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait Build {
|
||||
|
@ -23,6 +25,7 @@ pub enum Tag {
|
|||
Dialog,
|
||||
Article,
|
||||
Header,
|
||||
Footer,
|
||||
|
||||
Div,
|
||||
|
||||
|
@ -51,6 +54,7 @@ pub enum Tag {
|
|||
Anchor,
|
||||
Button,
|
||||
|
||||
HeaderGroup,
|
||||
Header1,
|
||||
Header2,
|
||||
Header3,
|
||||
|
@ -85,6 +89,7 @@ impl Tag {
|
|||
Self::Dialog => "dialog",
|
||||
Self::Article => "article",
|
||||
Self::Header => "header",
|
||||
Self::Footer => "footer",
|
||||
|
||||
Self::Div => "div",
|
||||
|
||||
|
@ -111,6 +116,7 @@ impl Tag {
|
|||
Self::Anchor => "a",
|
||||
Self::Button => "button",
|
||||
|
||||
Self::HeaderGroup => "hgroup",
|
||||
Self::Header1 => "h1",
|
||||
Self::Header2 => "h2",
|
||||
Self::Header3 => "h3",
|
||||
|
@ -187,7 +193,7 @@ impl Build for HtmxBuilder {
|
|||
}
|
||||
}
|
||||
if self.tag != Tag::JustText && self.tag != Tag::Empty {
|
||||
string.push_str(">");
|
||||
string.push('>');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -225,23 +231,23 @@ impl HtmxBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn hx_get(mut self, uri: &str) -> Self {
|
||||
pub fn hx_get(self, uri: &str) -> Self {
|
||||
self.attribute("hx-get", uri)
|
||||
}
|
||||
|
||||
pub fn hx_post(mut self, uri: &str) -> Self {
|
||||
pub fn hx_post(self, uri: &str) -> Self {
|
||||
self.attribute("hx-post", uri)
|
||||
}
|
||||
|
||||
pub fn hx_swap(mut self, swap_method: SwapMethod) -> Self {
|
||||
pub fn hx_swap(self, swap_method: SwapMethod) -> Self {
|
||||
self.attribute("hx-swap", swap_method.as_str())
|
||||
}
|
||||
|
||||
pub fn hx_trigger(mut self, trigger: &str) -> Self {
|
||||
pub fn hx_trigger(self, trigger: &str) -> Self {
|
||||
self.attribute("hx-trigger", trigger)
|
||||
}
|
||||
|
||||
pub fn hx_target(mut self, target: &str) -> Self {
|
||||
pub fn hx_target(self, target: &str) -> Self {
|
||||
self.attribute("hx-target", target)
|
||||
}
|
||||
|
||||
|
|
158
src/main.rs
158
src/main.rs
|
@ -1,28 +1,25 @@
|
|||
#![feature(stmt_expr_attributes)]
|
||||
#![feature(proc_macro_hygiene)]
|
||||
#![feature(async_closure)]
|
||||
// #![feature(stmt_expr_attributes)]
|
||||
// #![feature(proc_macro_hygiene)]
|
||||
// #![feature(async_closure)]
|
||||
|
||||
mod auth;
|
||||
mod db;
|
||||
mod htmx;
|
||||
mod media;
|
||||
mod page;
|
||||
mod routes;
|
||||
pub mod settings;
|
||||
|
||||
use axum::http::{HeaderValue, Method};
|
||||
use axum::routing::{delete, get, post};
|
||||
use axum::http::Method;
|
||||
use axum::routing::{get, post};
|
||||
use axum::Router;
|
||||
use futures::StreamExt;
|
||||
use settings::ApiState;
|
||||
use songbird::tracks::TrackQueue;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serenity::async_trait;
|
||||
use serenity::model::prelude::{Channel, ChannelId, GuildId, Member, Ready};
|
||||
use serenity::model::voice::VoiceState;
|
||||
|
@ -31,7 +28,7 @@ use serenity::prelude::*;
|
|||
use songbird::SerenityInit;
|
||||
use tracing::*;
|
||||
|
||||
use crate::settings::{Intro, Settings};
|
||||
use crate::settings::Settings;
|
||||
|
||||
enum HandlerMessage {
|
||||
Ready(Context),
|
||||
|
@ -119,16 +116,17 @@ impl EventHandler for Handler {
|
|||
}
|
||||
}
|
||||
|
||||
fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
||||
fn spawn_api(db: Arc<tokio::sync::Mutex<db::Database>>) {
|
||||
let secrets = auth::DiscordSecret {
|
||||
client_id: env::var("DISCORD_CLIENT_ID").expect("expected DISCORD_CLIENT_ID env var"),
|
||||
client_secret: env::var("DISCORD_CLIENT_SECRET")
|
||||
.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 state = ApiState {
|
||||
settings,
|
||||
db,
|
||||
secrets,
|
||||
origin: origin.clone(),
|
||||
};
|
||||
|
@ -139,6 +137,16 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
|||
.route("/index.html", get(page::home))
|
||||
.route("/login", get(page::login))
|
||||
.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/intros/add/:guild_id/:channel",
|
||||
post(routes::v2_add_intro_to_user),
|
||||
|
@ -147,25 +155,14 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
|||
"/v2/intros/remove/:guild_id/:channel",
|
||||
post(routes::v2_remove_intro_from_user),
|
||||
)
|
||||
.route("/v2/intros/:guild/add", get(routes::v2_add_guild_intro))
|
||||
.route(
|
||||
"/v2/intros/:guild/upload",
|
||||
post(routes::v2_upload_guild_intro),
|
||||
)
|
||||
.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))
|
||||
.route("/v2/auth", get(routes::v2_auth))
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
// TODO: move this to env variable
|
||||
.allow_origin([origin.parse().unwrap()])
|
||||
.allow_headers(Any)
|
||||
.allow_methods([Method::GET, Method::POST, Method::DELETE]),
|
||||
|
@ -180,7 +177,7 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
|||
});
|
||||
}
|
||||
|
||||
async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
|
||||
async fn spawn_bot(db: Arc<tokio::sync::Mutex<db::Database>>) {
|
||||
let token = env::var("DISCORD_TOKEN").expect("expected DISCORD_TOKEN env var");
|
||||
let songbird = songbird::Songbird::serenity();
|
||||
|
||||
|
@ -210,12 +207,19 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
|
|||
match msg {
|
||||
HandlerMessage::Ready(ctx) => {
|
||||
info!("Got Ready message");
|
||||
let settings = settings.lock().await;
|
||||
|
||||
let songbird = songbird::get(&ctx).await.expect("no songbird instance");
|
||||
|
||||
for guild_id in settings.guilds.keys() {
|
||||
let handler_lock = songbird.get_or_insert(GuildId(*guild_id));
|
||||
let guilds = match db.lock().await.get_guilds() {
|
||||
Ok(guilds) => guilds,
|
||||
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;
|
||||
|
||||
|
@ -223,7 +227,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
|
|||
songbird::Event::Track(songbird::TrackEvent::End),
|
||||
TrackEventHandler {
|
||||
tx: tx.clone(),
|
||||
guild_id: GuildId(*guild_id),
|
||||
guild_id: GuildId(guild.id),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -246,7 +250,6 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
|
|||
|
||||
HandlerMessage::PlaySound(ctx, member, channel_id) => {
|
||||
info!("Got PlaySound message");
|
||||
let settings = settings.lock().await;
|
||||
|
||||
let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx.cache)
|
||||
else {
|
||||
|
@ -254,60 +257,35 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
|
|||
continue;
|
||||
};
|
||||
|
||||
let Some(guild_settings) = settings.guilds.get(channel.guild_id.as_u64())
|
||||
else {
|
||||
error!("couldn't get guild from id: {}", channel.guild_id.as_u64());
|
||||
continue;
|
||||
};
|
||||
let Some(channel_settings) = guild_settings.channels.get(channel.name()) else {
|
||||
error!(
|
||||
"couldn't get channel_settings from name: {}",
|
||||
channel.name()
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let Some(user) = channel_settings.users.get(&member.user.name) else {
|
||||
error!(
|
||||
"couldn't get user settings from name: {}",
|
||||
&member.user.name
|
||||
);
|
||||
continue;
|
||||
let intros = match db.lock().await.get_user_channel_intros(
|
||||
&member.user.name,
|
||||
channel.guild_id.0,
|
||||
channel.name(),
|
||||
) {
|
||||
Ok(intros) => intros,
|
||||
Err(err) => {
|
||||
error!(
|
||||
?err,
|
||||
"failed to get user channel intros when playing sound through bot"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: randomly choose a intro to play
|
||||
let Some(intro) = user.intros.first() else {
|
||||
let Some(intro) = intros.first() else {
|
||||
error!("couldn't get user intro, none exist");
|
||||
continue;
|
||||
};
|
||||
|
||||
let source = match guild_settings.intros.get(&intro.index) {
|
||||
Some(Intro::Online(intro)) => match songbird::ytdl(&intro.url).await {
|
||||
Ok(source) => source,
|
||||
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 => {
|
||||
let source = match songbird::ffmpeg(format!("sounds/{}", &intro.filename)).await
|
||||
{
|
||||
Ok(source) => source,
|
||||
Err(err) => {
|
||||
error!(
|
||||
"Failed to find intro for user {} on guild {} in channel {}, IntroIndex: {}",
|
||||
member.user.name,
|
||||
channel.guild_id.as_u64(),
|
||||
channel.name(),
|
||||
intro.index
|
||||
);
|
||||
"Error starting file source from {}: {err:?}",
|
||||
intro.filename
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
@ -341,23 +319,29 @@ async fn main() -> std::io::Result<()> {
|
|||
&std::fs::read_to_string("config/settings.json").expect("no config/settings.json"),
|
||||
)
|
||||
.expect("error parsing settings file");
|
||||
|
||||
let (run_api, run_bot) = (settings.run_api, settings.run_bot);
|
||||
|
||||
info!("{settings:?}");
|
||||
|
||||
let settings = Arc::new(Mutex::new(settings));
|
||||
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"),
|
||||
));
|
||||
|
||||
{
|
||||
// attempt to initialize the database with the schema
|
||||
let db = db.lock().await;
|
||||
db.init().expect("couldn't init db");
|
||||
}
|
||||
|
||||
if run_api {
|
||||
spawn_api(settings.clone());
|
||||
spawn_api(db.clone());
|
||||
}
|
||||
if run_bot {
|
||||
spawn_bot(settings.clone()).await;
|
||||
spawn_bot(db).await;
|
||||
}
|
||||
|
||||
info!("spawned background tasks");
|
||||
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
settings.lock().await.save()?;
|
||||
info!("Received Ctrl-C, shuttdown down.");
|
||||
|
||||
Ok(())
|
||||
|
|
615
src/page.rs
615
src/page.rs
|
@ -1,41 +1,167 @@
|
|||
use crate::{
|
||||
auth::{self, User},
|
||||
htmx::{Build, HtmxBuilder, SwapMethod, Tag},
|
||||
settings::{ApiState, Intro, IntroFriendlyName},
|
||||
auth,
|
||||
db::{self, User},
|
||||
htmx::{Build, HtmxBuilder, Tag},
|
||||
settings::ApiState,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::{Html, Redirect},
|
||||
};
|
||||
use iter_tools::Itertools;
|
||||
use tracing::error;
|
||||
|
||||
pub(crate) async fn home(user: Option<User>) -> Redirect {
|
||||
if user.is_some() {
|
||||
Redirect::to("/guild/588149178912473103")
|
||||
fn page_header(title: &str) -> HtmxBuilder {
|
||||
HtmxBuilder::new(Tag::Html).head(|b| {
|
||||
b.title(title)
|
||||
.script(
|
||||
"https://unpkg.com/htmx.org@1.9.3",
|
||||
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
||||
)
|
||||
// Not currently using
|
||||
// .script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
||||
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css")
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn home(
|
||||
State(state): State<ApiState>,
|
||||
user: Option<User>,
|
||||
) -> Result<Html<String>, Redirect> {
|
||||
if let Some(user) = user {
|
||||
let db = state.db.lock().await;
|
||||
|
||||
let needs_setup = db
|
||||
.get_guilds()
|
||||
.map_err(|err| {
|
||||
error!(?err, "failed to get user guilds");
|
||||
// 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(
|
||||
page_header("MemeJoin - Home")
|
||||
.builder(Tag::Div, |b| {
|
||||
let mut b = b.push_builder(guild_list);
|
||||
|
||||
if !needs_setup && can_add_guild && !discord_guilds.is_empty() {
|
||||
b = b
|
||||
.attribute("class", "container")
|
||||
.builder_text(Tag::Header2, "Add a Guild")
|
||||
.push_builder(setup_guild_list(&state.origin, &discord_guilds));
|
||||
}
|
||||
|
||||
b
|
||||
})
|
||||
.build(),
|
||||
))
|
||||
} else {
|
||||
Redirect::to("/login")
|
||||
Err(Redirect::to(&format!("{}/login", state.origin)))
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_guild_list(origin: &str, user_guilds: &[crate::routes::DiscordUserGuild]) -> HtmxBuilder {
|
||||
HtmxBuilder::new(Tag::Empty).ul(|b| {
|
||||
let mut b = b;
|
||||
for guild in user_guilds {
|
||||
b = b.li(|b| {
|
||||
b.link(
|
||||
&guild.name,
|
||||
// TODO: url encode the name
|
||||
&format!("{}/guild/{}/setup?name={}", origin, guild.id, guild.name),
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
b
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
fn intro_list<'a>(
|
||||
intros: impl Iterator<Item = (&'a String, &'a Intro)>,
|
||||
intros: impl Iterator<Item = &'a db::Intro>,
|
||||
label: &str,
|
||||
post: &str,
|
||||
) -> HtmxBuilder {
|
||||
HtmxBuilder::new(Tag::Empty).form(|b| {
|
||||
b.attribute("class", "container")
|
||||
.hx_post(post)
|
||||
.hx_target("closest #channel-intro-selector")
|
||||
.attribute("hx-encoding", "multipart/form-data")
|
||||
.builder(Tag::FieldSet, |b| {
|
||||
let mut b = b
|
||||
.attribute("class", "container")
|
||||
.attribute("style", "max-height: 20%; overflow-y: scroll");
|
||||
.attribute("style", "height: 256px; overflow: auto");
|
||||
for intro in intros {
|
||||
b = b.builder(Tag::Label, |b| {
|
||||
b.builder(Tag::Input, |b| {
|
||||
b.attribute("type", "checkbox").attribute("name", &intro.0)
|
||||
b.attribute("type", "checkbox")
|
||||
.attribute("name", &intro.id.to_string())
|
||||
})
|
||||
.builder_text(Tag::Paragraph, intro.1.friendly_name())
|
||||
.builder_text(Tag::Paragraph, &intro.name)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -50,112 +176,399 @@ pub(crate) async fn guild_dashboard(
|
|||
user: User,
|
||||
Path(guild_id): Path<u64>,
|
||||
) -> Result<Html<String>, Redirect> {
|
||||
let settings = state.settings.lock().await;
|
||||
let (guild_name, guild_intros, guild_channels, all_user_intros, user_permissions) = {
|
||||
let db = state.db.lock().await;
|
||||
|
||||
let Some(guild) = settings.guilds.get(&guild_id) else {
|
||||
error!(%guild_id, "no such guild");
|
||||
return Err(Redirect::to("/"));
|
||||
};
|
||||
let Some(guild_user) = guild.users.get(&user.name) else {
|
||||
error!(%guild_id, %user.name, "no user in guild");
|
||||
return Err(Redirect::to("/"));
|
||||
let guild_name = db.get_guild(guild_id).map_err(|err| {
|
||||
error!(?err, %guild_id, "couldn't get guild");
|
||||
// TODO: change to actual error
|
||||
Redirect::to(&format!("{}/login", state.origin))
|
||||
})?;
|
||||
|
||||
let guild_intros = db.get_guild_intros(guild_id).map_err(|err| {
|
||||
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 is_moderator = guild_user.permissions.can(auth::Permission::DeleteSounds);
|
||||
let can_upload = user_permissions.can(auth::Permission::UploadSounds);
|
||||
let can_add_channel = user_permissions.can(auth::Permission::AddChannel);
|
||||
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(
|
||||
HtmxBuilder::new(Tag::Html)
|
||||
.head(|b| {
|
||||
b.title("MemeJoin - Dashboard")
|
||||
.script(
|
||||
"https://unpkg.com/htmx.org@1.9.3",
|
||||
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
||||
)
|
||||
.script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
||||
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
|
||||
})
|
||||
.push_builder(page_header("MemeJoin - Dashboard"))
|
||||
.builder(Tag::Nav, |b| {
|
||||
b.builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros"))
|
||||
.builder_text(Tag::Paragraph, &user.name)
|
||||
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, &format!("{} - {}", user.name, guild_name))
|
||||
})
|
||||
})
|
||||
.builder(Tag::Main, |b| {
|
||||
if is_moderator {
|
||||
b.builder(Tag::Article, |b| {
|
||||
b.builder_text(Tag::Header, "Wow, you're a moderator")
|
||||
.builder(Tag::Empty, |b| {
|
||||
let mut b = if is_moderator || can_add_channel {
|
||||
b.builder(Tag::Div, |b| {
|
||||
b.attribute("class", "container")
|
||||
.builder(Tag::Article, |b| {
|
||||
b.builder_text(Tag::Header, "Server Settings")
|
||||
.push_builder(mod_dashboard)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
b
|
||||
}
|
||||
.builder(Tag::Article, |b| {
|
||||
let mut b = b.builder_text(Tag::Header, "Guild Settings");
|
||||
|
||||
for (channel_name, channel_settings) in &guild.channels {
|
||||
if let Some(channel_user) = channel_settings.users.get(&user.name) {
|
||||
let current_intros =
|
||||
channel_user.intros.iter().filter_map(|intro_index| {
|
||||
Some((
|
||||
&intro_index.index,
|
||||
guild.intros.get(&intro_index.index)?,
|
||||
))
|
||||
});
|
||||
let available_intros = guild.intros.iter().filter_map(|intro| {
|
||||
if !channel_user
|
||||
.intros
|
||||
.iter()
|
||||
.any(|intro_index| intro.0 == &intro_index.index)
|
||||
{
|
||||
Some((intro.0, intro.1))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
b = b
|
||||
.builder_text(Tag::Strong, channel_name)
|
||||
.builder(Tag::Div, |b| {
|
||||
b.builder_text(Tag::Strong, "Your Current Intros")
|
||||
.push_builder(intro_list(
|
||||
current_intros,
|
||||
"Remove Intro",
|
||||
&format!(
|
||||
"/v2/intros/remove/{}/{}",
|
||||
guild_id, channel_name
|
||||
),
|
||||
))
|
||||
.builder_text(Tag::Strong, "Select Intros")
|
||||
.push_builder(intro_list(
|
||||
available_intros,
|
||||
"Add Intro",
|
||||
&format!(
|
||||
"/v2/intros/add/{}/{}",
|
||||
guild_id, channel_name
|
||||
),
|
||||
))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
b = if can_upload {
|
||||
b.builder(Tag::Div, |b| {
|
||||
b.attribute("class", "container")
|
||||
.builder(Tag::Article, |b| {
|
||||
b.builder_text(Tag::Header, "Upload New Intro")
|
||||
.push_builder(upload_form(&state.origin, guild_id))
|
||||
})
|
||||
})
|
||||
.builder(Tag::Div, |b| {
|
||||
b.attribute("class", "container")
|
||||
.builder(Tag::Article, |b| {
|
||||
b.builder_text(Tag::Header, "Upload New Intro from Url")
|
||||
.push_builder(ytdl_form(&state.origin, guild_id))
|
||||
})
|
||||
})
|
||||
} else {
|
||||
b
|
||||
};
|
||||
|
||||
b.builder(Tag::Div, |b| {
|
||||
b.attribute("class", "container")
|
||||
.builder(Tag::Article, |b| {
|
||||
let mut b = b.builder_text(Tag::Header, "Guild Intros");
|
||||
|
||||
let mut user_intros = user_intros.into_iter().peekable();
|
||||
|
||||
for guild_channel_name in &guild_channels {
|
||||
// Get user intros for this channel
|
||||
let intros = user_intros
|
||||
.peeking_take_while(|(channel_name, _)| {
|
||||
channel_name == &guild_channel_name
|
||||
})
|
||||
.map(|(_, intros)| intros.map(|intro| &intro.intro))
|
||||
.flatten();
|
||||
|
||||
b = b.builder(Tag::Details, |b| {
|
||||
let mut b = b;
|
||||
if guild_channels.len() < 2 {
|
||||
b = b.attribute("open", "");
|
||||
}
|
||||
b.builder_text(Tag::Summary, guild_channel_name).builder(
|
||||
Tag::Div,
|
||||
|b| {
|
||||
b.attribute("id", "channel-intro-selector")
|
||||
.attribute("style", "display: flex; align-items: flex-end; max-height: 50%; overflow: hidden;")
|
||||
.push_builder(channel_intro_selector(
|
||||
&state.origin,
|
||||
guild_id,
|
||||
guild_channel_name,
|
||||
intros,
|
||||
guild_intros.iter(),
|
||||
))
|
||||
},
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
b
|
||||
})
|
||||
})
|
||||
})
|
||||
.build(),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) async fn login(State(state): State<ApiState>) -> Html<String> {
|
||||
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);
|
||||
|
||||
Html(
|
||||
HtmxBuilder::new(Tag::Html)
|
||||
.head(|b| {
|
||||
b.title("MemeJoin - Login")
|
||||
.script(
|
||||
"https://unpkg.com/htmx.org@1.9.3",
|
||||
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
||||
)
|
||||
.script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
||||
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
|
||||
})
|
||||
.link("Login", &authorize_uri)
|
||||
.build(),
|
||||
)
|
||||
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 {
|
||||
HtmxBuilder::new(Tag::Empty).form(|b| {
|
||||
b.attribute("class", "container")
|
||||
.hx_post(&format!("{}/v2/intros/{}/upload", origin, guild_id))
|
||||
.attribute("hx-encoding", "multipart/form-data")
|
||||
.builder(Tag::FieldSet, |b| {
|
||||
b.attribute("class", "container")
|
||||
.attribute("role", "group")
|
||||
.input(|b| b.attribute("type", "file").attribute("name", "file"))
|
||||
.input(|b| {
|
||||
b.attribute("name", "name")
|
||||
.attribute("placeholder", "enter intro title")
|
||||
})
|
||||
.button(|b| b.attribute("type", "submit").text("Upload"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn ytdl_form(origin: &str, guild_id: u64) -> HtmxBuilder {
|
||||
HtmxBuilder::new(Tag::Empty).form(|b| {
|
||||
b.attribute("class", "container")
|
||||
.hx_get(&format!("{}/v2/intros/{}/add", origin, guild_id))
|
||||
.builder(Tag::FieldSet, |b| {
|
||||
b.attribute("class", "container")
|
||||
.attribute("role", "group")
|
||||
.input(|b| {
|
||||
b.attribute("placeholder", "enter video url")
|
||||
.attribute("name", "url")
|
||||
})
|
||||
.input(|b| {
|
||||
b.attribute("placeholder", "enter intro title")
|
||||
.attribute("name", "name")
|
||||
})
|
||||
.button(|b| b.attribute("type", "submit").text("Upload"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async fn moderator_dashboard(
|
||||
state: &ApiState,
|
||||
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 {
|
||||
let db = state.db.lock().await;
|
||||
let added_guild_channels = db.get_guild_channels(guild_id).unwrap_or_default();
|
||||
|
||||
let mut got_channels = true;
|
||||
let client = reqwest::Client::new();
|
||||
let channels: Vec<String> = {
|
||||
match client
|
||||
.get(format!(
|
||||
"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(),
|
||||
//)
|
||||
}
|
||||
|
|
871
src/routes.rs
871
src/routes.rs
File diff suppressed because it is too large
Load Diff
148
src/settings.rs
148
src/settings.rs
|
@ -1,41 +1,52 @@
|
|||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth;
|
||||
use crate::{
|
||||
auth,
|
||||
db::{self, Database},
|
||||
};
|
||||
use axum::{async_trait, extract::FromRequestParts, http::request::Parts, response::Redirect};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use chrono::Utc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::prelude::TypeMapKey;
|
||||
use tracing::trace;
|
||||
use uuid::Uuid;
|
||||
|
||||
type UserToken = String;
|
||||
use tracing::error;
|
||||
|
||||
// TODO: make this is wrapped type so cloning isn't happening
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ApiState {
|
||||
pub settings: Arc<tokio::sync::Mutex<Settings>>,
|
||||
pub db: Arc<tokio::sync::Mutex<Database>>,
|
||||
pub secrets: auth::DiscordSecret,
|
||||
pub origin: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl FromRequestParts<ApiState> for crate::auth::User {
|
||||
impl FromRequestParts<ApiState> for db::User {
|
||||
type Rejection = Redirect;
|
||||
|
||||
async fn from_request_parts(
|
||||
Parts { headers, .. }: &mut Parts,
|
||||
state: &ApiState,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let jar = CookieJar::from_headers(&headers);
|
||||
let jar = CookieJar::from_headers(headers);
|
||||
|
||||
if let Some(token) = jar.get("access_token") {
|
||||
match state.settings.lock().await.auth_users.get(token.value()) {
|
||||
// :vomit:
|
||||
Some(user) => Ok(user.clone()),
|
||||
None => Err(Redirect::to("/login")),
|
||||
match state.db.lock().await.get_user_from_api_key(token.value()) {
|
||||
Ok(user) => {
|
||||
let now = Utc::now().naive_utc();
|
||||
if user.api_key_expires_at < now || user.discord_token_expires_at < now {
|
||||
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 {
|
||||
Err(Redirect::to("/login"))
|
||||
Err(Redirect::to(&format!("{}/login", state.origin)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,116 +58,7 @@ pub(crate) struct Settings {
|
|||
pub(crate) run_api: bool,
|
||||
#[serde(default)]
|
||||
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 {
|
||||
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>,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue