From 84810f2bb22a161b7ef5b6e081acde5cadcfd155 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Garc=C3=ADa?=
 <dani-garcia@users.noreply.github.com>
Date: Tue, 16 Mar 2021 18:10:23 +0100
Subject: [PATCH] Remove unnecessary fields from send access

---
 src/api/core/sends.rs |  2 +-
 src/db/models/send.rs | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/api/core/sends.rs b/src/api/core/sends.rs
index b69c4309..86f73af4 100644
--- a/src/api/core/sends.rs
+++ b/src/api/core/sends.rs
@@ -231,7 +231,7 @@ fn post_access(access_id: String, data: JsonUpcase<SendAccessData>, conn: DbConn
 
     send.save(&conn)?;
 
-    Ok(Json(send.to_json()))
+    Ok(Json(send.to_json_access()))
 }
 
 #[post("/sends/<send_id>/access/file/<file_id>", data = "<data>")]
diff --git a/src/db/models/send.rs b/src/db/models/send.rs
index d7a7a872..8834a356 100644
--- a/src/db/models/send.rs
+++ b/src/db/models/send.rs
@@ -130,6 +130,24 @@ impl Send {
             "Object": "send",
         })
     }
+
+    pub fn to_json_access(&self) -> Value {
+        use crate::util::format_date;
+
+        let data: Value = serde_json::from_str(&self.data).unwrap_or_default();
+
+        json!({
+            "Id": self.uuid,
+            "Type": self.atype,
+
+            "Name": self.name,
+            "Text": if self.atype == SendType::Text as i32 { Some(&data) } else { None },
+            "File": if self.atype == SendType::File as i32 { Some(&data) } else { None },
+
+            "ExpirationDate": self.expiration_date.as_ref().map(format_date),
+            "Object": "send-access",
+        })
+    }
 }
 
 use crate::db::DbConn;
-- 
GitLab