adding card dav and cald dav

This commit is contained in:
DioCrafts
2025-04-13 01:04:04 +02:00
parent f2ecbc1a39
commit 52d8250d51
52 changed files with 8056 additions and 536 deletions
+8 -6
View File
@@ -10,7 +10,7 @@
*/
use uuid::Uuid;
use chrono::{DateTime, Utc, Duration};
use chrono::{DateTime, Utc, Duration, TimeZone};
use thiserror::Error;
use crate::common::errors::{Result, DomainError, ErrorKind};
@@ -404,11 +404,13 @@ impl CalendarEvent {
));
}
// Clone the summary before updating the struct
let summary_clone = summary.clone();
self.summary = summary;
self.updated_at = Utc::now();
// Update iCalendar data
self.update_ical_property("SUMMARY", &self.summary);
// Update iCalendar data using the cloned value
self.update_ical_property("SUMMARY", &summary_clone);
Ok(())
}
@@ -613,7 +615,7 @@ impl CalendarEvent {
*/
pub fn occurs_in_range(&self, start: &DateTime<Utc>, end: &DateTime<Utc>) -> bool {
// Basic case: event directly overlaps with range
if (self.start_time <= *end && self.end_time >= *start) {
if self.start_time <= *end && self.end_time >= *start {
return true;
}
@@ -708,7 +710,7 @@ impl CalendarEvent {
.map_err(|_| "Invalid day".to_string())?;
return match chrono::NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => Ok(DateTime::<Utc>::from_utc(date.and_hms_opt(0, 0, 0).unwrap(), Utc)),
Some(date) => Ok(Utc.from_utc_datetime(&date.and_hms_opt(0, 0, 0).unwrap())),
None => Err("Invalid date components".to_string()),
};
}
@@ -735,7 +737,7 @@ impl CalendarEvent {
match chrono::NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => match date.and_hms_opt(hour, minute, second) {
Some(datetime) => Ok(DateTime::<Utc>::from_utc(datetime, Utc)),
Some(datetime) => Ok(Utc.from_utc_datetime(&datetime)),
None => Err("Invalid time components".to_string()),
},
None => Err("Invalid date components".to_string()),
+127
View File
@@ -0,0 +1,127 @@
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use sqlx::types::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddressBook {
pub id: Uuid,
pub name: String,
pub owner_id: String,
pub description: Option<String>,
pub color: Option<String>,
pub is_public: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl Default for AddressBook {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
name: "Default Address Book".to_string(),
owner_id: "default".to_string(),
description: None,
color: None,
is_public: false,
created_at: Utc::now(),
updated_at: Utc::now(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Email {
pub email: String,
pub r#type: String, // home, work, other
pub is_primary: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Phone {
pub number: String,
pub r#type: String, // mobile, home, work, fax, other
pub is_primary: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
pub street: Option<String>,
pub city: Option<String>,
pub state: Option<String>,
pub postal_code: Option<String>,
pub country: Option<String>,
pub r#type: String, // home, work, other
pub is_primary: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contact {
pub id: Uuid,
pub address_book_id: Uuid,
pub uid: String,
pub full_name: Option<String>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub nickname: Option<String>,
pub email: Vec<Email>,
pub phone: Vec<Phone>,
pub address: Vec<Address>,
pub organization: Option<String>,
pub title: Option<String>,
pub notes: Option<String>,
pub photo_url: Option<String>,
pub birthday: Option<NaiveDate>,
pub anniversary: Option<NaiveDate>,
pub vcard: String,
pub etag: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl Default for Contact {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
address_book_id: Uuid::new_v4(),
uid: format!("{}@oxicloud", Uuid::new_v4()),
full_name: None,
first_name: None,
last_name: None,
nickname: None,
email: Vec::new(),
phone: Vec::new(),
address: Vec::new(),
organization: None,
title: None,
notes: None,
photo_url: None,
birthday: None,
anniversary: None,
vcard: "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD".to_string(),
etag: Uuid::new_v4().to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactGroup {
pub id: Uuid,
pub address_book_id: Uuid,
pub name: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl Default for ContactGroup {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
address_book_id: Uuid::new_v4(),
name: "New Group".to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
}
}
}
+3
View File
@@ -1,3 +1,6 @@
pub mod calendar;
pub mod calendar_event;
pub mod contact;
pub mod file;
pub mod folder;
pub mod user;
@@ -0,0 +1,22 @@
use async_trait::async_trait;
use sqlx::types::Uuid;
use std::result::Result;
use crate::common::errors::DomainError;
use crate::domain::entities::contact::AddressBook;
pub type AddressBookRepositoryResult<T> = Result<T, DomainError>;
#[async_trait]
pub trait AddressBookRepository: Send + Sync + 'static {
async fn create_address_book(&self, address_book: AddressBook) -> AddressBookRepositoryResult<AddressBook>;
async fn update_address_book(&self, address_book: AddressBook) -> AddressBookRepositoryResult<AddressBook>;
async fn delete_address_book(&self, id: &Uuid) -> AddressBookRepositoryResult<()>;
async fn get_address_book_by_id(&self, id: &Uuid) -> AddressBookRepositoryResult<Option<AddressBook>>;
async fn get_address_books_by_owner(&self, owner_id: &str) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn get_shared_address_books(&self, user_id: &str) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn get_public_address_books(&self) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn share_address_book(&self, address_book_id: &Uuid, user_id: &str, can_write: bool) -> AddressBookRepositoryResult<()>;
async fn unshare_address_book(&self, address_book_id: &Uuid, user_id: &str) -> AddressBookRepositoryResult<()>;
async fn get_address_book_shares(&self, address_book_id: &Uuid) -> AddressBookRepositoryResult<Vec<(String, bool)>>;
}
@@ -0,0 +1,62 @@
use async_trait::async_trait;
use uuid::Uuid;
use chrono::{DateTime, Utc};
use crate::common::errors::DomainError;
use crate::domain::entities::calendar_event::CalendarEvent;
pub type CalendarEventRepositoryResult<T> = Result<T, DomainError>;
/// Repository interface for CalendarEvent entity operations
#[async_trait]
pub trait CalendarEventRepository: Send + Sync + 'static {
/// Creates a new calendar event
async fn create_event(&self, event: CalendarEvent) -> CalendarEventRepositoryResult<CalendarEvent>;
/// Updates an existing calendar event
async fn update_event(&self, event: CalendarEvent) -> CalendarEventRepositoryResult<CalendarEvent>;
/// Deletes a calendar event by ID
async fn delete_event(&self, id: &Uuid) -> CalendarEventRepositoryResult<()>;
/// Finds a calendar event by its ID
async fn find_event_by_id(&self, id: &Uuid) -> CalendarEventRepositoryResult<CalendarEvent>;
/// Lists all events in a specific calendar
async fn list_events_by_calendar(&self, calendar_id: &Uuid) -> CalendarEventRepositoryResult<Vec<CalendarEvent>>;
/// Finds events in a calendar by their summary/title (partial match)
async fn find_events_by_summary(&self, calendar_id: &Uuid, summary: &str) -> CalendarEventRepositoryResult<Vec<CalendarEvent>>;
/// Gets events in a specific time range for a calendar
async fn get_events_in_time_range(
&self,
calendar_id: &Uuid,
start: &DateTime<Utc>,
end: &DateTime<Utc>
) -> CalendarEventRepositoryResult<Vec<CalendarEvent>>;
/// Finds an event by its iCalendar UID in a specific calendar
async fn find_event_by_ical_uid(&self, calendar_id: &Uuid, ical_uid: &str) -> CalendarEventRepositoryResult<Option<CalendarEvent>>;
/// Counts events in a calendar
async fn count_events_in_calendar(&self, calendar_id: &Uuid) -> CalendarEventRepositoryResult<i64>;
/// Deletes all events in a calendar
async fn delete_all_events_in_calendar(&self, calendar_id: &Uuid) -> CalendarEventRepositoryResult<i64>;
/// Lists events by calendar with pagination
async fn list_events_by_calendar_paginated(
&self,
calendar_id: &Uuid,
limit: i64,
offset: i64
) -> CalendarEventRepositoryResult<Vec<CalendarEvent>>;
/// Finds events with recurrence rules that might occur in a time range
async fn find_recurring_events_in_range(
&self,
calendar_id: &Uuid,
start: &DateTime<Utc>,
end: &DateTime<Utc>
) -> CalendarEventRepositoryResult<Vec<CalendarEvent>>;
}
@@ -0,0 +1,58 @@
use async_trait::async_trait;
use uuid::Uuid;
use crate::common::errors::DomainError;
use crate::domain::entities::calendar::Calendar;
pub type CalendarRepositoryResult<T> = Result<T, DomainError>;
/// Repository interface for Calendar entity operations
#[async_trait]
pub trait CalendarRepository: Send + Sync + 'static {
/// Creates a new calendar
async fn create_calendar(&self, calendar: Calendar) -> CalendarRepositoryResult<Calendar>;
/// Updates an existing calendar
async fn update_calendar(&self, calendar: Calendar) -> CalendarRepositoryResult<Calendar>;
/// Deletes a calendar by ID
async fn delete_calendar(&self, id: &Uuid) -> CalendarRepositoryResult<()>;
/// Finds a calendar by its ID
async fn find_calendar_by_id(&self, id: &Uuid) -> CalendarRepositoryResult<Calendar>;
/// Lists all calendars for a specific user
async fn list_calendars_by_owner(&self, owner_id: &str) -> CalendarRepositoryResult<Vec<Calendar>>;
/// Finds a calendar by name and owner
async fn find_calendar_by_name_and_owner(&self, name: &str, owner_id: &str) -> CalendarRepositoryResult<Calendar>;
/// Lists calendars shared with a specific user
async fn list_calendars_shared_with_user(&self, user_id: &str) -> CalendarRepositoryResult<Vec<Calendar>>;
/// List public calendars
async fn list_public_calendars(&self, limit: i64, offset: i64) -> CalendarRepositoryResult<Vec<Calendar>>;
/// Checks if a user has access to a calendar
async fn user_has_calendar_access(&self, calendar_id: &Uuid, user_id: &str) -> CalendarRepositoryResult<bool>;
/// Gets a custom property for a calendar
async fn get_calendar_property(&self, calendar_id: &Uuid, property_name: &str) -> CalendarRepositoryResult<Option<String>>;
/// Sets a custom property for a calendar
async fn set_calendar_property(&self, calendar_id: &Uuid, property_name: &str, property_value: &str) -> CalendarRepositoryResult<()>;
/// Removes a custom property from a calendar
async fn remove_calendar_property(&self, calendar_id: &Uuid, property_name: &str) -> CalendarRepositoryResult<()>;
/// Gets all custom properties for a calendar
async fn get_calendar_properties(&self, calendar_id: &Uuid) -> CalendarRepositoryResult<std::collections::HashMap<String, String>>;
/// Share calendar with another user
async fn share_calendar(&self, calendar_id: &Uuid, user_id: &str, access_level: &str) -> CalendarRepositoryResult<()>;
/// Remove calendar sharing for a user
async fn remove_calendar_sharing(&self, calendar_id: &Uuid, user_id: &str) -> CalendarRepositoryResult<()>;
/// Get calendar sharing information (who has access to this calendar)
async fn get_calendar_shares(&self, calendar_id: &Uuid) -> CalendarRepositoryResult<Vec<(String, String)>>;
}
@@ -0,0 +1,34 @@
use async_trait::async_trait;
use sqlx::types::Uuid;
use std::result::Result;
use crate::common::errors::DomainError;
use crate::domain::entities::contact::{Contact, ContactGroup};
pub type ContactRepositoryResult<T> = Result<T, DomainError>;
#[async_trait]
pub trait ContactRepository: Send + Sync + 'static {
async fn create_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact>;
async fn update_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact>;
async fn delete_contact(&self, id: &Uuid) -> ContactRepositoryResult<()>;
async fn get_contact_by_id(&self, id: &Uuid) -> ContactRepositoryResult<Option<Contact>>;
async fn get_contact_by_uid(&self, address_book_id: &Uuid, uid: &str) -> ContactRepositoryResult<Option<Contact>>;
async fn get_contacts_by_address_book(&self, address_book_id: &Uuid) -> ContactRepositoryResult<Vec<Contact>>;
async fn get_contacts_by_email(&self, email: &str) -> ContactRepositoryResult<Vec<Contact>>;
async fn get_contacts_by_group(&self, group_id: &Uuid) -> ContactRepositoryResult<Vec<Contact>>;
async fn search_contacts(&self, address_book_id: &Uuid, query: &str) -> ContactRepositoryResult<Vec<Contact>>;
}
#[async_trait]
pub trait ContactGroupRepository: Send + Sync + 'static {
async fn create_group(&self, group: ContactGroup) -> ContactRepositoryResult<ContactGroup>;
async fn update_group(&self, group: ContactGroup) -> ContactRepositoryResult<ContactGroup>;
async fn delete_group(&self, id: &Uuid) -> ContactRepositoryResult<()>;
async fn get_group_by_id(&self, id: &Uuid) -> ContactRepositoryResult<Option<ContactGroup>>;
async fn get_groups_by_address_book(&self, address_book_id: &Uuid) -> ContactRepositoryResult<Vec<ContactGroup>>;
async fn add_contact_to_group(&self, group_id: &Uuid, contact_id: &Uuid) -> ContactRepositoryResult<()>;
async fn remove_contact_from_group(&self, group_id: &Uuid, contact_id: &Uuid) -> ContactRepositoryResult<()>;
async fn get_contacts_in_group(&self, group_id: &Uuid) -> ContactRepositoryResult<Vec<Contact>>;
async fn get_groups_for_contact(&self, contact_id: &Uuid) -> ContactRepositoryResult<Vec<ContactGroup>>;
}
+6 -2
View File
@@ -1,6 +1,10 @@
pub mod address_book_repository;
pub mod calendar_repository;
pub mod calendar_event_repository;
pub mod contact_repository;
pub mod file_repository;
pub mod folder_repository;
pub mod user_repository;
pub mod session_repository;
pub mod share_repository;
pub mod trash_repository;
pub mod trash_repository;
pub mod user_repository;
+1 -1
View File
@@ -39,7 +39,7 @@ impl From<UserRepositoryError> for DomainError {
DomainError::internal_error("Database", msg)
},
UserRepositoryError::ValidationError(msg) => {
DomainError::validation_error("User", msg)
DomainError::validation_error(msg)
},
UserRepositoryError::Timeout(msg) => {
DomainError::timeout("Database", msg)