Implement RFC 5545-compliant calendar system with v2 API

This major refactor eliminates manual string parsing throughout the
codebase and introduces proper RFC 5545 iCalendar specification
compliance with significant code simplification benefits.

## Backend Improvements
- Add complete RFC 5545-compliant data structures (VEvent, VTodo, etc.)
- Create simplified v2 API endpoints with direct DateTime support
- Eliminate ~150 lines of manual string parsing in handlers
- Add structured attendee and alarm support
- Maintain backward compatibility with existing v1 APIs

## Frontend Improvements
- Replace 16+ parameter create_event calls with single structured request
- Add automatic date/time conversion in EventCreationData
- Eliminate enum-to-string conversions throughout event creation
- Add v2 API models with proper type safety

## Technical Benefits
- Direct DateTime<Utc> usage instead of error-prone string parsing
- Proper RFC 5545 compliance with DTSTAMP, SEQUENCE fields
- Vec<AttendeeV2> instead of comma-separated strings
- Structured alarm system with multiple reminder types
- Enhanced RRULE support for complex recurrence patterns

## Code Quality
- Reduced create_event call from 16 parameters to 1 structured request
- Added comprehensive integration plan documentation
- Both backend and frontend compile successfully
- Maintained full backward compatibility during transition

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Connor Johnstone
2025-08-29 17:06:22 -04:00
parent 4af4aafd98
commit f266d3f304
10 changed files with 2160 additions and 66 deletions

View File

