New Feature: Settings sync to server.

This commit is contained in:
Paul
2021-05-26 13:02:40 +01:00
parent 5aa9624d5c
commit 3cd3bc54af
7 changed files with 113 additions and 2 deletions

View File

@@ -1,3 +1,3 @@
#!/bin/bash
export version=0.4.2-alpha.0
export version=0.4.2-alpha.1
echo "pub const VERSION: &str = \"${version}\";" > src/version.rs

View File

@@ -1,3 +1,5 @@
use std::collections::HashMap;
use hive_pubsub::PubSub;
use rauth::auth::Session;
use rocket_contrib::json::JsonValue;
@@ -100,6 +102,10 @@ pub enum ClientboundNotification {
id: String,
online: bool,
},
UserSettingsUpdate {
id: String,
update: HashMap<String, String>
}
}
impl ClientboundNotification {

View File

@@ -8,6 +8,7 @@ mod onboard;
mod push;
mod root;
mod users;
mod sync;
pub fn mount(rocket: Rocket) -> Rocket {
rocket
@@ -17,4 +18,5 @@ pub fn mount(rocket: Rocket) -> Rocket {
.mount("/channels", channels::routes())
.mount("/guild", guild::routes())
.mount("/push", push::routes())
.mount("/sync", sync::routes())
}

View File

@@ -0,0 +1,40 @@
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};
#[derive(Serialize, Deserialize)]
pub struct Options {
keys: Vec<String>
}
#[post("/settings/fetch", data = "<options>")]
pub async fn req(user: User, options: Json<Options>) -> Result<JsonValue> {
let options = options.into_inner();
let mut projection = doc! {
"_id": 0,
};
for key in options.keys {
projection.insert(key, 1);
}
if let Some(doc) = get_collection("user_settings")
.find_one(
doc! {
"_id": user.id
},
FindOneOptions::builder()
.projection(projection)
.build()
)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user_settings" })? {
Ok(json!(doc))
} else {
Ok(json!({ }))
}
}

11
src/routes/sync/mod.rs Normal file
View File

@@ -0,0 +1,11 @@
use rocket::Route;
mod get_settings;
mod set_settings;
pub fn routes() -> Vec<Route> {
routes![
get_settings::req,
set_settings::req
]
}

View File

@@ -0,0 +1,52 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use chrono::prelude::*;
use std::collections::HashMap;
use rocket_contrib::json::Json;
use mongodb::bson::{doc, to_bson};
use mongodb::options::UpdateOptions;
type Data = HashMap<String, String>;
#[post("/settings/set", data = "<data>")]
pub async fn req(user: User, data: Json<Data>) -> Result<()> {
let data = data.into_inner();
let mut set = doc! {};
for (key, data) in &data {
set.insert(
key.clone(),
vec! [
to_bson(&Utc::now().timestamp_millis()).unwrap(),
to_bson(&data.clone()).unwrap()
]
);
}
if set.len() > 0 {
get_collection("user_settings")
.update_one(
doc! {
"_id": &user.id
},
doc! {
"$set": set
},
UpdateOptions::builder()
.upsert(true)
.build()
)
.await
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user_settings" })?;
}
ClientboundNotification::UserSettingsUpdate {
id: user.id.clone(),
update: data
}
.publish(user.id);
Ok(())
}

View File

@@ -1 +1 @@
pub const VERSION: &str = "0.4.2-alpha.0";
pub const VERSION: &str = "0.4.2-alpha.1";