mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 15:12:23 +00:00
Rename dav crate to caldav to prepare splitting dav functionality into dav crate
This commit is contained in:
@@ -1,175 +0,0 @@
|
||||
use std::{io::Write, sync::Arc};
|
||||
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use quick_xml::{events::BytesText, Writer};
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_store::calendar::{Calendar, CalendarStore};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{
|
||||
proptypes::{write_href_prop, write_string_prop},
|
||||
resource::Resource,
|
||||
xml_snippets::write_resourcetype,
|
||||
};
|
||||
|
||||
pub struct CalendarResource<C: CalendarStore> {
|
||||
pub cal_store: Arc<RwLock<C>>,
|
||||
pub calendar: Calendar,
|
||||
pub path: String,
|
||||
pub prefix: String,
|
||||
pub principal: String,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<C: CalendarStore> Resource for CalendarResource<C> {
|
||||
type MemberType = Self;
|
||||
type UriComponents = (String, String); // principal, calendar_id
|
||||
|
||||
async fn acquire_from_request(
|
||||
req: HttpRequest,
|
||||
_auth_info: AuthInfo,
|
||||
uri_components: Self::UriComponents,
|
||||
prefix: String,
|
||||
) -> Result<Self> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
let (principal, cid) = uri_components;
|
||||
let calendar = cal_store.read().await.get_calendar(&cid).await?;
|
||||
Ok(Self {
|
||||
cal_store,
|
||||
calendar,
|
||||
path: req.path().to_string(),
|
||||
prefix,
|
||||
principal,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||
// As of now the calendar resource has no members
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn list_dead_props() -> Vec<&'static str> {
|
||||
vec![
|
||||
"resourcetype",
|
||||
"current-user-principal",
|
||||
"displayname",
|
||||
"supported-calendar-component-set",
|
||||
"supported-calendar-data",
|
||||
"getcontenttype",
|
||||
"calendar-description",
|
||||
"owner",
|
||||
"calendar-color",
|
||||
"current-user-privilege-set",
|
||||
"max-resource-size",
|
||||
]
|
||||
}
|
||||
fn write_prop<W: Write>(&self, writer: &mut Writer<W>, prop: &str) -> Result<()> {
|
||||
match prop {
|
||||
"resourcetype" => write_resourcetype(writer, vec!["C:calendar", "collection"])?,
|
||||
"current-user-principal" | "owner" => {
|
||||
write_href_prop(
|
||||
writer,
|
||||
prop,
|
||||
&format!("{}/{}/", self.prefix, self.principal),
|
||||
)?;
|
||||
}
|
||||
"displayname" => {
|
||||
let el = writer.create_element("displayname");
|
||||
if let Some(name) = self.calendar.clone().name {
|
||||
el.write_text_content(BytesText::new(&name))?;
|
||||
} else {
|
||||
el.write_empty()?;
|
||||
}
|
||||
}
|
||||
"calendar-color" => {
|
||||
let el = writer.create_element("IC:calendar-color");
|
||||
if let Some(color) = self.calendar.clone().color {
|
||||
el.write_text_content(BytesText::new(&color))?;
|
||||
} else {
|
||||
el.write_empty()?;
|
||||
}
|
||||
}
|
||||
"calendar-description" => {
|
||||
let el = writer.create_element("C:calendar-description");
|
||||
if let Some(description) = self.calendar.clone().description {
|
||||
el.write_text_content(BytesText::new(&description))?;
|
||||
} else {
|
||||
el.write_empty()?;
|
||||
}
|
||||
}
|
||||
"supported-calendar-component-set" => {
|
||||
writer
|
||||
.create_element("C:supported-calendar-component-set")
|
||||
.write_inner_content(|writer| {
|
||||
writer
|
||||
.create_element("C:comp")
|
||||
.with_attribute(("name", "VEVENT"))
|
||||
.write_empty()?;
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
"supported-calendar-data" => {
|
||||
writer
|
||||
.create_element("C:supported-calendar-data")
|
||||
.write_inner_content(|writer| {
|
||||
// <cal:calendar-data content-type="text/calendar" version="2.0" />
|
||||
writer
|
||||
.create_element("C:calendar-data")
|
||||
.with_attributes(vec![
|
||||
("content-type", "text/calendar"),
|
||||
("version", "2.0"),
|
||||
])
|
||||
.write_empty()?;
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
"getcontenttype" => {
|
||||
write_string_prop(writer, "getcontenttype", "text/calendar")?;
|
||||
}
|
||||
"max-resource-size" => {
|
||||
write_string_prop(writer, "max-resource-size", "10000000")?;
|
||||
}
|
||||
"current-user-privilege-set" => {
|
||||
writer
|
||||
.create_element("current-user-privilege-set")
|
||||
// These are just hard-coded for now and will possibly change in the future
|
||||
.write_inner_content(|writer| {
|
||||
for privilege in [
|
||||
"read",
|
||||
"read-acl",
|
||||
"write",
|
||||
"write-acl",
|
||||
"write-content",
|
||||
"read-current-user-privilege-set",
|
||||
"bind",
|
||||
"unbind",
|
||||
] {
|
||||
writer
|
||||
.create_element("privilege")
|
||||
.write_inner_content(|writer| {
|
||||
writer.create_element(privilege).write_empty()?;
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
_ => {
|
||||
return Err(anyhow!("invalid prop"));
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{proptypes::write_string_prop, resource::Resource};
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_store::calendar::{CalendarStore, Event};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub struct EventResource<C: CalendarStore> {
|
||||
pub cal_store: Arc<RwLock<C>>,
|
||||
pub path: String,
|
||||
pub event: Event,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<C: CalendarStore> Resource for EventResource<C> {
|
||||
type UriComponents = (String, String, String); // principal, calendar, event
|
||||
type MemberType = Self;
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
async fn acquire_from_request(
|
||||
req: HttpRequest,
|
||||
_auth_info: AuthInfo,
|
||||
uri_components: Self::UriComponents,
|
||||
_prefix: String,
|
||||
) -> Result<Self> {
|
||||
let (_principal, cid, uid) = uri_components;
|
||||
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
let event = cal_store.read().await.get_event(&cid, &uid).await?;
|
||||
|
||||
Ok(Self {
|
||||
cal_store,
|
||||
event,
|
||||
path: req.path().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn write_prop<W: std::io::Write>(
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
prop: &str,
|
||||
) -> Result<()> {
|
||||
match prop {
|
||||
"getetag" => {
|
||||
write_string_prop(writer, "getetag", &self.event.get_etag())?;
|
||||
}
|
||||
"calendar-data" => {
|
||||
write_string_prop(writer, "C:calendar-data", self.event.to_ics())?;
|
||||
}
|
||||
"getcontenttype" => {
|
||||
write_string_prop(writer, "getcontenttype", "text/calendar;charset=utf-8")?;
|
||||
}
|
||||
_ => return Err(anyhow!("invalid prop!")),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_dead_props() -> Vec<&'static str> {
|
||||
vec!["getetag", "calendar-data", "getcontenttype"]
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
pub mod calendar;
|
||||
pub mod event;
|
||||
pub mod principal;
|
||||
pub mod root;
|
||||
@@ -1,114 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{proptypes::write_href_prop, resource::Resource, xml_snippets::write_resourcetype};
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use quick_xml::events::BytesText;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_store::calendar::CalendarStore;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use super::calendar::CalendarResource;
|
||||
|
||||
pub struct PrincipalCalendarsResource<C: CalendarStore> {
|
||||
prefix: String,
|
||||
principal: String,
|
||||
path: String,
|
||||
cal_store: Arc<RwLock<C>>,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<C: CalendarStore> Resource for PrincipalCalendarsResource<C> {
|
||||
type UriComponents = ();
|
||||
type MemberType = CalendarResource<C>;
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||
let calendars = self
|
||||
.cal_store
|
||||
.read()
|
||||
.await
|
||||
.get_calendars(&self.principal)
|
||||
.await?;
|
||||
let mut out = Vec::new();
|
||||
for calendar in calendars {
|
||||
let path = format!("{}/{}", &self.path, &calendar.id);
|
||||
out.push(CalendarResource {
|
||||
cal_store: self.cal_store.clone(),
|
||||
calendar,
|
||||
path,
|
||||
prefix: self.prefix.clone(),
|
||||
principal: self.principal.clone(),
|
||||
})
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
async fn acquire_from_request(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
_uri_components: Self::UriComponents,
|
||||
prefix: String,
|
||||
) -> Result<Self> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||
.clone()
|
||||
.into_inner();
|
||||
Ok(Self {
|
||||
cal_store,
|
||||
prefix,
|
||||
principal: auth_info.user_id,
|
||||
path: req.path().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn write_prop<W: std::io::Write>(
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
prop: &str,
|
||||
) -> Result<()> {
|
||||
match prop {
|
||||
"resourcetype" => write_resourcetype(writer, vec!["principal", "collection"])?,
|
||||
"current-user-principal" | "principal-URL" => {
|
||||
write_href_prop(
|
||||
writer,
|
||||
prop,
|
||||
&format!("{}/{}/", self.prefix, self.principal),
|
||||
)?;
|
||||
}
|
||||
"calendar-home-set" | "calendar-user-address-set" => {
|
||||
writer
|
||||
.create_element(&format!("C:{prop}"))
|
||||
.write_inner_content(|writer| {
|
||||
writer
|
||||
.create_element("href")
|
||||
.write_text_content(BytesText::new(&format!(
|
||||
"{}/{}/",
|
||||
self.prefix, self.principal
|
||||
)))?;
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
"allprops" => {}
|
||||
_ => {
|
||||
return Err(anyhow!("invalid prop"));
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_dead_props() -> Vec<&'static str> {
|
||||
vec![
|
||||
"resourcetype",
|
||||
"current-user-principal",
|
||||
"principal-URL",
|
||||
"calendar-home-set",
|
||||
"calendar-user-address-set",
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
use crate::{resource::Resource, xml_snippets::write_resourcetype};
|
||||
use actix_web::HttpRequest;
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use quick_xml::events::BytesText;
|
||||
use rustical_auth::AuthInfo;
|
||||
|
||||
pub struct RootResource {
|
||||
prefix: String,
|
||||
principal: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Resource for RootResource {
|
||||
type UriComponents = ();
|
||||
type MemberType = Self;
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
async fn acquire_from_request(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
_uri_components: Self::UriComponents,
|
||||
prefix: String,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
prefix,
|
||||
principal: auth_info.user_id,
|
||||
path: req.path().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn write_prop<W: std::io::Write>(
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
prop: &str,
|
||||
) -> Result<()> {
|
||||
match prop {
|
||||
"resourcetype" => write_resourcetype(writer, vec!["collection"])?,
|
||||
"current-user-principal" => {
|
||||
writer
|
||||
.create_element("current-user-principal")
|
||||
.write_inner_content(|writer| {
|
||||
writer
|
||||
.create_element("href")
|
||||
.write_text_content(BytesText::new(&format!(
|
||||
"{}/{}",
|
||||
self.prefix, self.principal
|
||||
)))?;
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
_ => return Err(anyhow!("invalid prop!")),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_dead_props() -> Vec<&'static str> {
|
||||
vec!["resourcetype", "current-user-principal"]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user