Send deletion thread and updated users revision

This commit is contained in:
Daniel García 2021-03-22 19:57:35 +01:00
parent 46a1a013cd
commit 1fc6c30652
No known key found for this signature in database
GPG key ID: FC8A7D14C3CD543A
6 changed files with 34 additions and 5 deletions

View file

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

View file

@ -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)] #[derive(Deserialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub struct SendData { 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") 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)?; send.delete(&conn)?;
nt.send_user_update(UpdateType::SyncSendDelete, &headers.user); nt.send_user_update(UpdateType::SyncSendDelete, &headers.user);

View file

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

View file

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

View file

@ -194,6 +194,10 @@ impl Send {
pub fn delete(&self, conn: &DbConn) -> EmptyResult { pub fn delete(&self, conn: &DbConn) -> EmptyResult {
self.update_users_revision(conn); 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: { db_run! { conn: {
diesel::delete(sends::table.filter(sends::uuid.eq(&self.uuid))) diesel::delete(sends::table.filter(sends::uuid.eq(&self.uuid)))
.execute(conn) .execute(conn)
@ -202,7 +206,7 @@ impl Send {
} }
pub fn update_users_revision(&self, conn: &DbConn) { pub fn update_users_revision(&self, conn: &DbConn) {
match self.user_uuid { match &self.user_uuid {
Some(user_uuid) => { Some(user_uuid) => {
User::update_uuid_revision(&user_uuid, conn); User::update_uuid_revision(&user_uuid, conn);
} }
@ -219,6 +223,12 @@ impl Send {
Ok(()) 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> { pub fn find_by_access_id(access_id: &str, conn: &DbConn) -> Option<Self> {
use data_encoding::BASE64URL_NOPAD; use data_encoding::BASE64URL_NOPAD;
use uuid::Uuid; use uuid::Uuid;

View file

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