Sync: Allow custom timestamp; return correct data structure.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
Rocket.toml
|
Rocket.toml
|
||||||
/target
|
/target
|
||||||
|
/target_backup
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
.mongo
|
.mongo
|
||||||
.env
|
.env
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ pub enum ClientboundNotification {
|
|||||||
},
|
},
|
||||||
UserSettingsUpdate {
|
UserSettingsUpdate {
|
||||||
id: String,
|
id: String,
|
||||||
update: HashMap<String, String>
|
update: JsonValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,23 +3,40 @@ use crate::notifications::events::ClientboundNotification;
|
|||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
|
use rocket::request::Form;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
use mongodb::bson::{doc, to_bson};
|
use mongodb::bson::{doc, to_bson};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
use mongodb::options::UpdateOptions;
|
use mongodb::options::UpdateOptions;
|
||||||
|
|
||||||
type Data = HashMap<String, String>;
|
type Data = HashMap<String, String>;
|
||||||
|
|
||||||
#[post("/settings/set", data = "<data>")]
|
#[derive(Serialize, Deserialize, FromForm)]
|
||||||
pub async fn req(user: User, data: Json<Data>) -> Result<()> {
|
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<()> {
|
||||||
let data = data.into_inner();
|
let data = data.into_inner();
|
||||||
|
let current_time = Utc::now().timestamp_millis();
|
||||||
|
let timestamp = if let Some(timestamp) = options.timestamp {
|
||||||
|
if timestamp > current_time {
|
||||||
|
current_time
|
||||||
|
} else {
|
||||||
|
timestamp
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
current_time
|
||||||
|
};
|
||||||
|
|
||||||
let mut set = doc! {};
|
let mut set = doc! {};
|
||||||
for (key, data) in &data {
|
for (key, data) in &data {
|
||||||
set.insert(
|
set.insert(
|
||||||
key.clone(),
|
key.clone(),
|
||||||
vec! [
|
vec! [
|
||||||
to_bson(&Utc::now().timestamp_millis()).unwrap(),
|
to_bson(×tamp).unwrap(),
|
||||||
to_bson(&data.clone()).unwrap()
|
to_bson(&data.clone()).unwrap()
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
@@ -32,7 +49,7 @@ pub async fn req(user: User, data: Json<Data>) -> Result<()> {
|
|||||||
"_id": &user.id
|
"_id": &user.id
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": set
|
"$set": &set
|
||||||
},
|
},
|
||||||
UpdateOptions::builder()
|
UpdateOptions::builder()
|
||||||
.upsert(true)
|
.upsert(true)
|
||||||
@@ -44,7 +61,7 @@ pub async fn req(user: User, data: Json<Data>) -> Result<()> {
|
|||||||
|
|
||||||
ClientboundNotification::UserSettingsUpdate {
|
ClientboundNotification::UserSettingsUpdate {
|
||||||
id: user.id.clone(),
|
id: user.id.clone(),
|
||||||
update: data
|
update: json!(set)
|
||||||
}
|
}
|
||||||
.publish(user.id);
|
.publish(user.id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user