Compare commits
2 Commits
b7b351416d
...
5b0e84121b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b0e84121b | ||
|
|
f6fa745775 |
@@ -25,5 +25,13 @@ chrono = { version = "0.4", features = ["serde"] }
|
|||||||
uuid = { version = "1.0", features = ["v4", "serde"] }
|
uuid = { version = "1.0", features = ["v4", "serde"] }
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
|
||||||
|
# CalDAV dependencies
|
||||||
|
reqwest = { version = "0.11", features = ["json"] }
|
||||||
|
ical = "0.7"
|
||||||
|
regex = "1.0"
|
||||||
|
dotenvy = "0.15"
|
||||||
|
base64 = "0.21"
|
||||||
|
thiserror = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.0", features = ["macros", "rt"] }
|
tokio = { version = "1.0", features = ["macros", "rt"] }
|
||||||
641
backend/src/calendar.rs
Normal file
641
backend/src/calendar.rs
Normal file
@@ -0,0 +1,641 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
/// Represents a calendar event with all its properties
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct CalendarEvent {
|
||||||
|
/// Unique identifier for the event (UID field in iCal)
|
||||||
|
pub uid: String,
|
||||||
|
|
||||||
|
/// Summary/title of the event
|
||||||
|
pub summary: Option<String>,
|
||||||
|
|
||||||
|
/// Detailed description of the event
|
||||||
|
pub description: Option<String>,
|
||||||
|
|
||||||
|
/// Start date and time of the event
|
||||||
|
pub start: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// End date and time of the event
|
||||||
|
pub end: Option<DateTime<Utc>>,
|
||||||
|
|
||||||
|
/// Location where the event takes place
|
||||||
|
pub location: Option<String>,
|
||||||
|
|
||||||
|
/// Event status (TENTATIVE, CONFIRMED, CANCELLED)
|
||||||
|
pub status: EventStatus,
|
||||||
|
|
||||||
|
/// Event classification (PUBLIC, PRIVATE, CONFIDENTIAL)
|
||||||
|
pub class: EventClass,
|
||||||
|
|
||||||
|
/// Event priority (0-9, where 0 is undefined, 1 is highest, 9 is lowest)
|
||||||
|
pub priority: Option<u8>,
|
||||||
|
|
||||||
|
/// Organizer of the event
|
||||||
|
pub organizer: Option<String>,
|
||||||
|
|
||||||
|
/// List of attendees
|
||||||
|
pub attendees: Vec<String>,
|
||||||
|
|
||||||
|
/// Categories/tags for the event
|
||||||
|
pub categories: Vec<String>,
|
||||||
|
|
||||||
|
/// Date and time when the event was created
|
||||||
|
pub created: Option<DateTime<Utc>>,
|
||||||
|
|
||||||
|
/// Date and time when the event was last modified
|
||||||
|
pub last_modified: Option<DateTime<Utc>>,
|
||||||
|
|
||||||
|
/// Recurrence rule (RRULE)
|
||||||
|
pub recurrence_rule: Option<String>,
|
||||||
|
|
||||||
|
/// All-day event flag
|
||||||
|
pub all_day: bool,
|
||||||
|
|
||||||
|
/// ETag from CalDAV server for conflict detection
|
||||||
|
pub etag: Option<String>,
|
||||||
|
|
||||||
|
/// URL/href of this event on the CalDAV server
|
||||||
|
pub href: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Event status enumeration
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub enum EventStatus {
|
||||||
|
Tentative,
|
||||||
|
Confirmed,
|
||||||
|
Cancelled,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EventStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
EventStatus::Confirmed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Event classification enumeration
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub enum EventClass {
|
||||||
|
Public,
|
||||||
|
Private,
|
||||||
|
Confidential,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EventClass {
|
||||||
|
fn default() -> Self {
|
||||||
|
EventClass::Public
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// CalDAV client for fetching and parsing calendar events
|
||||||
|
pub struct CalDAVClient {
|
||||||
|
config: crate::config::CalDAVConfig,
|
||||||
|
http_client: reqwest::Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CalDAVClient {
|
||||||
|
/// Create a new CalDAV client with the given configuration
|
||||||
|
pub fn new(config: crate::config::CalDAVConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
config,
|
||||||
|
http_client: reqwest::Client::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch calendar events from the CalDAV server
|
||||||
|
///
|
||||||
|
/// This method performs a REPORT request to get calendar data and parses
|
||||||
|
/// the returned iCalendar format into CalendarEvent structs.
|
||||||
|
pub async fn fetch_events(&self, calendar_path: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||||
|
// CalDAV REPORT request to get calendar events
|
||||||
|
let report_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||||
|
<d:prop>
|
||||||
|
<d:getetag/>
|
||||||
|
<c:calendar-data/>
|
||||||
|
</d:prop>
|
||||||
|
<c:filter>
|
||||||
|
<c:comp-filter name="VCALENDAR">
|
||||||
|
<c:comp-filter name="VEVENT"/>
|
||||||
|
</c:comp-filter>
|
||||||
|
</c:filter>
|
||||||
|
</c:calendar-query>"#;
|
||||||
|
|
||||||
|
let url = if calendar_path.starts_with("http") {
|
||||||
|
calendar_path.to_string()
|
||||||
|
} else {
|
||||||
|
format!("{}{}", self.config.server_url.trim_end_matches('/'), calendar_path)
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = self.http_client
|
||||||
|
.request(reqwest::Method::from_bytes(b"REPORT").unwrap(), &url)
|
||||||
|
.header("Authorization", format!("Basic {}", self.config.get_basic_auth()))
|
||||||
|
.header("Content-Type", "application/xml")
|
||||||
|
.header("Depth", "1")
|
||||||
|
.header("User-Agent", "calendar-app/0.1.0")
|
||||||
|
.body(report_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(CalDAVError::RequestError)?;
|
||||||
|
|
||||||
|
if !response.status().is_success() && response.status().as_u16() != 207 {
|
||||||
|
return Err(CalDAVError::ServerError(response.status().as_u16()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = response.text().await.map_err(CalDAVError::RequestError)?;
|
||||||
|
self.parse_calendar_response(&body)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse CalDAV XML response containing calendar data
|
||||||
|
fn parse_calendar_response(&self, xml_response: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||||
|
let mut events = Vec::new();
|
||||||
|
|
||||||
|
// Extract calendar data from XML response
|
||||||
|
// This is a simplified parser - in production, you'd want a proper XML parser
|
||||||
|
let calendar_data_sections = self.extract_calendar_data(xml_response);
|
||||||
|
|
||||||
|
for calendar_data in calendar_data_sections {
|
||||||
|
if let Ok(parsed_events) = self.parse_ical_data(&calendar_data.data) {
|
||||||
|
for mut event in parsed_events {
|
||||||
|
event.etag = calendar_data.etag.clone();
|
||||||
|
event.href = calendar_data.href.clone();
|
||||||
|
events.push(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(events)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract calendar data sections from CalDAV XML response
|
||||||
|
fn extract_calendar_data(&self, xml_response: &str) -> Vec<CalendarDataSection> {
|
||||||
|
let mut sections = Vec::new();
|
||||||
|
|
||||||
|
// Simple regex-based extraction (in production, use a proper XML parser)
|
||||||
|
// Look for <d:response> blocks containing calendar data
|
||||||
|
for response_block in xml_response.split("<d:response>").skip(1) {
|
||||||
|
if let Some(end_pos) = response_block.find("</d:response>") {
|
||||||
|
let response_content = &response_block[..end_pos];
|
||||||
|
|
||||||
|
let href = self.extract_xml_content(response_content, "href").unwrap_or_default();
|
||||||
|
let etag = self.extract_xml_content(response_content, "getetag").unwrap_or_default();
|
||||||
|
|
||||||
|
if let Some(calendar_data) = self.extract_xml_content(response_content, "cal:calendar-data") {
|
||||||
|
sections.push(CalendarDataSection {
|
||||||
|
href: if href.is_empty() { None } else { Some(href) },
|
||||||
|
etag: if etag.is_empty() { None } else { Some(etag) },
|
||||||
|
data: calendar_data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sections
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract content from XML tags (simple implementation)
|
||||||
|
fn extract_xml_content(&self, xml: &str, tag: &str) -> Option<String> {
|
||||||
|
// Handle both with and without namespace prefixes
|
||||||
|
let patterns = [
|
||||||
|
format!("(?s)<{}>(.*?)</{}>", tag, tag), // <tag>content</tag>
|
||||||
|
format!("(?s)<{}>(.*?)</.*:{}>", tag, tag.split(':').last().unwrap_or(tag)), // <tag>content</ns:tag>
|
||||||
|
format!("(?s)<.*:{}>(.*?)</{}>", tag.split(':').last().unwrap_or(tag), tag), // <ns:tag>content</tag>
|
||||||
|
format!("(?s)<.*:{}>(.*?)</.*:{}>", tag.split(':').last().unwrap_or(tag), tag.split(':').last().unwrap_or(tag)), // <ns:tag>content</ns:tag>
|
||||||
|
format!("(?s)<{}[^>]*>(.*?)</{}>", tag, tag), // <tag attr>content</tag>
|
||||||
|
format!("(?s)<{}[^>]*>(.*?)</.*:{}>", tag, tag.split(':').last().unwrap_or(tag)),
|
||||||
|
];
|
||||||
|
|
||||||
|
for pattern in &patterns {
|
||||||
|
if let Ok(re) = regex::Regex::new(pattern) {
|
||||||
|
if let Some(captures) = re.captures(xml) {
|
||||||
|
if let Some(content) = captures.get(1) {
|
||||||
|
return Some(content.as_str().trim().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse iCalendar data into CalendarEvent structs
|
||||||
|
fn parse_ical_data(&self, ical_data: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||||
|
let mut events = Vec::new();
|
||||||
|
|
||||||
|
// Parse the iCal data using the ical crate
|
||||||
|
let reader = ical::IcalParser::new(ical_data.as_bytes());
|
||||||
|
|
||||||
|
for calendar in reader {
|
||||||
|
let calendar = calendar.map_err(|e| CalDAVError::ParseError(e.to_string()))?;
|
||||||
|
|
||||||
|
for event in calendar.events {
|
||||||
|
if let Ok(calendar_event) = self.parse_ical_event(event) {
|
||||||
|
events.push(calendar_event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(events)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a single iCal event into a CalendarEvent struct
|
||||||
|
fn parse_ical_event(&self, event: ical::parser::ical::component::IcalEvent) -> Result<CalendarEvent, CalDAVError> {
|
||||||
|
let mut properties: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
|
// Extract all properties from the event
|
||||||
|
for property in event.properties {
|
||||||
|
properties.insert(property.name.to_uppercase(), property.value.unwrap_or_default());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Required UID field
|
||||||
|
let uid = properties.get("UID")
|
||||||
|
.ok_or_else(|| CalDAVError::ParseError("Missing UID field".to_string()))?
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
// Parse start time (required)
|
||||||
|
let start = properties.get("DTSTART")
|
||||||
|
.ok_or_else(|| CalDAVError::ParseError("Missing DTSTART field".to_string()))?;
|
||||||
|
let start = self.parse_datetime(start, properties.get("DTSTART"))?;
|
||||||
|
|
||||||
|
// Parse end time (optional - use start time if not present)
|
||||||
|
let end = if let Some(dtend) = properties.get("DTEND") {
|
||||||
|
Some(self.parse_datetime(dtend, properties.get("DTEND"))?)
|
||||||
|
} else if let Some(duration) = properties.get("DURATION") {
|
||||||
|
// TODO: Parse duration and add to start time
|
||||||
|
Some(start)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Determine if it's an all-day event
|
||||||
|
let all_day = properties.get("DTSTART")
|
||||||
|
.map(|s| !s.contains("T"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
// Parse status
|
||||||
|
let status = properties.get("STATUS")
|
||||||
|
.map(|s| match s.to_uppercase().as_str() {
|
||||||
|
"TENTATIVE" => EventStatus::Tentative,
|
||||||
|
"CANCELLED" => EventStatus::Cancelled,
|
||||||
|
_ => EventStatus::Confirmed,
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
// Parse classification
|
||||||
|
let class = properties.get("CLASS")
|
||||||
|
.map(|s| match s.to_uppercase().as_str() {
|
||||||
|
"PRIVATE" => EventClass::Private,
|
||||||
|
"CONFIDENTIAL" => EventClass::Confidential,
|
||||||
|
_ => EventClass::Public,
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
// Parse priority
|
||||||
|
let priority = properties.get("PRIORITY")
|
||||||
|
.and_then(|s| s.parse::<u8>().ok())
|
||||||
|
.filter(|&p| p <= 9);
|
||||||
|
|
||||||
|
// Parse categories
|
||||||
|
let categories = properties.get("CATEGORIES")
|
||||||
|
.map(|s| s.split(',').map(|c| c.trim().to_string()).collect())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
// Parse dates
|
||||||
|
let created = properties.get("CREATED")
|
||||||
|
.and_then(|s| self.parse_datetime(s, None).ok());
|
||||||
|
|
||||||
|
let last_modified = properties.get("LAST-MODIFIED")
|
||||||
|
.and_then(|s| self.parse_datetime(s, None).ok());
|
||||||
|
|
||||||
|
Ok(CalendarEvent {
|
||||||
|
uid,
|
||||||
|
summary: properties.get("SUMMARY").cloned(),
|
||||||
|
description: properties.get("DESCRIPTION").cloned(),
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
location: properties.get("LOCATION").cloned(),
|
||||||
|
status,
|
||||||
|
class,
|
||||||
|
priority,
|
||||||
|
organizer: properties.get("ORGANIZER").cloned(),
|
||||||
|
attendees: Vec::new(), // TODO: Parse attendees
|
||||||
|
categories,
|
||||||
|
created,
|
||||||
|
last_modified,
|
||||||
|
recurrence_rule: properties.get("RRULE").cloned(),
|
||||||
|
all_day,
|
||||||
|
etag: None, // Set by caller
|
||||||
|
href: None, // Set by caller
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Discover available calendar collections on the server
|
||||||
|
pub async fn discover_calendars(&self) -> Result<Vec<String>, CalDAVError> {
|
||||||
|
// First, try to discover user calendars if we have a calendar path in config
|
||||||
|
if let Some(calendar_path) = &self.config.calendar_path {
|
||||||
|
println!("Using configured calendar path: {}", calendar_path);
|
||||||
|
return Ok(vec![calendar_path.clone()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("No calendar path configured, discovering calendars...");
|
||||||
|
|
||||||
|
// Try different common CalDAV discovery paths
|
||||||
|
let user_calendar_path = format!("/calendars/{}/", self.config.username);
|
||||||
|
let user_dav_calendar_path = format!("/dav.php/calendars/{}/", self.config.username);
|
||||||
|
|
||||||
|
let discovery_paths = vec![
|
||||||
|
"/calendars/",
|
||||||
|
user_calendar_path.as_str(),
|
||||||
|
user_dav_calendar_path.as_str(),
|
||||||
|
"/dav.php/calendars/",
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut all_calendars = Vec::new();
|
||||||
|
|
||||||
|
for path in discovery_paths {
|
||||||
|
println!("Trying discovery path: {}", path);
|
||||||
|
if let Ok(calendars) = self.discover_calendars_at_path(&path).await {
|
||||||
|
println!("Found {} calendar(s) at {}", calendars.len(), path);
|
||||||
|
all_calendars.extend(calendars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove duplicates
|
||||||
|
all_calendars.sort();
|
||||||
|
all_calendars.dedup();
|
||||||
|
|
||||||
|
Ok(all_calendars)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Discover calendars at a specific path
|
||||||
|
async fn discover_calendars_at_path(&self, path: &str) -> Result<Vec<String>, CalDAVError> {
|
||||||
|
let propfind_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype />
|
||||||
|
<d:displayname />
|
||||||
|
<c:supported-calendar-component-set />
|
||||||
|
</d:prop>
|
||||||
|
</d:propfind>"#;
|
||||||
|
|
||||||
|
let url = format!("{}{}", self.config.server_url.trim_end_matches('/'), path);
|
||||||
|
|
||||||
|
let response = self.http_client
|
||||||
|
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), &url)
|
||||||
|
.header("Authorization", format!("Basic {}", self.config.get_basic_auth()))
|
||||||
|
.header("Content-Type", "application/xml")
|
||||||
|
.header("Depth", "2") // Deeper search to find actual calendars
|
||||||
|
.header("User-Agent", "calendar-app/0.1.0")
|
||||||
|
.body(propfind_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(CalDAVError::RequestError)?;
|
||||||
|
|
||||||
|
if response.status().as_u16() != 207 {
|
||||||
|
return Err(CalDAVError::ServerError(response.status().as_u16()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = response.text().await.map_err(CalDAVError::RequestError)?;
|
||||||
|
println!("Discovery response for {}: {}", path, body);
|
||||||
|
|
||||||
|
let mut calendar_paths = Vec::new();
|
||||||
|
|
||||||
|
// Extract calendar collection URLs from the response
|
||||||
|
for response_block in body.split("<d:response>").skip(1) {
|
||||||
|
if let Some(end_pos) = response_block.find("</d:response>") {
|
||||||
|
let response_content = &response_block[..end_pos];
|
||||||
|
|
||||||
|
// Look for actual calendar collections (not just containers)
|
||||||
|
if response_content.contains("<c:supported-calendar-component-set") ||
|
||||||
|
(response_content.contains("<d:collection/>") &&
|
||||||
|
response_content.contains("calendar")) {
|
||||||
|
if let Some(href) = self.extract_xml_content(response_content, "href") {
|
||||||
|
// Only include actual calendar paths, not container directories
|
||||||
|
if href.ends_with('/') && href.contains("calendar") && !href.ends_with("/calendars/") {
|
||||||
|
calendar_paths.push(href);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(calendar_paths)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse iCal datetime format
|
||||||
|
fn parse_datetime(&self, datetime_str: &str, _original_property: Option<&String>) -> Result<DateTime<Utc>, CalDAVError> {
|
||||||
|
use chrono::TimeZone;
|
||||||
|
|
||||||
|
// Handle different iCal datetime formats
|
||||||
|
let cleaned = datetime_str.replace("TZID=", "").trim().to_string();
|
||||||
|
|
||||||
|
// Try different parsing formats
|
||||||
|
let formats = [
|
||||||
|
"%Y%m%dT%H%M%SZ", // UTC format: 20231225T120000Z
|
||||||
|
"%Y%m%dT%H%M%S", // Local format: 20231225T120000
|
||||||
|
"%Y%m%d", // Date only: 20231225
|
||||||
|
];
|
||||||
|
|
||||||
|
for format in &formats {
|
||||||
|
if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(&cleaned, format) {
|
||||||
|
return Ok(Utc.from_utc_datetime(&dt));
|
||||||
|
}
|
||||||
|
if let Ok(date) = chrono::NaiveDate::parse_from_str(&cleaned, format) {
|
||||||
|
return Ok(Utc.from_utc_datetime(&date.and_hms_opt(0, 0, 0).unwrap()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(CalDAVError::ParseError(format!("Unable to parse datetime: {}", datetime_str)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper struct for extracting calendar data from XML responses
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct CalendarDataSection {
|
||||||
|
pub href: Option<String>,
|
||||||
|
pub etag: Option<String>,
|
||||||
|
pub data: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// CalDAV-specific error types
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum CalDAVError {
|
||||||
|
#[error("HTTP request failed: {0}")]
|
||||||
|
RequestError(#[from] reqwest::Error),
|
||||||
|
|
||||||
|
#[error("CalDAV server returned error: {0}")]
|
||||||
|
ServerError(u16),
|
||||||
|
|
||||||
|
#[error("Failed to parse calendar data: {0}")]
|
||||||
|
ParseError(String),
|
||||||
|
|
||||||
|
#[error("Configuration error: {0}")]
|
||||||
|
ConfigError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::config::CalDAVConfig;
|
||||||
|
|
||||||
|
/// Integration test that fetches real calendar events from the Baikal server
|
||||||
|
///
|
||||||
|
/// This test requires a valid .env file and a calendar with some events
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_fetch_calendar_events() {
|
||||||
|
let config = CalDAVConfig::from_env()
|
||||||
|
.expect("Failed to load CalDAV config from environment");
|
||||||
|
|
||||||
|
let client = CalDAVClient::new(config);
|
||||||
|
|
||||||
|
// First discover available calendars using PROPFIND
|
||||||
|
println!("Discovering calendars...");
|
||||||
|
let discovery_result = client.discover_calendars().await;
|
||||||
|
|
||||||
|
match discovery_result {
|
||||||
|
Ok(calendar_paths) => {
|
||||||
|
println!("Found {} calendar collection(s)", calendar_paths.len());
|
||||||
|
|
||||||
|
if calendar_paths.is_empty() {
|
||||||
|
println!("No calendars found - this might be normal for a new server");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try the first available calendar
|
||||||
|
let calendar_path = &calendar_paths[0];
|
||||||
|
println!("Trying to fetch events from: {}", calendar_path);
|
||||||
|
|
||||||
|
match client.fetch_events(calendar_path).await {
|
||||||
|
Ok(events) => {
|
||||||
|
println!("Successfully fetched {} calendar events", events.len());
|
||||||
|
|
||||||
|
for (i, event) in events.iter().take(3).enumerate() {
|
||||||
|
println!("\n--- Event {} ---", i + 1);
|
||||||
|
println!("UID: {}", event.uid);
|
||||||
|
println!("Summary: {:?}", event.summary);
|
||||||
|
println!("Start: {}", event.start);
|
||||||
|
println!("End: {:?}", event.end);
|
||||||
|
println!("All Day: {}", event.all_day);
|
||||||
|
println!("Status: {:?}", event.status);
|
||||||
|
println!("Location: {:?}", event.location);
|
||||||
|
println!("Description: {:?}", event.description);
|
||||||
|
println!("ETag: {:?}", event.etag);
|
||||||
|
println!("HREF: {:?}", event.href);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate that events have required fields
|
||||||
|
for event in &events {
|
||||||
|
assert!(!event.uid.is_empty(), "Event UID should not be empty");
|
||||||
|
// All events should have a start time
|
||||||
|
assert!(event.start > DateTime::from_timestamp(0, 0).unwrap(), "Event should have valid start time");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\n✓ Calendar event fetching test passed!");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Error fetching events from {}: {:?}", calendar_path, e);
|
||||||
|
println!("This might be normal if the calendar is empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Error discovering calendars: {:?}", e);
|
||||||
|
println!("This might be normal if no calendars are set up yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test parsing a sample iCal event
|
||||||
|
#[test]
|
||||||
|
fn test_parse_ical_event() {
|
||||||
|
let sample_ical = r#"BEGIN:VCALENDAR
|
||||||
|
VERSION:2.0
|
||||||
|
PRODID:-//Test//Test//EN
|
||||||
|
BEGIN:VEVENT
|
||||||
|
UID:test-event-123@example.com
|
||||||
|
DTSTART:20231225T120000Z
|
||||||
|
DTEND:20231225T130000Z
|
||||||
|
SUMMARY:Test Event
|
||||||
|
DESCRIPTION:This is a test event
|
||||||
|
LOCATION:Test Location
|
||||||
|
STATUS:CONFIRMED
|
||||||
|
CLASS:PUBLIC
|
||||||
|
PRIORITY:5
|
||||||
|
CREATED:20231220T100000Z
|
||||||
|
LAST-MODIFIED:20231221T150000Z
|
||||||
|
CATEGORIES:work,important
|
||||||
|
END:VEVENT
|
||||||
|
END:VCALENDAR"#;
|
||||||
|
|
||||||
|
let config = CalDAVConfig {
|
||||||
|
server_url: "https://example.com".to_string(),
|
||||||
|
username: "test".to_string(),
|
||||||
|
password: "test".to_string(),
|
||||||
|
calendar_path: None,
|
||||||
|
tasks_path: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = CalDAVClient::new(config);
|
||||||
|
let events = client.parse_ical_data(sample_ical)
|
||||||
|
.expect("Should be able to parse sample iCal data");
|
||||||
|
|
||||||
|
assert_eq!(events.len(), 1);
|
||||||
|
|
||||||
|
let event = &events[0];
|
||||||
|
assert_eq!(event.uid, "test-event-123@example.com");
|
||||||
|
assert_eq!(event.summary, Some("Test Event".to_string()));
|
||||||
|
assert_eq!(event.description, Some("This is a test event".to_string()));
|
||||||
|
assert_eq!(event.location, Some("Test Location".to_string()));
|
||||||
|
assert_eq!(event.status, EventStatus::Confirmed);
|
||||||
|
assert_eq!(event.class, EventClass::Public);
|
||||||
|
assert_eq!(event.priority, Some(5));
|
||||||
|
assert_eq!(event.categories, vec!["work", "important"]);
|
||||||
|
assert!(!event.all_day);
|
||||||
|
|
||||||
|
println!("✓ iCal parsing test passed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test datetime parsing
|
||||||
|
#[test]
|
||||||
|
fn test_datetime_parsing() {
|
||||||
|
let config = CalDAVConfig {
|
||||||
|
server_url: "https://example.com".to_string(),
|
||||||
|
username: "test".to_string(),
|
||||||
|
password: "test".to_string(),
|
||||||
|
calendar_path: None,
|
||||||
|
tasks_path: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = CalDAVClient::new(config);
|
||||||
|
|
||||||
|
// Test UTC format
|
||||||
|
let dt1 = client.parse_datetime("20231225T120000Z", None)
|
||||||
|
.expect("Should parse UTC datetime");
|
||||||
|
println!("Parsed UTC datetime: {}", dt1);
|
||||||
|
|
||||||
|
// Test date-only format (should be treated as all-day)
|
||||||
|
let dt2 = client.parse_datetime("20231225", None)
|
||||||
|
.expect("Should parse date-only");
|
||||||
|
println!("Parsed date-only: {}", dt2);
|
||||||
|
|
||||||
|
// Test local format
|
||||||
|
let dt3 = client.parse_datetime("20231225T120000", None)
|
||||||
|
.expect("Should parse local datetime");
|
||||||
|
println!("Parsed local datetime: {}", dt3);
|
||||||
|
|
||||||
|
println!("✓ Datetime parsing test passed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test event status parsing
|
||||||
|
#[test]
|
||||||
|
fn test_event_enums() {
|
||||||
|
// Test status parsing
|
||||||
|
assert_eq!(EventStatus::default(), EventStatus::Confirmed);
|
||||||
|
|
||||||
|
// Test class parsing
|
||||||
|
assert_eq!(EventClass::default(), EventClass::Public);
|
||||||
|
|
||||||
|
println!("✓ Event enum tests passed!");
|
||||||
|
}
|
||||||
|
}
|
||||||
284
backend/src/config.rs
Normal file
284
backend/src/config.rs
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::env;
|
||||||
|
use base64::prelude::*;
|
||||||
|
|
||||||
|
/// Configuration for CalDAV server connection and authentication.
|
||||||
|
///
|
||||||
|
/// This struct holds all the necessary information to connect to a CalDAV server,
|
||||||
|
/// including server URL, credentials, and optional collection paths.
|
||||||
|
///
|
||||||
|
/// # Security Note
|
||||||
|
///
|
||||||
|
/// The password field contains sensitive information and should be handled carefully.
|
||||||
|
/// This struct implements `Debug` but in production, consider implementing a custom
|
||||||
|
/// `Debug` that masks the password field.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use crate::config::CalDAVConfig;
|
||||||
|
///
|
||||||
|
/// // Load configuration from environment variables
|
||||||
|
/// let config = CalDAVConfig::from_env()?;
|
||||||
|
///
|
||||||
|
/// // Use the configuration for HTTP requests
|
||||||
|
/// let auth_header = format!("Basic {}", config.get_basic_auth());
|
||||||
|
/// ```
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CalDAVConfig {
|
||||||
|
/// The base URL of the CalDAV server (e.g., "https://caldav.example.com/dav/")
|
||||||
|
pub server_url: String,
|
||||||
|
|
||||||
|
/// Username for authentication with the CalDAV server
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// Password for authentication with the CalDAV server
|
||||||
|
///
|
||||||
|
/// **Security Note**: This contains sensitive information
|
||||||
|
pub password: String,
|
||||||
|
|
||||||
|
/// Optional path to the calendar collection on the server
|
||||||
|
///
|
||||||
|
/// If not provided, the client will need to discover available calendars
|
||||||
|
/// through CalDAV PROPFIND requests
|
||||||
|
pub calendar_path: Option<String>,
|
||||||
|
|
||||||
|
/// Optional path to the tasks/todo collection on the server
|
||||||
|
///
|
||||||
|
/// Some CalDAV servers store tasks separately from calendar events
|
||||||
|
pub tasks_path: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CalDAVConfig {
|
||||||
|
/// Creates a new CalDAVConfig by loading values from environment variables.
|
||||||
|
///
|
||||||
|
/// This method will attempt to load a `.env` file from the current directory
|
||||||
|
/// and then read the following required environment variables:
|
||||||
|
///
|
||||||
|
/// - `CALDAV_SERVER_URL`: The CalDAV server base URL
|
||||||
|
/// - `CALDAV_USERNAME`: Username for authentication
|
||||||
|
/// - `CALDAV_PASSWORD`: Password for authentication
|
||||||
|
///
|
||||||
|
/// Optional environment variables:
|
||||||
|
///
|
||||||
|
/// - `CALDAV_CALENDAR_PATH`: Path to calendar collection
|
||||||
|
/// - `CALDAV_TASKS_PATH`: Path to tasks collection
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns `ConfigError::MissingVar` if any required environment variable
|
||||||
|
/// is not set or cannot be read.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use crate::config::CalDAVConfig;
|
||||||
|
///
|
||||||
|
/// match CalDAVConfig::from_env() {
|
||||||
|
/// Ok(config) => {
|
||||||
|
/// println!("Loaded config for server: {}", config.server_url);
|
||||||
|
/// }
|
||||||
|
/// Err(e) => {
|
||||||
|
/// eprintln!("Failed to load config: {}", e);
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn from_env() -> Result<Self, ConfigError> {
|
||||||
|
// Attempt to load .env file, but don't fail if it doesn't exist
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
|
let server_url = env::var("CALDAV_SERVER_URL")
|
||||||
|
.map_err(|_| ConfigError::MissingVar("CALDAV_SERVER_URL".to_string()))?;
|
||||||
|
|
||||||
|
let username = env::var("CALDAV_USERNAME")
|
||||||
|
.map_err(|_| ConfigError::MissingVar("CALDAV_USERNAME".to_string()))?;
|
||||||
|
|
||||||
|
let password = env::var("CALDAV_PASSWORD")
|
||||||
|
.map_err(|_| ConfigError::MissingVar("CALDAV_PASSWORD".to_string()))?;
|
||||||
|
|
||||||
|
// Optional paths - it's fine if these are not set
|
||||||
|
let calendar_path = env::var("CALDAV_CALENDAR_PATH").ok();
|
||||||
|
let tasks_path = env::var("CALDAV_TASKS_PATH").ok();
|
||||||
|
|
||||||
|
Ok(CalDAVConfig {
|
||||||
|
server_url,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
calendar_path,
|
||||||
|
tasks_path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates a Base64-encoded string for HTTP Basic Authentication.
|
||||||
|
///
|
||||||
|
/// This method combines the username and password in the format
|
||||||
|
/// `username:password` and encodes it using Base64, which is the
|
||||||
|
/// standard format for the `Authorization: Basic` HTTP header.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A Base64-encoded string that can be used directly in the
|
||||||
|
/// `Authorization` header: `Authorization: Basic <returned_value>`
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use crate::config::CalDAVConfig;
|
||||||
|
///
|
||||||
|
/// let config = CalDAVConfig {
|
||||||
|
/// server_url: "https://example.com".to_string(),
|
||||||
|
/// username: "user".to_string(),
|
||||||
|
/// password: "pass".to_string(),
|
||||||
|
/// calendar_path: None,
|
||||||
|
/// tasks_path: None,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// let auth_value = config.get_basic_auth();
|
||||||
|
/// let auth_header = format!("Basic {}", auth_value);
|
||||||
|
/// ```
|
||||||
|
pub fn get_basic_auth(&self) -> String {
|
||||||
|
let credentials = format!("{}:{}", self.username, self.password);
|
||||||
|
BASE64_STANDARD.encode(&credentials)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Errors that can occur when loading or using CalDAV configuration.
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum ConfigError {
|
||||||
|
/// A required environment variable is missing or cannot be read.
|
||||||
|
///
|
||||||
|
/// This error occurs when calling `CalDAVConfig::from_env()` and one of the
|
||||||
|
/// required environment variables (`CALDAV_SERVER_URL`, `CALDAV_USERNAME`,
|
||||||
|
/// or `CALDAV_PASSWORD`) is not set.
|
||||||
|
#[error("Missing environment variable: {0}")]
|
||||||
|
MissingVar(String),
|
||||||
|
|
||||||
|
/// The configuration contains invalid or malformed values.
|
||||||
|
///
|
||||||
|
/// This could include malformed URLs, invalid authentication credentials,
|
||||||
|
/// or other configuration issues that prevent proper CalDAV operation.
|
||||||
|
#[error("Invalid configuration: {0}")]
|
||||||
|
Invalid(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic_auth_encoding() {
|
||||||
|
let config = CalDAVConfig {
|
||||||
|
server_url: "https://example.com".to_string(),
|
||||||
|
username: "testuser".to_string(),
|
||||||
|
password: "testpass".to_string(),
|
||||||
|
calendar_path: None,
|
||||||
|
tasks_path: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let auth = config.get_basic_auth();
|
||||||
|
let expected = BASE64_STANDARD.encode("testuser:testpass");
|
||||||
|
assert_eq!(auth, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Integration test that authenticates with the actual Baikal CalDAV server
|
||||||
|
///
|
||||||
|
/// This test requires a valid .env file with:
|
||||||
|
/// - CALDAV_SERVER_URL
|
||||||
|
/// - CALDAV_USERNAME
|
||||||
|
/// - CALDAV_PASSWORD
|
||||||
|
///
|
||||||
|
/// Run with: `cargo test test_baikal_auth`
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_baikal_auth() {
|
||||||
|
// Load config from .env
|
||||||
|
let config = CalDAVConfig::from_env()
|
||||||
|
.expect("Failed to load CalDAV config from environment");
|
||||||
|
|
||||||
|
println!("Testing authentication to: {}", config.server_url);
|
||||||
|
|
||||||
|
// Create HTTP client
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
// Make a simple OPTIONS request to test authentication
|
||||||
|
let response = client
|
||||||
|
.request(reqwest::Method::OPTIONS, &config.server_url)
|
||||||
|
.header("Authorization", format!("Basic {}", config.get_basic_auth()))
|
||||||
|
.header("User-Agent", "calendar-app/0.1.0")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to send request to CalDAV server");
|
||||||
|
|
||||||
|
println!("Response status: {}", response.status());
|
||||||
|
println!("Response headers: {:#?}", response.headers());
|
||||||
|
|
||||||
|
// Check if we got a successful response or at least not a 401 Unauthorized
|
||||||
|
assert!(
|
||||||
|
response.status().is_success() || response.status() != 401,
|
||||||
|
"Authentication failed with status: {}. Check your credentials in .env",
|
||||||
|
response.status()
|
||||||
|
);
|
||||||
|
|
||||||
|
// For Baikal/CalDAV servers, we should see DAV headers
|
||||||
|
assert!(
|
||||||
|
response.headers().contains_key("dav") ||
|
||||||
|
response.headers().contains_key("DAV") ||
|
||||||
|
response.status().is_success(),
|
||||||
|
"Server doesn't appear to be a CalDAV server - missing DAV headers"
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("✓ Authentication test passed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test making a PROPFIND request to discover calendars
|
||||||
|
///
|
||||||
|
/// This test requires a valid .env file and makes an actual CalDAV PROPFIND request
|
||||||
|
///
|
||||||
|
/// Run with: `cargo test test_propfind_calendars`
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_propfind_calendars() {
|
||||||
|
let config = CalDAVConfig::from_env()
|
||||||
|
.expect("Failed to load CalDAV config from environment");
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
// CalDAV PROPFIND request to discover calendars
|
||||||
|
let propfind_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype />
|
||||||
|
<d:displayname />
|
||||||
|
<c:calendar-description />
|
||||||
|
<c:supported-calendar-component-set />
|
||||||
|
</d:prop>
|
||||||
|
</d:propfind>"#;
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), &config.server_url)
|
||||||
|
.header("Authorization", format!("Basic {}", config.get_basic_auth()))
|
||||||
|
.header("Content-Type", "application/xml")
|
||||||
|
.header("Depth", "1")
|
||||||
|
.header("User-Agent", "calendar-app/0.1.0")
|
||||||
|
.body(propfind_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to send PROPFIND request");
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
println!("PROPFIND Response status: {}", status);
|
||||||
|
|
||||||
|
let body = response.text().await.expect("Failed to read response body");
|
||||||
|
println!("PROPFIND Response body: {}", body);
|
||||||
|
|
||||||
|
// We should get a 207 Multi-Status for PROPFIND
|
||||||
|
assert_eq!(
|
||||||
|
status,
|
||||||
|
reqwest::StatusCode::from_u16(207).unwrap(),
|
||||||
|
"PROPFIND should return 207 Multi-Status"
|
||||||
|
);
|
||||||
|
|
||||||
|
// The response should contain XML with calendar information
|
||||||
|
assert!(body.contains("calendar"), "Response should contain calendar information");
|
||||||
|
|
||||||
|
println!("✓ PROPFIND calendars test passed!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,77 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::State,
|
extract::{State, Query},
|
||||||
http::HeaderMap,
|
http::HeaderMap,
|
||||||
response::Json,
|
response::Json,
|
||||||
};
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use chrono::Datelike;
|
||||||
|
|
||||||
use crate::{AppState, models::{LoginRequest, RegisterRequest, AuthResponse, ApiError}};
|
use crate::{AppState, models::{LoginRequest, RegisterRequest, AuthResponse, ApiError}};
|
||||||
|
use crate::calendar::{CalDAVClient, CalendarEvent};
|
||||||
|
use crate::config::CalDAVConfig;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CalendarQuery {
|
||||||
|
pub year: Option<i32>,
|
||||||
|
pub month: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_calendar_events(
|
||||||
|
State(_state): State<Arc<AppState>>,
|
||||||
|
Query(params): Query<CalendarQuery>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
) -> Result<Json<Vec<CalendarEvent>>, ApiError> {
|
||||||
|
// Verify authentication (extract token from Authorization header)
|
||||||
|
let _token = if let Some(auth_header) = headers.get("authorization") {
|
||||||
|
let auth_str = auth_header
|
||||||
|
.to_str()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid authorization header".to_string()))?;
|
||||||
|
|
||||||
|
if auth_str.starts_with("Bearer ") {
|
||||||
|
auth_str.strip_prefix("Bearer ").unwrap().to_string()
|
||||||
|
} else {
|
||||||
|
return Err(ApiError::Unauthorized("Invalid authorization format".to_string()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ApiError::Unauthorized("Missing authorization header".to_string()));
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Validate JWT token here
|
||||||
|
|
||||||
|
// Load CalDAV configuration
|
||||||
|
let config = CalDAVConfig::from_env()
|
||||||
|
.map_err(|e| ApiError::Internal(format!("Failed to load CalDAV config: {}", e)))?;
|
||||||
|
|
||||||
|
let client = CalDAVClient::new(config);
|
||||||
|
|
||||||
|
// Discover calendars if needed
|
||||||
|
let calendar_paths = client.discover_calendars()
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiError::Internal(format!("Failed to discover calendars: {}", e)))?;
|
||||||
|
|
||||||
|
if calendar_paths.is_empty() {
|
||||||
|
return Ok(Json(vec![])); // No calendars found
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch events from the first calendar
|
||||||
|
let calendar_path = &calendar_paths[0];
|
||||||
|
let events = client.fetch_events(calendar_path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiError::Internal(format!("Failed to fetch events: {}", e)))?;
|
||||||
|
|
||||||
|
// Filter events by month if specified
|
||||||
|
let filtered_events = if let (Some(year), Some(month)) = (params.year, params.month) {
|
||||||
|
events.into_iter().filter(|event| {
|
||||||
|
let event_date = event.start.date_naive();
|
||||||
|
event_date.year() == year && event_date.month() == month
|
||||||
|
}).collect()
|
||||||
|
} else {
|
||||||
|
events
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Json(filtered_events))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn register(
|
pub async fn register(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ use std::sync::Arc;
|
|||||||
mod auth;
|
mod auth;
|
||||||
mod models;
|
mod models;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
mod calendar;
|
||||||
|
mod config;
|
||||||
|
|
||||||
use auth::AuthService;
|
use auth::AuthService;
|
||||||
|
|
||||||
@@ -50,6 +52,7 @@ pub async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.route("/api/auth/register", post(handlers::register))
|
.route("/api/auth/register", post(handlers::register))
|
||||||
.route("/api/auth/login", post(handlers::login))
|
.route("/api/auth/login", post(handlers::login))
|
||||||
.route("/api/auth/verify", get(handlers::verify_token))
|
.route("/api/auth/verify", get(handlers::verify_token))
|
||||||
|
.route("/api/calendar/events", get(handlers::get_calendar_events))
|
||||||
.layer(
|
.layer(
|
||||||
CorsLayer::new()
|
CorsLayer::new()
|
||||||
.allow_origin(Any)
|
.allow_origin(Any)
|
||||||
|
|||||||
239
index.html
239
index.html
@@ -183,58 +183,235 @@
|
|||||||
|
|
||||||
/* Calendar View */
|
/* Calendar View */
|
||||||
.calendar-view {
|
.calendar-view {
|
||||||
|
height: calc(100vh - 140px); /* Full height minus header and padding */
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-loading, .calendar-error {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100%;
|
||||||
background: white;
|
background: white;
|
||||||
padding: 2rem;
|
border-radius: 12px;
|
||||||
border-radius: 8px;
|
box-shadow: 0 4px 16px rgba(0,0,0,0.1);
|
||||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.calendar-view h2 {
|
.calendar-loading p {
|
||||||
color: #333;
|
font-size: 1.2rem;
|
||||||
margin-bottom: 1rem;
|
color: #666;
|
||||||
}
|
}
|
||||||
|
|
||||||
.demo-section {
|
.calendar-error p {
|
||||||
margin: 2rem 0;
|
font-size: 1.2rem;
|
||||||
padding: 1rem;
|
color: #d32f2f;
|
||||||
background: #f8f9fa;
|
|
||||||
border-radius: 4px;
|
|
||||||
border-left: 4px solid #667eea;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.demo-section h3 {
|
/* Calendar Component */
|
||||||
margin-bottom: 1rem;
|
.calendar {
|
||||||
color: #333;
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 4px 16px rgba(0,0,0,0.1);
|
||||||
|
overflow: hidden;
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.demo-section button {
|
.calendar-header {
|
||||||
background-color: #007bff;
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 1.5rem 2rem;
|
||||||
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
color: white;
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.month-year {
|
||||||
|
font-size: 1.8rem;
|
||||||
|
font-weight: 600;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-button {
|
||||||
|
background: rgba(255,255,255,0.2);
|
||||||
border: none;
|
border: none;
|
||||||
padding: 0.5rem 1rem;
|
color: white;
|
||||||
border-radius: 4px;
|
font-size: 1.5rem;
|
||||||
|
font-weight: bold;
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border-radius: 50%;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
margin-right: 1rem;
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
transition: background-color 0.2s;
|
||||||
}
|
}
|
||||||
|
|
||||||
.demo-section button:hover {
|
.nav-button:hover {
|
||||||
background-color: #0056b3;
|
background: rgba(255,255,255,0.3);
|
||||||
}
|
}
|
||||||
|
|
||||||
.calendar-placeholder {
|
.calendar-grid {
|
||||||
margin-top: 2rem;
|
display: grid;
|
||||||
|
grid-template-columns: repeat(7, 1fr);
|
||||||
|
flex: 1;
|
||||||
|
background: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weekday-header {
|
||||||
|
background: #f8f9fa;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
background: #e9ecef;
|
text-align: center;
|
||||||
border-radius: 4px;
|
font-weight: 600;
|
||||||
|
color: #666;
|
||||||
|
border-bottom: 1px solid #e9ecef;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.calendar-placeholder ul {
|
.calendar-day {
|
||||||
margin: 1rem 0;
|
border: 1px solid #f0f0f0;
|
||||||
padding-left: 2rem;
|
padding: 0.75rem;
|
||||||
|
min-height: 100px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
.calendar-placeholder li {
|
.calendar-day:hover {
|
||||||
margin: 0.5rem 0;
|
background-color: #f8f9ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.current-month {
|
||||||
|
background: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.prev-month,
|
||||||
|
.calendar-day.next-month {
|
||||||
|
background: #fafafa;
|
||||||
|
color: #ccc;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.today {
|
||||||
|
background: #e3f2fd;
|
||||||
|
border: 2px solid #2196f3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.has-events {
|
||||||
|
background: #fff3e0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.today.has-events {
|
||||||
|
background: #e1f5fe;
|
||||||
|
}
|
||||||
|
|
||||||
|
.day-number {
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day.today .day-number {
|
||||||
|
color: #1976d2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.event-indicators {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.event-box {
|
||||||
|
background: #2196f3;
|
||||||
|
color: white;
|
||||||
|
padding: 2px 4px;
|
||||||
|
border-radius: 3px;
|
||||||
|
font-size: 0.7rem;
|
||||||
|
line-height: 1.2;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.event-box:hover {
|
||||||
|
background: #1976d2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.event-dot {
|
||||||
|
background: #ff9800;
|
||||||
|
height: 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
margin-bottom: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-events {
|
||||||
|
font-size: 0.7rem;
|
||||||
|
color: #666;
|
||||||
|
margin-top: 2px;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Responsive Design */
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
.calendar-header {
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.month-year {
|
||||||
|
font-size: 1.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-button {
|
||||||
|
width: 35px;
|
||||||
|
height: 35px;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weekday-header {
|
||||||
|
padding: 0.5rem;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-day {
|
||||||
|
min-height: 70px;
|
||||||
|
padding: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.day-number {
|
||||||
|
font-size: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-main {
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar-view {
|
||||||
|
height: calc(100vh - 120px);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
.calendar-day {
|
||||||
|
min-height: 60px;
|
||||||
|
padding: 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weekday-header {
|
||||||
|
padding: 0.5rem 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.day-number {
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 768px) {
|
||||||
|
|||||||
94
src/app.rs
94
src/app.rs
@@ -1,7 +1,10 @@
|
|||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew_router::prelude::*;
|
use yew_router::prelude::*;
|
||||||
use gloo_storage::{LocalStorage, Storage};
|
use gloo_storage::{LocalStorage, Storage};
|
||||||
use crate::components::{Login, Register};
|
use crate::components::{Login, Register, Calendar};
|
||||||
|
use crate::services::CalendarService;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use chrono::{Local, NaiveDate, Datelike};
|
||||||
|
|
||||||
#[derive(Clone, Routable, PartialEq)]
|
#[derive(Clone, Routable, PartialEq)]
|
||||||
enum Route {
|
enum Route {
|
||||||
@@ -104,37 +107,76 @@ pub fn App() -> Html {
|
|||||||
|
|
||||||
#[function_component]
|
#[function_component]
|
||||||
fn CalendarView() -> Html {
|
fn CalendarView() -> Html {
|
||||||
let counter = use_state(|| 0);
|
let events = use_state(|| HashMap::<NaiveDate, Vec<String>>::new());
|
||||||
let onclick = {
|
let loading = use_state(|| true);
|
||||||
let counter = counter.clone();
|
let error = use_state(|| None::<String>);
|
||||||
move |_| {
|
|
||||||
let value = *counter + 1;
|
// Get current auth token
|
||||||
counter.set(value);
|
let auth_token: Option<String> = LocalStorage::get("auth_token").ok();
|
||||||
|
|
||||||
|
let today = Local::now().date_naive();
|
||||||
|
let current_year = today.year();
|
||||||
|
let current_month = today.month();
|
||||||
|
|
||||||
|
// Fetch events when component mounts
|
||||||
|
{
|
||||||
|
let events = events.clone();
|
||||||
|
let loading = loading.clone();
|
||||||
|
let error = error.clone();
|
||||||
|
let auth_token = auth_token.clone();
|
||||||
|
|
||||||
|
use_effect_with((), move |_| {
|
||||||
|
if let Some(token) = auth_token {
|
||||||
|
let events = events.clone();
|
||||||
|
let loading = loading.clone();
|
||||||
|
let error = error.clone();
|
||||||
|
|
||||||
|
wasm_bindgen_futures::spawn_local(async move {
|
||||||
|
let calendar_service = CalendarService::new();
|
||||||
|
|
||||||
|
match calendar_service.fetch_events_for_month(&token, current_year, current_month).await {
|
||||||
|
Ok(calendar_events) => {
|
||||||
|
let grouped_events = CalendarService::group_events_by_date(calendar_events);
|
||||||
|
events.set(grouped_events);
|
||||||
|
loading.set(false);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error.set(Some(format!("Failed to load events: {}", err)));
|
||||||
|
loading.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
loading.set(false);
|
||||||
|
error.set(Some("No authentication token found".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|| ()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
<div class="calendar-view">
|
<div class="calendar-view">
|
||||||
<h2>{"Welcome to your Calendar!"}</h2>
|
{
|
||||||
<p>{"You are now authenticated and can access your calendar."}</p>
|
if *loading {
|
||||||
|
html! {
|
||||||
// Temporary counter demo - will be replaced with calendar functionality
|
<div class="calendar-loading">
|
||||||
<div class="demo-section">
|
<p>{"Loading calendar events..."}</p>
|
||||||
<h3>{"Demo Counter"}</h3>
|
|
||||||
<button {onclick}>{ "Click me!" }</button>
|
|
||||||
<p>{ format!("Counter: {}", *counter) }</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
<div class="calendar-placeholder">
|
} else if let Some(err) = (*error).clone() {
|
||||||
<p>{"Calendar functionality will be implemented here."}</p>
|
html! {
|
||||||
<p>{"This will include:"}</p>
|
<div class="calendar-error">
|
||||||
<ul>
|
<p>{format!("Error: {}", err)}</p>
|
||||||
<li>{"Calendar view with events"}</li>
|
<Calendar events={HashMap::new()} />
|
||||||
<li>{"Integration with CalDAV server"}</li>
|
|
||||||
<li>{"Event creation and editing"}</li>
|
|
||||||
<li>{"Synchronization with Baikal server"}</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
html! {
|
||||||
|
<Calendar events={(*events).clone()} />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
196
src/components/calendar.rs
Normal file
196
src/components/calendar.rs
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
use yew::prelude::*;
|
||||||
|
use chrono::{Datelike, Local, NaiveDate, Duration, Weekday};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Properties, PartialEq)]
|
||||||
|
pub struct CalendarProps {
|
||||||
|
#[prop_or_default]
|
||||||
|
pub events: HashMap<NaiveDate, Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[function_component]
|
||||||
|
pub fn Calendar(props: &CalendarProps) -> Html {
|
||||||
|
let today = Local::now().date_naive();
|
||||||
|
let current_month = use_state(|| today);
|
||||||
|
|
||||||
|
let first_day_of_month = current_month.with_day(1).unwrap();
|
||||||
|
let days_in_month = get_days_in_month(*current_month);
|
||||||
|
let first_weekday = first_day_of_month.weekday();
|
||||||
|
let days_from_prev_month = get_days_from_previous_month(*current_month, first_weekday);
|
||||||
|
|
||||||
|
let prev_month = {
|
||||||
|
let current_month = current_month.clone();
|
||||||
|
Callback::from(move |_| {
|
||||||
|
let prev = *current_month - Duration::days(1);
|
||||||
|
let first_of_prev = prev.with_day(1).unwrap();
|
||||||
|
current_month.set(first_of_prev);
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let next_month = {
|
||||||
|
let current_month = current_month.clone();
|
||||||
|
Callback::from(move |_| {
|
||||||
|
let next = if current_month.month() == 12 {
|
||||||
|
NaiveDate::from_ymd_opt(current_month.year() + 1, 1, 1).unwrap()
|
||||||
|
} else {
|
||||||
|
NaiveDate::from_ymd_opt(current_month.year(), current_month.month() + 1, 1).unwrap()
|
||||||
|
};
|
||||||
|
current_month.set(next);
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
html! {
|
||||||
|
<div class="calendar">
|
||||||
|
<div class="calendar-header">
|
||||||
|
<button class="nav-button" onclick={prev_month}>{"‹"}</button>
|
||||||
|
<h2 class="month-year">{format!("{} {}", get_month_name(current_month.month()), current_month.year())}</h2>
|
||||||
|
<button class="nav-button" onclick={next_month}>{"›"}</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="calendar-grid">
|
||||||
|
// Weekday headers
|
||||||
|
<div class="weekday-header">{"Sun"}</div>
|
||||||
|
<div class="weekday-header">{"Mon"}</div>
|
||||||
|
<div class="weekday-header">{"Tue"}</div>
|
||||||
|
<div class="weekday-header">{"Wed"}</div>
|
||||||
|
<div class="weekday-header">{"Thu"}</div>
|
||||||
|
<div class="weekday-header">{"Fri"}</div>
|
||||||
|
<div class="weekday-header">{"Sat"}</div>
|
||||||
|
|
||||||
|
// Days from previous month (grayed out)
|
||||||
|
{
|
||||||
|
days_from_prev_month.iter().map(|day| {
|
||||||
|
html! {
|
||||||
|
<div class="calendar-day prev-month">{*day}</div>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Days of current month
|
||||||
|
{
|
||||||
|
(1..=days_in_month).map(|day| {
|
||||||
|
let date = current_month.with_day(day).unwrap();
|
||||||
|
let is_today = date == today;
|
||||||
|
let events = props.events.get(&date).cloned().unwrap_or_default();
|
||||||
|
|
||||||
|
let mut classes = vec!["calendar-day", "current-month"];
|
||||||
|
if is_today {
|
||||||
|
classes.push("today");
|
||||||
|
}
|
||||||
|
if !events.is_empty() {
|
||||||
|
classes.push("has-events");
|
||||||
|
}
|
||||||
|
|
||||||
|
html! {
|
||||||
|
<div class={classes!(classes)}>
|
||||||
|
<div class="day-number">{day}</div>
|
||||||
|
{
|
||||||
|
if !events.is_empty() {
|
||||||
|
html! {
|
||||||
|
<div class="event-indicators">
|
||||||
|
{
|
||||||
|
events.iter().take(2).map(|event| {
|
||||||
|
html! {
|
||||||
|
<div class="event-box" title={event.clone()}>
|
||||||
|
{
|
||||||
|
if event.len() > 15 {
|
||||||
|
format!("{}...", &event[..12])
|
||||||
|
} else {
|
||||||
|
event.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()
|
||||||
|
}
|
||||||
|
{
|
||||||
|
if events.len() > 2 {
|
||||||
|
html! { <div class="more-events">{format!("+{} more", events.len() - 2)}</div> }
|
||||||
|
} else {
|
||||||
|
html! {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
html! {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()
|
||||||
|
}
|
||||||
|
|
||||||
|
{ render_next_month_days(days_from_prev_month.len(), days_in_month) }
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_next_month_days(prev_days_count: usize, current_days_count: u32) -> Html {
|
||||||
|
let total_slots = 42; // 6 rows x 7 days
|
||||||
|
let used_slots = prev_days_count + current_days_count as usize;
|
||||||
|
let remaining_slots = if used_slots < total_slots { total_slots - used_slots } else { 0 };
|
||||||
|
|
||||||
|
(1..=remaining_slots).map(|day| {
|
||||||
|
html! {
|
||||||
|
<div class="calendar-day next-month">{day}</div>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_days_in_month(date: NaiveDate) -> u32 {
|
||||||
|
NaiveDate::from_ymd_opt(
|
||||||
|
if date.month() == 12 { date.year() + 1 } else { date.year() },
|
||||||
|
if date.month() == 12 { 1 } else { date.month() + 1 },
|
||||||
|
1
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.pred_opt()
|
||||||
|
.unwrap()
|
||||||
|
.day()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_days_from_previous_month(current_month: NaiveDate, first_weekday: Weekday) -> Vec<u32> {
|
||||||
|
let days_before = match first_weekday {
|
||||||
|
Weekday::Sun => 0,
|
||||||
|
Weekday::Mon => 1,
|
||||||
|
Weekday::Tue => 2,
|
||||||
|
Weekday::Wed => 3,
|
||||||
|
Weekday::Thu => 4,
|
||||||
|
Weekday::Fri => 5,
|
||||||
|
Weekday::Sat => 6,
|
||||||
|
};
|
||||||
|
|
||||||
|
if days_before == 0 {
|
||||||
|
vec![]
|
||||||
|
} else {
|
||||||
|
// Calculate the previous month
|
||||||
|
let prev_month = if current_month.month() == 1 {
|
||||||
|
NaiveDate::from_ymd_opt(current_month.year() - 1, 12, 1).unwrap()
|
||||||
|
} else {
|
||||||
|
NaiveDate::from_ymd_opt(current_month.year(), current_month.month() - 1, 1).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let prev_month_days = get_days_in_month(prev_month);
|
||||||
|
((prev_month_days - days_before as u32 + 1)..=prev_month_days).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_month_name(month: u32) -> &'static str {
|
||||||
|
match month {
|
||||||
|
1 => "January",
|
||||||
|
2 => "February",
|
||||||
|
3 => "March",
|
||||||
|
4 => "April",
|
||||||
|
5 => "May",
|
||||||
|
6 => "June",
|
||||||
|
7 => "July",
|
||||||
|
8 => "August",
|
||||||
|
9 => "September",
|
||||||
|
10 => "October",
|
||||||
|
11 => "November",
|
||||||
|
12 => "December",
|
||||||
|
_ => "Invalid"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
|
pub mod calendar;
|
||||||
|
|
||||||
pub use login::Login;
|
pub use login::Login;
|
||||||
pub use register::Register;
|
pub use register::Register;
|
||||||
|
pub use calendar::Calendar;
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
mod app;
|
mod app;
|
||||||
mod auth;
|
mod auth;
|
||||||
mod components;
|
mod components;
|
||||||
|
mod services;
|
||||||
|
|
||||||
use app::App;
|
use app::App;
|
||||||
|
|
||||||
|
|||||||
108
src/services/calendar_service.rs
Normal file
108
src/services/calendar_service.rs
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
use chrono::{DateTime, Utc, NaiveDate};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use wasm_bindgen_futures::JsFuture;
|
||||||
|
use web_sys::{Request, RequestInit, RequestMode, Response};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CalendarEvent {
|
||||||
|
pub uid: String,
|
||||||
|
pub summary: Option<String>,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub start: DateTime<Utc>,
|
||||||
|
pub end: Option<DateTime<Utc>>,
|
||||||
|
pub location: Option<String>,
|
||||||
|
pub status: String,
|
||||||
|
pub all_day: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CalendarEvent {
|
||||||
|
/// Get the date for this event (for calendar display)
|
||||||
|
pub fn get_date(&self) -> NaiveDate {
|
||||||
|
if self.all_day {
|
||||||
|
self.start.date_naive()
|
||||||
|
} else {
|
||||||
|
self.start.date_naive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get display title for the event
|
||||||
|
pub fn get_title(&self) -> String {
|
||||||
|
self.summary.clone().unwrap_or_else(|| "Untitled Event".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CalendarService {
|
||||||
|
base_url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CalendarService {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let base_url = option_env!("BACKEND_API_URL")
|
||||||
|
.unwrap_or("http://localhost:3000/api")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Self { base_url }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch calendar events for a specific month
|
||||||
|
pub async fn fetch_events_for_month(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
year: i32,
|
||||||
|
month: u32
|
||||||
|
) -> Result<Vec<CalendarEvent>, String> {
|
||||||
|
let window = web_sys::window().ok_or("No global window exists")?;
|
||||||
|
|
||||||
|
let opts = RequestInit::new();
|
||||||
|
opts.set_method("GET");
|
||||||
|
opts.set_mode(RequestMode::Cors);
|
||||||
|
|
||||||
|
let url = format!("{}/calendar/events?year={}&month={}", self.base_url, year, month);
|
||||||
|
let request = Request::new_with_str_and_init(&url, &opts)
|
||||||
|
.map_err(|e| format!("Request creation failed: {:?}", e))?;
|
||||||
|
|
||||||
|
request.headers().set("Authorization", &format!("Bearer {}", token))
|
||||||
|
.map_err(|e| format!("Authorization header setting failed: {:?}", e))?;
|
||||||
|
|
||||||
|
let resp_value = JsFuture::from(window.fetch_with_request(&request))
|
||||||
|
.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 events: Vec<CalendarEvent> = serde_json::from_str(&text_string)
|
||||||
|
.map_err(|e| format!("JSON parsing failed: {}", e))?;
|
||||||
|
Ok(events)
|
||||||
|
} else {
|
||||||
|
Err(format!("Request failed with status {}: {}", resp.status(), text_string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert events to a HashMap grouped by date for calendar display
|
||||||
|
pub fn group_events_by_date(events: Vec<CalendarEvent>) -> HashMap<NaiveDate, Vec<String>> {
|
||||||
|
let mut grouped = HashMap::new();
|
||||||
|
|
||||||
|
for event in events {
|
||||||
|
let date = event.get_date();
|
||||||
|
let title = event.get_title();
|
||||||
|
|
||||||
|
grouped.entry(date)
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
grouped
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/services/mod.rs
Normal file
3
src/services/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod calendar_service;
|
||||||
|
|
||||||
|
pub use calendar_service::CalendarService;
|
||||||
Reference in New Issue
Block a user