Migrate the rest of the code.

This commit is contained in:
Paul
2021-08-04 10:11:21 +01:00
committed by Paul Makles
parent 9fec4e48c0
commit 382972ea22
12 changed files with 49 additions and 216 deletions

197
Cargo.lock generated
View File

@@ -94,6 +94,7 @@ dependencies = [
"num_cpus",
"once_cell",
"tokio 0.2.25",
"tokio 1.9.0",
]
[[package]]
@@ -501,16 +502,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "cookie"
version = "0.15.0-dev"
source = "git+https://github.com/SergioBenitez/cookie-rs.git?rev=1c3ca83#1c3ca838543b60a4448d279dc4b903cc7a2bc22a"
dependencies = [
"percent-encoding",
"time 0.2.25",
"version_check",
]
[[package]]
name = "cookie"
version = "0.15.1"
@@ -641,32 +632,14 @@ dependencies = [
"syn",
]
[[package]]
name = "devise"
version = "0.3.0"
source = "git+https://github.com/SergioBenitez/Devise.git?rev=3648468#3648468a9ede9ca896cd35bc1eb818c7a9fb3047"
dependencies = [
"devise_codegen 0.3.0",
"devise_core 0.3.0",
]
[[package]]
name = "devise"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50c7580b072f1c8476148f16e0a0d5dedddab787da98d86c5082c5e9ed8ab595"
dependencies = [
"devise_codegen 0.3.1",
"devise_core 0.3.1",
]
[[package]]
name = "devise_codegen"
version = "0.3.0"
source = "git+https://github.com/SergioBenitez/Devise.git?rev=3648468#3648468a9ede9ca896cd35bc1eb818c7a9fb3047"
dependencies = [
"devise_core 0.3.0",
"quote",
"devise_codegen",
"devise_core",
]
[[package]]
@@ -675,22 +648,10 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "123c73e7a6e51b05c75fe1a1b2f4e241399ea5740ed810b0e3e6cacd9db5e7b2"
dependencies = [
"devise_core 0.3.1",
"devise_core",
"quote",
]
[[package]]
name = "devise_core"
version = "0.3.0"
source = "git+https://github.com/SergioBenitez/Devise.git?rev=3648468#3648468a9ede9ca896cd35bc1eb818c7a9fb3047"
dependencies = [
"bitflags",
"proc-macro2",
"proc-macro2-diagnostics",
"quote",
"syn",
]
[[package]]
name = "devise_core"
version = "0.3.1"
@@ -1690,17 +1651,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "mio-uds"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0"
dependencies = [
"iovec",
"libc",
"mio 0.6.23",
]
[[package]]
name = "miow"
version = "0.2.2"
@@ -2272,20 +2222,6 @@ dependencies = [
"yansi",
]
[[package]]
name = "prometheus"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8425533e7122f0c3cc7a37e6244b16ad3a2cc32ae7ac6276e2a75da0d9c200d"
dependencies = [
"cfg-if 1.0.0",
"fnv",
"lazy_static",
"parking_lot",
"regex",
"thiserror",
]
[[package]]
name = "quanta"
version = "0.4.1"
@@ -2541,7 +2477,7 @@ dependencies = [
[[package]]
name = "rauth"
version = "0.3.0"
source = "git+https://gitlab.insrt.uk/insert/rauth?rev=51546e5a1ba0dfe3ec3bd0b060fa51fc3102c96f#51546e5a1ba0dfe3ec3bd0b060fa51fc3102c96f"
source = "git+https://gitlab.insrt.uk/insert/rauth?rev=840ad003263fd5570aba4866e7d72e4fc1b3e95a#840ad003263fd5570aba4866e7d72e4fc1b3e95a"
dependencies = [
"chrono",
"handlebars",
@@ -2552,8 +2488,7 @@ dependencies = [
"nanoid 0.3.0",
"regex",
"reqwest",
"rocket 0.5.0-dev",
"rocket_contrib",
"rocket",
"rust-argon2",
"serde",
"serde_json",
@@ -2711,10 +2646,9 @@ dependencies = [
"rauth",
"regex",
"reqwest",
"rocket 0.5.0-rc.1",
"rocket",
"rocket-governor",
"rocket_cors",
"rocket_prometheus",
"serde",
"serde_json",
"time 0.2.25",
@@ -2739,35 +2673,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "rocket"
version = "0.5.0-dev"
source = "git+https://github.com/SergioBenitez/Rocket?rev=031948c1daaa146128d8a435be116476f2adde00#031948c1daaa146128d8a435be116476f2adde00"
dependencies = [
"async-trait",
"atomic",
"atty",
"binascii",
"either",
"figment",
"futures",
"log",
"memchr",
"num_cpus",
"parking_lot",
"rand 0.7.3",
"ref-cast",
"rocket_codegen 0.5.0-dev",
"rocket_http 0.5.0-dev",
"serde",
"state 0.4.2",
"time 0.2.25",
"tokio 0.2.25",
"ubyte",
"version_check",
"yansi",
]
[[package]]
name = "rocket"
version = "0.5.0-rc.1"
@@ -2792,11 +2697,11 @@ dependencies = [
"pin-project-lite 0.2.4",
"rand 0.8.3",
"ref-cast",
"rocket_codegen 0.5.0-rc.1",
"rocket_http 0.5.0-rc.1",
"rocket_codegen",
"rocket_http",
"serde",
"serde_json",
"state 0.5.2",
"state",
"tempfile",
"time 0.2.25",
"tokio 1.9.0",
@@ -2815,19 +2720,7 @@ checksum = "3a7603d1d66d1c4c41997f1f80fb624c16fd8a063a0f562d6e511e7eec4d26d2"
dependencies = [
"governor",
"lazy_static",
"rocket 0.5.0-rc.1",
]
[[package]]
name = "rocket_codegen"
version = "0.5.0-dev"
source = "git+https://github.com/SergioBenitez/Rocket?rev=031948c1daaa146128d8a435be116476f2adde00#031948c1daaa146128d8a435be116476f2adde00"
dependencies = [
"devise 0.3.0",
"glob",
"indexmap",
"quote",
"rocket_http 0.5.0-dev",
"rocket",
]
[[package]]
@@ -2836,36 +2729,24 @@ version = "0.5.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66f5fa462f7eb958bba8710c17c5d774bbbd59809fa76fb1957af7e545aea8bb"
dependencies = [
"devise 0.3.1",
"devise",
"glob",
"indexmap",
"proc-macro2",
"quote",
"rocket_http 0.5.0-rc.1",
"rocket_http",
"syn",
"unicode-xid",
]
[[package]]
name = "rocket_contrib"
version = "0.5.0-dev"
source = "git+https://github.com/SergioBenitez/Rocket?rev=031948c1daaa146128d8a435be116476f2adde00#031948c1daaa146128d8a435be116476f2adde00"
dependencies = [
"log",
"rocket 0.5.0-dev",
"serde",
"serde_json",
"tokio 0.2.25",
]
[[package]]
name = "rocket_cors"
version = "0.5.2"
source = "git+https://github.com/insertish/rocket_cors?branch=master#a301882a3ae80be9cbe2de7030c5ca7842e1307e"
source = "git+https://github.com/lawliet89/rocket_cors?rev=5843861a88958c16bfaa0b40f0d8910772bcd2f6#5843861a88958c16bfaa0b40f0d8910772bcd2f6"
dependencies = [
"log",
"regex",
"rocket 0.5.0-dev",
"rocket",
"serde",
"serde_derive",
"unicase",
@@ -2873,38 +2754,13 @@ dependencies = [
"url",
]
[[package]]
name = "rocket_http"
version = "0.5.0-dev"
source = "git+https://github.com/SergioBenitez/Rocket?rev=031948c1daaa146128d8a435be116476f2adde00#031948c1daaa146128d8a435be116476f2adde00"
dependencies = [
"cookie 0.15.0-dev",
"either",
"http",
"hyper 0.13.10",
"indexmap",
"log",
"mime",
"parking_lot",
"pear",
"percent-encoding",
"ref-cast",
"smallvec",
"state 0.4.2",
"time 0.2.25",
"tokio 0.2.25",
"uncased",
"unicode-xid",
"version_check",
]
[[package]]
name = "rocket_http"
version = "0.5.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c8b7d512d2fcac2316ebe590cde67573844b99e6cc9ee0f53375fa16e25ebd"
dependencies = [
"cookie 0.15.1",
"cookie",
"either",
"http",
"hyper 0.14.11",
@@ -2920,21 +2776,12 @@ dependencies = [
"serde",
"smallvec",
"stable-pattern",
"state 0.5.2",
"state",
"time 0.2.25",
"tokio 1.9.0",
"uncased",
]
[[package]]
name = "rocket_prometheus"
version = "0.7.0"
source = "git+https://github.com/insertish/rocket_prometheus?rev=3d825aedb42793246c306a81fe67c5b187948983#3d825aedb42793246c306a81fe67c5b187948983"
dependencies = [
"prometheus",
"rocket 0.5.0-dev",
]
[[package]]
name = "rust-argon2"
version = "0.8.3"
@@ -3275,12 +3122,6 @@ dependencies = [
"version_check",
]
[[package]]
name = "state"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483"
[[package]]
name = "state"
version = "0.5.2"
@@ -3529,16 +3370,12 @@ dependencies = [
"futures-core",
"iovec",
"lazy_static",
"libc",
"memchr",
"mio 0.6.23",
"mio-uds",
"num_cpus",
"pin-project-lite 0.1.11",
"signal-hook-registry",
"slab",
"tokio-macros 0.2.6",
"winapi 0.3.9",
]
[[package]]

View File

@@ -33,16 +33,15 @@ lazy_static = "1.4.0"
urlencoding = "1.1.1"
many-to-many = "0.1.2"
lettre = "0.10.0-alpha.4"
rocket-governor = "0.0.1-rc.9"
reqwest = { version = "0.10.8", features = ["json"] }
serde = { version = "1.0.115", features = ["derive"] }
validator = { version = "0.11", features = ["derive"] }
ctrlc = { version = "3.0", features = ["termination"] }
async-std = { version = "1.8.0", features = ["tokio02", "attributes"] }
rocket-governor = { version = "0.0.1-rc.9", features = ["logger"] }
async-tungstenite = { version = "0.10.0", features = ["async-std-runtime"] }
async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] }
rocket = { version = "0.5.0-rc.1", default-features = false, features = ["json"] }
rocket_cors = { git = "https://github.com/insertish/rocket_cors", branch = "master" }
mongodb = { version = "1.2.2", features = ["tokio-runtime"], default-features = false }
rauth = { git = "https://gitlab.insrt.uk/insert/rauth", rev = "51546e5a1ba0dfe3ec3bd0b060fa51fc3102c96f" }
rocket_prometheus = { git = "https://github.com/insertish/rocket_prometheus", rev = "3d825aedb42793246c306a81fe67c5b187948983" }
rauth = { git = "https://gitlab.insrt.uk/insert/rauth", rev = "840ad003263fd5570aba4866e7d72e4fc1b3e95a" }
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", rev = "5843861a88958c16bfaa0b40f0d8910772bcd2f6" }
hive_pubsub = { git = "https://gitlab.insrt.uk/insert/hive", rev = "a89826df2b30166220e68a6ed01a58b751456604", features = ["mongo"] }

View File

@@ -2,7 +2,6 @@ use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::bson::{doc, from_document};
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use validator::Validate;
@@ -127,9 +126,9 @@ impl User {
}
impl<'r> FromParam<'r> for Ref {
type Error = &'r RawStr;
type Error = &'r str;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
if let Ok(result) = Ref::from(param.to_string()) {
if result.validate().is_ok() {
return Ok(result);

View File

@@ -3,15 +3,14 @@ use crate::database::*;
use mongodb::bson::{doc, from_document};
use rauth::auth::Session;
use rocket::http::Status;
use rocket::outcome::try_outcome;
use rocket::request::{self, FromRequest, Outcome, Request};
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for User {
impl<'r> FromRequest<'r> for User {
type Error = rauth::util::Error;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let session: Session = try_outcome!(request.guard::<Session>().await);
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let session: Session = request.guard::<Session>().await.unwrap();
if let Ok(result) = get_collection("users")
.find_one(

View File

@@ -4,6 +4,8 @@
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate impl_ops;
@@ -27,11 +29,10 @@ use rauth::{
options::{Template, Templates},
};
use rocket_cors::AllowedOrigins;
use rocket_prometheus::PrometheusMetrics;
use rocket::catchers;
use util::variables::{
APP_URL, HCAPTCHA_KEY, INVITE_ONLY, PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD,
SMTP_USERNAME, USE_EMAIL, USE_HCAPTCHA, USE_PROMETHEUS,
SMTP_USERNAME, USE_EMAIL, USE_HCAPTCHA,
};
#[async_std::main]
@@ -122,16 +123,7 @@ Reset your password here: {{url}}",
}
let auth = Auth::new(database::get_collection("accounts"), options);
let mut rocket = rocket::ignite();
if *USE_PROMETHEUS {
info!("Enabled Prometheus metrics!");
let prometheus = PrometheusMetrics::new();
rocket = rocket
.attach(prometheus.clone())
.mount("/metrics", prometheus);
}
let rocket = rocket::build();
routes::mount(rocket)
.mount("/", rocket_cors::catch_all_options_routes())

View File

@@ -53,7 +53,7 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
}
let obj = update.as_object_mut().unwrap();
obj.insert("embeds".to_string(), json!(new_embeds).0);
obj.insert("embeds".to_string(), json!(new_embeds));
set.insert("embeds", new_embeds);
}

View File

@@ -8,7 +8,6 @@ use mongodb::{
bson::{doc, from_document},
options::FindOptions,
};
use rocket::form::Form;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
use validator::Validate;
@@ -37,7 +36,7 @@ pub struct Options {
}
#[get("/<target>/messages?<options..>")]
pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<Value> {
pub async fn req(user: User, target: Ref, options: Options) -> Result<Value> {
options
.validate()
.map_err(|error| Error::FailedValidation { error })?;

View File

@@ -1,6 +1,6 @@
pub use rocket::response::Redirect;
pub use rocket::http::Status;
use rocket::{Phase, Rocket};
use rocket::{Build, Rocket};
mod channels;
mod invites;
@@ -11,9 +11,9 @@ mod servers;
mod sync;
mod users;
pub fn mount<T: Phase>(rocket: Rocket<T>) -> Rocket<T> {
pub fn mount(rocket: Rocket<Build>) -> Rocket<Build> {
rocket
.mount("/", routes![root::root])
.mount("/", routes![root::root, root::ping])
.mount("/onboard", onboard::routes())
.mount("/users", users::routes())
.mount("/channels", channels::routes())

View File

@@ -4,7 +4,7 @@ use crate::util::{ratelimit::RateLimitGuard, variables::{
}};
use mongodb::bson::doc;
use rocket::serde::json::Value;
use rocket::{http::Status, serde::json::Value};
use rocket_governor::RocketGovernor;
#[get("/")]
@@ -37,3 +37,8 @@ pub async fn root(_limitguard: RocketGovernor<'_, RateLimitGuard>) -> Value {
"vapid": *VAPID_PUBLIC_KEY
})
}
#[get("/ping")]
pub async fn ping(_limitguard: RocketGovernor<'_, RateLimitGuard>) -> Status {
Status::Ok
}

View File

@@ -5,20 +5,20 @@ use crate::util::result::{Error, Result};
use chrono::prelude::*;
use mongodb::bson::{doc, to_bson};
use mongodb::options::UpdateOptions;
use rocket::form::Form;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
type Data = HashMap<String, String>;
#[derive(Serialize, Deserialize, FromForm)]
#[derive(FromForm, Serialize, Deserialize)]
pub struct Options {
timestamp: Option<i64>,
}
#[post("/settings/set?<options..>", data = "<data>")]
pub async fn req(user: User, data: Json<Data>, options: Form<Options>) -> Result<()> {
pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<()> {
let data = data.into_inner();
let current_time = Utc::now().timestamp_millis();
let timestamp = if let Some(timestamp) = options.timestamp {

View File

@@ -1,9 +1,13 @@
use rocket_governor::{Method, Quota, RocketGovernable, RocketGovernor};
use std::time::Duration;
use rocket_governor::{Method, Quota, RocketGovernable};
pub struct RateLimitGuard;
impl<'r> RocketGovernable<'r> for RateLimitGuard {
fn quota(_method: Method, _route_name: &str) -> Quota {
dbg!(_method, _route_name);
Quota::per_second(Self::nonzero(1u32))
.allow_burst(Self::nonzero(10u32))
}
}

View File

@@ -46,7 +46,6 @@ lazy_static! {
|v| v == *"1"
);
pub static ref USE_HCAPTCHA: bool = env::var("REVOLT_HCAPTCHA_KEY").is_ok();
pub static ref USE_PROMETHEUS: bool = env::var("REVOLT_ENABLE_PROMETHEUS").map_or(false, |v| v == "1");
pub static ref USE_AUTUMN: bool = env::var("AUTUMN_PUBLIC_URL").is_ok();
pub static ref USE_JANUARY: bool = env::var("JANUARY_PUBLIC_URL").is_ok();
pub static ref USE_VOSO: bool = env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok();