From 3f2f4fa7337070660c958158901e00b3a432010b Mon Sep 17 00:00:00 2001 From: ModZero Date: Sun, 19 Mar 2023 22:59:11 +0100 Subject: [PATCH] Actually save things! Yay! --- Cargo.lock | 637 +++++++++++++++++- Cargo.toml | 15 +- build.rs | 5 + ...20230319163531_timestamp_function.down.sql | 1 + .../20230319163531_timestamp_function.up.sql | 7 + migrations/20230319165242_facts.down.sql | 1 + migrations/20230319165242_facts.up.sql | 15 + sqlx-data.json | 59 ++ src/main.rs | 230 +++++-- 9 files changed, 926 insertions(+), 44 deletions(-) create mode 100644 build.rs create mode 100644 migrations/20230319163531_timestamp_function.down.sql create mode 100644 migrations/20230319163531_timestamp_function.up.sql create mode 100644 migrations/20230319165242_facts.down.sql create mode 100644 migrations/20230319165242_facts.up.sql create mode 100644 sqlx-data.json diff --git a/Cargo.lock b/Cargo.lock index 7a9a190..15f88f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "alloc-no-stdlib" version = "2.0.4" @@ -23,6 +34,12 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "anyhow" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" + [[package]] name = "async-trait" version = "0.1.64" @@ -34,6 +51,15 @@ dependencies = [ "syn", ] +[[package]] +name = "atoi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c57d12312ff59c811c0643f4d80830505833c9ffaebd193d819392b265be8e" +dependencies = [ + "num-traits", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -90,6 +116,18 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-macros" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39bcef27b56d5cad8912d735d5ed1286f073f7bcb88cc31b38a15b514fcf8600" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "base64" version = "0.13.1" @@ -150,6 +188,12 @@ version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.4.0" @@ -199,6 +243,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crc" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86ec7a15cbe22e59248fc7eadb1907dab5ba09372595da4d73dd805ed4417dfe" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cace84e55f07e7301bae1c519df89cdad8cc3cd868413d3fdbdeca9ff3db484" + [[package]] name = "crc32fast" version = "1.3.2" @@ -208,6 +267,25 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -251,6 +329,27 @@ checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer", "crypto-common", + "subtle", +] + +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", ] [[package]] @@ -282,6 +381,51 @@ dependencies = [ "zeroize", ] +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +dependencies = [ + "serde", +] + +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + [[package]] name = "fiat-crypto" version = "0.1.18" @@ -304,6 +448,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.1.0" @@ -320,6 +479,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -328,6 +488,17 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608" +[[package]] +name = "futures-intrusive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a604f7a68fbf8103337523b1fadc8ade7361ee3f112f7c680ad179651616aed5" +dependencies = [ + "futures-core", + "lock_api", + "parking_lot 0.11.2", +] + [[package]] name = "futures-sink" version = "0.3.26" @@ -347,6 +518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1" dependencies = [ "futures-core", + "futures-sink", "futures-task", "pin-project-lite", "pin-utils", @@ -377,19 +549,25 @@ dependencies = [ name = "god-replacement-product" version = "0.1.0" dependencies = [ + "anyhow", "axum", + "axum-macros", "base64 0.21.0", "dotenvy", "ed25519-dalek", "hex", "serde", "serde_json", + "sqlx", + "time", "tokio", "twilight-http", "twilight-interactions", + "twilight-mention", "twilight-model", "twilight-util", "ureq", + "uuid", ] [[package]] @@ -416,6 +594,27 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashlink" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69fe1fcf8b4278d860ad0548329f892a3631fb63f82574df68275f34cdbe0ffa" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +dependencies = [ + "unicode-segmentation", +] [[package]] name = "hermit-abi" @@ -432,6 +631,24 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hkdf" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791a029f6b9fc27657f6f188ec6e5e43f6911f6f878e0dc5501396e09809d437" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.9" @@ -530,6 +747,34 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfa919a82ea574332e2de6e74b4c36e74d41982b335080fa59d4ef31be20fdf3" +dependencies = [ + "libc", + "windows-sys 0.45.0", +] + +[[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.5" @@ -545,6 +790,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.139" @@ -557,6 +808,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + [[package]] name = "lock_api" version = "0.4.9" @@ -582,6 +839,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" +[[package]] +name = "md-5" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca" +dependencies = [ + "digest", +] + [[package]] name = "memchr" version = "2.5.0" @@ -594,6 +860,12 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.6.2" @@ -615,6 +887,34 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -640,12 +940,51 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "openssl" +version = "0.10.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd2523381e46256e40930512c7fd25562b9eae4812cb52078f155e87217c9d1e" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-sys" +version = "0.9.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "176be2629957c157240f68f61f2d0053ad3a4ecfdd9ebf1e6521d18d9635cf67" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "ordered-float" version = "2.10.0" @@ -665,6 +1004,17 @@ dependencies = [ "libm", ] +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -672,7 +1022,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ "lock_api", - "parking_lot_core", + "parking_lot_core 0.9.7", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", ] [[package]] @@ -688,6 +1052,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "paste" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -736,6 +1106,12 @@ dependencies = [ "spki", ] +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" + [[package]] name = "platforms" version = "3.0.2" @@ -805,6 +1181,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "ring" version = "0.16.20" @@ -820,6 +1207,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustix" +version = "0.36.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd5c6ff11fecd55b40746d1995a02f2eb375bf8c00d192d521ee09f42bef37bc" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.45.0", +] + [[package]] name = "rustls" version = "0.20.8" @@ -986,6 +1387,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sha2" version = "0.10.6" @@ -1054,10 +1466,127 @@ dependencies = [ ] [[package]] -name = "subtle" -version = "2.5.0" +name = "sqlformat" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "0c12bc9199d1db8234678b7051747c07f517cdcf019262d1847b94ec8b1aee3e" +dependencies = [ + "itertools", + "nom", + "unicode_categories", +] + +[[package]] +name = "sqlx" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9249290c05928352f71c077cc44a464d880c63f26f7534728cca008e135c0428" +dependencies = [ + "sqlx-core", + "sqlx-macros", +] + +[[package]] +name = "sqlx-core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcbc16ddba161afc99e14d1713a453747a2b07fc097d2009f4c300ec99286105" +dependencies = [ + "ahash", + "atoi", + "base64 0.13.1", + "bitflags", + "byteorder", + "bytes", + "crc", + "crossbeam-queue", + "dirs", + "dotenvy", + "either", + "event-listener", + "futures-channel", + "futures-core", + "futures-intrusive", + "futures-util", + "hashlink", + "hex", + "hkdf", + "hmac", + "indexmap", + "itoa", + "libc", + "log", + "md-5", + "memchr", + "once_cell", + "paste", + "percent-encoding", + "rand", + "serde", + "serde_json", + "sha1", + "sha2", + "smallvec", + "sqlformat", + "sqlx-rt", + "stringprep", + "thiserror", + "time", + "tokio-stream", + "url", + "uuid", + "whoami", +] + +[[package]] +name = "sqlx-macros" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b850fa514dc11f2ee85be9d055c512aa866746adfacd1cb42d867d68e6a5b0d9" +dependencies = [ + "dotenvy", + "either", + "heck", + "hex", + "once_cell", + "proc-macro2", + "quote", + "serde", + "serde_json", + "sha2", + "sqlx-core", + "sqlx-rt", + "syn", + "url", +] + +[[package]] +name = "sqlx-rt" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24c5b2d25fa654cc5f841750b8e1cdedbe21189bf9a9382ee90bfa9dd3562396" +dependencies = [ + "native-tls", + "once_cell", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "stringprep" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ee348cb74b87454fff4b551cbf727025810a004f88aeacae7f85b87f4e9a1c1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" @@ -1076,12 +1605,46 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "tempfile" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af18f7ae1acd354b992402e9ec5864359d693cd8a79dcbef59f76891701c1e95" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys 0.42.0", +] + +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" dependencies = [ + "itoa", "serde", "time-core", "time-macros", @@ -1129,7 +1692,7 @@ dependencies = [ "memchr", "mio", "num_cpus", - "parking_lot", + "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", "socket2", @@ -1148,6 +1711,16 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.23.4" @@ -1159,6 +1732,17 @@ dependencies = [ "webpki", ] +[[package]] +name = "tokio-stream" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fb52b74f05dbf495a8fba459fdc331812b96aa086d9eb78101fa0d4569c3313" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.7" @@ -1312,6 +1896,15 @@ dependencies = [ "syn", ] +[[package]] +name = "twilight-mention" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6acbf19063f56c21017ab0e16c6d5de4c7c52b4261466bca219b0d4b9fb3c050" +dependencies = [ + "twilight-model", +] + [[package]] name = "twilight-model" version = "0.15.1" @@ -1372,6 +1965,18 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + +[[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + [[package]] name = "untrusted" version = "0.7.1" @@ -1407,6 +2012,18 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" @@ -1512,6 +2129,16 @@ dependencies = [ "webpki", ] +[[package]] +name = "whoami" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c70234412ca409cc04e864e89523cb0fc37f5e1344ebed5a3ebf4192b6b9f68" +dependencies = [ + "wasm-bindgen", + "web-sys", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index dd5e8f8..0a19957 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,19 +4,32 @@ version = "0.1.0" edition = "2021" license = "AGPL-3.0-or-later" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.dev.package.sqlx-macros] +opt-level = 3 [dependencies] +anyhow = "1.0.70" axum = "0.6.9" +axum-macros = "0.3.6" base64 = "0.21.0" dotenvy = "0.15.6" ed25519-dalek = "2.0.0-pre.0" hex = "0.4.3" serde = "1.0.152" serde_json = "1.0.93" +sqlx = { version = "0.6.2", features = [ + "offline", + "postgres", + "runtime-tokio-native-tls", + "time", + "uuid" +]} +time = "0.3.20" tokio = { version = "1.26.0", features = ["full"] } twilight-http = "0.15.1" twilight-interactions = "0.15.0" +twilight-mention = "0.15.1" twilight-model = "0.15.1" twilight-util = { version = "0.15.1", features = ["builder"] } ureq = { version = "2.6.2", features = ["json"] } +uuid = "1.3.0" \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +// generated by `sqlx migrate build-script` +fn main() { + // trigger recompilation when a new migration is added + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/migrations/20230319163531_timestamp_function.down.sql b/migrations/20230319163531_timestamp_function.down.sql new file mode 100644 index 0000000..4b195b3 --- /dev/null +++ b/migrations/20230319163531_timestamp_function.down.sql @@ -0,0 +1 @@ +DROP FUNCTION IF EXISTS set_updated_timestamp; \ No newline at end of file diff --git a/migrations/20230319163531_timestamp_function.up.sql b/migrations/20230319163531_timestamp_function.up.sql new file mode 100644 index 0000000..c72a943 --- /dev/null +++ b/migrations/20230319163531_timestamp_function.up.sql @@ -0,0 +1,7 @@ +CREATE OR REPLACE FUNCTION set_updated_timestamp() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = now(); + RETURN NEW; +END; +$$ language 'plpgsql'; \ No newline at end of file diff --git a/migrations/20230319165242_facts.down.sql b/migrations/20230319165242_facts.down.sql new file mode 100644 index 0000000..7645fbc --- /dev/null +++ b/migrations/20230319165242_facts.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS "facts"; \ No newline at end of file diff --git a/migrations/20230319165242_facts.up.sql b/migrations/20230319165242_facts.up.sql new file mode 100644 index 0000000..34333d5 --- /dev/null +++ b/migrations/20230319165242_facts.up.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS "facts" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), + "channel_id" text, + "author_id" text NOT NULL, + "last_interaction_id" text NOT NULL, + "name" text NOT NULL, + "value" text NOT NULL, + "expiration" interval NULL, + "version" int NOT NULL DEFAULT 0, + "created_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT facts_origin_key UNIQUE NULLS NOT DISTINCT ("channel_id", "author_id", "name") +); +CREATE TRIGGER "set_facts_updated" BEFORE +UPDATE ON "facts" FOR EACH ROW EXECUTE PROCEDURE set_updated_timestamp(); \ No newline at end of file diff --git a/sqlx-data.json b/sqlx-data.json new file mode 100644 index 0000000..450f1c6 --- /dev/null +++ b/sqlx-data.json @@ -0,0 +1,59 @@ +{ + "db": "PostgreSQL", + "06ff93d6978b99c498a4b08c0a04ade650c225b3bc0a3d99b69fa4fc6b7fd6fa": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Left": [ + "Text", + "Text", + "Text", + "Text", + "Text" + ] + } + }, + "query": "\nINSERT INTO facts (\"last_interaction_id\", \"channel_id\", \"author_id\", \"name\", \"value\") \nVALUES ($1, $2, $3, $4, $5)\nON CONFLICT ON CONSTRAINT facts_origin_key DO UPDATE SET value = $5, version = facts.version + 1\n " + }, + "8aeb82857796f9a8b0b41c3ac600ca4fdd5075efdba27e16f08d33ab9cf0b7bc": { + "describe": { + "columns": [ + { + "name": "value", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "version", + "ordinal": 1, + "type_info": "Int4" + }, + { + "name": "created_at", + "ordinal": 2, + "type_info": "Timestamptz" + }, + { + "name": "updated_at", + "ordinal": 3, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + false, + false, + false + ], + "parameters": { + "Left": [ + "Text", + "Text", + "Text" + ] + } + }, + "query": "\nSELECT \"value\", \"version\", \"created_at\", \"updated_at\"\nFROM facts\nWHERE\n channel_id IS NOT DISTINCT FROM $1 AND\n author_id = $2 AND\n name = $3\n " + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7ce5e65..068c6bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,72 @@ use std::{net::SocketAddr, str::FromStr}; use axum::{ + extract::State, http::{HeaderMap, StatusCode}, routing::post, Json, Router, }; -use base64::{engine, alphabet, Engine}; +use base64::{alphabet, engine, Engine}; use ed25519_dalek::{Signature, VerifyingKey}; use serde::Deserialize; +use sqlx::{postgres::PgPoolOptions, PgPool}; use twilight_http::Client; -use twilight_interactions::command::{CommandModel, CreateCommand, CommandInputData}; +use twilight_interactions::command::{CommandInputData, CommandModel, CreateCommand}; +use twilight_mention::{timestamp::{Timestamp, TimestampStyle}, Mention}; use twilight_model::{ - application::{ - interaction::{Interaction, InteractionType, InteractionData}, - }, - http::interaction::{InteractionResponse, InteractionResponseType, InteractionResponseData}, id::Id, + application::interaction::{Interaction, InteractionData, InteractionType}, + http::interaction::{InteractionResponse, InteractionResponseData, InteractionResponseType}, + id::{Id, marker::{UserMarker, ChannelMarker, InteractionMarker}}, channel::message::MessageFlags, }; - #[derive(CommandModel, CreateCommand)] -#[command(name="save_fact", desc="Quietly save a fact")] -struct SaveFactCommand { - #[command(rename="name", desc="Fact name")] +#[command(name = "set_fact", desc = "Quietly save a fact")] +struct SetFactCommand { + #[command(rename = "name", desc = "Fact name")] fact_name: String, - #[command(rename="value", desc="Fact value")] + #[command(rename = "value", desc = "Fact value")] fact_value: String, } +#[derive(CommandModel, CreateCommand)] +#[command(name = "get_fact", desc = "Retrieve and display the value of a fact")] +struct GetFactCommand { + #[command(rename = "name", desc = "Fact name")] + fact_name: String, + #[command(desc = "Should it be displayed publically, by default it won't be")] + public: Option, +} + #[tokio::main] -async fn main() { +async fn main() -> anyhow::Result<()> { let port = 4635; - let app = Router::new().route("/", post(post_interaction)); + dotenvy::dotenv().ok(); + + let pg_pool = PgPoolOptions::new() + .max_connections(5) + .connect(database_url().as_str()) + .await?; + + sqlx::migrate!().run(&pg_pool).await?; + + let app = Router::new() + .route("/", post(post_interaction)) + .with_state(pg_pool); let addr = SocketAddr::from(([127, 0, 0, 1], port)); - dotenvy::dotenv().ok(); - register_command().await; + + register_commands().await; axum::Server::bind(&addr) .serve(app.into_make_service()) - .await - .unwrap(); + .await?; + + Ok(()) } type InteractionResult = Result<(StatusCode, Json), (StatusCode, String)>; -async fn post_interaction(headers: HeaderMap, body: String) -> InteractionResult { +fn validate_request(headers: HeaderMap, body: String) -> Result { let Ok(interaction): Result = serde_json::from_str(&body) else { return Err((StatusCode::BAD_REQUEST, "request contained invalid json".to_string())) }; @@ -68,6 +90,119 @@ async fn post_interaction(headers: HeaderMap, body: String) -> InteractionResult return Err((StatusCode::UNAUTHORIZED, "interaction failed signature verification".to_string())) }; + return Ok(interaction); +} + +async fn set_fact( + interaction_id: Id, + channel_id: Option>, + author_id: Id, + command_data: SetFactCommand, + pg_pool: &PgPool, +) -> Result { + let Ok(rows) = sqlx::query!(" +INSERT INTO facts (\"last_interaction_id\", \"channel_id\", \"author_id\", \"name\", \"value\") +VALUES ($1, $2, $3, $4, $5) +ON CONFLICT ON CONSTRAINT facts_origin_key DO UPDATE SET value = $5, version = facts.version + 1 + ", + interaction_id.to_string(), + channel_id.map(|cid| cid.to_string()), + author_id.to_string(), + command_data.fact_name, + command_data.fact_value, + ).execute(pg_pool).await.and_then(|rows| Ok(rows.rows_affected())) else { + return Err((StatusCode::INTERNAL_SERVER_ERROR, "Error saving fact.".to_string())); + }; + + if rows != 1 { + return Err((StatusCode::INTERNAL_SERVER_ERROR, "Error saving fact".to_string())); + } + + Ok(InteractionResponse { + kind: InteractionResponseType::ChannelMessageWithSource, + data: Some(InteractionResponseData { + content: Some(format!( + "Set {0} to {1}", + command_data.fact_name, command_data.fact_value + )), + flags: Some(MessageFlags::EPHEMERAL), + ..Default::default() + }), + }) +} + +struct FactResponse { + value: String, + version: i32, + created_at: time::OffsetDateTime, + updated_at: time::OffsetDateTime, +} + +async fn get_fact( + channel_id: Option>, + author_id: Id, + command_data: GetFactCommand, + pg_pool: &PgPool, +) -> Result { + let Ok(facts) = sqlx::query_as!(FactResponse, + " +SELECT \"value\", \"version\", \"created_at\", \"updated_at\" +FROM facts +WHERE + channel_id IS NOT DISTINCT FROM $1 AND + author_id = $2 AND + name = $3 + ", channel_id.map(|cid| cid.to_string()), author_id.to_string(), command_data.fact_name).fetch_all(pg_pool).await else { + return Err((StatusCode::INTERNAL_SERVER_ERROR, "Querying facts failed".to_string())); + }; + if facts.len() == 0 { + return Ok(InteractionResponse { + kind: InteractionResponseType::ChannelMessageWithSource, + data: Some(InteractionResponseData { + content: Some(format!("Fact {0} in channel {1} by you, {2}, was not found.", + command_data.fact_name, + channel_id.map_or("".to_string(), |cid| cid.mention().to_string()), + author_id.mention().to_string(), + )), + flags: match command_data.public { Some(true) => None, _ => Some(MessageFlags::EPHEMERAL) }, + ..Default::default() + }), + }); + } + if facts.len() > 1 { + return Err((StatusCode::INTERNAL_SERVER_ERROR, "Too many facts found, wtf, impossible".to_string())); + } + + let fact = &facts[0]; + + Ok(InteractionResponse { + kind: InteractionResponseType::ChannelMessageWithSource, + data: Some(InteractionResponseData { + content: Some(format!( + "Fact **{0}** was set to **{1}** by {2} at {3}, and was reset {4} times in total since {5}.", + command_data.fact_name, + fact.value, + author_id.mention().to_string(), + Timestamp::new(fact.updated_at.unix_timestamp().try_into().unwrap(), Some(TimestampStyle::RelativeTime)).mention(), + fact.version, + Timestamp::new(fact.created_at.unix_timestamp().try_into().unwrap(), Some(TimestampStyle::ShortDateTime)).mention(), + )), + flags: match command_data.public { Some(true) => None, _ => Some(MessageFlags::EPHEMERAL) }, + ..Default::default() + }), + }) +} + +async fn post_interaction( + headers: HeaderMap, + State(pg_pool): State, + body: String, +) -> InteractionResult { + let interaction = match validate_request(headers, body) { + Ok(interaction) => interaction, + Err(error) => return Err(error), + }; + match interaction.kind { InteractionType::Ping => { let pong = InteractionResponse { @@ -77,25 +212,38 @@ async fn post_interaction(headers: HeaderMap, body: String) -> InteractionResult Ok((StatusCode::OK, Json(pong))) } InteractionType::ApplicationCommand => { + let author_id = interaction.author_id(); let Some(InteractionData::ApplicationCommand(data)) = interaction.data else { return not_found(); }; let command_input_data = CommandInputData::from(*data.clone()); match &*data.name { - "save_fact" => { - let Ok(command_data) = SaveFactCommand::from_interaction(command_input_data) else { - return Err((StatusCode::BAD_REQUEST, "invalid save fact command".to_string())); + "set_fact" => { + let Ok(command_data) = SetFactCommand::from_interaction(command_input_data) else { + return Err((StatusCode::BAD_REQUEST, "invalid set fact command".to_string())); }; - let reply = InteractionResponse { - kind: InteractionResponseType::ChannelMessageWithSource, - data: Some(InteractionResponseData { - content: Some(format!("Set {0} to {1}", command_data.fact_name, command_data.fact_value)), - ..Default::default() - }) + let Some(author_id) = author_id else { + return Err((StatusCode::BAD_REQUEST, "save_fact requires a user".to_string())); }; - Ok((StatusCode::OK, Json(reply))) - } - _ => not_found(), + match set_fact(interaction.id, interaction.channel_id, author_id, command_data, &pg_pool).await { + Ok(response) => Ok((StatusCode::OK, Json(response))), + Err(err) => Err(err), + } + }, + "get_fact" => { + let Ok(command_data) = GetFactCommand::from_interaction(command_input_data) else { + return Err((StatusCode::BAD_REQUEST, "invalid get fact command".to_string())); + }; + let Some(author_id) = author_id else { + return Err((StatusCode::BAD_REQUEST, "get_fact requires a user".to_string())); + }; + + match get_fact(interaction.channel_id, author_id, command_data, &pg_pool).await { + Ok(response) => Ok((StatusCode::OK, Json(response))), + Err(err) => Err(err), + } + }, + _ => not_found(), } } _ => not_found(), @@ -118,10 +266,13 @@ fn discord_pub_key() -> VerifyingKey { VerifyingKey::from_bytes(&pub_key_bytes).unwrap() } -async fn register_command() { +async fn register_commands() { discord_client() .interaction(Id::from_str(&discord_client_id()).unwrap()) - .set_global_commands(&[SaveFactCommand::create_command().into()]) + .set_global_commands(&[ + GetFactCommand::create_command().into(), + SetFactCommand::create_command().into(), + ]) .await .unwrap(); } @@ -133,11 +284,7 @@ struct ClientCredentialsResponse { fn authorization() -> String { let engine = engine::GeneralPurpose::new(&alphabet::STANDARD, engine::general_purpose::PAD); - let auth = format!( - "{}:{}", - discord_client_id(), - discord_client_secret(), - ); + let auth = format!("{}:{}", discord_client_id(), discord_client_secret(),); engine.encode(auth) } @@ -147,7 +294,10 @@ fn client_credentials_grant() -> ClientCredentialsResponse { .send_form(&[ ("grant_type", "client_credentials"), ("scope", "applications.commands.update"), - ]).unwrap().into_json().unwrap() + ]) + .unwrap() + .into_json() + .unwrap() } fn discord_client_id() -> String { @@ -162,3 +312,7 @@ fn discord_client() -> Client { let token = client_credentials_grant().access_token; Client::new(format!("Bearer {token}")) } + +fn database_url() -> String { + std::env::var("DATABASE_URL").unwrap() +}