Browse Source

Send deletion thread and updated users revision

Daniel García 4 years ago
parent
commit
1fc6c30652
6 changed files with 34 additions and 5 deletions
  1. 2 0
      src/api/core/mod.rs
  2. 17 4
      src/api/core/sends.rs
  3. 1 0
      src/api/mod.rs
  4. 1 0
      src/db/mod.rs
  5. 11 1
      src/db/models/send.rs
  6. 2 0
      src/main.rs

+ 2 - 0
src/api/core/mod.rs

@@ -5,6 +5,8 @@ mod organizations;
 pub mod two_factor;
 mod sends;
 
+pub use sends::start_send_deletion_scheduler;
+
 pub fn routes() -> Vec<Route> {
     let mut mod_routes = routes![
         clear_device_token,

+ 17 - 4
src/api/core/sends.rs

@@ -25,6 +25,23 @@ pub fn routes() -> Vec<rocket::Route> {
     ]
 }
 
+pub fn start_send_deletion_scheduler(pool: crate::db::DbPool) {
+    std::thread::spawn(move || {
+        loop {
+            if let Ok(conn) = pool.get() {
+                info!("Initiating send deletion");
+                for send in Send::find_all(&conn) {
+                    if chrono::Utc::now().naive_utc() >= send.deletion_date {
+                        send.delete(&conn).ok();
+                    }
+                }
+            }
+
+            std::thread::sleep(std::time::Duration::from_secs(3600));
+        }
+    });
+}
+
 #[derive(Deserialize)]
 #[allow(non_snake_case)]
 pub struct SendData {
@@ -370,10 +387,6 @@ fn delete_send(id: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyR
         err!("Send is not owned by user")
     }
 
-    if send.atype == SendType::File as i32 {
-        std::fs::remove_dir_all(Path::new(&CONFIG.sends_folder()).join(&send.uuid)).ok();
-    }
-
     send.delete(&conn)?;
     nt.send_user_update(UpdateType::SyncSendDelete, &headers.user);
 

+ 1 - 0
src/api/mod.rs

@@ -11,6 +11,7 @@ use serde_json::Value;
 pub use crate::api::{
     admin::routes as admin_routes,
     core::routes as core_routes,
+    core::start_send_deletion_scheduler,
     icons::routes as icons_routes,
     identity::routes as identity_routes,
     notifications::routes as notifications_routes,

+ 1 - 0
src/db/mod.rs

@@ -37,6 +37,7 @@ macro_rules! generate_connections {
         pub enum DbConn { $( #[cfg($name)] $name(PooledConnection<ConnectionManager< $ty >>), )+ }
 
         #[allow(non_camel_case_types)]
+        #[derive(Clone)]
         pub enum DbPool { $( #[cfg($name)] $name(Pool<ConnectionManager< $ty >>), )+ }
 
         impl DbPool {

+ 11 - 1
src/db/models/send.rs

@@ -194,6 +194,10 @@ impl Send {
     pub fn delete(&self, conn: &DbConn) -> EmptyResult {
         self.update_users_revision(conn);
 
+        if self.atype == SendType::File as i32 {
+            std::fs::remove_dir_all(std::path::Path::new(&crate::CONFIG.sends_folder()).join(&self.uuid)).ok();
+        }
+
         db_run! { conn: {
             diesel::delete(sends::table.filter(sends::uuid.eq(&self.uuid)))
                 .execute(conn)
@@ -202,7 +206,7 @@ impl Send {
     }
 
     pub fn update_users_revision(&self, conn: &DbConn) {
-        match self.user_uuid {
+        match &self.user_uuid {
             Some(user_uuid) => {
                 User::update_uuid_revision(&user_uuid, conn);
             }
@@ -219,6 +223,12 @@ impl Send {
         Ok(())
     }
 
+    pub fn find_all(conn: &DbConn) -> Vec<Self> {
+        db_run! {conn: {
+            sends::table.load::<SendDb>(conn).expect("Error loading sends").from_db()
+        }}
+    }
+
     pub fn find_by_access_id(access_id: &str, conn: &DbConn) -> Option<Self> {
         use data_encoding::BASE64URL_NOPAD;
         use uuid::Uuid;

+ 2 - 0
src/main.rs

@@ -313,6 +313,8 @@ fn launch_rocket(extra_debug: bool) {
         }
     };
 
+    api::start_send_deletion_scheduler(pool.clone());
+
     let basepath = &CONFIG.domain_path();
 
     // If adding more paths here, consider also adding them to