mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 01:12:24 +00:00
Refactor how ResourceService works
This commit is contained in:
@@ -19,7 +19,7 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path.into_inner();
|
||||
|
||||
@@ -27,12 +27,14 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
|
||||
let addressbook = store.get_addressbook(&principal, &cal_id).await?;
|
||||
let addressbook = store.get_addressbook(&principal, &addressbook_id).await?;
|
||||
if user.id != addressbook.principal {
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
|
||||
let object = store.get_object(&principal, &cal_id, &object_id).await?;
|
||||
let object = store
|
||||
.get_object(&principal, &addressbook_id, &object_id)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.insert_header(("ETag", object.get_etag()))
|
||||
@@ -51,7 +53,7 @@ pub async fn put_object<AS: AddressbookStore + ?Sized>(
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id: addressbook_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path.into_inner();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{principal::PrincipalResource, Error};
|
||||
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::{
|
||||
@@ -16,9 +16,12 @@ use super::methods::{get_object, put_object};
|
||||
|
||||
pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
|
||||
addr_store: Arc<AS>,
|
||||
principal: String,
|
||||
cal_id: String,
|
||||
object_id: String,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> AddressObjectResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -89,7 +92,7 @@ impl Resource for AddressObjectResource {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddressObjectPathComponents {
|
||||
pub principal: String,
|
||||
pub cal_id: String,
|
||||
pub addressbook_id: String,
|
||||
pub object_id: String,
|
||||
}
|
||||
|
||||
@@ -99,13 +102,13 @@ impl<'de> Deserialize<'de> for AddressObjectPathComponents {
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
type Inner = (String, String, String);
|
||||
let (principal, calendar, mut object) = Inner::deserialize(deserializer)?;
|
||||
let (principal, addressbook_id, mut object) = Inner::deserialize(deserializer)?;
|
||||
if object.ends_with(".vcf") {
|
||||
object.truncate(object.len() - 4);
|
||||
}
|
||||
Ok(Self {
|
||||
principal,
|
||||
cal_id: calendar,
|
||||
addressbook_id,
|
||||
object_id: object,
|
||||
})
|
||||
}
|
||||
@@ -118,44 +121,35 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressObjectResourceSer
|
||||
type MemberType = AddressObjectResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let AddressObjectPathComponents {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path_components;
|
||||
|
||||
let addr_store = req
|
||||
.app_data::<Data<AS>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
addr_store,
|
||||
principal,
|
||||
cal_id,
|
||||
object_id,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
}: &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
let object = self
|
||||
.addr_store
|
||||
.get_object(&self.principal, &self.cal_id, &self.object_id)
|
||||
.get_object(principal, addressbook_id, object_id)
|
||||
.await?;
|
||||
Ok(AddressObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
AddressObjectPathComponents {
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
}: &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
|
||||
.delete_object(principal, addressbook_id, object_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::web;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
@@ -21,8 +20,12 @@ use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
|
||||
addr_store: Arc<AS>,
|
||||
principal: String,
|
||||
addressbook_id: String,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> AddressbookResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -157,10 +160,13 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
type Resource = AddressbookResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Error> {
|
||||
let addressbook = self
|
||||
.addr_store
|
||||
.get_addressbook(&self.principal, &self.addressbook_id)
|
||||
.get_addressbook(principal, addressbook_id)
|
||||
.await
|
||||
.map_err(|_e| Error::NotFound)?;
|
||||
Ok(addressbook.into())
|
||||
@@ -168,60 +174,48 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
Ok(self
|
||||
.addr_store
|
||||
.get_objects(&self.principal, &self.addressbook_id)
|
||||
.get_objects(principal, addressbook_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|object| {
|
||||
(
|
||||
AddressObjectResource::get_url(
|
||||
rmap,
|
||||
vec![&self.principal, &self.addressbook_id, object.get_id()],
|
||||
vec![principal, addressbook_id, object.get_id()],
|
||||
)
|
||||
.unwrap(),
|
||||
AddressObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let addr_store = req
|
||||
.app_data::<Data<AS>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
principal: path_components.0,
|
||||
addressbook_id: path_components.1,
|
||||
addr_store,
|
||||
})
|
||||
}
|
||||
|
||||
async fn save_resource(&self, file: Self::Resource) -> Result<(), Self::Error> {
|
||||
async fn save_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
file: Self::Resource,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.update_addressbook(
|
||||
self.principal.to_owned(),
|
||||
self.addressbook_id.to_owned(),
|
||||
file.into(),
|
||||
)
|
||||
.update_addressbook(principal.to_owned(), addressbook_id.to_owned(), file.into())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.delete_addressbook(&self.principal, &self.addressbook_id, use_trashbin)
|
||||
.delete_addressbook(principal, addressbook_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -57,17 +57,21 @@ pub fn configure_dav<AP: AuthenticationProvider, A: AddressbookStore + ?Sized>(
|
||||
}),
|
||||
)
|
||||
.app_data(Data::from(store.clone()))
|
||||
.service(RootResourceService::<PrincipalResource>::actix_resource())
|
||||
.service(RootResourceService::<PrincipalResource>::default().actix_resource())
|
||||
.service(
|
||||
web::scope("/user").service(
|
||||
web::scope("/{principal}")
|
||||
.service(PrincipalResourceService::<A>::actix_resource())
|
||||
.service(PrincipalResourceService::<A>::new(store.clone()).actix_resource())
|
||||
.service(
|
||||
web::scope("/{addressbook}")
|
||||
.service(AddressbookResourceService::<A>::actix_resource())
|
||||
.service(
|
||||
AddressbookResourceService::<A>::new(store.clone())
|
||||
.actix_resource(),
|
||||
)
|
||||
.service(
|
||||
web::scope("/{object}").service(
|
||||
AddressObjectResourceService::<A>::actix_resource(),
|
||||
AddressObjectResourceService::<A>::new(store.clone())
|
||||
.actix_resource(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use crate::addressbook::resource::AddressbookResource;
|
||||
use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::HttpRequest;
|
||||
use async_trait::async_trait;
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
@@ -14,10 +12,15 @@ use std::sync::Arc;
|
||||
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
|
||||
principal: String,
|
||||
addr_store: Arc<A>,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> PrincipalResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PrincipalResource {
|
||||
principal: String,
|
||||
@@ -97,39 +100,26 @@ impl<A: AddressbookStore + ?Sized> ResourceService for PrincipalResourceService<
|
||||
type Resource = PrincipalResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
(principal,): Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let addr_store = req
|
||||
.app_data::<Data<A>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
addr_store,
|
||||
principal,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
Ok(PrincipalResource {
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
let addressbooks = self.addr_store.get_addressbooks(&self.principal).await?;
|
||||
let addressbooks = self.addr_store.get_addressbooks(principal).await?;
|
||||
Ok(addressbooks
|
||||
.into_iter()
|
||||
.map(|addressbook| {
|
||||
(
|
||||
AddressbookResource::get_url(rmap, vec![&self.principal, &addressbook.id])
|
||||
.unwrap(),
|
||||
AddressbookResource::get_url(rmap, vec![principal, &addressbook.id]).unwrap(),
|
||||
addressbook.into(),
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user