| 1 | use chatsvc::ForcedKind; |
| 2 | use chrono::{DateTime, Datelike, Duration, NaiveTime, TimeZone, Utc}; |
| 3 | |
| 4 | pub const STATUS_CHOICES: [ForcedKind; 6] = [ |
| 5 | ForcedKind::Available, |
| 6 | ForcedKind::Busy, |
| 7 | ForcedKind::DoNotDisturb, |
| 8 | ForcedKind::BeRightBack, |
| 9 | ForcedKind::Away, |
| 10 | ForcedKind::Offline, |
| 11 | ]; |
| 12 | pub const STATUS_DURATIONS: [StatusDuration; 5] = [ |
| 13 | StatusDuration::HalfHour, |
| 14 | StatusDuration::OneHour, |
| 15 | StatusDuration::TwoHours, |
| 16 | StatusDuration::Today, |
| 17 | StatusDuration::ThisWeek, |
| 18 | ]; |
| 19 | |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub enum StatusDuration { |
| 22 | HalfHour, |
| 23 | OneHour, |
| 24 | TwoHours, |
| 25 | Today, |
| 26 | ThisWeek, |
| 27 | } |
| 28 | |
| 29 | impl StatusDuration { |
| 30 | pub fn label(self) -> &'static str { |
| 31 | match self { |
| 32 | StatusDuration::HalfHour => "30 minutes", |
| 33 | StatusDuration::OneHour => "1 hour", |
| 34 | StatusDuration::TwoHours => "2 hours", |
| 35 | StatusDuration::Today => "Today", |
| 36 | StatusDuration::ThisWeek => "This week", |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn expires_at<Zone: TimeZone>(self, now: DateTime<Zone>) -> DateTime<Utc> { |
| 41 | match self { |
| 42 | StatusDuration::HalfHour => after(&now, Duration::minutes(30)), |
| 43 | StatusDuration::OneHour => after(&now, Duration::hours(1)), |
| 44 | StatusDuration::TwoHours => after(&now, Duration::hours(2)), |
| 45 | StatusDuration::Today => end_of_day(&now), |
| 46 | StatusDuration::ThisWeek => end_of_week(&now), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn after<Zone: TimeZone>(now: &DateTime<Zone>, span: Duration) -> DateTime<Utc> { |
| 52 | (now.clone() + span).with_timezone(&Utc) |
| 53 | } |
| 54 | |
| 55 | fn end_of_local_date<Zone: TimeZone>(now: &DateTime<Zone>, days_ahead: i64) -> DateTime<Utc> { |
| 56 | let last_moment = NaiveTime::from_hms_milli_opt(23, 59, 59, 999).unwrap_or(NaiveTime::MIN); |
| 57 | let date = now.date_naive() + Duration::days(days_ahead); |
| 58 | now.timezone() |
| 59 | .from_local_datetime(&date.and_time(last_moment)) |
| 60 | .latest() |
| 61 | .map_or_else( |
| 62 | || after(now, Duration::days(days_ahead + 1)), |
| 63 | |moment| moment.with_timezone(&Utc), |
| 64 | ) |
| 65 | } |
| 66 | |
| 67 | pub fn end_of_day<Zone: TimeZone>(now: &DateTime<Zone>) -> DateTime<Utc> { |
| 68 | end_of_local_date(now, 0) |
| 69 | } |
| 70 | |
| 71 | pub fn end_of_week<Zone: TimeZone>(now: &DateTime<Zone>) -> DateTime<Utc> { |
| 72 | let days_since_sunday = i64::from(now.weekday().num_days_from_sunday()); |
| 73 | end_of_local_date(now, 6 - days_since_sunday) |
| 74 | } |
| 75 | |
| 76 | pub fn choice_label(kind: ForcedKind) -> &'static str { |
| 77 | match kind { |
| 78 | ForcedKind::Away => "Appear away", |
| 79 | ForcedKind::Offline => "Appear offline", |
| 80 | other => state_label(other), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn state_label(kind: ForcedKind) -> &'static str { |
| 85 | match kind { |
| 86 | ForcedKind::Available => "Available", |
| 87 | ForcedKind::Busy => "Busy", |
| 88 | ForcedKind::DoNotDisturb => "Do not disturb", |
| 89 | ForcedKind::BeRightBack => "Be right back", |
| 90 | ForcedKind::Away => "Away", |
| 91 | ForcedKind::Offline => "Offline", |
| 92 | } |
| 93 | } |