| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::sync::Arc; |
| 9 |
|
| 10 |
use chrono::Utc; |
| 11 |
use goingson_core::{EmailId, EmailSource, Validate, event_from_email, task_from_email}; |
| 12 |
use tauri::State; |
| 13 |
use tracing::{instrument, warn}; |
| 14 |
|
| 15 |
use super::{ApiError, OptionNotFound}; |
| 16 |
use crate::commands::{EventResponse, TaskResponse}; |
| 17 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
fn sender_contact_id(state: &Arc<AppState>, from: &str) -> Option<goingson_core::ContactId> { |
| 25 |
let address = goingson_core::email_compose::extract_email_address(from); |
| 26 |
if address.is_empty() { |
| 27 |
return None; |
| 28 |
} |
| 29 |
match state.contacts.find_by_email(DESKTOP_USER_ID, address) { |
| 30 |
Ok(contact) => contact.map(|c| c.id), |
| 31 |
Err(e) => { |
| 32 |
warn!("failed to resolve sender contact for {address}: {e}"); |
| 33 |
None |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[tauri::command] |
| 47 |
#[instrument(skip_all)] |
| 48 |
pub async fn create_task_from_email( |
| 49 |
state: State<'_, Arc<AppState>>, |
| 50 |
id: EmailId, |
| 51 |
) -> Result<TaskResponse, ApiError> { |
| 52 |
let email = state |
| 53 |
.emails |
| 54 |
.get_by_id(id, DESKTOP_USER_ID)? |
| 55 |
.or_not_found("email", id)?; |
| 56 |
|
| 57 |
let contact_id = sender_contact_id(&state, &email.from); |
| 58 |
let source = EmailSource { |
| 59 |
id: email.id, |
| 60 |
subject: &email.subject, |
| 61 |
from: &email.from, |
| 62 |
body: &email.body, |
| 63 |
project_id: email.project_id, |
| 64 |
}; |
| 65 |
|
| 66 |
let new_task = task_from_email(&source, contact_id, Utc::now()); |
| 67 |
new_task.validate()?; |
| 68 |
|
| 69 |
let task = state.tasks.create(DESKTOP_USER_ID, new_task)?; |
| 70 |
Ok(TaskResponse::from(task)) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[tauri::command] |
| 84 |
#[instrument(skip_all)] |
| 85 |
pub async fn create_event_from_email( |
| 86 |
state: State<'_, Arc<AppState>>, |
| 87 |
id: EmailId, |
| 88 |
) -> Result<EventResponse, ApiError> { |
| 89 |
let email = state |
| 90 |
.emails |
| 91 |
.get_by_id(id, DESKTOP_USER_ID)? |
| 92 |
.or_not_found("email", id)?; |
| 93 |
|
| 94 |
let contact_id = sender_contact_id(&state, &email.from); |
| 95 |
let source = EmailSource { |
| 96 |
id: email.id, |
| 97 |
subject: &email.subject, |
| 98 |
from: &email.from, |
| 99 |
body: &email.body, |
| 100 |
project_id: email.project_id, |
| 101 |
}; |
| 102 |
|
| 103 |
let mut new_event = event_from_email(&source, contact_id, Utc::now()); |
| 104 |
new_event.user_id = Some(DESKTOP_USER_ID); |
| 105 |
new_event.validate()?; |
| 106 |
|
| 107 |
let event = state.events.create(DESKTOP_USER_ID, new_event)?; |
| 108 |
Ok(EventResponse::from(event)) |
| 109 |
} |
| 110 |
|