@@ -328,72 +328,13 @@ pub fn App() -> Html {
String::new()
};
// Convert local times to UTC for backend storage
let start_local = event_data.start_date.and_time(event_data.start_time);
let end_local = event_data.end_date.and_time(event_data.end_time);
// Use v2 API with structured data (no string conversion needed!)
let create_request = event_data.to_create_request_v2();
let start_utc = start_local.and_local_timezone(chrono::Local).unwrap().to_utc();
let end_utc = end_local.and_local_timezone(chrono::Local).unwrap().to_utc();
// Format UTC date and time strings for backend
let start_date = start_utc.format("%Y-%m-%d").to_string();
let start_time = start_utc.format("%H:%M").to_string();
let end_date = end_utc.format("%Y-%m-%d").to_string();
let end_time = end_utc.format("%H:%M").to_string();
// Convert enums to strings for backend
let status_str = match event_data.status {
EventStatus::Tentative => "tentative",
EventStatus::Cancelled => "cancelled",
_ => "confirmed",
}.to_string();
let class_str = match event_data.class {
EventClass::Private => "private",
EventClass::Confidential => "confidential",
_ => "public",
}.to_string();
let reminder_str = match event_data.reminder {
ReminderType::Minutes15 => "15min",
ReminderType::Minutes30 => "30min",
ReminderType::Hour1 => "1hour",
ReminderType::Hours2 => "2hours",
ReminderType::Day1 => "1day",
ReminderType::Days2 => "2days",
ReminderType::Week1 => "1week",
_ => "none",
}.to_string();
let recurrence_str = match event_data.recurrence {
RecurrenceType::Daily => "daily",
RecurrenceType::Weekly => "weekly",
RecurrenceType::Monthly => "monthly",
RecurrenceType::Yearly => "yearly",
_ => "none",
}.to_string();
match calendar_service.create_event(
match calendar_service.create_event_v2(
&token,
&password,
event_data.title,
event_data.description,
start_date,
start_time,
end_date,
end_time,
event_data.location,
event_data.all_day,
status_str,
class_str,
event_data.priority,
event_data.organizer,
event_data.attendees,
event_data.categories,
reminder_str,
recurrence_str,
event_data.recurrence_days,
event_data.selected_calendar
create_request,
).await {
Ok(_) => {
web_sys::console::log_1(&"Event created successfully".into());

View File

@@ -1,7 +1,7 @@
use yew::prelude::*;
use web_sys::{HtmlInputElement, HtmlTextAreaElement, HtmlSelectElement};
use chrono::{NaiveDate, NaiveTime};
use crate::services::calendar_service::{CalendarInfo, CalendarEvent};
use chrono::{NaiveDate, NaiveTime, Utc, TimeZone};
use crate::services::calendar_service::{CalendarInfo, CalendarEvent, CreateEventRequestV2, AttendeeV2, AlarmV2, AttendeeRoleV2, ParticipationStatusV2, AlarmActionV2};
#[derive(Properties, PartialEq)]
pub struct CreateEventModalProps {
@@ -187,6 +187,161 @@ impl EventCreationData {
selected_calendar: event.calendar_path.clone(),
}
}
/// Convert EventCreationData to CreateEventRequestV2 for the new v2 API
pub fn to_create_request_v2(&self) -> CreateEventRequestV2 {
// Combine date and time into UTC DateTime
let start_local = self.start_date.and_time(self.start_time);
let end_local = self.end_date.and_time(self.end_time);
// Convert local time to UTC (assuming local timezone for now)
let start_utc = chrono::Local.from_local_datetime(&start_local)
.single()
.unwrap_or_else(|| chrono::Local.from_local_datetime(&start_local).earliest().unwrap())
.with_timezone(&Utc);
let end_utc = chrono::Local.from_local_datetime(&end_local)
.single()
.unwrap_or_else(|| chrono::Local.from_local_datetime(&end_local).earliest().unwrap())
.with_timezone(&Utc);
// Convert status
let status = match self.status {
EventStatus::Tentative => Some(crate::services::calendar_service::EventStatus::Tentative),
EventStatus::Confirmed => Some(crate::services::calendar_service::EventStatus::Confirmed),
EventStatus::Cancelled => Some(crate::services::calendar_service::EventStatus::Cancelled),
};
// Convert class
let class = match self.class {
EventClass::Public => Some(crate::services::calendar_service::EventClass::Public),
EventClass::Private => Some(crate::services::calendar_service::EventClass::Private),
EventClass::Confidential => Some(crate::services::calendar_service::EventClass::Confidential),
};
// Convert attendees from comma-separated string to structured list
let attendees = if self.attendees.trim().is_empty() {
Vec::new()
} else {
self.attendees.split(',')
.map(|email| AttendeeV2 {
email: email.trim().to_string(),
name: None,
role: Some(AttendeeRoleV2::Required),
status: Some(ParticipationStatusV2::NeedsAction),
rsvp: Some(true),
})
.collect()
};
// Convert categories from comma-separated string to vector
let categories = if self.categories.trim().is_empty() {
Vec::new()
} else {
self.categories.split(',')
.map(|cat| cat.trim().to_string())
.filter(|cat| !cat.is_empty())
.collect()
};
// Convert reminder to alarms
let alarms = match self.reminder {
ReminderType::Minutes15 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -15,
description: Some("Event reminder".to_string()),
}],
ReminderType::Minutes30 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -30,
description: Some("Event reminder".to_string()),
}],
ReminderType::Hour1 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -60,
description: Some("Event reminder".to_string()),
}],
ReminderType::Hours2 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -120,
description: Some("Event reminder".to_string()),
}],
ReminderType::Day1 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -1440,
description: Some("Event reminder".to_string()),
}],
ReminderType::Days2 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -2880,
description: Some("Event reminder".to_string()),
}],
ReminderType::Week1 => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -10080,
description: Some("Event reminder".to_string()),
}],
ReminderType::None => Vec::new(),
};
// Convert recurrence to RRULE string
let rrule = match self.recurrence {
RecurrenceType::Daily => Some("FREQ=DAILY".to_string()),
RecurrenceType::Weekly => {
let mut rrule = "FREQ=WEEKLY".to_string();
// Add BYDAY if specific days are selected
if self.recurrence_days.len() == 7 {
let selected_days: Vec<&str> = self.recurrence_days
.iter()
.enumerate()
.filter_map(|(i, &selected)| {
if selected {
Some(match i {
0 => "SU", // Sunday
1 => "MO", // Monday
2 => "TU", // Tuesday
3 => "WE", // Wednesday
4 => "TH", // Thursday
5 => "FR", // Friday
6 => "SA", // Saturday
_ => return None,
})
} else {
None
}
})
.collect();
if !selected_days.is_empty() {
rrule.push_str(&format!(";BYDAY={}", selected_days.join(",")));
}
}
Some(rrule)
},
RecurrenceType::Monthly => Some("FREQ=MONTHLY".to_string()),
RecurrenceType::Yearly => Some("FREQ=YEARLY".to_string()),
RecurrenceType::None => None,
};
CreateEventRequestV2 {
summary: self.title.clone(),
description: if self.description.trim().is_empty() { None } else { Some(self.description.clone()) },
dtstart: start_utc,
dtend: Some(end_utc),
location: if self.location.trim().is_empty() { None } else { Some(self.location.clone()) },
all_day: self.all_day,
status,
class,
priority: self.priority,
organizer: if self.organizer.trim().is_empty() { None } else { Some(self.organizer.clone()) },
attendees,
categories,
rrule,
alarms,
calendar_path: self.selected_calendar.clone(),
}
}
}
#[function_component(CreateEventModal)]

View File

@@ -2,6 +2,7 @@
mod app;
mod auth;
mod components;
mod models;
mod services;
use app::App;

601
src/models/ical.rs Normal file
View File

