diff --git a/crates/dav/src/routes/event.rs b/crates/dav/src/routes/event.rs index e16a23a..4d489c4 100644 --- a/crates/dav/src/routes/event.rs +++ b/crates/dav/src/routes/event.rs @@ -19,7 +19,7 @@ pub async fn delete_event( .store .write() .await - .delete_event(&uid) + .delete_event(&cid, &uid) .await .map_err(|_e| Error::InternalError)?; @@ -32,12 +32,15 @@ pub async fn get_event( _auth: AuthInfoExtractor, ) -> Result { // TODO: verify whether user is authorized + let (_principal, cid, mut uid) = path.into_inner(); + if uid.ends_with(".ics") { + uid.truncate(uid.len() - 4); } let event = context .store .read() .await - .get_event(&uid) + .get_event(&cid, &uid) .await .map_err(|_e| Error::NotFound)?; @@ -63,7 +66,7 @@ pub async fn put_event( .store .write() .await - .upsert_event(uid, body) + .upsert_event(cid, uid, body) .await .map_err(|_e| Error::InternalError)?; diff --git a/crates/store/src/calendar.rs b/crates/store/src/calendar.rs index 1f9f588..325fda7 100644 --- a/crates/store/src/calendar.rs +++ b/crates/store/src/calendar.rs @@ -88,23 +88,38 @@ impl CalendarStore for TomlCalendarStore { .collect()) } - async fn get_events(&self, _cid: &str) -> Result> { - Ok(self.events.values().cloned().collect()) + async fn get_events(&self, cid: &str) -> Result> { + if let Some(events) = self.events.get(cid) { + Ok(events.values().cloned().collect()) + } else { + Ok(Vec::new()) + } } - async fn get_event(&self, uid: &str) -> Result { - Ok(self.events.get(uid).ok_or(anyhow!("not found"))?.clone()) + async fn get_event(&self, cid: &str, uid: &str) -> Result { + let events = self.events.get(cid).ok_or(anyhow!("not found"))?; + Ok(events.get(uid).ok_or(anyhow!("not found"))?.clone()) } - async fn upsert_event(&mut self, uid: String, ics: String) -> Result<()> { - self.events.insert(uid.clone(), Event { uid, ics }); + async fn upsert_event(&mut self, cid: String, uid: String, ics: String) -> Result<()> { + let events = self.events.entry(cid).or_insert(HashMap::new()); + events.insert( + uid.clone(), + Event { + uid, + ics, + summary: None, + }, + ); self.save().await.unwrap(); Ok(()) } - async fn delete_event(&mut self, uid: &str) -> Result<()> { - self.events.remove(uid); - self.save().await?; + async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<()> { + if let Some(events) = self.events.get_mut(cid) { + events.remove(uid); + self.save().await?; + } Ok(()) } }