diff --git a/.gitignore b/.gitignore index a78433b..f1af4cc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /invites-manager/bin/ /invites-manager/lib/ +server-api/src/schema.rs diff --git a/server-api/.gitignore b/server-api/.gitignore index 24e4eed..60ffa8a 100644 --- a/server-api/.gitignore +++ b/server-api/.gitignore @@ -4,3 +4,5 @@ target static/css/ dev-sql/ diesel.toml + +*.log diff --git a/server-api/Cargo.lock b/server-api/Cargo.lock index c005f7a..84f9040 100644 --- a/server-api/Cargo.lock +++ b/server-api/Cargo.lock @@ -115,7 +115,7 @@ dependencies = [ "num-bigint 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -197,7 +197,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -309,6 +309,7 @@ version = "0.1.0" dependencies = [ "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "mysql_async 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -402,8 +403,8 @@ dependencies = [ "hyper 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)", "mysql_async 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-test 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -810,8 +811,8 @@ dependencies = [ "native-tls 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project 0.4.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tls 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -840,8 +841,8 @@ dependencies = [ "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "rust_decimal 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1118,7 +1119,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1189,15 +1190,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.115" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive" -version = "1.0.115" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1207,12 +1208,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.57" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1289,8 +1290,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1302,9 +1303,9 @@ dependencies = [ "base-x 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1871,9 +1872,9 @@ dependencies = [ "checksum security-framework-sys 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "17bf11d99252f512695eb468de5516e5cf75455521e69dfe343f3b74e4748405" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" -"checksum serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" -"checksum serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" +"checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +"checksum serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +"checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum sha2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" "checksum signal-hook-registry 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" diff --git a/server-api/build.sh b/server-api/build.sh new file mode 100755 index 0000000..a419e3c --- /dev/null +++ b/server-api/build.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +_help() { +cat </dev/null 2>&1 + cargo test 1>/dev/null 2>&1 + cargo run -- -s & + server=$! + echo Waiting on server to spin up && sleep 2 + + export CARGO_BIN=$HOME/.cargo/bin/cargo + python3 client-tests/client.py + kill -9 $server + ;; + b)cargo build;; + r)cargo build --release;; + *) _help;; + esac +done diff --git a/server-api/client-tests/client.py b/server-api/client-tests/client.py new file mode 100644 index 0000000..2e967e2 --- /dev/null +++ b/server-api/client-tests/client.py @@ -0,0 +1,117 @@ +import time +import subprocess, os +import json, requests + +class Response: + def __init__(self, body, code): + self.body = body + self.code = code + + def __str__(self): + return f'{self.code} => {self.body}' + +class Test: + def __init__(self, base='http://localhost:8888', create_admin=False, admin=None): + ''' + @opt:base = base url string + @opt:create_admin = creates admin account directly off cargo -c + @opt:admin = optionally pass in dictionary with some admin credentials + potentially from another instance to run multiple tests at once + ''' + + self.test_count = 0 + if create_admin: + self.body = Test.__create_admin() + elif admin is not None: + self.body = body + else: + # for now we use this because my dev server has this fake ass acc on it + self.body = { + 'secret': 'utO088fltYrTZg323NZfAGrAkArMkDfwjPmt0ooAYta2oJOYDWcAd1FnrpVVMqZtMeUX4_Hu57-LHCkXy8gedg==', + 'id': 23, + 'name': 'admin', + 'joindate':1602385239, + 'status': 0, + 'permissions': 18446744073709551615 + } + + self.base = base + + @staticmethod + def __create_admin(cargo_path: str): + # /home/$user/.cargo/bin/cargo <- normally + # prolly some awful shit on pipes + CARGO_BIN = os.getenv(cargo_path) + raw_json = subprocess.run(f'{CARGO_BIN} run --release -- -c dev-test'.split(), text=True, capture_output=True) + #raw_json = raw_json_b[2:-1].replace('\\n', ' ').strip() + return json.loads(raw_json.stdout) + + + @staticmethod + def log(url: str, method: str, request: requests.Response): + print(f'{method} {url}') + print(f'\t[Status Code]: {request.status_code}') + print(f'\t[Body]: {request.text}') + + def _build_req_body(self, **options): + _map = self.body + for key in options: + _map[key] = options[key] + + return json.dumps(_map) + + + + def post(self, url, **opts): + ''' + @returns text of response + ''' + body = self._build_req_body(**opts) + r = requests.post(self.base + url, data=body) + self.log(self.base + url, 'POST', r) + return r.text + + def get(self, url, **opts): + ''' + @returns text of response + ''' + body = self._build_req_body(**opts) + r = requests.get(self.base + url, data=body) + self.log(self.base + url, 'GET', r) + return r.text + + def delete(self, url, **opts): + ''' + @returns text of response + ''' + body = self._build_req_body(**opts) + r = requests.delete(self.base + url, data=body) + self.log(self.base + url, 'DELETE', r) + return r.text + + def creds(self): + return self.body + + +if __name__ == '__main__': + worker = Test(create_admin=False) + + # First the invites api gets some basic tests + worker.get('/invite/create') + + # Channels things + VOICE_CHANNEL = 1 + TEXT_CHANNEL = 2 + + new_channel_response = worker.post('/channels/create', name=f'{time.time()}', kind=TEXT_CHANNEL) + new_channel = json.loads(new_channel_response) + + channel_list = json.loads(worker.get('/channels/list')) + worker.delete('/channels/delete', channel_id=new_channel['id']) + worker.get('/channels/list') + + # Messaging + + worker.post('/channels/create', name='send-channel', kind=TEXT_CHANNEL) + worker.post('/message/send', channel='send-channel', content="some random content") + worker.delete('/channels/delete', name='send-channel') diff --git a/server-api/db/Cargo.lock b/server-api/db/Cargo.lock index 433aa78..645cd67 100644 --- a/server-api/db/Cargo.lock +++ b/server-api/db/Cargo.lock @@ -2,9 +2,9 @@ # It is not intended for manual editing. [[package]] name = "addr2line" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b6a2d3371669ab3ca9797670853d61402b03d0b4b9ebf33d677dfa720203072" +checksum = "7c0929d69e78dd9bf5408269919fcbcaeb2e35e5d43e5815517cdc6a8e11a423" dependencies = [ "gimli", ] @@ -17,24 +17,24 @@ checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" [[package]] name = "aho-corasick" -version = "0.7.13" +version = "0.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" dependencies = [ "memchr", ] [[package]] name = "arrayvec" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "async-trait" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "687c230d85c0a52504709705fc8a53e4a692b83a2184f03dae73e38e1e93a783" +checksum = "b246867b8b3b6ae56035f1eb1ed557c1d8eae97f0d53696138a50fa0e3a3b8c0" dependencies = [ "proc-macro2", "quote", @@ -49,12 +49,12 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.50" +version = "0.3.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46254cf2fdcdf1badb5934448c1bcbe046a56537b3987d96c51a7afc5d03f293" +checksum = "2baad346b2d4e94a24347adeee9c7a93f412ee94b9cc26e5b59dea23848e9f28" dependencies = [ "addr2line", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide", "object", @@ -63,9 +63,9 @@ dependencies = [ [[package]] name = "base-x" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b20b618342cf9891c292c4f5ac2cde7287cc5c87e87e9c769d617793607dec1" +checksum = "c2734baf8ed08920ccecce1b48a2dfce4ac74a973144add031163bd21a1c5dab" [[package]] name = "base64" @@ -138,9 +138,9 @@ checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "cc" -version = "1.0.59" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" +checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d" [[package]] name = "cfg-if" @@ -149,22 +149,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] -name = "chrono" -version = "0.4.15" +name = "cfg-if" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ + "libc", "num-integer", "num-traits", "serde", "time 0.1.44", + "winapi 0.3.9", ] [[package]] name = "const_fn" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce90df4c658c62f12d78f7508cf92f9173e5184a539c10bfe54a3107b3ffd0f2" +checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" [[package]] name = "core-foundation" @@ -184,11 +192,11 @@ checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" [[package]] name = "crc32fast" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -197,7 +205,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-channel", "crossbeam-deque", "crossbeam-epoch", @@ -207,12 +215,12 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ee0cc8804d5393478d743b035099520087a5186f3b93fa58cec08fa62407b6" +checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" dependencies = [ - "cfg-if", "crossbeam-utils", + "maybe-uninit", ] [[package]] @@ -233,7 +241,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "lazy_static", "maybe-uninit", @@ -247,7 +255,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "maybe-uninit", ] @@ -259,7 +267,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] @@ -269,6 +277,7 @@ version = "0.1.0" dependencies = [ "async-trait", "mysql_async", + "serde", ] [[package]] @@ -316,11 +325,11 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] name = "flate2" -version = "1.0.17" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94" +checksum = "7411863d55df97a419aa64cb4d2f167103ea9d767e2c54a1868b7ac3f6b47129" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crc32fast", "libc", "libz-sys", @@ -366,15 +375,15 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" [[package]] name = "futures-core" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" +checksum = "18eaa56102984bed2c88ea39026cff3ce3b4c7f508ca970cedf2450ea10d4e46" [[package]] name = "futures-macro" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +checksum = "e36fccf3fc58563b4a14d265027c627c3b665d7fed489427e88e7cc929559efe" dependencies = [ "proc-macro-hack", "proc-macro2", @@ -384,29 +393,29 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc" +checksum = "0e3ca3f17d6e8804ae5d3df7a7d35b2b3a6fe89dac84b31872720fc3060a0b11" [[package]] name = "futures-task" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" +checksum = "96d502af37186c4fef99453df03e374683f8a1eec9dcc1e66b3b82dc8278ce3c" dependencies = [ "once_cell", ] [[package]] name = "futures-util" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +checksum = "abcb44342f62e6f3e8ac427b8aa815f724fd705dfad060b18ac7866c15bb8e34" dependencies = [ "futures-core", "futures-macro", "futures-task", - "pin-project", + "pin-project 1.0.1", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -424,20 +433,20 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] [[package]] name = "gimli" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" +checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" [[package]] name = "idna" @@ -487,7 +496,7 @@ version = "5.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28ff8a57641758c89a37b2d28556a68978b3ea3f709f39953e153acea3dbacf" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "lexical-core", ] @@ -499,22 +508,22 @@ checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" dependencies = [ "arrayvec", "bitflags", - "cfg-if", + "cfg-if 0.1.10", "ryu", "static_assertions", ] [[package]] name = "libc" -version = "0.2.76" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" [[package]] name = "libz-sys" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b34178653005c1181711c333f0e5604a14a1b5115c814fd42304bdd16245e0" +checksum = "602113192b08db8f38796c4e85c39e960c145965140e918018bcde1952429655" dependencies = [ "cc", "pkg-config", @@ -527,7 +536,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -544,26 +553,27 @@ checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" [[package]] name = "memchr" -version = "2.3.3" +version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "memoffset" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c198b026e1bbf08a937e94c6c60f9ec4a2267f5b0d2eec9c1b21b061ce2be55f" +checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" dependencies = [ "autocfg", ] [[package]] name = "miniz_oxide" -version = "0.4.1" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d7559a8a40d0f97e1edea3220f698f78b1c5ab67532e49f68fde3910323b722" +checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d" dependencies = [ "adler", + "autocfg", ] [[package]] @@ -572,7 +582,7 @@ version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "fuchsia-zircon", "fuchsia-zircon-sys", "iovec", @@ -646,7 +656,7 @@ dependencies = [ "mysql_common", "native-tls", "percent-encoding", - "pin-project", + "pin-project 0.4.27", "serde", "serde_json", "thiserror", @@ -682,7 +692,7 @@ dependencies = [ "serde_json", "sha1", "sha2", - "time 0.2.17", + "time 0.2.22", "twox-hash", "uuid", ] @@ -707,11 +717,11 @@ dependencies = [ [[package]] name = "net2" -version = "0.2.34" +version = "0.2.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7" +checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "winapi 0.3.9", ] @@ -729,9 +739,9 @@ dependencies = [ [[package]] name = "num-integer" -version = "0.1.43" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ "autocfg", "num-traits", @@ -739,18 +749,18 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ "autocfg", ] [[package]] name = "object" -version = "0.20.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" +checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" [[package]] name = "once_cell" @@ -771,7 +781,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 0.1.10", "foreign-types", "lazy_static", "libc", @@ -805,18 +815,38 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pin-project" -version = "0.4.23" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa" +checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15" dependencies = [ - "pin-project-internal", + "pin-project-internal 0.4.27", +] + +[[package]] +name = "pin-project" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee41d838744f60d959d7074e3afb6b35c7456d0f61cad38a24e35e6553f73841" +dependencies = [ + "pin-project-internal 1.0.1", ] [[package]] name = "pin-project-internal" -version = "0.4.23" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" +checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81a4ffa594b66bff340084d4081df649a7dc049ac8d7fc458d8e628bfbbb2f86" dependencies = [ "proc-macro2", "quote", @@ -825,9 +855,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.1.7" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715" +checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" [[package]] name = "pin-utils" @@ -837,21 +867,21 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" [[package]] name = "ppv-lite86" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro-hack" -version = "0.5.18" +version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro-nested" @@ -861,9 +891,9 @@ checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" [[package]] name = "proc-macro2" -version = "1.0.19" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ "unicode-xid", ] @@ -926,9 +956,9 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "regex" -version = "1.3.9" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" dependencies = [ "aho-corasick", "memchr", @@ -938,9 +968,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.18" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" +checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" [[package]] name = "remove_dir_all" @@ -953,9 +983,9 @@ dependencies = [ [[package]] name = "rust_decimal" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95ba36e8c41bf675947e200af432325f332f60a0aea0ef2dc456636c2f6037d7" +checksum = "c9e81662973c7a8d9663e64a0de4cd642b89a21d64966e3d99606efdc5fb0cc6" dependencies = [ "num-traits", "serde", @@ -963,9 +993,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.16" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" [[package]] name = "rustc_version" @@ -1038,15 +1068,15 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.115" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" [[package]] name = "serde_derive" -version = "1.0.115" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" dependencies = [ "proc-macro2", "quote", @@ -1055,9 +1085,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.57" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" +checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" dependencies = [ "itoa", "ryu", @@ -1090,11 +1120,11 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" [[package]] name = "socket2" -version = "0.3.12" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "redox_syscall", "winapi 0.3.9", @@ -1102,9 +1132,9 @@ dependencies = [ [[package]] name = "standback" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33a71ea1ea5f8747d1af1979bfb7e65c3a025a70609f04ceb78425bc5adad8e6" +checksum = "f4e0831040d2cf2bdfd51b844be71885783d489898a192f254ae25d57cce725c" dependencies = [ "version_check", ] @@ -1166,9 +1196,9 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" [[package]] name = "syn" -version = "1.0.39" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891d8d6567fe7c7f8835a3a98af4208f3846fba258c1bc3c31d6e506239f11f9" +checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" dependencies = [ "proc-macro2", "quote", @@ -1193,7 +1223,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "rand", "redox_syscall", @@ -1203,18 +1233,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.20" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" +checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.20" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" dependencies = [ "proc-macro2", "quote", @@ -1243,9 +1273,9 @@ dependencies = [ [[package]] name = "time" -version = "0.2.17" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca7ec98a72285d12e0febb26f0847b12d54be24577618719df654c66cadab55d" +checksum = "55b7151c9065e80917fbf285d9a5d1432f60db41d170ccafc749a136b41a93af" dependencies = [ "const_fn", "libc", @@ -1258,9 +1288,9 @@ dependencies = [ [[package]] name = "time-macros" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ae9b6e9f095bc105e183e3cd493d72579be3181ad4004fceb01adbe9eecab2d" +checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1" dependencies = [ "proc-macro-hack", "time-macros-impl", @@ -1342,11 +1372,13 @@ dependencies = [ [[package]] name = "twox-hash" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" +checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59" dependencies = [ + "cfg-if 0.1.10", "rand", + "static_assertions", ] [[package]] @@ -1422,19 +1454,19 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0563a9a4b071746dd5aedbc3a28c6fe9be4586fb3fbadb67c400d4f53c6b16c" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc71e4c5efa60fb9e74160e89b93353bc24059999c0ae0fb03affc39770310b0" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" dependencies = [ "bumpalo", "lazy_static", @@ -1447,9 +1479,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97c57cefa5fa80e2ba15641578b44d36e7a64279bc5ed43c6dbaf329457a2ed2" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1457,9 +1489,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" dependencies = [ "proc-macro2", "quote", @@ -1470,9 +1502,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] name = "winapi" diff --git a/server-api/db/Cargo.toml b/server-api/db/Cargo.toml index b5b3f65..7517282 100644 --- a/server-api/db/Cargo.toml +++ b/server-api/db/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" [dependencies] mysql_async = "0.23.1" async-trait = "0.1.40" +serde = "1.0.117" diff --git a/server-api/db/src/channels.rs b/server-api/db/src/channels.rs index 9ff7a5b..06a2a70 100644 --- a/server-api/db/src/channels.rs +++ b/server-api/db/src/channels.rs @@ -8,7 +8,10 @@ use crate::{VarChar, UBigInt, Integer}; use crate::common::FromDB; use crate::{sql_err, no_conn, Response}; +use serde::Serialize; + #[allow(dead_code)] +#[derive(Serialize)] pub struct Channel { pub id: UBigInt, pub name: VarChar, @@ -16,12 +19,21 @@ pub struct Channel { pub kind: Integer } +pub const VOICE_CHANNEL: Integer = 1; +pub const TEXT_CHANNEL: Integer = 2; + #[async_trait] -impl FromDB for Channel { +impl FromDB for Channel { // id name desc kind type Row = Option<(UBigInt, VarChar, Option, Integer)>; async fn get(p: &Pool, id: UBigInt) -> Response { + //! Retrieves a Full single Channel row from the DB or fails in a + //! fashion described by crate::Response + //! @param p -> SqlPool + //! @param id -> UBigInt + //! @return on_success -> Response::Row(Channel) + //! @return on_fail -> Response::{Empty, Other} if let Ok(conn) = p.get_conn().await { let q = "SELECT id, name, description, kind FROM channels WHERE id = :id"; let result: Result<(Conn, Self::Row), SqlError> = @@ -44,6 +56,11 @@ impl FromDB for Channel { } async fn update(p: &Pool, row: Self) -> Response { + //! Updates a whole single based on a given Row Of Channel Type + //! @param p -> SqlPool + //! @param row -> Channel + //! @return on_success -> Response::Success + //! @return on_failure -> Response::Other if let Ok(conn) = p.get_conn().await { let q = "UPDATE channels SET name = :name, description = :desc, kind = :kind @@ -64,16 +81,113 @@ impl FromDB for Channel { } async fn delete(p: &Pool, id: UBigInt) -> Response { - let conn = p.get_conn().await; - if conn.is_err() { - return Response::Other(no_conn!("Member::FromDB::delete")) + //! Deletes channel given UBigInt as the row key + //! @param p -> SqlPool + //! @param id -> UBigInt + //! @return on_success -> Response::Success + //! @return on_failure -> Response::Other + if let Ok(conn) = p.get_conn().await { + let q = "DELETE FROM channels WHERE id = :id"; + let result: Result = + conn.drop_exec(q, params!{"id" => id}).await; + return match result { + Ok(_) => Response::Success, + Err(_) => Response::Other(sql_err!("Member::FromDB::delete Listen i dont know either")) + } } + } - // TODO: do something with the return values where possible - return match conn.unwrap().prep_exec("DELETE FROM CHANNELS WHERE id = :id", params!{"id" => id}).await { - Ok(_) => Response::Success, - Err(_) => Response::Other(sql_err!("Channels::FromDB::delete")) + async fn filter(p: &Pool, kind: Integer) -> Response { + //! @returns -> on success : Response::Set(Vec) + //! @returns -> on empty set : Response::Set(EmptyVector) + //! @params -> on fail : Response::Other + + // NOTE: used for mapping datasets to vectors + let map_rows = |row| { + let (id, name, desc, k): (UBigInt, VarChar, Option, Integer) = + mysql_async::from_row(row); + Channel { + id: id, + name: name, + description: desc, + kind: k + } + }; + return match (p.get_conn().await, kind) { + // Filter for text/voice channels specifically + (Ok(conn), VOICE_CHANNEL..=TEXT_CHANNEL) => { // @NOTE: voice channel and text_channel are literally 1 & 2 respectively + let q = "SELECT id, name, description, kind FROM channels WHERE kind = :kind"; + let q_result = conn.prep_exec(q, params!{"kind" => kind}).await; + if let Ok(res) = q_result { + let mapping_result = res.map_and_drop(map_rows).await; + return match mapping_result { + Ok((_, channels)) => Response::Set(channels), + Err(_) => Response::Other(sql_err!("db::Channels::filter @with params")) + }; + } + else { + Response::Other(sql_err!("")) + } + }, + /* + * Here we are attempting to get all the channels with no filters applied + * This should fetch everything basically in our channels registry + */ + (Ok(conn), _) => { + let q = "SELECT id, name, description, kind FROM channels"; + if let Ok(query) = conn.prep_exec(q, ()).await { + let mapping_r = query.map_and_drop(map_rows).await; + + return match mapping_r { + Ok((_, channels)) => Response::Set(channels), + Err(_) => Response::Other(sql_err!("db::Channels::filter @no params")) + }; + } + else { + Response::Other(sql_err!("db::Channels::filter @no params @no initial query")) + } + }, + (Err(_), _) => {Response::Other(no_conn!("Channel::FromDB::filter"))} } } } +impl Channel { + pub async fn add(p: &Pool, name: &str, desc: &str, kind: Integer) -> Response { + //! @returns on success -> Response::Row + //! @returns on partial success -> Response::Empty + //! @returns on failure -> Response::Other + if let Ok(conn) = p.get_conn().await { + let q = "INSERT INTO channels (name, description, kind) VALUES (:n, :d, :k)"; + let insert_result = conn.drop_exec(q, params!{ + "n" => name, + "d" => desc, + "k" => kind + }).await; + if let Ok(conn) = insert_result { + // This is only kosher because names are enforced as unique by sql + let q = "SELECT id FROM channels WHERE name = :name"; + let fetch_result : Result<(Conn, Option), SqlError> = + conn.first_exec(q, params!{"name" => name}).await; + + return match fetch_result { + Ok((_, id_opt)) => { + if let Some(id) = id_opt { + Response::Row(Channel { + id: id, + name: name.into(), + description: Some(desc.into()), + kind: kind + }) + } + else { Response::Empty } + }, + Err(_) => Response::Empty + }; + } + return Response::Other("Could fetch new channel".into()); + } + return Response::Other(no_conn!("db::channels::add")) + } +} + diff --git a/server-api/db/src/common.rs b/server-api/db/src/common.rs index 66a6620..22669bd 100644 --- a/server-api/db/src/common.rs +++ b/server-api/db/src/common.rs @@ -18,6 +18,13 @@ macro_rules! sql_err { } } +#[macro_export] +macro_rules! sql_err_log { + ($spec:expr) => { + println!($spec); + } +} + /* * NOTE: pay attention to work on async in traits for rust * Each of one these funcs will implicitly do a single heap allocation which @@ -28,7 +35,7 @@ macro_rules! sql_err { * */ #[async_trait] -pub trait FromDB { +pub trait FromDB { type Row; async fn get(p: &Pool, id: UBigInt) -> Response; @@ -36,4 +43,6 @@ pub trait FromDB { async fn update(p: &Pool, row: T) -> Response; async fn delete(p: &Pool, id: UBigInt) -> Response; + + async fn filter(p: &Pool, filter_val: FilterType) -> Response; } diff --git a/server-api/db/src/invites.rs b/server-api/db/src/invites.rs index 5b854e5..b64d58a 100644 --- a/server-api/db/src/invites.rs +++ b/server-api/db/src/invites.rs @@ -6,7 +6,7 @@ use async_trait::async_trait; use crate::{UBigInt, BigInt}; use crate::common::FromDB; -use crate::Response; +use crate::{Response, no_conn}; #[allow(dead_code)] pub struct Invite { @@ -16,7 +16,7 @@ pub struct Invite { } #[async_trait] -impl FromDB for Invite { +impl FromDB for Invite { type Row = Option<(BigInt, Option, bool)>; async fn get(p: &Pool, id: UBigInt) -> Response { @@ -84,4 +84,30 @@ impl FromDB for Invite { } return Response::Success; } + async fn filter(p: &Pool, expirey_flag: bool) -> Response { + if let Ok(conn) = p.get_conn().await { + let q = "SELECT id, uses, expires FROM invites WHERE expires = :exp"; + if let Ok(query) = conn.prep_exec(q, params!{"exp" => expirey_flag}).await { + let mapping_r = query.map_and_drop(|row| { + let (id, uses): (BigInt, Option) = mysql_async::from_row(row); + Invite { + id: id, + uses: uses, + expires: expirey_flag + } + }).await; + return match mapping_r { + Ok((_, invites)) => Response::Set(invites), + Err(_) => Response::Empty + } + } + else { + return Response::Other(no_conn!("db::Invite::filter")); + } + } + else { + return Response::Other(no_conn!("db::Invites::filter")); + } + } } + diff --git a/server-api/db/src/lib.rs b/server-api/db/src/lib.rs index 6f9cdc2..914b576 100644 --- a/server-api/db/src/lib.rs +++ b/server-api/db/src/lib.rs @@ -1,8 +1,9 @@ -mod member; -mod common; -mod invites; -mod channels; -mod messages; +extern crate serde; +pub mod member; +pub mod common; +pub mod invites; +pub mod channels; +pub mod messages; use std::vec::Vec; diff --git a/server-api/db/src/member.rs b/server-api/db/src/member.rs index bb443a9..617163e 100644 --- a/server-api/db/src/member.rs +++ b/server-api/db/src/member.rs @@ -9,7 +9,6 @@ use crate::{UBigInt, BigInt, Integer, VarChar}; use crate::common::{FromDB}; -#[allow(dead_code)] // only because some fields are read by the user pub struct Member { pub id: UBigInt, pub secret: VarChar, @@ -19,6 +18,11 @@ pub struct Member { pub permissions: UBigInt, } + +pub const STATUS_ONLINE: Integer = 0; +pub const STATUS_OFFLINE: Integer = 1; +pub const STATUS_AWAY: Integer = 2; +pub const STATUS_DO_NOT_DISTURB: Integer = 3; /* * * conn = getconn @@ -27,7 +31,7 @@ pub struct Member { * */ #[async_trait] -impl FromDB for Member { +impl FromDB for Member { type Row = Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>; async fn get(p: &Pool, id: UBigInt) -> Response { @@ -108,4 +112,42 @@ impl FromDB for Member { return Response::Empty; } + + async fn filter(p: &Pool, status: Integer) -> Response { + //! @params status + return match (p.get_conn().await, status) { + (Ok(conn), STATUS_ONLINE) | (Ok(conn), STATUS_OFFLINE) | + (Ok(conn), STATUS_AWAY) | (Ok(conn), STATUS_DO_NOT_DISTURB) => { + // TODO: Allow people to query somewhere in the middle of this set + // i.e. we should be able to get user 100 -> 150 instead of just the + // first 1000 people + let q = + "SELECT id, name, status, permissions FROM memebrs + WHERE status = :stat LIMIT 1000"; // high limit for now + if let Ok(query) = conn.prep_exec(q, params!{"stat" => status}).await { + let mapping_r = query.map_and_drop(|row| { + let (id, name, status, permissions): (UBigInt, VarChar, Integer, UBigInt) = + mysql_async::from_row(row); + + Member { + id: id, + secret: "".into(), // no show for obv reasons + name: name, + joindate: 0, // doesn't matter + status: status, + permissions: permissions + } + }).await; + match mapping_r { + Ok((_, members)) => Response::Set(members), + Err(_) => Response::Other(sql_err!("db::Members::filter")) + } + } + else { + Response::Other(sql_err!("db::Members::filter")) + } + } + _ => Response::Other(sql_err!("err")) + } + } } diff --git a/server-api/db/src/messages.rs b/server-api/db/src/messages.rs index 1a81109..e11ed9b 100644 --- a/server-api/db/src/messages.rs +++ b/server-api/db/src/messages.rs @@ -5,7 +5,7 @@ use mysql_async::error::Error as SqlError; use async_trait::async_trait; use crate::{Response, no_conn, sql_err}; -use crate::{UBigInt, BigInt, Integer, VarChar}; +use crate::{UBigInt, BigInt, VarChar}; use crate::common::{FromDB}; @@ -19,10 +19,12 @@ pub struct Message { } #[async_trait] -impl FromDB for Message { +impl FromDB for Message { type Row = Option<(UBigInt, BigInt, VarChar, UBigInt, VarChar)>; async fn get(p: &Pool, id: UBigInt) -> Response { + //! Typically used as the backend to the .update(...) method to + //! pick out a message to later edit if let Ok(conn) = p.get_conn().await { let q = "SELECT id, time, content, author_id, channel_name WHERE id = :id"; let result: Result<(Conn, Self::Row), SqlError> = @@ -45,6 +47,7 @@ impl FromDB for Message { } async fn update(p: &Pool, row: Self) -> Response { + //! Primarily used by users to edit previous comments // NOTE: we only allow the changing of content in this since // no other column has good reason to be modified if let Ok(conn) = p.get_conn().await { @@ -62,6 +65,8 @@ impl FromDB for Message { } async fn delete(p: &Pool, id: UBigInt) -> Response { + //! Deletes a single message + //! Typically used by normal users/bots to remove unwanted comments if let Ok(conn) = p.get_conn().await { let q = "DELETE FROM messages WHERE id = :id"; let result: Result = @@ -73,4 +78,37 @@ impl FromDB for Message { } return Response::Other(no_conn!("Message::FromDB::update")) } + + async fn filter(p: &Pool, (time, channel_id): (BigInt, UBigInt)) -> Response { + //! FIlter happens via unix_timestamp and channel_id respectively + if let Ok(conn) = p.get_conn().await { + let q = "SELECT id, time, content, author_id"; + if let Ok(query)= conn.prep_exec(q, params!{"time" => time, "cid" => channel_id}).await { + let mapping_r = query.map_and_drop(|row| { + let (id, time, content, uid): (UBigInt, BigInt, VarChar, UBigInt) = + mysql_async::from_row(row); + + Message { + id: id, + time: time, + content: content, + author_id: uid, + channel_name: "".into() // no point at this point tbh + } + }).await; + + match mapping_r { + Ok((_, messages)) => Response::Set(messages), + Err(_) => Response::Other(sql_err!("db::Message::filter")) + } + } + else { + return Response::Empty; + } + + } + else { + return Response::Other(no_conn!("db::Message::filter")); + } + } } diff --git a/server-api/migrations/2020-03-11-005217_channels/up.sql b/server-api/migrations/2020-03-11-005217_channels/up.sql index ccc7987..de4b94c 100644 --- a/server-api/migrations/2020-03-11-005217_channels/up.sql +++ b/server-api/migrations/2020-03-11-005217_channels/up.sql @@ -1,7 +1,8 @@ +-- TODO: somehow make the name colum unique CREATE TABLE IF NOT EXISTS `channels` ( `id` BIGINT UNSIGNED NOT NULL auto_increment, - `name` UNIQUE VARCHAR(255) NOT NULL, - `description` VARCHAR(4096), + `name` VARCHAR(255) NOT NULL, + `description` VARCHAR(1024), `kind` INTEGER NOT NULL, PRIMARY KEY(`id`), UNIQUE KEY(`name`) ); diff --git a/server-api/src/auth.rs b/server-api/src/auth.rs index ba1f371..8a11c6a 100644 --- a/server-api/src/auth.rs +++ b/server-api/src/auth.rs @@ -1,10 +1,13 @@ use bcrypt; -use mysql_async::{Conn, Pool}; -use mysql_async::prelude::{params, Queryable}; -use crate::db_types::{BigInt, Integer, UBigInt, VarChar}; +use mysql_async::{Pool}; +use mysql_async::error::Error as SqlError; + use crate::routes; +use db::{member::Member, common::FromDB}; +use db::Response; + // used when we create a new users for the first time pub const BCRYPT_COST: u32 = 14; pub enum AuthReason { @@ -15,30 +18,24 @@ pub enum AuthReason { } -fn valid_user(secret: &str, row: &Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>) -> bool { - match row { - Some(row) => { - match bcrypt::verify(secret, &row.0) { - Ok(result) => result, - Err(_) => return false - } - }, - _ => return false +fn valid_user(given_pass: &str, hash: &str) -> bool { + return match bcrypt::verify(given_pass, hash) { + Ok(result) => result, + Err(_) => return false } } -fn valid_perms(user_opt: &Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>, path: &str) -> bool { +fn valid_perms(member: Member, path: &str) -> bool { use crate::perms; - if let Some(user) = user_opt { - if let Some(p) = perms::get_perm_mask(path) { - return (p & user.4) == p; - } - return true; // no perms required + // if there are perms on the current path make sure the user has them + if let Some(p) = perms::get_perm_mask(path) { + return (p & member.permissions) == p; } - return false; + // if no perms then we don't care + return true; } -pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { +pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { // Start by Checking if the api key is in our keystore if routes::is_open(path) { Ok(AuthReason::OpenAuth) @@ -48,21 +45,23 @@ pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> /* * If we apparantly have user data then check for validity in credentials */ - (Some(id_v), Some(secret_v)) => { /* unwrapping because i couldn't care less about poorly formatted request data */ - let id = id_v.as_u64().unwrap(); - let secret = secret_v.as_str().unwrap(); - let conn = pool.get_conn().await?; - let db_tup: (Conn, Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>) = conn.first_exec( - "SELECT secret, name, joindate, status, permissions FROM members WHERE id = :id", - mysql_async::params!{"id" => id}).await?; - let user_data = &db_tup.1; - if valid_user(secret, user_data) && valid_perms(user_data, path) { - Ok(AuthReason::Good) - } - else { - Ok(AuthReason::BadKey) + let id = id_v.as_u64().unwrap_or(0); // basically nobody is allowed to have 0 as its supposed to be reserved + let secret = secret_v.as_str().unwrap_or(""); + use std::borrow::Cow; + return match Member::get(pool, id).await { + Response::Row(user) => { + if valid_user(secret, &user.secret) && valid_perms(user, path){ + Ok(AuthReason::Good) + } + else { + Ok(AuthReason::BadKey) + } + }, + Response::Empty => Ok(AuthReason::BadKey), + Response::Other(err) => Err(SqlError::Other(Cow::from(err))), + _ => Err(SqlError::Other(Cow::from("Undefined result"))) } }, _ => { diff --git a/server-api/src/channels.rs b/server-api/src/channels.rs index fdb537d..2126f41 100644 --- a/server-api/src/channels.rs +++ b/server-api/src/channels.rs @@ -1,181 +1,65 @@ -use hyper::{StatusCode, Response, Body}; -use hyper::header::HeaderValue; +use hyper::{ + StatusCode, Response, Body, + header::HeaderValue +}; -use mysql_async::{Conn, Pool}; -use mysql_async::error::Error; -use mysql_async::prelude::{params, Queryable}; +use mysql_async::Pool; -use serde_json::Value; +use serde_json::{Value, to_string}; -use crate::db_types::{UBigInt, VarChar, Integer}; -use crate::common; +use db::{ + self, + common::FromDB, + channels::Channel +}; -#[derive(Debug)] -pub enum ChannelType { - Voice, - Text, - Undefined -} - -impl ChannelType { - // These funcs are mainly here to help translation from mysql - pub fn from_i32(x: i32) -> ChannelType { - match x { - 1 => ChannelType::Voice, - 2 => ChannelType::Text, - _ => ChannelType::Undefined - } - } - pub fn as_i32(&self) -> i32 { - match self { - ChannelType::Voice => 1, - ChannelType::Text => 2, - ChannelType::Undefined => 3, - - } - } -} - -// Primary way of interpretting sql data on our channels table -pub struct Channel { - pub id: u64, - pub name: String, - pub description: String, - pub kind: ChannelType -} - -#[derive(Debug)] -struct InsertableChannel { - name: String, - kind: ChannelType -} - -impl Channel { - /* - * When our sql library queries things we generally get back tuples rather reasily - * we can use this method to get something that makes more sense - */ - fn from_tup(tup: (UBigInt, VarChar, Option, Integer)) -> Channel { - let desc = match tup.2 { - Some(val) => val, - None => "None".into() - }; - - Channel { - id: tup.0, - name: tup.1, - description: desc, - kind: ChannelType::from_i32(tup.3) - } - } - /* - * When responding with some channel data to the client we use json - * this itemizes a single struct as the following(without the pretty output) - * { - * "id": id, - * "name": "", - * "description": Option<"">, - * "kind": kind - * } - */ - fn as_json_str(&self) -> String { - let mut base = String::from("{"); - base.push_str(&format!("\"id\":{},", self.id)); - base.push_str(&format!("\"name\":\"{}\",", self.name)); - base.push_str(&format!("\"description\":\"{}\",", self.description)); - base.push_str(&format!("\"kind\":{}}}", self.kind.as_i32())); - return base; - } -} - - -/* - * Forwarding SQL errors as we can handle those error in caller site for this leaf function - * On success we back a Vec which we can JSON'ify later and use as a response -*/ -async fn get_channels_vec(conn: Conn) -> Result, Error> { - let rows_db = conn.prep_exec(r"SELECT * FROM channels", ()).await?; - let (_, rows) = rows_db.map_and_drop(|row| { - let (id, name, desc, kind): (UBigInt, VarChar, Option, Integer) = mysql_async::from_row(row); - Channel::from_tup((id, name, desc, kind)) - }).await?; - - Ok(rows) -} pub async fn list_channels(pool: &Pool, response: &mut Response) { /* - * Primary dispatcher for dealing with the CHANNELS_LIST route - * For the most part this function will have a lot more error handling as it - * should know what kind of issues its child functions will have + * @user-params -> for now none as i don't feel like dealing with it + * @TODO: add in a let var that actually */ - if let Ok(conn) = pool.get_conn().await { - match get_channels_vec(conn).await { - Ok(chans) => { - *response.status_mut() = StatusCode::OK; - response.headers_mut().insert("Content-Type", - HeaderValue::from_static("application/json")); - - // At this point we build the content of our response body - // which is a json payload hence why there is weird string manipulation - // because we're trying to avoid dependancy issues and serializing things ourselves - let mut new_body = String::from("{\"channels\":["); - for chan in chans.iter() { - let s = format!("{},", chan.as_json_str()); - new_body.push_str(&s); - } - if new_body.ends_with(',') {new_body.pop();} - new_body.push_str("]}"); + return match db::channels::Channel::filter(pool, 0).await { + db::Response::Set(channels) => { + response.headers_mut().insert("Content-Type", + HeaderValue::from_static("application/json")); - *response.body_mut() = Body::from(new_body); - }, - Err(_) => { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } - } - } - else { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } -} - - -async fn insert_channel(pool: &Pool, name: &str, desc: &str, kind: i64) -> Result<(), Error>{ - let conn = pool.get_conn().await?; - conn.prep_exec( - "INSERT INTO channels (name, description, kind) VALUES (:name, :description, :kind)", - params!{"name" => name, "kind" => kind, "description" => desc}).await?; - Ok(()) + *response.body_mut() = Body::from(to_string(&channels).unwrap_or("{}".into())) + }, + db::Response::Other(_msg) => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR, + _ => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR, + }; } pub async fn create_channel(pool: &Pool, response: &mut Response, params: Value) { /* * Create a channel base on a few parameters that may or may not be there + * @responds with the data of the newly created channel */ // Theres an extra un-needed unwrap to be cut out from this proc // specifically with the desc parameter + use std::convert::TryInto; + let req_params: (Option<&str>, Option<&str>, Option) = match (params.get("name"), params.get("description"), params.get("kind")) { (Some(name), Some(desc), Some(kind)) => (name.as_str(), desc.as_str(), kind.as_i64()), (Some(name), None, Some(kind)) => (name.as_str(), Some("No Description"), kind.as_i64()), _ => (None, None, None) }; + match req_params { (Some(name), Some(desc), Some(kind)) => { - match insert_channel(pool, name, desc, kind).await { - // Server Errors are generally _ok_ to reveal in body I suppose - Err(Error::Server(se)) => { - common::db_err_response_body(response, se); - //*response.status_mut() = StatusCode::BAD_REQUEST; - //let b = format!("Server code: {}\nServer Message:{}", se.code, se.message); - //*response.body_mut() = Body::from(b); + // Send the data up to the db, then return the new channel back to the user(?) + match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await { + db::Response::Row(row) => { + response.headers_mut().insert("Content-Type", + HeaderValue::from_static("application/json")); + + *response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into())); }, - // generic errors get a 500 - Err(_) => { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } - // Nothing to do when things go right - _ => {} + db::Response::Empty => {}, + db::Response::Other(msg) => {}, + _ => {*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;} } }, // basically one of the parameter gets failed so we bail on all of this @@ -183,21 +67,20 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: } } -async fn db_delete_channel(pool: &Pool, name: &Value) -> Result<(), Error> { - let conn = pool.get_conn().await?; - conn.prep_exec(r"DELETE FROM channels WHERE name = :name", params!{"name" => name.as_str().unwrap()}).await?; - Ok(()) -} - pub async fn delete_channel(pool: &Pool, response: &mut Response, params: Value) { // make sure we have the right parameters provided - if let Some(name) = params.get("name") { - match db_delete_channel(pool, name).await { - Ok(_) => *response.status_mut() = StatusCode::OK, - Err(e) => { - *response.body_mut() = Body::from(format!("delete_chanel sql error :\n{}", e)); + if let Some(name) = params.get("channel_id") { + if let Some(id) = name.as_u64() { + // TODO: something more intelligent with the logging im ngl + match Channel::delete(pool, id).await { + db::Response::Success => {}, + db::Response::Other(data) => println!("\t{}", data), + _ => {} } } + else { + *response.status_mut() = StatusCode::BAD_REQUEST; + } } else { *response.status_mut() = StatusCode::BAD_REQUEST; diff --git a/server-api/src/common.rs b/server-api/src/common.rs deleted file mode 100644 index 72f6780..0000000 --- a/server-api/src/common.rs +++ /dev/null @@ -1,38 +0,0 @@ -use mysql_async::error::ServerError; -use hyper::{Body, Response, StatusCode}; -use serde::Serialize; -use serde_json::to_string as json; - -#[derive(Serialize)] -struct GenericErrData { - status: u16, - message: &'static str, - note: &'static str, -} - - -pub fn db_err_response_body(response: &mut Response, err: ServerError) { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - match err.code { - // Duplicate unique value was (tried to be) inserted - 1062 => { - - let s = json(&GenericErrData { - status: 1062, - message: "Duplicate key was inserted and failed", - note: "Check parameters given" - }).unwrap(); - *response.body_mut() = Body::from(s); - }, - // Generic errors - _ => { - let s = json(&GenericErrData{ - status: 500, - message: "Generic Backend error", - note:"" - }).unwrap(); - *response.body_mut() = Body::from(s); - } - } -} - diff --git a/server-api/src/db_types.rs b/server-api/src/db_types.rs deleted file mode 100644 index 4969457..0000000 --- a/server-api/src/db_types.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub type Integer = i32; - -pub type UBigInt = u64; -pub type BigInt = i64; - -pub type VarChar = String; - diff --git a/server-api/src/invites.rs b/server-api/src/invites.rs index 9236ff1..846b7ad 100644 --- a/server-api/src/invites.rs +++ b/server-api/src/invites.rs @@ -10,7 +10,7 @@ use hyper::{Response, Body, StatusCode}; use chrono::Utc; -use crate::db_types::{UBigInt, BigInt}; +use db::{UBigInt, BigInt}; use crate::members::{self, Member}; #[derive(Serialize)] diff --git a/server-api/src/main.rs b/server-api/src/main.rs index b2f0449..58de189 100644 --- a/server-api/src/main.rs +++ b/server-api/src/main.rs @@ -35,8 +35,6 @@ mod messages; mod admin; mod http_params; -mod db_types; -mod common; mod testing; const NO_ERR: u16 = 0; @@ -48,13 +46,14 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, use routes::resolve_dynamic_route; const GET: &Method = &Method::GET; const POST: &Method = &Method::POST; + const DELETE: &Method = &Method::DELETE; match (meth, path) { /* INVITES */ (GET, routes::INVITE_CREATE) => invites::create(pool, resp, params).await, /* CHANNELS */ (GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await, (POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await, - (POST, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await, + (DELETE, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await, /* MESSAGING */ (POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, params).await, /* ADMIN */ @@ -207,7 +206,7 @@ OPTIONS: if let Some(owner_name) = args.value_of("create-owner") { let p = Pool::new(&env::var("DATABASE_URL").unwrap()); - println!("Creating owner {{ {} }}...", owner_name); + eprintln!("Creating owner {{ {} }}...", owner_name); if let Ok(owner) = members::insert_new_member(&p, owner_name.to_string(), std::u64::MAX).await { println!("{}", serde_json::to_string(&owner).unwrap()); } diff --git a/server-api/src/members.rs b/server-api/src/members.rs index 005ef3b..6dd2302 100644 --- a/server-api/src/members.rs +++ b/server-api/src/members.rs @@ -4,7 +4,8 @@ use mysql_async::{Conn, Pool, error::Error as SqlError}; use mysql_async::prelude::{params, Queryable}; use serde::Serialize; -use crate::db_types::{UBigInt, BigInt, Integer, VarChar}; +use db::{UBigInt, BigInt, Integer, VarChar}; +use db::member::STATUS_ONLINE; use crate::auth; #[derive(Serialize)] @@ -17,10 +18,6 @@ pub struct Member { pub permissions: UBigInt, } -const STATUS_ONLINE: Integer = 0; -const _STATUS_OFFLINE: Integer = 1; -const _STATUS_AWAY: Integer = 2; -const _STATUS_DO_NOT_DISTURB: Integer = 3; pub async fn insert_new_member(p: &Pool, name: VarChar, perms: u64) -> Result { use crate::auth::generate_secret; diff --git a/server-api/src/messages.rs b/server-api/src/messages.rs index ffe0f6f..220f6df 100644 --- a/server-api/src/messages.rs +++ b/server-api/src/messages.rs @@ -7,7 +7,7 @@ use hyper::{Response, Body, StatusCode}; use serde_json::Value; use chrono::Utc; -use crate::db_types::{UBigInt}; +use db::UBigInt; pub async fn insert_message(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt) diff --git a/server-api/src/testing/mod.rs b/server-api/src/testing/mod.rs index 31cdd81..8fedc84 100644 --- a/server-api/src/testing/mod.rs +++ b/server-api/src/testing/mod.rs @@ -20,8 +20,8 @@ pub fn hyper_resp() -> hyper::Response { #[cfg(test)] -pub async fn tmp_channel_params(p: &mysql_async::Pool, chan_name: &'static str) -> crate::channels::Channel { - use crate::channels::{Channel, ChannelType}; +pub async fn tmp_channel_params(p: &mysql_async::Pool, chan_name: &'static str) -> db::channels::Channel { + use db::channels::{Channel, TEXT_CHANNEL}; use mysql_async::{params, prelude::Queryable}; let conn = p.get_conn().await.unwrap(); @@ -32,7 +32,7 @@ pub async fn tmp_channel_params(p: &mysql_async::Pool, chan_name: &'static str) Channel { id: 0, name: chan_name.into(), - kind: ChannelType::Text, - description: "no description for testing".into() + kind: TEXT_CHANNEL, + description: Some("no description for testing".into()) } }