Merge branch 'development_rhai'

This commit is contained in:
timurgordon
2025-06-25 20:46:15 +03:00
137 changed files with 7274 additions and 6786 deletions

View File

@@ -1,4 +1,3 @@
use chrono::{DateTime, Utc};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use rhai::{CustomType, TypeBuilder};
@@ -6,7 +5,7 @@ use rhai_autobind_macros::rhai_model_export;
use serde::{Deserialize, Serialize};
/// Represents the status of an attendee for an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum AttendanceStatus {
Accepted = 0,
Declined = 1,
@@ -22,9 +21,31 @@ pub enum EventStatus {
Cancelled = 2,
}
impl AttendanceStatus {
/// Convert a string to an AttendanceStatus
pub fn from_string(s: &str) -> Result<Self, String> {
match s {
"Accepted" => Ok(AttendanceStatus::Accepted),
"Declined" => Ok(AttendanceStatus::Declined),
"Tentative" => Ok(AttendanceStatus::Tentative),
"NoResponse" => Ok(AttendanceStatus::NoResponse),
_ => Err(format!("Invalid attendance status: '{}'", s)),
}
}
/// Convert an AttendanceStatus to a string
pub fn to_string(&self) -> String {
match self {
AttendanceStatus::Accepted => "Accepted".to_string(),
AttendanceStatus::Declined => "Declined".to_string(),
AttendanceStatus::Tentative => "Tentative".to_string(),
AttendanceStatus::NoResponse => "NoResponse".to_string(),
}
}
}
/// Represents an attendee of an event
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Attendee {
/// Base model data
pub base_data: BaseModelData,
@@ -71,15 +92,16 @@ pub struct Event {
/// Base model data
pub base_data: BaseModelData,
/// Title of the event
#[index]
pub title: String,
/// Optional description of the event
pub description: Option<String>,
/// Start time of the event
pub start_time: DateTime<Utc>,
/// End time of the event
pub end_time: DateTime<Utc>,
/// List of attendee IDs for the event
pub attendees: Vec<u32>,
/// Start time of the event (Unix timestamp)
pub start_time: i64,
/// End time of the event (Unix timestamp)
pub end_time: i64,
/// List of attendees for the event
pub attendees: Vec<Attendee>,
/// Optional location of the event
pub location: Option<String>,
/// Color for the event (hex color code)
@@ -110,46 +132,18 @@ impl Event {
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
}
/// Creates a new event with auto-generated ID
pub fn new(title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
impl Event {
/// Creates a new event
pub fn new() -> Self {
let now = chrono::Utc::now().timestamp();
Self {
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
title: title.to_string(),
base_data: BaseModelData::new(),
title: String::new(),
description: None,
start_time,
end_time,
attendees: Vec::new(),
location: None,
color: Some("#4285F4".to_string()), // Default blue color
all_day: false,
created_by: None,
status: EventStatus::Published,
is_recurring: false,
timezone: None,
category: None,
reminder_minutes: None,
}
}
/// Creates a new event with optional ID (use None for auto-generated ID)
pub fn new_with_id(
id: Option<u32>,
title: impl ToString,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {
base_data.update_id(id);
}
Self {
base_data,
title: title.to_string(),
description: None,
start_time,
end_time,
start_time: now,
end_time: now + 3600, // Add 1 hour in seconds
attendees: Vec::new(),
location: None,
color: Some("#4285F4".to_string()), // Default blue color
@@ -238,18 +232,26 @@ impl Event {
self
}
/// Removes an attendee from the event by attendee ID
pub fn remove_attendee(mut self, attendee_id: u32) -> Self {
self.attendees.retain(|&a_id| a_id != attendee_id);
/// Removes an attendee from the event by user_id
pub fn remove_attendee(mut self, contact_id: u32) -> Self {
self.attendees.retain(|a| a.contact_id != contact_id);
self
}
/// Updates the status of an existing attendee
pub fn update_attendee_status(mut self, contact_id: u32, status: AttendanceStatus) -> Self {
if let Some(attendee) = self
.attendees
.iter_mut()
.find(|a| a.contact_id == contact_id)
{
attendee.status = status;
}
self
}
/// Reschedules the event to new start and end times
pub fn reschedule(
mut self,
new_start_time: DateTime<Utc>,
new_end_time: DateTime<Utc>,
) -> Self {
pub fn reschedule(mut self, new_start_time: i64, new_end_time: i64) -> Self {
// Basic validation: end_time should be after start_time
if new_end_time > new_start_time {
self.start_time = new_start_time;
@@ -261,13 +263,17 @@ impl Event {
}
/// Represents a calendar with events
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
// Temporarily removed rhai_model_export macro to fix compilation issues
// #[rhai_model_export(
// db_type = "std::sync::Arc<crate::db::hero::OurDB>",
// )]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
pub struct Calendar {
/// Base model data
pub base_data: BaseModelData,
/// Name of the calendar
#[index]
pub name: String,
/// Optional description of the calendar
pub description: Option<String>,