From a4c1fee4cce94d92463e973bfb5668e29225a6b8 Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Tue, 1 Jun 2021 13:25:47 +0100
Subject: [PATCH] Sync: Allow custom timestamp; return correct data structure.

---
 .gitignore                      |  1 +
 src/notifications/events.rs     |  2 +-
 src/routes/sync/set_settings.rs | 27 ++++++++++++++++++++++-----
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore
index 27e8598..187298e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 Rocket.toml
 /target
+/target_backup
 **/*.rs.bk
 .mongo
 .env
diff --git a/src/notifications/events.rs b/src/notifications/events.rs
index fa8c438..2f96107 100644
--- a/src/notifications/events.rs
+++ b/src/notifications/events.rs
@@ -104,7 +104,7 @@ pub enum ClientboundNotification {
     },
     UserSettingsUpdate {
         id: String,
-        update: HashMap<String, String>
+        update: JsonValue
     }
 }
 
diff --git a/src/routes/sync/set_settings.rs b/src/routes/sync/set_settings.rs
index 65c6f52..f25405c 100644
--- a/src/routes/sync/set_settings.rs
+++ b/src/routes/sync/set_settings.rs
@@ -3,23 +3,40 @@ 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;
 
 type Data = HashMap<String, String>;
 
-#[post("/settings/set", data = "<data>")]
-pub async fn req(user: User, data: Json<Data>) -> Result<()> {
+#[derive(Serialize, Deserialize, FromForm)]
+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 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! {};
     for (key, data) in &data {
         set.insert(
             key.clone(),
             vec! [
-                to_bson(&Utc::now().timestamp_millis()).unwrap(),
+                to_bson(&timestamp).unwrap(),
                 to_bson(&data.clone()).unwrap()
             ]
         );
@@ -32,7 +49,7 @@ pub async fn req(user: User, data: Json<Data>) -> Result<()> {
                     "_id": &user.id
                 },
                 doc! {
-                    "$set": set
+                    "$set": &set
                 },
                 UpdateOptions::builder()
                     .upsert(true)
@@ -44,7 +61,7 @@ pub async fn req(user: User, data: Json<Data>) -> Result<()> {
 
     ClientboundNotification::UserSettingsUpdate {
         id: user.id.clone(),
-        update: data
+        update: json!(set)
     }
     .publish(user.id);
     
-- 
GitLab