forked from jmug/stoatchat
Servers: Core objects required. Includes creation. Bump version to 0.5.0.
This commit is contained in:
@@ -2,13 +2,13 @@ use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::options::FindOneOptions;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Options {
|
||||
keys: Vec<String>
|
||||
keys: Vec<String>,
|
||||
}
|
||||
|
||||
#[post("/settings/fetch", data = "<options>")]
|
||||
@@ -27,14 +27,16 @@ pub async fn req(user: User, options: Json<Options>) -> Result<JsonValue> {
|
||||
doc! {
|
||||
"_id": user.id
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.projection(projection)
|
||||
.build()
|
||||
FindOneOptions::builder().projection(projection).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user_settings" })? {
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user_settings",
|
||||
})?
|
||||
{
|
||||
Ok(json!(doc))
|
||||
} else {
|
||||
Ok(json!({ }))
|
||||
Ok(json!({}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,5 @@ mod get_settings;
|
||||
mod set_settings;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
get_settings::req,
|
||||
set_settings::req
|
||||
]
|
||||
routes![get_settings::req, set_settings::req]
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use rocket::request::Form;
|
||||
use std::collections::HashMap;
|
||||
use rocket_contrib::json::Json;
|
||||
use mongodb::bson::{doc, to_bson};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use mongodb::options::UpdateOptions;
|
||||
use rocket::request::Form;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
type Data = HashMap<String, String>;
|
||||
|
||||
@@ -35,10 +35,10 @@ pub async fn req(user: User, data: Json<Data>, options: Form<Options>) -> Result
|
||||
for (key, data) in &data {
|
||||
set.insert(
|
||||
key.clone(),
|
||||
vec! [
|
||||
vec![
|
||||
to_bson(×tamp).unwrap(),
|
||||
to_bson(&data.clone()).unwrap()
|
||||
]
|
||||
to_bson(&data.clone()).unwrap(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -51,19 +51,20 @@ pub async fn req(user: User, data: Json<Data>, options: Form<Options>) -> Result
|
||||
doc! {
|
||||
"$set": &set
|
||||
},
|
||||
UpdateOptions::builder()
|
||||
.upsert(true)
|
||||
.build()
|
||||
UpdateOptions::builder().upsert(true).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user_settings" })?;
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user_settings",
|
||||
})?;
|
||||
}
|
||||
|
||||
ClientboundNotification::UserSettingsUpdate {
|
||||
id: user.id.clone(),
|
||||
update: json!(set)
|
||||
update: json!(set),
|
||||
}
|
||||
.publish(user.id);
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user