mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
Fix data model to fix event collisions with multiple principals
This commit is contained in:
@@ -29,7 +29,7 @@ pub async fn route_delete_calendar<A: CheckAuthentication, C: CalendarStore + ?S
|
|||||||
.store
|
.store
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
.delete_calendar(&cid, !no_trash)
|
.delete_calendar(&principal, &cid, !no_trash)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().body(""))
|
Ok(HttpResponse::Ok().body(""))
|
||||||
|
|||||||
@@ -69,16 +69,22 @@ pub async fn route_mkcol_calendar<A: CheckAuthentication, C: CalendarStore + ?Si
|
|||||||
|
|
||||||
let calendar = Calendar {
|
let calendar = Calendar {
|
||||||
id: cid.to_owned(),
|
id: cid.to_owned(),
|
||||||
owner: principal,
|
principal: principal.to_owned(),
|
||||||
order: request.order.unwrap_or(0),
|
order: request.order.unwrap_or(0),
|
||||||
name: request.displayname,
|
displayname: request.displayname,
|
||||||
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_at: None,
|
deleted_at: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match context.store.read().await.get_calendar(&cid).await {
|
match context
|
||||||
|
.store
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get_calendar(&principal, &cid)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Err(rustical_store::Error::NotFound) => {
|
Err(rustical_store::Error::NotFound) => {
|
||||||
// No conflict, no worries
|
// No conflict, no worries
|
||||||
}
|
}
|
||||||
@@ -92,13 +98,7 @@ pub async fn route_mkcol_calendar<A: CheckAuthentication, C: CalendarStore + ?Si
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match context
|
match context.store.write().await.insert_calendar(calendar).await {
|
||||||
.store
|
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.insert_calendar(cid, calendar)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
// The spec says we should return a mkcalendar-response but I don't know what goes into it.
|
// The spec says we should return a mkcalendar-response but I don't know what goes into it.
|
||||||
// However, it works without one but breaks on iPadOS when using an empty one :)
|
// However, it works without one but breaks on iPadOS when using an empty one :)
|
||||||
Ok(()) => Ok(HttpResponse::Created()
|
Ok(()) => Ok(HttpResponse::Created()
|
||||||
|
|||||||
@@ -120,20 +120,22 @@ pub enum ReportRequest {
|
|||||||
|
|
||||||
async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
||||||
_cal_query: CalendarQueryRequest,
|
_cal_query: CalendarQueryRequest,
|
||||||
|
principal: &str,
|
||||||
cid: &str,
|
cid: &str,
|
||||||
store: &RwLock<C>,
|
store: &RwLock<C>,
|
||||||
) -> Result<Vec<Event>, Error> {
|
) -> Result<Vec<Event>, Error> {
|
||||||
// TODO: Implement filtering
|
// TODO: Implement filtering
|
||||||
Ok(store.read().await.get_events(cid).await?)
|
Ok(store.read().await.get_events(principal, cid).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||||
_cal_query: CalendarMultigetRequest,
|
_cal_query: CalendarMultigetRequest,
|
||||||
|
principal: &str,
|
||||||
cid: &str,
|
cid: &str,
|
||||||
store: &RwLock<C>,
|
store: &RwLock<C>,
|
||||||
) -> Result<Vec<Event>, Error> {
|
) -> Result<Vec<Event>, Error> {
|
||||||
// TODO: proper implementation
|
// TODO: proper implementation
|
||||||
Ok(store.read().await.get_events(cid).await?)
|
Ok(store.read().await.get_events(principal, cid).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||||
@@ -152,10 +154,10 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?S
|
|||||||
let request: ReportRequest = quick_xml::de::from_str(&body)?;
|
let request: ReportRequest = quick_xml::de::from_str(&body)?;
|
||||||
let events = match request.clone() {
|
let events = match request.clone() {
|
||||||
ReportRequest::CalendarQuery(cal_query) => {
|
ReportRequest::CalendarQuery(cal_query) => {
|
||||||
get_events_calendar_query(cal_query, &cid, &cal_store).await?
|
get_events_calendar_query(cal_query, &principal, &cid, &cal_store).await?
|
||||||
}
|
}
|
||||||
ReportRequest::CalendarMultiget(cal_multiget) => {
|
ReportRequest::CalendarMultiget(cal_multiget) => {
|
||||||
get_events_calendar_multiget(cal_multiget, &cid, &cal_store).await?
|
get_events_calendar_multiget(cal_multiget, &principal, &cid, &cal_store).await?
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ impl Resource for CalendarFile {
|
|||||||
prefix, self.principal
|
prefix, self.principal
|
||||||
)))),
|
)))),
|
||||||
CalendarPropName::Displayname => {
|
CalendarPropName::Displayname => {
|
||||||
Ok(CalendarProp::Displayname(self.calendar.name.clone()))
|
Ok(CalendarProp::Displayname(self.calendar.displayname.clone()))
|
||||||
}
|
}
|
||||||
CalendarPropName::CalendarColor => {
|
CalendarPropName::CalendarColor => {
|
||||||
Ok(CalendarProp::CalendarColor(self.calendar.color.clone()))
|
Ok(CalendarProp::CalendarColor(self.calendar.color.clone()))
|
||||||
@@ -238,8 +238,8 @@ impl Resource for CalendarFile {
|
|||||||
CalendarProp::Resourcetype(_) => Err(rustical_dav::Error::PropReadOnly),
|
CalendarProp::Resourcetype(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
CalendarProp::CurrentUserPrincipal(_) => Err(rustical_dav::Error::PropReadOnly),
|
CalendarProp::CurrentUserPrincipal(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
CalendarProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
|
CalendarProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
CalendarProp::Displayname(name) => {
|
CalendarProp::Displayname(displayname) => {
|
||||||
self.calendar.name = name;
|
self.calendar.displayname = displayname;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
CalendarProp::CalendarColor(color) => {
|
CalendarProp::CalendarColor(color) => {
|
||||||
@@ -289,7 +289,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
.cal_store
|
.cal_store
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.get_calendar(&self.calendar_id)
|
.get_calendar(&self.principal, &self.calendar_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|_e| Error::NotFound)?;
|
.map_err(|_e| Error::NotFound)?;
|
||||||
Ok(CalendarFile {
|
Ok(CalendarFile {
|
||||||
@@ -308,7 +308,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
.cal_store
|
.cal_store
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.get_events(&self.calendar_id)
|
.get_events(&self.principal, &self.calendar_id)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|event| EventFile {
|
.map(|event| EventFile {
|
||||||
@@ -341,7 +341,11 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
self.cal_store
|
self.cal_store
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
.update_calendar(self.calendar_id.to_owned(), file.calendar)
|
.update_calendar(
|
||||||
|
self.principal.to_owned(),
|
||||||
|
self.calendar_id.to_owned(),
|
||||||
|
file.calendar,
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
) -> 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
|
||||||
let (_principal, mut cid, uid) = path.into_inner();
|
let (principal, mut cid, uid) = path.into_inner();
|
||||||
if cid.ends_with(".ics") {
|
if cid.ends_with(".ics") {
|
||||||
cid.truncate(cid.len() - 4);
|
cid.truncate(cid.len() - 4);
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
.store
|
.store
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
.delete_event(&cid, &uid, !no_trash)
|
.delete_event(&principal, &cid, &uid, !no_trash)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().body(""))
|
Ok(HttpResponse::Ok().body(""))
|
||||||
@@ -46,15 +46,25 @@ pub async fn get_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
let calendar = context
|
||||||
if auth.inner.user_id != calendar.owner {
|
.store
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get_calendar(&principal, &cid)
|
||||||
|
.await?;
|
||||||
|
if auth.inner.user_id != calendar.principal {
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
if uid.ends_with(".ics") {
|
if uid.ends_with(".ics") {
|
||||||
uid.truncate(uid.len() - 4);
|
uid.truncate(uid.len() - 4);
|
||||||
}
|
}
|
||||||
let event = context.store.read().await.get_event(&cid, &uid).await?;
|
let event = context
|
||||||
|
.store
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get_event(&principal, &cid, &uid)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.insert_header(("ETag", event.get_etag()))
|
.insert_header(("ETag", event.get_etag()))
|
||||||
@@ -73,8 +83,13 @@ pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
let calendar = context
|
||||||
if auth_info.user_id != calendar.owner {
|
.store
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get_calendar(&principal, &cid)
|
||||||
|
.await?;
|
||||||
|
if auth_info.user_id != calendar.principal {
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +101,7 @@ pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
.store
|
.store
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
.put_event(cid, uid, body)
|
.put_event(principal, cid, uid, body)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().body(""))
|
Ok(HttpResponse::Ok().body(""))
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use tokio::sync::RwLock;
|
|||||||
pub struct EventResource<C: CalendarStore + ?Sized> {
|
pub struct EventResource<C: CalendarStore + ?Sized> {
|
||||||
pub cal_store: Arc<RwLock<C>>,
|
pub cal_store: Arc<RwLock<C>>,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
pub principal: String,
|
||||||
pub cid: String,
|
pub cid: String,
|
||||||
pub uid: String,
|
pub uid: String,
|
||||||
}
|
}
|
||||||
@@ -94,7 +95,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
|||||||
_auth_info: AuthInfo,
|
_auth_info: AuthInfo,
|
||||||
path_components: Self::PathComponents,
|
path_components: Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
let (_principal, cid, uid) = path_components;
|
let (principal, cid, uid) = path_components;
|
||||||
|
|
||||||
let cal_store = req
|
let cal_store = req
|
||||||
.app_data::<Data<RwLock<C>>>()
|
.app_data::<Data<RwLock<C>>>()
|
||||||
@@ -104,6 +105,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
cal_store,
|
cal_store,
|
||||||
|
principal,
|
||||||
cid,
|
cid,
|
||||||
uid,
|
uid,
|
||||||
path: req.path().to_string(),
|
path: req.path().to_string(),
|
||||||
@@ -115,7 +117,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
|||||||
.cal_store
|
.cal_store
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.get_event(&self.cid, &self.uid)
|
.get_event(&self.principal, &self.cid, &self.uid)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(EventFile {
|
Ok(EventFile {
|
||||||
event,
|
event,
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
CREATE TABLE calendars (
|
CREATE TABLE calendars (
|
||||||
id TEXT PRIMARY KEY NOT NULL,
|
principal TEXT NOT NULL,
|
||||||
owner TEXT NOT NULL,
|
id TEXT NOT NULL,
|
||||||
name TEXT,
|
displayname TEXT,
|
||||||
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
|
deleted_at DATETIME,
|
||||||
|
PRIMARY KEY (principal, id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE events (
|
CREATE TABLE events (
|
||||||
uid TEXT NOT NULL,
|
principal TEXT NOT NULL,
|
||||||
cid TEXT NOT NULL,
|
cid TEXT NOT NULL,
|
||||||
|
uid TEXT NOT NULL,
|
||||||
ics TEXT NOT NULL,
|
ics TEXT NOT NULL,
|
||||||
deleted_at DATETIME,
|
deleted_at DATETIME,
|
||||||
PRIMARY KEY (cid, uid),
|
PRIMARY KEY (principal, cid, uid),
|
||||||
FOREIGN KEY (cid) REFERENCES calendars(id)
|
FOREIGN KEY (principal, cid) REFERENCES calendars(principal, id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||||
pub struct Calendar {
|
pub struct Calendar {
|
||||||
|
pub principal: String,
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: Option<String>,
|
pub displayname: Option<String>,
|
||||||
pub owner: String,
|
|
||||||
pub order: i64,
|
pub order: i64,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
|
|||||||
@@ -32,10 +32,13 @@ impl TryFrom<EventRow> for Event {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CalendarStore for SqliteCalendarStore {
|
impl CalendarStore for SqliteCalendarStore {
|
||||||
async fn get_calendar(&self, id: &str) -> Result<Calendar, Error> {
|
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> {
|
||||||
let cal = sqlx::query_as!(
|
let cal = sqlx::query_as!(
|
||||||
Calendar,
|
Calendar,
|
||||||
r#"SELECT id, name, owner, "order", description, color, timezone FROM calendars WHERE id = ?"#,
|
r#"SELECT principal, id, "order", displayname, description, color, timezone, deleted_at
|
||||||
|
FROM calendars
|
||||||
|
WHERE (principal, id) = (?, ?)"#,
|
||||||
|
principal,
|
||||||
id
|
id
|
||||||
)
|
)
|
||||||
.fetch_one(&self.db)
|
.fetch_one(&self.db)
|
||||||
@@ -43,40 +46,54 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
Ok(cal)
|
Ok(cal)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_calendars(&self, _owner: &str) -> Result<Vec<Calendar>, Error> {
|
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
|
||||||
let cals = sqlx::query_as!(
|
let cals = sqlx::query_as!(
|
||||||
Calendar,
|
Calendar,
|
||||||
r#"SELECT id, name, owner, "order", description, color, timezone FROM calendars"#,
|
r#"SELECT principal, id, displayname, "order", description, color, timezone, deleted_at
|
||||||
|
FROM calendars
|
||||||
|
WHERE principal = ? AND deleted_at IS NULL"#,
|
||||||
|
principal
|
||||||
)
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(cals)
|
Ok(cals)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> {
|
async fn insert_calendar(&mut self, calendar: Calendar) -> Result<(), Error> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"INSERT INTO calendars (id, name, description, owner, "order", color, timezone) VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone)
|
||||||
cid,
|
VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
||||||
calendar.name,
|
calendar.principal,
|
||||||
|
calendar.id,
|
||||||
|
calendar.displayname,
|
||||||
calendar.description,
|
calendar.description,
|
||||||
calendar.owner,
|
|
||||||
calendar.order,
|
calendar.order,
|
||||||
calendar.color,
|
calendar.color,
|
||||||
calendar.timezone
|
calendar.timezone
|
||||||
).execute(&self.db).await?;
|
)
|
||||||
|
.execute(&self.db)
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> {
|
async fn update_calendar(
|
||||||
|
&mut self,
|
||||||
|
principal: String,
|
||||||
|
id: String,
|
||||||
|
calendar: Calendar,
|
||||||
|
) -> Result<(), Error> {
|
||||||
let result = sqlx::query!(
|
let result = sqlx::query!(
|
||||||
r#"UPDATE calendars SET name = ?, description = ?, owner = ?, "order" = ?, color = ?, timezone = ? WHERE id = ?"#,
|
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?
|
||||||
calendar.name,
|
WHERE (principal, id) = (?, ?)"#,
|
||||||
|
calendar.principal,
|
||||||
|
calendar.id,
|
||||||
|
calendar.displayname,
|
||||||
calendar.description,
|
calendar.description,
|
||||||
calendar.owner,
|
|
||||||
calendar.order,
|
calendar.order,
|
||||||
calendar.color,
|
calendar.color,
|
||||||
calendar.timezone,
|
calendar.timezone,
|
||||||
cid,
|
principal,
|
||||||
|
id
|
||||||
).execute(&self.db).await?;
|
).execute(&self.db).await?;
|
||||||
if result.rows_affected() == 0 {
|
if result.rows_affected() == 0 {
|
||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
@@ -84,15 +101,54 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> {
|
// Does not actually delete the calendar but just disables it
|
||||||
sqlx::query!("DELETE FROM calendars WHERE id = ?", cid)
|
async fn delete_calendar(
|
||||||
|
&mut self,
|
||||||
|
principal: &str,
|
||||||
|
id: &str,
|
||||||
|
use_trashbin: bool,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match use_trashbin {
|
||||||
|
true => {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"UPDATE calendars SET deleted_at = datetime() WHERE (principal, id) = (?, ?)"#,
|
||||||
|
principal, id
|
||||||
|
)
|
||||||
|
.execute(&self.db)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
false => {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"DELETE FROM calendars WHERE (principal, id) = (?, ?)"#,
|
||||||
|
principal,
|
||||||
|
id
|
||||||
|
)
|
||||||
|
.execute(&self.db)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Does not actually delete the calendar but just disables it
|
||||||
|
async fn restore_calendar(&mut self, principal: &str, id: &str) -> Result<(), Error> {
|
||||||
|
sqlx::query!(
|
||||||
|
r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
|
||||||
|
principal,
|
||||||
|
id
|
||||||
|
)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_events(&self, cid: &str) -> Result<Vec<Event>, Error> {
|
async fn get_events(&self, principal: &str, cid: &str) -> Result<Vec<Event>, Error> {
|
||||||
sqlx::query_as!(EventRow, "SELECT uid, ics FROM events WHERE cid = ?", cid)
|
sqlx::query_as!(
|
||||||
|
EventRow,
|
||||||
|
"SELECT uid, ics FROM events WHERE principal = ? AND cid = ?",
|
||||||
|
principal,
|
||||||
|
cid
|
||||||
|
)
|
||||||
.fetch_all(&self.db)
|
.fetch_all(&self.db)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -100,10 +156,11 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_event(&self, cid: &str, uid: &str) -> Result<Event, Error> {
|
async fn get_event(&self, principal: &str, cid: &str, uid: &str) -> Result<Event, Error> {
|
||||||
let event = sqlx::query_as!(
|
let event = sqlx::query_as!(
|
||||||
EventRow,
|
EventRow,
|
||||||
"SELECT uid, ics FROM events where cid = ? AND uid = ?",
|
"SELECT uid, ics FROM events WHERE (principal, cid, uid) = (?, ?, ?)",
|
||||||
|
principal,
|
||||||
cid,
|
cid,
|
||||||
uid
|
uid
|
||||||
)
|
)
|
||||||
@@ -113,10 +170,17 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
Ok(event)
|
Ok(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn put_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error> {
|
async fn put_event(
|
||||||
|
&mut self,
|
||||||
|
principal: String,
|
||||||
|
cid: String,
|
||||||
|
uid: String,
|
||||||
|
ics: String,
|
||||||
|
) -> Result<(), Error> {
|
||||||
let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?;
|
let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?;
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"REPLACE INTO events (cid, uid, ics) VALUES (?, ?, ?)",
|
"REPLACE INTO events (principal, cid, uid, ics) VALUES (?, ?, ?, ?)",
|
||||||
|
principal,
|
||||||
cid,
|
cid,
|
||||||
uid,
|
uid,
|
||||||
ics,
|
ics,
|
||||||
@@ -126,10 +190,42 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<(), Error> {
|
async fn delete_event(
|
||||||
|
&mut self,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
uid: &str,
|
||||||
|
use_trashbin: bool,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match use_trashbin {
|
||||||
|
true => {
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE events SET deleted_at = datetime() WHERE (principal, cid, uid) = (?, ?, ?)",
|
||||||
|
principal,
|
||||||
|
cid,
|
||||||
|
uid
|
||||||
|
)
|
||||||
|
.execute(&self.db)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
false => {
|
||||||
sqlx::query!("DELETE FROM events WHERE cid = ? AND uid = ?", cid, uid)
|
sqlx::query!("DELETE FROM events WHERE cid = ? AND uid = ?", cid, uid)
|
||||||
.execute(&self.db)
|
.execute(&self.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn restore_event(&mut self, principal: &str, cid: &str, uid: &str) -> Result<(), Error> {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"UPDATE events SET deleted_at = NULL WHERE (principal, cid, uid) = (?, ?, ?)"#,
|
||||||
|
principal,
|
||||||
|
cid,
|
||||||
|
uid
|
||||||
|
)
|
||||||
|
.execute(&self.db)
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,39 @@ use crate::{calendar::Calendar, event::Event};
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CalendarStore: Send + Sync + 'static {
|
pub trait CalendarStore: Send + Sync + 'static {
|
||||||
async fn get_calendar(&self, id: &str) -> Result<Calendar, Error>;
|
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error>;
|
||||||
async fn get_calendars(&self, owner: &str) -> Result<Vec<Calendar>, Error>;
|
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, 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 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 update_calendar(
|
||||||
async fn get_event(&self, cid: &str, uid: &str) -> Result<Event, Error>;
|
&mut self,
|
||||||
async fn put_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error>;
|
principal: String,
|
||||||
async fn delete_event(&mut self, cid: &str, uid: &str, use_trashbin: bool)
|
id: String,
|
||||||
-> Result<(), Error>;
|
calendar: Calendar,
|
||||||
async fn restore_event(&mut self, cid: &str, uid: &str) -> Result<(), Error>;
|
) -> Result<(), Error>;
|
||||||
|
async fn insert_calendar(&mut self, calendar: Calendar) -> Result<(), Error>;
|
||||||
|
async fn delete_calendar(
|
||||||
|
&mut self,
|
||||||
|
principal: &str,
|
||||||
|
name: &str,
|
||||||
|
use_trashbin: bool,
|
||||||
|
) -> Result<(), Error>;
|
||||||
|
async fn restore_calendar(&mut self, principal: &str, name: &str) -> Result<(), Error>;
|
||||||
|
|
||||||
|
async fn get_events(&self, principal: &str, cid: &str) -> Result<Vec<Event>, Error>;
|
||||||
|
async fn get_event(&self, principal: &str, cid: &str, uid: &str) -> Result<Event, Error>;
|
||||||
|
async fn put_event(
|
||||||
|
&mut self,
|
||||||
|
principal: String,
|
||||||
|
cid: String,
|
||||||
|
uid: String,
|
||||||
|
ics: String,
|
||||||
|
) -> Result<(), Error>;
|
||||||
|
async fn delete_event(
|
||||||
|
&mut self,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
uid: &str,
|
||||||
|
use_trashbin: bool,
|
||||||
|
) -> Result<(), Error>;
|
||||||
|
async fn restore_event(&mut self, principal: &str, cid: &str, uid: &str) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ async fn cal_store<CS: CalendarStore>(
|
|||||||
|
|
||||||
#[apply(cal_store)]
|
#[apply(cal_store)]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_init<CS: CalendarStore>(store: CS) {
|
async fn test_init<CS: CalendarStore>(_store: CS) {
|
||||||
store.get_events("asd").await.unwrap();
|
// store.get_events("asd").await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[apply(cal_store)]
|
#[apply(cal_store)]
|
||||||
@@ -27,25 +27,27 @@ async fn test_init<CS: CalendarStore>(store: CS) {
|
|||||||
async fn test_create_event<CS: CalendarStore>(store: CS) {
|
async fn test_create_event<CS: CalendarStore>(store: CS) {
|
||||||
let mut store = store;
|
let mut store = store;
|
||||||
store
|
store
|
||||||
.insert_calendar(
|
.insert_calendar(rustical_store::calendar::Calendar {
|
||||||
"test".to_owned(),
|
|
||||||
rustical_store::calendar::Calendar {
|
|
||||||
id: "test".to_owned(),
|
id: "test".to_owned(),
|
||||||
name: Some("Test Calendar".to_owned()),
|
displayname: Some("Test Calendar".to_owned()),
|
||||||
owner: "Test User".to_owned(),
|
principal: "testuser".to_owned(),
|
||||||
timezone: Some(TIMEZONE.to_owned()),
|
timezone: Some(TIMEZONE.to_owned()),
|
||||||
..Default::default() // timezone: TIMEZONE.to_owned(),
|
..Default::default() // timezone: TIMEZONE.to_owned(),
|
||||||
},
|
})
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
store
|
store
|
||||||
.put_event("test".to_owned(), "asd".to_owned(), EVENT.to_owned())
|
.put_event(
|
||||||
|
"testuser".to_owned(),
|
||||||
|
"test".to_owned(),
|
||||||
|
"asd".to_owned(),
|
||||||
|
EVENT.to_owned(),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let event = store.get_event("test", "asd").await.unwrap();
|
let event = store.get_event("testuser", "test", "asd").await.unwrap();
|
||||||
assert_eq!(event.get_ics(), EVENT);
|
assert_eq!(event.get_ics(), EVENT);
|
||||||
assert_eq!(event.get_uid(), "asd");
|
assert_eq!(event.get_uid(), "asd");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user