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

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();