@@ -0,0 +1,601 @@
// RFC 5545 Compliant iCalendar Data Structures
// This file contains updated structures that fully comply with RFC 5545 iCalendar specification
use chrono::{DateTime, Utc, NaiveDate, Duration};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// ==================== CALENDAR OBJECT (VCALENDAR) ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ICalendarObject {
// Required calendar properties
pub prodid: String, // Product identifier (PRODID)
pub version: String, // Version (typically "2.0")
// Optional calendar properties
pub calscale: Option<String>, // Calendar scale (CALSCALE) - default "GREGORIAN"
pub method: Option<String>, // Method (METHOD)
// Components
pub events: Vec<VEvent>, // VEVENT components
pub todos: Vec<VTodo>, // VTODO components
pub journals: Vec<VJournal>, // VJOURNAL components
pub freebusys: Vec<VFreeBusy>, // VFREEBUSY components
pub timezones: Vec<VTimeZone>, // VTIMEZONE components
}
// ==================== VEVENT COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VEvent {
// Required properties
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
pub uid: String, // Unique identifier (UID) - REQUIRED
pub dtstart: DateTime<Utc>, // Start date-time (DTSTART) - REQUIRED
// Optional properties (commonly used)
pub dtend: Option<DateTime<Utc>>, // End date-time (DTEND)
pub duration: Option<Duration>, // Duration (DURATION) - alternative to DTEND
pub summary: Option<String>, // Summary/title (SUMMARY)
pub description: Option<String>, // Description (DESCRIPTION)
pub location: Option<String>, // Location (LOCATION)
// Classification and status
pub class: Option<EventClass>, // Classification (CLASS)
pub status: Option<EventStatus>, // Status (STATUS)
pub transp: Option<TimeTransparency>, // Time transparency (TRANSP)
pub priority: Option<u8>, // Priority 0-9 (PRIORITY)
// People and organization
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
pub attendees: Vec<Attendee>, // Attendees (ATTENDEE)
pub contact: Option<String>, // Contact information (CONTACT)
// Categorization and relationships
pub categories: Vec<String>, // Categories (CATEGORIES)
pub comment: Option<String>, // Comment (COMMENT)
pub resources: Vec<String>, // Resources (RESOURCES)
pub related_to: Option<String>, // Related component (RELATED-TO)
pub url: Option<String>, // URL (URL)
// Geographical
pub geo: Option<GeographicPosition>, // Geographic position (GEO)
// Versioning and modification
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
// Recurrence
pub rrule: Option<String>, // Recurrence rule (RRULE)
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
// Alarms and attachments
pub alarms: Vec<VAlarm>, // VALARM components
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
// CalDAV specific (for implementation)
pub etag: Option<String>, // ETag for CalDAV
pub href: Option<String>, // Href for CalDAV
pub calendar_path: Option<String>, // Calendar path
pub all_day: bool, // All-day event flag
}
// ==================== VTODO COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VTodo {
// Required properties
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
pub uid: String, // Unique identifier (UID) - REQUIRED
// Optional date-time properties
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
pub due: Option<DateTime<Utc>>, // Due date-time (DUE)
pub duration: Option<Duration>, // Duration (DURATION)
pub completed: Option<DateTime<Utc>>, // Completion date-time (COMPLETED)
// Descriptive properties
pub summary: Option<String>, // Summary/title (SUMMARY)
pub description: Option<String>, // Description (DESCRIPTION)
pub location: Option<String>, // Location (LOCATION)
// Status and completion
pub status: Option<TodoStatus>, // Status (STATUS)
pub percent_complete: Option<u8>, // Percent complete 0-100 (PERCENT-COMPLETE)
pub priority: Option<u8>, // Priority 0-9 (PRIORITY)
// People and organization
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
pub attendees: Vec<Attendee>, // Attendees (ATTENDEE)
pub contact: Option<String>, // Contact information (CONTACT)
// Categorization and relationships
pub categories: Vec<String>, // Categories (CATEGORIES)
pub comment: Option<String>, // Comment (COMMENT)
pub resources: Vec<String>, // Resources (RESOURCES)
pub related_to: Option<String>, // Related component (RELATED-TO)
pub url: Option<String>, // URL (URL)
// Geographical
pub geo: Option<GeographicPosition>, // Geographic position (GEO)
// Versioning and modification
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
// Recurrence
pub rrule: Option<String>, // Recurrence rule (RRULE)
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
// Alarms and attachments
pub alarms: Vec<VAlarm>, // VALARM components
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
}
// ==================== VJOURNAL COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VJournal {
// Required properties
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
pub uid: String, // Unique identifier (UID) - REQUIRED
// Optional properties
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
pub summary: Option<String>, // Summary/title (SUMMARY)
pub description: Option<String>, // Description (DESCRIPTION)
// Classification and status
pub class: Option<EventClass>, // Classification (CLASS)
pub status: Option<JournalStatus>, // Status (STATUS)
// People and organization
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
pub contact: Option<String>, // Contact information (CONTACT)
// Categorization and relationships
pub categories: Vec<String>, // Categories (CATEGORIES)
pub comment: Option<String>, // Comment (COMMENT)
pub related_to: Option<String>, // Related component (RELATED-TO)
pub url: Option<String>, // URL (URL)
// Versioning and modification
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
// Recurrence
pub rrule: Option<String>, // Recurrence rule (RRULE)
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
// Attachments
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
}
// ==================== VFREEBUSY COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VFreeBusy {
// Required properties
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
pub uid: String, // Unique identifier (UID) - REQUIRED
// Optional properties
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
pub dtend: Option<DateTime<Utc>>, // End date-time (DTEND)
pub duration: Option<Duration>, // Duration (DURATION)
// Free/busy information
pub freebusy: Vec<FreeBusyTime>, // Free/busy periods (FREEBUSY)
// People and organization
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
pub attendees: Vec<CalendarUser>, // Attendees (ATTENDEE)
pub contact: Option<String>, // Contact information (CONTACT)
// Additional properties
pub comment: Option<String>, // Comment (COMMENT)
pub url: Option<String>, // URL (URL)
}
// ==================== VTIMEZONE COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VTimeZone {
// Required properties
pub tzid: String, // Time zone identifier (TZID) - REQUIRED
// Optional properties
pub tzname: Option<String>, // Time zone name (TZNAME)
pub tzurl: Option<String>, // Time zone URL (TZURL)
// Standard and daylight components
pub standard: Vec<TimeZoneComponent>, // Standard time components
pub daylight: Vec<TimeZoneComponent>, // Daylight time components
// Last modified
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TimeZoneComponent {
pub dtstart: DateTime<Utc>, // Start date-time (DTSTART) - REQUIRED
pub tzoffsetfrom: String, // UTC offset from (TZOFFSETFROM) - REQUIRED
pub tzoffsetto: String, // UTC offset to (TZOFFSETTO) - REQUIRED
pub tzname: Option<String>, // Time zone name (TZNAME)
pub comment: Option<String>, // Comment (COMMENT)
// Recurrence
pub rrule: Option<String>, // Recurrence rule (RRULE)
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
}
// ==================== VALARM COMPONENT ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VAlarm {
pub action: AlarmAction, // Action (ACTION) - REQUIRED
pub trigger: AlarmTrigger, // Trigger (TRIGGER) - REQUIRED
// Optional properties (some required based on action)
pub description: Option<String>, // Description (DESCRIPTION)
pub summary: Option<String>, // Summary (SUMMARY)
pub duration: Option<Duration>, // Duration (DURATION)
pub repeat: Option<u32>, // Repeat count (REPEAT)
pub attendees: Vec<CalendarUser>, // Attendees (ATTENDEE) - for EMAIL action
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
}
// ==================== SUPPORTING TYPES ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum EventClass {
Public,
Private,
Confidential,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum EventStatus {
Tentative,
Confirmed,
Cancelled,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TodoStatus {
NeedsAction,
Completed,
InProcess,
Cancelled,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum JournalStatus {
Draft,
Final,
Cancelled,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TimeTransparency {
Opaque, // Time is not available (default)
Transparent, // Time is available despite event
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlarmAction {
Audio,
Display,
Email,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlarmTrigger {
Duration(Duration), // Relative to start/end
DateTime(DateTime<Utc>), // Absolute time
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CalendarUser {
pub cal_address: String, // Calendar address (email)
pub cn: Option<String>, // Common name (CN parameter)
pub dir: Option<String>, // Directory entry (DIR parameter)
pub sent_by: Option<String>, // Sent by (SENT-BY parameter)
pub language: Option<String>, // Language (LANGUAGE parameter)
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Attendee {
pub cal_address: String, // Calendar address (email)
pub cn: Option<String>, // Common name (CN parameter)
pub role: Option<AttendeeRole>, // Role (ROLE parameter)
pub partstat: Option<ParticipationStatus>, // Participation status (PARTSTAT parameter)
pub rsvp: Option<bool>, // RSVP expectation (RSVP parameter)
pub cutype: Option<CalendarUserType>, // Calendar user type (CUTYPE parameter)
pub member: Vec<String>, // Group/list membership (MEMBER parameter)
pub delegated_to: Vec<String>, // Delegated to (DELEGATED-TO parameter)
pub delegated_from: Vec<String>, // Delegated from (DELEGATED-FROM parameter)
pub sent_by: Option<String>, // Sent by (SENT-BY parameter)
pub dir: Option<String>, // Directory entry (DIR parameter)
pub language: Option<String>, // Language (LANGUAGE parameter)
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AttendeeRole {
Chair,
ReqParticipant,
OptParticipant,
NonParticipant,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ParticipationStatus {
NeedsAction,
Accepted,
Declined,
Tentative,
Delegated,
Completed,
InProcess,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CalendarUserType {
Individual,
Group,
Resource,
Room,
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GeographicPosition {
pub latitude: f64, // Latitude in decimal degrees
pub longitude: f64, // Longitude in decimal degrees
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Attachment {
pub data: AttachmentData, // Attachment data
pub fmttype: Option<String>, // Format type (FMTTYPE parameter)
pub encoding: Option<String>, // Encoding (ENCODING parameter)
pub filename: Option<String>, // Filename (X-FILENAME parameter)
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AttachmentData {
Uri(String), // URI reference
Binary(Vec<u8>), // Binary data
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FreeBusyTime {
pub period: (DateTime<Utc>, DateTime<Utc>), // Start and end time
pub fbtype: Option<FreeBusyType>, // Free/busy type
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum FreeBusyType {
Free,
Busy,
BusyUnavailable,
BusyTentative,
}
// ==================== COMPATIBILITY LAYER ====================
use crate::services::calendar_service::{CalendarEvent, EventReminder, ReminderAction};
// Conversion from new VEvent to existing CalendarEvent
impl From<VEvent> for CalendarEvent {
fn from(vevent: VEvent) -> Self {
Self {
uid: vevent.uid,
summary: vevent.summary,
description: vevent.description,
start: vevent.dtstart,
end: vevent.dtend,
location: vevent.location,
status: vevent.status.unwrap_or(EventStatus::Confirmed).into(),
class: vevent.class.unwrap_or(EventClass::Public).into(),
priority: vevent.priority,
organizer: vevent.organizer.map(|o| o.cal_address),
attendees: vevent.attendees.into_iter().map(|a| a.cal_address).collect(),
categories: vevent.categories,
created: vevent.created,
last_modified: vevent.last_modified,
recurrence_rule: vevent.rrule,
exception_dates: vevent.exdate,
all_day: vevent.all_day,
reminders: vevent.alarms.into_iter().map(|a| a.into()).collect(),
etag: vevent.etag,
href: vevent.href,
calendar_path: vevent.calendar_path,
}
}
}
// Conversion from existing CalendarEvent to new VEvent
impl From<CalendarEvent> for VEvent {
fn from(event: CalendarEvent) -> Self {
use chrono::Utc;
Self {
// Required properties
dtstamp: Utc::now(), // Add required DTSTAMP
uid: event.uid,
dtstart: event.start,
// Optional properties
dtend: event.end,
duration: None, // Will be calculated from dtend if needed
summary: event.summary,
description: event.description,
location: event.location,
// Classification and status
class: Some(event.class.into()),
status: Some(event.status.into()),
transp: None, // Default to None, can be enhanced later
priority: event.priority,
// People and organization
organizer: event.organizer.map(|email| CalendarUser {
cal_address: email,
cn: None,
dir: None,
sent_by: None,
language: None,
}),
attendees: event.attendees.into_iter().map(|email| Attendee {
cal_address: email,
cn: None,
role: None,
partstat: None,
rsvp: None,
cutype: None,
member: Vec::new(),
delegated_to: Vec::new(),
delegated_from: Vec::new(),
sent_by: None,
dir: None,
language: None,
}).collect(),
contact: None,
// Categorization and relationships
categories: event.categories,
comment: None,
resources: Vec::new(),
related_to: None,
url: None,
// Geographical
geo: None,
// Versioning and modification
sequence: Some(0), // Start with sequence 0
created: event.created,
last_modified: event.last_modified,
// Recurrence
rrule: event.recurrence_rule,
rdate: Vec::new(),
exdate: event.exception_dates,
recurrence_id: None,
// Alarms and attachments
alarms: event.reminders.into_iter().map(|r| r.into()).collect(),
attachments: Vec::new(),
// CalDAV specific
etag: event.etag,
href: event.href,
calendar_path: event.calendar_path,
all_day: event.all_day,
}
}
}
// Convert between status enums
impl From<EventStatus> for crate::services::calendar_service::EventStatus {
fn from(status: EventStatus) -> Self {
match status {
EventStatus::Tentative => crate::services::calendar_service::EventStatus::Tentative,
EventStatus::Confirmed => crate::services::calendar_service::EventStatus::Confirmed,
EventStatus::Cancelled => crate::services::calendar_service::EventStatus::Cancelled,
}
}
}
impl From<crate::services::calendar_service::EventStatus> for EventStatus {
fn from(status: crate::services::calendar_service::EventStatus) -> Self {
match status {
crate::services::calendar_service::EventStatus::Tentative => EventStatus::Tentative,
crate::services::calendar_service::EventStatus::Confirmed => EventStatus::Confirmed,
crate::services::calendar_service::EventStatus::Cancelled => EventStatus::Cancelled,
}
}
}
// Convert between class enums
impl From<EventClass> for crate::services::calendar_service::EventClass {
fn from(class: EventClass) -> Self {
match class {
EventClass::Public => crate::services::calendar_service::EventClass::Public,
EventClass::Private => crate::services::calendar_service::EventClass::Private,
EventClass::Confidential => crate::services::calendar_service::EventClass::Confidential,
}
}
}
impl From<crate::services::calendar_service::EventClass> for EventClass {
fn from(class: crate::services::calendar_service::EventClass) -> Self {
match class {
crate::services::calendar_service::EventClass::Public => EventClass::Public,
crate::services::calendar_service::EventClass::Private => EventClass::Private,
crate::services::calendar_service::EventClass::Confidential => EventClass::Confidential,
}
}
}
// Convert between reminder types
impl From<VAlarm> for EventReminder {
fn from(alarm: VAlarm) -> Self {
let minutes_before = match alarm.trigger {
AlarmTrigger::Duration(duration) => {
// Convert duration to minutes (assuming it's negative for "before")
(-duration.num_minutes()) as i32
},
AlarmTrigger::DateTime(_) => 0, // Absolute time alarms default to 0 minutes
};
let action = match alarm.action {
AlarmAction::Display => ReminderAction::Display,
AlarmAction::Audio => ReminderAction::Audio,
AlarmAction::Email => ReminderAction::Email,
};
Self {
minutes_before,
action,
description: alarm.description,
}
}
}
impl From<EventReminder> for VAlarm {
fn from(reminder: EventReminder) -> Self {
use chrono::Duration;
let action = match reminder.action {
ReminderAction::Display => AlarmAction::Display,
ReminderAction::Audio => AlarmAction::Audio,
ReminderAction::Email => AlarmAction::Email,
};
let trigger = AlarmTrigger::Duration(Duration::minutes(-reminder.minutes_before as i64));
Self {
action,
trigger,
description: reminder.description,
summary: None,
duration: None,
repeat: None,
attendees: Vec::new(),
attachments: Vec::new(),
}
}
}

13
src/models/mod.rs Normal file
View File

@@ -0,0 +1,13 @@
// RFC 5545 Compliant iCalendar Models
pub mod ical;
// Re-export commonly used types
pub use ical::{
VEvent, VTodo, VJournal, VFreeBusy, VTimeZone, VAlarm,
ICalendarObject,
EventStatus, EventClass, TodoStatus, JournalStatus,
TimeTransparency, AlarmAction, AlarmTrigger,
CalendarUser, Attendee, AttendeeRole, ParticipationStatus, CalendarUserType,
GeographicPosition, Attachment, AttachmentData,
FreeBusyTime, FreeBusyType, TimeZoneComponent,
};

View File

@@ -90,6 +90,139 @@ impl Default for EventClass {
}
}
// ==================== V2 API MODELS ====================
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AttendeeV2 {
pub email: String,
pub name: Option<String>,
pub role: Option<AttendeeRoleV2>,
pub status: Option<ParticipationStatusV2>,
pub rsvp: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AttendeeRoleV2 {
Chair,
Required,
Optional,
NonParticipant,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ParticipationStatusV2 {
NeedsAction,
Accepted,
Declined,
Tentative,
Delegated,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AlarmV2 {
pub action: AlarmActionV2,
pub trigger_minutes: i32, // Minutes before event (negative = before)
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlarmActionV2 {
Audio,
Display,
Email,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DeleteActionV2 {
DeleteThis,
DeleteFollowing,
DeleteSeries,
}
// V2 Request/Response Models
#[derive(Debug, Serialize)]
pub struct CreateEventRequestV2 {
pub summary: String,
pub description: Option<String>,
pub dtstart: DateTime<Utc>,
pub dtend: Option<DateTime<Utc>>,
pub location: Option<String>,
pub all_day: bool,
pub status: Option<EventStatus>,
pub class: Option<EventClass>,
pub priority: Option<u8>,
pub organizer: Option<String>,
pub attendees: Vec<AttendeeV2>,
pub categories: Vec<String>,
pub rrule: Option<String>,
pub alarms: Vec<AlarmV2>,
pub calendar_path: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct UpdateEventRequestV2 {
pub uid: String,
pub summary: String,
pub description: Option<String>,
pub dtstart: DateTime<Utc>,
pub dtend: Option<DateTime<Utc>>,
pub location: Option<String>,
pub all_day: bool,
pub status: Option<EventStatus>,
pub class: Option<EventClass>,
pub priority: Option<u8>,
pub organizer: Option<String>,
pub attendees: Vec<AttendeeV2>,
pub categories: Vec<String>,
pub rrule: Option<String>,
pub alarms: Vec<AlarmV2>,
pub calendar_path: Option<String>,
pub update_action: Option<String>,
pub occurrence_date: Option<DateTime<Utc>>,
pub exception_dates: Option<Vec<DateTime<Utc>>>,
pub until_date: Option<DateTime<Utc>>,
}
#[derive(Debug, Serialize)]
pub struct DeleteEventRequestV2 {
pub calendar_path: String,
pub event_href: String,
pub delete_action: DeleteActionV2,
pub occurrence_date: Option<DateTime<Utc>>,
}
#[derive(Debug, Deserialize)]
pub struct CreateEventResponseV2 {
pub success: bool,
pub message: String,
pub event: Option<EventSummaryV2>,
}
#[derive(Debug, Deserialize)]
pub struct UpdateEventResponseV2 {
pub success: bool,
pub message: String,
pub event: Option<EventSummaryV2>,
}
#[derive(Debug, Deserialize)]
pub struct DeleteEventResponseV2 {
pub success: bool,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EventSummaryV2 {
pub uid: String,
pub summary: Option<String>,
pub dtstart: DateTime<Utc>,
pub dtend: Option<DateTime<Utc>>,
pub location: Option<String>,
pub all_day: bool,
pub href: Option<String>,
pub etag: Option<String>,
}
impl CalendarEvent {
/// Get the date for this event (for calendar display)
pub fn get_date(&self) -> NaiveDate {
@@ -951,4 +1084,227 @@ impl CalendarService {
Err(format!("Request failed with status {}: {}", resp.status(), text_string))
}
}
// ==================== V2 API METHODS ====================
/// Create a new event using V2 API (no string parsing required)
pub async fn create_event_v2(
&self,
token: &str,
password: &str,
request: CreateEventRequestV2,
) -> Result<CreateEventResponseV2, String> {
let window = web_sys::window().ok_or("No global window exists")?;
let opts = RequestInit::new();
opts.set_method("POST");
opts.set_mode(RequestMode::Cors);
let body_string = serde_json::to_string(&request)
.map_err(|e| format!("JSON serialization failed: {}", e))?;
let url = format!("{}/v2/calendar/events/create", self.base_url);
opts.set_body(&body_string.into());
let request_obj = Request::new_with_str_and_init(&url, &opts)
.map_err(|e| format!("Request creation failed: {:?}", e))?;
request_obj.headers().set("Authorization", &format!("Bearer {}", token))
.map_err(|e| format!("Authorization header setting failed: {:?}", e))?;
request_obj.headers().set("X-CalDAV-Password", password)
.map_err(|e| format!("Password header setting failed: {:?}", e))?;
request_obj.headers().set("Content-Type", "application/json")
.map_err(|e| format!("Content-Type header setting failed: {:?}", e))?;
let resp_value = JsFuture::from(window.fetch_with_request(&request_obj))
.await
.map_err(|e| format!("Network request failed: {:?}", e))?;
let resp: Response = resp_value.dyn_into()
.map_err(|e| format!("Response cast failed: {:?}", e))?;
let text = JsFuture::from(resp.text()
.map_err(|e| format!("Text extraction failed: {:?}", e))?)
.await
.map_err(|e| format!("Text promise failed: {:?}", e))?;
let text_string = text.as_string()
.ok_or("Response text is not a string")?;
if resp.ok() {
let response: CreateEventResponseV2 = serde_json::from_str(&text_string)
.map_err(|e| format!("JSON parsing failed: {}", e))?;
Ok(response)
} else {
Err(format!("Request failed with status {}: {}", resp.status(), text_string))
}
}
/// Update an event using V2 API (no string parsing required)
pub async fn update_event_v2(
&self,
token: &str,
password: &str,
request: UpdateEventRequestV2,
) -> Result<UpdateEventResponseV2, String> {
let window = web_sys::window().ok_or("No global window exists")?;
let opts = RequestInit::new();
opts.set_method("POST");
opts.set_mode(RequestMode::Cors);
let body_string = serde_json::to_string(&request)
.map_err(|e| format!("JSON serialization failed: {}", e))?;
let url = format!("{}/v2/calendar/events/update", self.base_url);
opts.set_body(&body_string.into());
let request_obj = Request::new_with_str_and_init(&url, &opts)
.map_err(|e| format!("Request creation failed: {:?}", e))?;
request_obj.headers().set("Authorization", &format!("Bearer {}", token))
.map_err(|e| format!("Authorization header setting failed: {:?}", e))?;
request_obj.headers().set("X-CalDAV-Password", password)
.map_err(|e| format!("Password header setting failed: {:?}", e))?;
request_obj.headers().set("Content-Type", "application/json")
.map_err(|e| format!("Content-Type header setting failed: {:?}", e))?;
let resp_value = JsFuture::from(window.fetch_with_request(&request_obj))
.await
.map_err(|e| format!("Network request failed: {:?}", e))?;
let resp: Response = resp_value.dyn_into()
.map_err(|e| format!("Response cast failed: {:?}", e))?;
let text = JsFuture::from(resp.text()
.map_err(|e| format!("Text extraction failed: {:?}", e))?)
.await
.map_err(|e| format!("Text promise failed: {:?}", e))?;
let text_string = text.as_string()
.ok_or("Response text is not a string")?;
if resp.ok() {
let response: UpdateEventResponseV2 = serde_json::from_str(&text_string)
.map_err(|e| format!("JSON parsing failed: {}", e))?;
Ok(response)
} else {
Err(format!("Request failed with status {}: {}", resp.status(), text_string))
}
}
/// Delete an event using V2 API (no string parsing required)
pub async fn delete_event_v2(
&self,
token: &str,
password: &str,
request: DeleteEventRequestV2,
) -> Result<DeleteEventResponseV2, String> {
let window = web_sys::window().ok_or("No global window exists")?;
let opts = RequestInit::new();
opts.set_method("POST");
opts.set_mode(RequestMode::Cors);
let body_string = serde_json::to_string(&request)
.map_err(|e| format!("JSON serialization failed: {}", e))?;
let url = format!("{}/v2/calendar/events/delete", self.base_url);
opts.set_body(&body_string.into());
let request_obj = Request::new_with_str_and_init(&url, &opts)
.map_err(|e| format!("Request creation failed: {:?}", e))?;
request_obj.headers().set("Authorization", &format!("Bearer {}", token))
.map_err(|e| format!("Authorization header setting failed: {:?}", e))?;
request_obj.headers().set("X-CalDAV-Password", password)
.map_err(|e| format!("Password header setting failed: {:?}", e))?;
request_obj.headers().set("Content-Type", "application/json")
.map_err(|e| format!("Content-Type header setting failed: {:?}", e))?;
let resp_value = JsFuture::from(window.fetch_with_request(&request_obj))
.await
.map_err(|e| format!("Network request failed: {:?}", e))?;
let resp: Response = resp_value.dyn_into()
.map_err(|e| format!("Response cast failed: {:?}", e))?;
let text = JsFuture::from(resp.text()
.map_err(|e| format!("Text extraction failed: {:?}", e))?)
.await
.map_err(|e| format!("Text promise failed: {:?}", e))?;
let text_string = text.as_string()
.ok_or("Response text is not a string")?;
if resp.ok() {
let response: DeleteEventResponseV2 = serde_json::from_str(&text_string)
.map_err(|e| format!("JSON parsing failed: {}", e))?;
Ok(response)
} else {
Err(format!("Request failed with status {}: {}", resp.status(), text_string))
}
}
/// Helper function to convert reminder string to structured alarms
pub fn reminder_string_to_alarms(reminder: &str) -> Vec<AlarmV2> {
match reminder.to_lowercase().as_str() {
"15min" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -15,
description: Some("Event reminder".to_string()),
}],
"30min" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -30,
description: Some("Event reminder".to_string()),
}],
"1hour" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -60,
description: Some("Event reminder".to_string()),
}],
"2hours" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -120,
description: Some("Event reminder".to_string()),
}],
"1day" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -1440,
description: Some("Event reminder".to_string()),
}],
"2days" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -2880,
description: Some("Event reminder".to_string()),
}],
"1week" => vec![AlarmV2 {
action: AlarmActionV2::Display,
trigger_minutes: -10080,
description: Some("Event reminder".to_string()),
}],
_ => Vec::new(),
}
}
/// Helper function to convert comma-separated attendees to structured attendees
pub fn attendees_string_to_structured(attendees: &str) -> Vec<AttendeeV2> {
if attendees.trim().is_empty() {
return Vec::new();
}
attendees.split(',')
.map(|email| AttendeeV2 {
email: email.trim().to_string(),
name: None,
role: Some(AttendeeRoleV2::Required),
status: Some(ParticipationStatusV2::NeedsAction),
rsvp: Some(true),
})
.collect()
}
}