Add trash bin feature

This commit is contained in:
Lennart
2024-06-21 19:26:45 +02:00
parent c1cc12e2ac
commit aed6bcff63
9 changed files with 45 additions and 9 deletions

1
Cargo.lock generated
View File

@@ -509,6 +509,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"wasm-bindgen", "wasm-bindgen",
"windows-targets 0.52.5", "windows-targets 0.52.5",
] ]

View File

@@ -15,10 +15,11 @@ a calendar server
- [ ] CardDAV - [ ] CardDAV
- [ ] Locking - [ ] Locking
- [ ] Web UI - [ ] Web UI
- [ ] Hiding calendars instead of deleting them
- [ ] Testing such that I'm confident enough to use it myself :) - [ ] Testing such that I'm confident enough to use it myself :)
- [ ] WebDAV sync extension [RFC 6578](https://www.rfc-editor.org/rfc/rfc6578) - [ ] WebDAV sync extension [RFC 6578](https://www.rfc-editor.org/rfc/rfc6578)
- [ ] implement getctag [see](https://github.com/apple/ccs-calendarserver/blob/master/doc/Extensions/caldav-ctag.txt) - [ ] implement getctag [see](https://github.com/apple/ccs-calendarserver/blob/master/doc/Extensions/caldav-ctag.txt)
- [ ] Ensure proper routing - [ ] Ensure proper routing
- [ ] Trash bin - [x] Trash bin
- [x] Hiding calendars instead of deleting them
- [ ] Restore endpoint

View File

@@ -1,5 +1,6 @@
use crate::CalDavContext; use crate::CalDavContext;
use crate::Error; use crate::Error;
use actix_web::HttpRequest;
use actix_web::{ use actix_web::{
web::{Data, Path}, web::{Data, Path},
HttpResponse, HttpResponse,
@@ -11,12 +12,25 @@ pub async fn route_delete_calendar<A: CheckAuthentication, C: CalendarStore + ?S
context: Data<CalDavContext<C>>, context: Data<CalDavContext<C>>,
path: Path<(String, String)>, path: Path<(String, String)>,
auth: AuthInfoExtractor<A>, auth: AuthInfoExtractor<A>,
req: HttpRequest,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (principal, cid) = path.into_inner(); let (principal, cid) = path.into_inner();
if principal != auth.inner.user_id { if principal != auth.inner.user_id {
return Err(Error::Unauthorized); return Err(Error::Unauthorized);
} }
context.store.write().await.delete_calendar(&cid).await?;
let no_trash = req
.headers()
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
context
.store
.write()
.await
.delete_calendar(&cid, !no_trash)
.await?;
Ok(HttpResponse::Ok().body("")) Ok(HttpResponse::Ok().body(""))
} }

View File

@@ -75,7 +75,7 @@ pub async fn route_mkcol_calendar<A: CheckAuthentication, C: CalendarStore + ?Si
timezone: request.calendar_timezone, timezone: request.calendar_timezone,
color: request.calendar_color, color: request.calendar_color,
description: request.calendar_description, description: request.calendar_description,
deleted: false, deleted_at: None,
}; };
match context.store.read().await.get_calendar(&cid).await { match context.store.read().await.get_calendar(&cid).await {

View File

@@ -1,6 +1,7 @@
use crate::CalDavContext; use crate::CalDavContext;
use crate::Error; use crate::Error;
use actix_web::web::{Data, Path}; use actix_web::web::{Data, Path};
use actix_web::HttpRequest;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication}; use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_store::CalendarStore; use rustical_store::CalendarStore;
@@ -9,6 +10,7 @@ pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>, context: Data<CalDavContext<C>>,
path: Path<(String, String, String)>, path: Path<(String, String, String)>,
auth: AuthInfoExtractor<A>, auth: AuthInfoExtractor<A>,
req: HttpRequest,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let _user = auth.inner.user_id; let _user = auth.inner.user_id;
// TODO: verify whether user is authorized // TODO: verify whether user is authorized
@@ -16,7 +18,18 @@ pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
if cid.ends_with(".ics") { if cid.ends_with(".ics") {
cid.truncate(cid.len() - 4); cid.truncate(cid.len() - 4);
} }
context.store.write().await.delete_event(&cid, &uid).await?; let no_trash = req
.headers()
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
context
.store
.write()
.await
.delete_event(&cid, &uid, !no_trash)
.await?;
Ok(HttpResponse::Ok().body("")) Ok(HttpResponse::Ok().body(""))
} }

View File

@@ -24,7 +24,7 @@ sqlx = { version = "0.7", features = [
tokio = { version = "1.35", features = ["sync", "full"] } tokio = { version = "1.35", features = ["sync", "full"] }
toml = "0.8" toml = "0.8"
ical = { version = "0.11", features = ["generator", "serde"] } ical = { version = "0.11", features = ["generator", "serde"] }
chrono = "0.4" chrono = { version = "0.4", features = ["serde"] }
regex = "1.10" regex = "1.10"
lazy_static = "1.4" lazy_static = "1.4"
rstest = "0.21" rstest = "0.21"

View File

@@ -5,13 +5,15 @@ CREATE TABLE calendars (
description TEXT, description TEXT,
'order' INT DEFAULT 0 NOT NULL, 'order' INT DEFAULT 0 NOT NULL,
color TEXT, color TEXT,
timezone TEXT NOT NULL timezone TEXT NOT NULL,
deleted_at DATETIME
); );
CREATE TABLE events ( CREATE TABLE events (
uid TEXT NOT NULL, uid TEXT NOT NULL,
cid TEXT NOT NULL, cid TEXT NOT NULL,
ics TEXT NOT NULL, ics TEXT NOT NULL,
deleted_at DATETIME,
PRIMARY KEY (cid, uid), PRIMARY KEY (cid, uid),
FOREIGN KEY (cid) REFERENCES calendars(id) FOREIGN KEY (cid) REFERENCES calendars(id)
); );

View File

@@ -1,3 +1,4 @@
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Deserialize, Serialize)] #[derive(Debug, Default, Clone, Deserialize, Serialize)]
@@ -9,4 +10,5 @@ pub struct Calendar {
pub description: Option<String>, pub description: Option<String>,
pub color: Option<String>, pub color: Option<String>,
pub timezone: Option<String>, pub timezone: Option<String>,
pub deleted_at: Option<NaiveDateTime>,
} }

View File

@@ -10,10 +10,13 @@ pub trait CalendarStore: Send + Sync + 'static {
async fn get_calendars(&self, owner: &str) -> Result<Vec<Calendar>, Error>; async fn get_calendars(&self, owner: &str) -> Result<Vec<Calendar>, Error>;
async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>; async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>;
async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>; async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>;
async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error>; async fn delete_calendar(&mut self, cid: &str, use_trashbin: bool) -> Result<(), Error>;
async fn restore_calendar(&mut self, cid: &str) -> Result<(), Error>;
async fn get_events(&self, cid: &str) -> Result<Vec<Event>, Error>; async fn get_events(&self, cid: &str) -> Result<Vec<Event>, Error>;
async fn get_event(&self, cid: &str, uid: &str) -> Result<Event, Error>; async fn get_event(&self, cid: &str, uid: &str) -> Result<Event, Error>;
async fn put_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error>; async fn put_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error>;
async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<(), Error>; async fn delete_event(&mut self, cid: &str, uid: &str, use_trashbin: bool)
-> Result<(), Error>;
async fn restore_event(&mut self, cid: &str, uid: &str) -> Result<(), Error>;
} }