Refactor: Rename uid to object_id

This commit is contained in:
Lennart
2024-10-14 10:13:32 +02:00
parent d32f50f472
commit d87b7dcb8d
24 changed files with 153 additions and 287 deletions

View File

@@ -59,7 +59,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
user: User,
context: Data<CalDavContext<C>>,
) -> Result<HttpResponse, Error> {
let (principal, cid) = path.into_inner();
let (principal, cal_id) = path.into_inner();
if principal != user.id {
return Err(Error::Unauthorized);
}
@@ -68,7 +68,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
let request = request.set.prop;
let calendar = Calendar {
id: cid.to_owned(),
id: cal_id.to_owned(),
principal: principal.to_owned(),
order: request.order.unwrap_or(0),
displayname: request.displayname,
@@ -83,7 +83,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
.store
.read()
.await
.get_calendar(&principal, &cid)
.get_calendar(&principal, &cal_id)
.await
{
Err(rustical_store::Error::NotFound) => {

View File

@@ -34,10 +34,11 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
cal_query: &CalendarMultigetRequest,
principal_url: &str,
principal: &str,
cid: &str,
cal_id: &str,
store: &RwLock<C>,
) -> Result<(Vec<CalendarObject>, Vec<String>), Error> {
let resource_def = ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cid}/{uid}"));
let resource_def =
ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cal_id}/{object_id}"));
let mut result = vec![];
let mut not_found = vec![];
@@ -48,11 +49,11 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
if !resource_def.capture_match_info(&mut path) {
not_found.push(href.to_owned());
};
if path.get("cid").unwrap() != cid {
if path.get("cal_id").unwrap() != cal_id {
not_found.push(href.to_owned());
}
let uid = path.get("uid").unwrap();
match store.get_object(principal, cid, uid).await {
let object_id = path.get("object_id").unwrap();
match store.get_object(principal, cal_id, object_id).await {
Ok(object) => result.push(object),
Err(rustical_store::Error::NotFound) => not_found.push(href.to_owned()),
// TODO: Maybe add error handling on a per-object basis
@@ -67,12 +68,12 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
cal_multiget: CalendarMultigetRequest,
req: HttpRequest,
principal: &str,
cid: &str,
cal_id: &str,
cal_store: &RwLock<C>,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap();
let (objects, not_found) =
get_objects_calendar_multiget(&cal_multiget, &principal_url, principal, cid, cal_store)
get_objects_calendar_multiget(&cal_multiget, &principal_url, principal, cal_id, cal_store)
.await?;
let props = match cal_multiget.prop {
@@ -88,7 +89,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
let mut responses = Vec::new();
for object in objects {
let path = format!("{}/{}", req.path(), object.get_uid());
let path = format!("{}/{}", req.path(), object.get_id());
responses.push(CalendarObjectResource::from(object).propfind(
&path,
props.clone(),

View File

@@ -194,10 +194,10 @@ pub struct CalendarQueryRequest {
pub async fn get_objects_calendar_query<C: CalendarStore + ?Sized>(
cal_query: &CalendarQueryRequest,
principal: &str,
cid: &str,
cal_id: &str,
store: &RwLock<C>,
) -> Result<Vec<CalendarObject>, Error> {
let mut objects = store.read().await.get_objects(principal, cid).await?;
let mut objects = store.read().await.get_objects(principal, cal_id).await?;
if let Some(filter) = &cal_query.filter {
objects.retain(|object| filter.matches(object));
}
@@ -208,10 +208,10 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
cal_query: CalendarQueryRequest,
req: HttpRequest,
principal: &str,
cid: &str,
cal_id: &str,
cal_store: &RwLock<C>,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let objects = get_objects_calendar_query(&cal_query, principal, cid, cal_store).await?;
let objects = get_objects_calendar_query(&cal_query, principal, cal_id, cal_store).await?;
let props = match cal_query.prop {
PropfindType::Allprop => {
@@ -228,7 +228,7 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
for object in objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cid, object.get_uid()],
vec![principal, cal_id, object.get_id()],
)
.unwrap();
responses.push(CalendarObjectResource::from(object).propfind(

View File

@@ -39,7 +39,7 @@ pub async fn route_report_calendar<C: CalendarStore + ?Sized>(
req: HttpRequest,
cal_store: Data<RwLock<C>>,
) -> Result<impl Responder, Error> {
let (principal, cid) = path.into_inner();
let (principal, cal_id) = path.into_inner();
if principal != user.id {
return Err(Error::Unauthorized);
}
@@ -48,13 +48,13 @@ pub async fn route_report_calendar<C: CalendarStore + ?Sized>(
Ok(match request.clone() {
ReportRequest::CalendarQuery(cal_query) => {
handle_calendar_query(cal_query, req, &principal, &cid, &cal_store).await?
handle_calendar_query(cal_query, req, &principal, &cal_id, &cal_store).await?
}
ReportRequest::CalendarMultiget(cal_multiget) => {
handle_calendar_multiget(cal_multiget, req, &principal, &cid, &cal_store).await?
handle_calendar_multiget(cal_multiget, req, &principal, &cal_id, &cal_store).await?
}
ReportRequest::SyncCollection(sync_collection) => {
handle_sync_collection(sync_collection, req, &principal, &cid, &cal_store).await?
handle_sync_collection(sync_collection, req, &principal, &cal_id, &cal_store).await?
}
})
}

View File

@@ -46,7 +46,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
sync_collection: SyncCollectionRequest,
req: HttpRequest,
principal: &str,
cid: &str,
cal_id: &str,
cal_store: &RwLock<C>,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let props = match sync_collection.prop {
@@ -64,14 +64,14 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
let (new_objects, deleted_objects, new_synctoken) = cal_store
.read()
.await
.sync_changes(principal, cid, old_synctoken)
.sync_changes(principal, cal_id, old_synctoken)
.await?;
let mut responses = Vec::new();
for object in new_objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cid, &object.get_uid()],
vec![principal, cal_id, &object.get_id()],
)
.unwrap();
responses.push(CalendarObjectResource::from(object).propfind(
@@ -81,10 +81,12 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
)?);
}
for object_uid in deleted_objects {
let path =
CalendarObjectResource::get_url(req.resource_map(), vec![principal, cid, &object_uid])
.unwrap();
for object_id in deleted_objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cal_id, &object_id],
)
.unwrap();
responses.push(ResponseElement {
href: path,
status: Some(format!("HTTP/1.1 {}", StatusCode::NOT_FOUND)),

View File

@@ -273,7 +273,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
(
CalendarObjectResource::get_url(
rmap,
vec![&self.principal, &self.calendar_id, object.get_uid()],
vec![&self.principal, &self.calendar_id, object.get_id()],
)
.unwrap(),
object.into(),

View File

@@ -22,8 +22,8 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
) -> Result<HttpResponse, Error> {
let CalendarObjectPathComponents {
principal,
cid,
uid,
cal_id,
object_id,
} = path.into_inner();
if user.id != principal {
@@ -34,7 +34,7 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
.store
.read()
.await
.get_calendar(&principal, &cid)
.get_calendar(&principal, &cal_id)
.await?;
if user.id != calendar.principal {
return Ok(HttpResponse::Unauthorized().body(""));
@@ -44,7 +44,7 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
.store
.read()
.await
.get_object(&principal, &cid, &uid)
.get_object(&principal, &cal_id, &object_id)
.await?;
Ok(HttpResponse::Ok()
@@ -64,8 +64,8 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
) -> Result<HttpResponse, Error> {
let CalendarObjectPathComponents {
principal,
cid,
uid,
cal_id,
object_id,
} = path.into_inner();
if user.id != principal {
@@ -76,7 +76,7 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
.store
.read()
.await
.get_calendar(&principal, &cid)
.get_calendar(&principal, &cal_id)
.await?;
if user.id != calendar.principal {
return Ok(HttpResponse::Unauthorized().body(""));
@@ -89,7 +89,7 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) {
// Only write if not existing
match store.get_object(&principal, &cid, &uid).await {
match store.get_object(&principal, &cal_id, &object_id).await {
Ok(_) => {
// Conflict
return Ok(HttpResponse::Conflict().body("Resource with this URI already exists"));
@@ -104,8 +104,8 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
}
}
let object = CalendarObject::from_ics(uid, body)?;
store.put_object(principal, cid, object).await?;
let object = CalendarObject::from_ics(object_id, body)?;
store.put_object(principal, cal_id, object).await?;
Ok(HttpResponse::Created().body(""))
}

View File

@@ -16,8 +16,8 @@ pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
pub cal_store: Arc<RwLock<C>>,
pub path: String,
pub principal: String,
pub cid: String,
pub uid: String,
pub cal_id: String,
pub object_id: String,
}
#[derive(EnumString, Debug, VariantNames, Clone)]
@@ -78,8 +78,8 @@ impl Resource for CalendarObjectResource {
#[derive(Debug, Clone)]
pub struct CalendarObjectPathComponents {
pub principal: String,
pub cid: String,
pub uid: String,
pub cal_id: String,
pub object_id: String,
}
impl<'de> Deserialize<'de> for CalendarObjectPathComponents {
@@ -94,8 +94,8 @@ impl<'de> Deserialize<'de> for CalendarObjectPathComponents {
}
Ok(Self {
principal,
cid: calendar,
uid: object,
cal_id: calendar,
object_id: object,
})
}
}
@@ -113,8 +113,8 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
) -> Result<Self, Self::Error> {
let CalendarObjectPathComponents {
principal,
cid,
uid,
cal_id,
object_id,
} = path_components;
let cal_store = req
@@ -126,8 +126,8 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
Ok(Self {
cal_store,
principal,
cid,
uid,
cal_id,
object_id,
path: req.path().to_string(),
})
}
@@ -140,7 +140,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
.cal_store
.read()
.await
.get_object(&self.principal, &self.cid, &self.uid)
.get_object(&self.principal, &self.cal_id, &self.object_id)
.await?;
Ok(event.into())
}
@@ -153,7 +153,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
self.cal_store
.write()
.await
.delete_object(&self.principal, &self.cid, &self.uid, use_trashbin)
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
.await?;
Ok(())
}