| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
pub(crate) mod attachment; |
| 20 |
mod contact; |
| 21 |
mod daily_note; |
| 22 |
mod day_planning; |
| 23 |
pub(crate) mod email; |
| 24 |
mod email_account; |
| 25 |
mod email_sync; |
| 26 |
pub mod error; |
| 27 |
mod event; |
| 28 |
mod export; |
| 29 |
mod import_external; |
| 30 |
mod milestone; |
| 31 |
mod monthly_review; |
| 32 |
mod oauth; |
| 33 |
mod plugin; |
| 34 |
mod project; |
| 35 |
mod saved_views; |
| 36 |
mod search; |
| 37 |
mod stats; |
| 38 |
mod sync; |
| 39 |
mod task; |
| 40 |
mod task_state; |
| 41 |
mod task_subtasks; |
| 42 |
mod time_tracking; |
| 43 |
mod themes; |
| 44 |
mod weekly_review; |
| 45 |
mod window; |
| 46 |
|
| 47 |
#[cfg(test)] |
| 48 |
mod tests; |
| 49 |
|
| 50 |
|
| 51 |
pub use error::ApiError; |
| 52 |
pub use error::{OptionNotFound, OptionApiError, ResultApiError}; |
| 53 |
|
| 54 |
|
| 55 |
pub use attachment::*; |
| 56 |
pub use contact::*; |
| 57 |
pub use daily_note::*; |
| 58 |
pub use day_planning::*; |
| 59 |
pub use email::*; |
| 60 |
pub use email_account::*; |
| 61 |
pub use email_sync::*; |
| 62 |
pub use event::*; |
| 63 |
pub use export::*; |
| 64 |
pub use import_external::*; |
| 65 |
pub use milestone::*; |
| 66 |
pub use monthly_review::*; |
| 67 |
pub use oauth::*; |
| 68 |
pub use project::*; |
| 69 |
pub use saved_views::*; |
| 70 |
pub use search::*; |
| 71 |
pub use stats::*; |
| 72 |
pub use sync::*; |
| 73 |
pub use task::*; |
| 74 |
pub use task_state::*; |
| 75 |
pub use task_subtasks::*; |
| 76 |
pub use time_tracking::*; |
| 77 |
pub use themes::*; |
| 78 |
pub use weekly_review::*; |
| 79 |
pub use window::*; |
| 80 |
pub use plugin::*; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
use chrono::{DateTime, Utc}; |
| 85 |
use serde::Deserialize; |
| 86 |
|
| 87 |
|
| 88 |
#[derive(Debug, Deserialize)] |
| 89 |
#[serde(rename_all = "camelCase")] |
| 90 |
pub struct SnoozeInput { |
| 91 |
pub until: DateTime<Utc>, |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
#[derive(Debug, Deserialize)] |
| 96 |
#[serde(rename_all = "camelCase")] |
| 97 |
pub struct WaitingInput { |
| 98 |
pub expected_response_date: Option<DateTime<Utc>>, |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
#[derive(Debug, Deserialize)] |
| 103 |
#[serde(rename_all = "camelCase")] |
| 104 |
pub struct LinkProjectInput { |
| 105 |
pub project_id: Option<goingson_core::ProjectId>, |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
use chrono::{Datelike, Duration, Local, NaiveTime, TimeZone, Timelike, Weekday}; |
| 111 |
use serde::Serialize; |
| 112 |
use tracing::instrument; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#[derive(Debug, Serialize)] |
| 120 |
#[serde(rename_all = "camelCase")] |
| 121 |
pub struct SnoozeOption { |
| 122 |
|
| 123 |
pub key: String, |
| 124 |
|
| 125 |
pub label: String, |
| 126 |
|
| 127 |
pub time: DateTime<Utc>, |
| 128 |
|
| 129 |
pub formatted: String, |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[derive(Debug, Serialize)] |
| 138 |
#[serde(rename_all = "camelCase")] |
| 139 |
pub struct SnoozeOptionsResponse { |
| 140 |
|
| 141 |
pub options: Vec<SnoozeOption>, |
| 142 |
|
| 143 |
pub min_custom: DateTime<Utc>, |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[tauri::command] |
| 154 |
#[instrument(skip_all)] |
| 155 |
pub fn get_snooze_options() -> SnoozeOptionsResponse { |
| 156 |
let now = Local::now(); |
| 157 |
let today = now.date_naive(); |
| 158 |
let mut options = Vec::new(); |
| 159 |
|
| 160 |
|
| 161 |
let later_today = if now.hour() >= 14 { |
| 162 |
|
| 163 |
let five_pm = NaiveTime::from_hms_opt(17, 0, 0).expect("17:00 is valid"); |
| 164 |
Local.from_local_datetime(&today.and_time(five_pm)).earliest() |
| 165 |
} else { |
| 166 |
|
| 167 |
Some(now + Duration::hours(3)) |
| 168 |
}; |
| 169 |
|
| 170 |
if let Some(lt) = later_today { |
| 171 |
if lt > now { |
| 172 |
let utc_time = lt.with_timezone(&Utc); |
| 173 |
options.push(SnoozeOption { |
| 174 |
key: "laterToday".to_string(), |
| 175 |
label: "Later Today".to_string(), |
| 176 |
time: utc_time, |
| 177 |
formatted: format_snooze_time(<), |
| 178 |
}); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
let tomorrow = today + Duration::days(1); |
| 184 |
if let Some(tomorrow_9am) = Local |
| 185 |
.from_local_datetime(&tomorrow.and_time(NaiveTime::from_hms_opt(9, 0, 0).expect("09:00 is valid"))) |
| 186 |
.earliest() |
| 187 |
{ |
| 188 |
options.push(SnoozeOption { |
| 189 |
key: "tomorrow".to_string(), |
| 190 |
label: "Tomorrow".to_string(), |
| 191 |
time: tomorrow_9am.with_timezone(&Utc), |
| 192 |
formatted: format_snooze_time(&tomorrow_9am), |
| 193 |
}); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
let days_until_saturday = (Weekday::Sat.num_days_from_monday() as i64 |
| 198 |
- now.weekday().num_days_from_monday() as i64 |
| 199 |
+ 7) % 7; |
| 200 |
let days_until_saturday = if days_until_saturday == 0 { 7 } else { days_until_saturday }; |
| 201 |
let saturday = today + Duration::days(days_until_saturday); |
| 202 |
if let Some(weekend) = Local |
| 203 |
.from_local_datetime(&saturday.and_time(NaiveTime::from_hms_opt(10, 0, 0).expect("10:00 is valid"))) |
| 204 |
.earliest() |
| 205 |
{ |
| 206 |
options.push(SnoozeOption { |
| 207 |
key: "weekend".to_string(), |
| 208 |
label: "This Weekend".to_string(), |
| 209 |
time: weekend.with_timezone(&Utc), |
| 210 |
formatted: format_snooze_time(&weekend), |
| 211 |
}); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
let days_until_monday = (Weekday::Mon.num_days_from_monday() as i64 |
| 216 |
- now.weekday().num_days_from_monday() as i64 |
| 217 |
+ 7) % 7; |
| 218 |
|
| 219 |
let days_until_monday = if days_until_monday <= 1 { days_until_monday + 7 } else { days_until_monday }; |
| 220 |
let monday = today + Duration::days(days_until_monday); |
| 221 |
if let Some(next_week) = Local |
| 222 |
.from_local_datetime(&monday.and_time(NaiveTime::from_hms_opt(9, 0, 0).expect("09:00 is valid"))) |
| 223 |
.earliest() |
| 224 |
{ |
| 225 |
options.push(SnoozeOption { |
| 226 |
key: "nextWeek".to_string(), |
| 227 |
label: "Next Week".to_string(), |
| 228 |
time: next_week.with_timezone(&Utc), |
| 229 |
formatted: format_snooze_time(&next_week), |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
options.sort_by_key(|o| o.time); |
| 235 |
|
| 236 |
SnoozeOptionsResponse { |
| 237 |
options, |
| 238 |
min_custom: now.with_timezone(&Utc), |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
fn format_snooze_time<Tz: TimeZone>(dt: &DateTime<Tz>) -> String |
| 244 |
where |
| 245 |
Tz::Offset: std::fmt::Display, |
| 246 |
{ |
| 247 |
dt.format("%a, %b %-d, %-I:%M %p").to_string() |
| 248 |
} |
| 249 |
|
| 250 |
|