use chrono::{Utc, Duration}; use herodb::db::{DBBuilder, GetId}; use herodb::models::mcc::{ Calendar, Event, Email, Attachment, Envelope, Contact, Message }; use herodb::models::circle::Circle; use std::path::PathBuf; use std::fs; fn main() -> Result<(), Box> { println!("DB Example MCC: Mail, Calendar, Contacts with Group Support"); println!("======================================================="); // Create a temporary directory for the database let db_path = PathBuf::from("/tmp/dbexample_mcc"); if db_path.exists() { fs::remove_dir_all(&db_path)?; } fs::create_dir_all(&db_path)?; println!("Database path: {:?}", db_path); // Create a database instance with our models registered let db = DBBuilder::new(&db_path) .register_type::("calendar") .register_type::("event") .register_type::("email") .register_type::("contact") .register_type::("message") .register_model::() // Circle still uses the Model trait .build()?; println!("\n1. Creating Circles (Groups)"); println!("---------------------------"); // Create circles (groups) let work_circle = Circle::new( 1, "Work".to_string(), "Work-related communications".to_string() ); let family_circle = Circle::new( 2, "Family".to_string(), "Family communications".to_string() ); let friends_circle = Circle::new( 3, "Friends".to_string(), "Friends communications".to_string() ); // Insert circles db.set::(&work_circle)?; db.set::(&family_circle)?; db.set::(&friends_circle)?; println!("Created circles:"); println!(" - Circle #{}: {}", work_circle.id, work_circle.name); println!(" - Circle #{}: {}", family_circle.id, family_circle.name); println!(" - Circle #{}: {}", friends_circle.id, friends_circle.name); println!("\n2. Creating Contacts with Group Support"); println!("------------------------------------"); // Create contacts let mut john = Contact::new( 1, "John".to_string(), "Doe".to_string(), "john.doe@example.com".to_string(), "work".to_string() ); john.add_group(work_circle.id); let mut alice = Contact::new( 2, "Alice".to_string(), "Smith".to_string(), "alice.smith@example.com".to_string(), "family".to_string() ); alice.add_group(family_circle.id); let mut bob = Contact::new( 3, "Bob".to_string(), "Johnson".to_string(), "bob.johnson@example.com".to_string(), "friends".to_string() ); bob.add_group(friends_circle.id); bob.add_group(work_circle.id); // Bob is both a friend and a work contact // Insert contacts db.set_any::(&john, "contact")?; db.set_any::(&alice, "contact")?; db.set_any::(&bob, "contact")?; println!("Created contacts:"); println!(" - {}: {} (Groups: {:?})", john.full_name(), john.email, john.groups); println!(" - {}: {} (Groups: {:?})", alice.full_name(), alice.email, alice.groups); println!(" - {}: {} (Groups: {:?})", bob.full_name(), bob.email, bob.groups); println!("\n3. Creating Calendars with Group Support"); println!("-------------------------------------"); // Create calendars let mut work_calendar = Calendar::new( 1, "Work Calendar".to_string(), "Work-related events".to_string() ); work_calendar.add_group(work_circle.id); let mut personal_calendar = Calendar::new( 2, "Personal Calendar".to_string(), "Personal events".to_string() ); personal_calendar.add_group(family_circle.id); personal_calendar.add_group(friends_circle.id); // Insert calendars db.set_any::(&work_calendar, "calendar")?; db.set_any::(&personal_calendar, "calendar")?; println!("Created calendars:"); println!(" - {}: {} (Groups: {:?})", work_calendar.id, work_calendar.title, work_calendar.groups); println!(" - {}: {} (Groups: {:?})", personal_calendar.id, personal_calendar.title, personal_calendar.groups); println!("\n4. Creating Events with Group Support"); println!("----------------------------------"); // Create events let now = Utc::now(); let tomorrow = now + Duration::days(1); let next_week = now + Duration::days(7); let mut work_meeting = Event::new( 1, work_calendar.id, "Team Meeting".to_string(), "Weekly team sync".to_string(), "Conference Room A".to_string(), tomorrow, tomorrow + Duration::hours(1), "organizer@example.com".to_string() ); work_meeting.add_group(work_circle.id); work_meeting.add_attendee(john.email.clone()); work_meeting.add_attendee(bob.email.clone()); let mut family_dinner = Event::new( 2, personal_calendar.id, "Family Dinner".to_string(), "Weekly family dinner".to_string(), "Home".to_string(), next_week, next_week + Duration::hours(2), "me@example.com".to_string() ); family_dinner.add_group(family_circle.id); family_dinner.add_attendee(alice.email.clone()); // Insert events db.set_any::(&work_meeting, "event")?; db.set_any::(&family_dinner, "event")?; println!("Created events:"); println!(" - {}: {} on {} (Groups: {:?})", work_meeting.id, work_meeting.title, work_meeting.start_time.format("%Y-%m-%d %H:%M"), work_meeting.groups ); println!(" - {}: {} on {} (Groups: {:?})", family_dinner.id, family_dinner.title, family_dinner.start_time.format("%Y-%m-%d %H:%M"), family_dinner.groups ); println!("\n5. Creating Emails with Group Support"); println!("----------------------------------"); // Create emails let mut work_email = Email::new( 1, 101, 1, "INBOX".to_string(), "Here are the meeting notes from yesterday's discussion.".to_string() ); work_email.add_group(work_circle.id); let work_attachment = Attachment { filename: "meeting_notes.pdf".to_string(), content_type: "application/pdf".to_string(), hash: "abc123def456".to_string(), size: 1024, }; work_email.add_attachment(work_attachment); let work_envelope = Envelope { date: now.timestamp(), subject: "Meeting Notes".to_string(), from: vec!["john.doe@example.com".to_string()], sender: vec!["john.doe@example.com".to_string()], reply_to: vec!["john.doe@example.com".to_string()], to: vec!["me@example.com".to_string()], cc: vec!["bob.johnson@example.com".to_string()], bcc: vec![], in_reply_to: "".to_string(), message_id: "msg123@example.com".to_string(), }; work_email.set_envelope(work_envelope); let mut family_email = Email::new( 2, 102, 2, "INBOX".to_string(), "Looking forward to seeing you at dinner next week!".to_string() ); family_email.add_group(family_circle.id); let family_envelope = Envelope { date: now.timestamp(), subject: "Family Dinner".to_string(), from: vec!["alice.smith@example.com".to_string()], sender: vec!["alice.smith@example.com".to_string()], reply_to: vec!["alice.smith@example.com".to_string()], to: vec!["me@example.com".to_string()], cc: vec![], bcc: vec![], in_reply_to: "".to_string(), message_id: "msg456@example.com".to_string(), }; family_email.set_envelope(family_envelope); // Insert emails db.set_any::(&work_email, "email")?; db.set_any::(&family_email, "email")?; println!("Created emails:"); println!(" - From: {}, Subject: {} (Groups: {:?})", work_email.envelope.as_ref().unwrap().from[0], work_email.envelope.as_ref().unwrap().subject, work_email.groups ); println!(" - From: {}, Subject: {} (Groups: {:?})", family_email.envelope.as_ref().unwrap().from[0], family_email.envelope.as_ref().unwrap().subject, family_email.groups ); println!("\n6. Creating Messages (Chat) with Group Support"); println!("-----------------------------------------"); // Create messages let mut work_chat = Message::new( 1, "thread_work_123".to_string(), "john.doe@example.com".to_string(), "Can we move the meeting to 3pm?".to_string() ); work_chat.add_group(work_circle.id); work_chat.add_recipient("me@example.com".to_string()); work_chat.add_recipient("bob.johnson@example.com".to_string()); let mut friends_chat = Message::new( 2, "thread_friends_456".to_string(), "bob.johnson@example.com".to_string(), "Are we still on for the game this weekend?".to_string() ); friends_chat.add_group(friends_circle.id); friends_chat.add_recipient("me@example.com".to_string()); friends_chat.add_reaction("👍".to_string()); // Insert messages db.set_any::(&work_chat, "message")?; db.set_any::(&friends_chat, "message")?; println!("Created messages:"); println!(" - From: {}, Content: {} (Groups: {:?})", work_chat.sender_id, work_chat.content, work_chat.groups ); println!(" - From: {}, Content: {} (Groups: {:?}, Reactions: {:?})", friends_chat.sender_id, friends_chat.content, friends_chat.groups, friends_chat.meta.reactions ); println!("\n7. Demonstrating Utility Methods"); println!("------------------------------"); // Filter contacts by group println!("\nFiltering contacts by work group (ID: {}):", work_circle.id); let all_contacts = db.list_any::()?; for contact in all_contacts { if contact.filter_by_groups(&[work_circle.id]) { println!(" - {} ({})", contact.full_name(), contact.email); } } // Search emails by subject println!("\nSearching emails with subject containing 'Meeting':"); let all_emails = db.list_any::()?; for email in all_emails { if email.search_by_subject("Meeting") { println!(" - Subject: {}, From: {}", email.envelope.as_ref().unwrap().subject, email.envelope.as_ref().unwrap().from[0] ); } } // Get events for a calendar println!("\nGetting events for Work Calendar (ID: {}):", work_calendar.id); let all_events = db.list_any::()?; let work_events: Vec = all_events .into_iter() .filter(|event| event.calendar_id == work_calendar.id) .collect(); for event in work_events { println!(" - {}: {} on {}", event.id, event.title, event.start_time.format("%Y-%m-%d %H:%M") ); } // Get attendee contacts for an event println!("\nGetting attendee contacts for Team Meeting (ID: {}):", work_meeting.id); let all_contacts = db.list_any::()?; let attendee_contacts: Vec = all_contacts .into_iter() .filter(|contact| work_meeting.attendees.contains(&contact.email)) .collect(); for contact in attendee_contacts { println!(" - {} ({})", contact.full_name(), contact.email); } // Convert email to message println!("\nConverting work email to message:"); let email_to_message = work_email.to_message(3, "thread_converted_789".to_string()); println!(" - Original Email Subject: {}", work_email.envelope.as_ref().unwrap().subject); println!(" - Converted Message Content: {}", email_to_message.content.split('\n').next().unwrap_or("")); println!(" - Converted Message Groups: {:?}", email_to_message.groups); // Insert the converted message db.set_any::(&email_to_message, "message")?; println!("\n8. Relationship Management"); println!("------------------------"); // Get the calendar for an event println!("\nGetting calendar for Family Dinner event (ID: {}):", family_dinner.id); let event_calendar = db.get_any::(family_dinner.calendar_id)?; println!(" - Calendar: {} ({})", event_calendar.title, event_calendar.description); // Get events for a contact println!("\nGetting events where John Doe is an attendee:"); let all_events = db.list_any::()?; let john_events: Vec = all_events .into_iter() .filter(|event| event.attendees.contains(&john.email)) .collect(); for event in john_events { println!(" - {}: {} on {}", event.id, event.title, event.start_time.format("%Y-%m-%d %H:%M") ); } // Get messages in the same thread println!("\nGetting all messages in the work chat thread:"); let all_messages = db.list_any::()?; let thread_messages: Vec = all_messages .into_iter() .filter(|message| message.thread_id == work_chat.thread_id) .collect(); for message in thread_messages { println!(" - From: {}, Content: {}", message.sender_id, message.content); } println!("\nExample completed successfully!"); Ok(()) }