fix(meetings): new meetings keep local time across DST; calling tests no longer crash at exit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -116,6 +116,8 @@ pub struct AppState {
116
116
pub image_viewer_request: Option<ViewerRequest>,
117
117
pub own_status: PresenceStatus,
118
118
pub own_email: String,
119
pub own_time_zone: Option<String>,
120
pub own_time_zone_requested: bool,
119
121
pub status_menu_open: bool,
120
122
pub status_message_request: bool,
121
123
pub profiles: ProfileCache,
@@ -222,6 +224,8 @@ impl AppState {
222
224
image_viewer_request: None,
223
225
own_status: PresenceStatus::default(),
224
226
own_email: String::new(),
227
own_time_zone: None,
228
own_time_zone_requested: false,
225
229
status_menu_open: false,
226
230
status_message_request: false,
227
231
profiles: ProfileCache::new(),
@@ -787,6 +791,7 @@ impl AppState {
787
791
self.calls_list.invalidate();
788
792
if !self.mode.demo {
789
793
self.reload_directory();
794
self.load_own_time_zone(cx);
790
795
}
791
796
cx.emit(AppEvent::Sidebar);
792
797
cx.notify();
@@ -801,6 +806,7 @@ impl AppState {
801
806
BackendEvent::Calls(launcher) => {
802
807
launcher.set_default_camera(self.call_camera.clone());
803
808
self.call_launcher = Some(launcher);
809
self.load_own_time_zone(cx);
804
810
}
805
811
BackendEvent::Calendar(events) => self.apply_calendar(events, cx),
806
812
BackendEvent::CalendarNudge(nudge) => self.calendar_nudge = Some(nudge),
@@ -116,6 +116,8 @@ pub struct AppState {116
pub image_viewer_request: Option<ViewerRequest>,116
pub image_viewer_request: Option<ViewerRequest>,117
pub own_status: PresenceStatus,117
pub own_status: PresenceStatus,118
pub own_email: String,118
pub own_email: String,119
pub own_time_zone: Option<String>,120
pub own_time_zone_requested: bool,119
pub status_menu_open: bool,121
pub status_menu_open: bool,120
pub status_message_request: bool,122
pub status_message_request: bool,121
pub profiles: ProfileCache,123
pub profiles: ProfileCache,@@ -222,6 +224,8 @@ impl AppState {222
image_viewer_request: None,224
image_viewer_request: None,223
own_status: PresenceStatus::default(),225
own_status: PresenceStatus::default(),224
own_email: String::new(),226
own_email: String::new(),227
own_time_zone: None,228
own_time_zone_requested: false,225
status_menu_open: false,229
status_menu_open: false,226
status_message_request: false,230
status_message_request: false,227
profiles: ProfileCache::new(),231
profiles: ProfileCache::new(),@@ -787,6 +791,7 @@ impl AppState {787
self.calls_list.invalidate();791
self.calls_list.invalidate();788
if !self.mode.demo {792
if !self.mode.demo {789
self.reload_directory();793
self.reload_directory();794
self.load_own_time_zone(cx);790
}795
}791
cx.emit(AppEvent::Sidebar);796
cx.emit(AppEvent::Sidebar);792
cx.notify();797
cx.notify();@@ -801,6 +806,7 @@ impl AppState {801
BackendEvent::Calls(launcher) => {806
BackendEvent::Calls(launcher) => {802
launcher.set_default_camera(self.call_camera.clone());807
launcher.set_default_camera(self.call_camera.clone());803
self.call_launcher = Some(launcher);808
self.call_launcher = Some(launcher);809
self.load_own_time_zone(cx);804
}810
}805
BackendEvent::Calendar(events) => self.apply_calendar(events, cx),811
BackendEvent::Calendar(events) => self.apply_calendar(events, cx),806
BackendEvent::CalendarNudge(nudge) => self.calendar_nudge = Some(nudge),812
BackendEvent::CalendarNudge(nudge) => self.calendar_nudge = Some(nudge),@@ -161,6 +161,9 @@ impl CallLauncher {
161
161
subject: draft.subject,
162
162
start: draft.start,
163
163
end: draft.end,
164
local_start: draft.local_start,
165
local_end: draft.local_end,
166
time_zone_name: draft.time_zone_name,
164
167
attendees,
165
168
repeat: draft.repeat,
166
169
details: draft.details,
@@ -171,6 +174,15 @@ impl CallLauncher {
171
174
.map_err(|error| error.to_string())
172
175
}
173
176
177
pub async fn time_zone_name(&self, mail: &str) -> Option<String> {
178
let session = self.engine()?.session();
179
graph::Graph::new(session)
180
.time_zone_name(mail)
181
.await
182
.ok()
183
.flatten()
184
}
185
174
186
pub async fn respond_to_event(
175
187
&self,
176
188
event_id: &str,
@@ -161,6 +161,9 @@ impl CallLauncher {161
subject: draft.subject,161
subject: draft.subject,162
start: draft.start,162
start: draft.start,163
end: draft.end,163
end: draft.end,164
local_start: draft.local_start,165
local_end: draft.local_end,166
time_zone_name: draft.time_zone_name,164
attendees,167
attendees,165
repeat: draft.repeat,168
repeat: draft.repeat,166
details: draft.details,169
details: draft.details,@@ -171,6 +174,15 @@ impl CallLauncher {171
.map_err(|error| error.to_string())174
.map_err(|error| error.to_string())172
}175
}173
176
177
pub async fn time_zone_name(&self, mail: &str) -> Option<String> {178
let session = self.engine()?.session();179
graph::Graph::new(session)180
.time_zone_name(mail)181
.await182
.ok()183
.flatten()184
}185
174
pub async fn respond_to_event(186
pub async fn respond_to_event(175
&self,187
&self,176
event_id: &str,188
event_id: &str,@@ -25,6 +25,9 @@ pub struct MeetingDraft {
25
25
pub subject: String,
26
26
pub start: DateTime<Utc>,
27
27
pub end: DateTime<Utc>,
28
pub local_start: NaiveDateTime,
29
pub local_end: NaiveDateTime,
30
pub time_zone_name: Option<String>,
28
31
pub repeat: Repeat,
29
32
pub details: String,
30
33
pub people: Vec<Invitee>,
@@ -59,7 +62,11 @@ pub struct FormValues {
59
62
pub people: Vec<Invitee>,
60
63
}
61
64
62
pub fn draft_from_form(values: FormValues, zone: &impl TimeZone) -> Result<MeetingDraft, String> {
65
pub fn draft_from_form(
66
values: FormValues,
67
zone: &impl TimeZone,
68
time_zone_name: Option<String>,
69
) -> Result<MeetingDraft, String> {
63
70
let subject = values.subject.trim();
64
71
if subject.is_empty() {
65
72
return Err("Add a title.".to_owned());
@@ -76,6 +83,9 @@ pub fn draft_from_form(values: FormValues, zone: &impl TimeZone) -> Result<Meeti
76
83
subject: subject.to_owned(),
77
84
start,
78
85
end,
86
local_start: date.and_time(values.start),
87
local_end: date.and_time(values.end),
88
time_zone_name,
79
89
repeat: values.repeat,
80
90
details: values.details.trim().to_owned(),
81
91
people: values.people,
@@ -121,6 +131,28 @@ fn demo_event(draft: &MeetingDraft, serial: u64) -> CalendarEvent {
121
131
}
122
132
123
133
impl AppState {
134
pub fn load_own_time_zone(&mut self, cx: &mut Context<Self>) {
135
if self.own_time_zone.is_some() || self.own_time_zone_requested || self.own_email.is_empty()
136
{
137
return;
138
}
139
let Some(launcher) = self.call_launcher.clone() else {
140
return;
141
};
142
self.own_time_zone_requested = true;
143
let mail = self.own_email.clone();
144
let looked_up = runtime::spawn(async move { launcher.time_zone_name(&mail).await });
145
cx.spawn(async move |this, cx| {
146
let time_zone_name = looked_up.await.ok().flatten();
147
this.update(cx, |state, _| {
148
state.own_time_zone_requested = time_zone_name.is_some();
149
state.own_time_zone = time_zone_name;
150
})
151
.ok();
152
})
153
.detach();
154
}
155
124
156
pub fn create_meeting(
125
157
&mut self,
126
158
draft: MeetingDraft,
@@ -283,6 +315,7 @@ mod tests {
283
315
people: Vec::new(),
284
316
},
285
317
&plus_two(),
318
Some("W. Europe Standard Time".into()),
286
319
)
287
320
.unwrap();
288
321
assert_eq!(draft.subject, "Planning");
@@ -296,6 +329,12 @@ mod tests {
296
329
Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap()
297
330
);
298
331
assert_eq!(draft.repeat, Repeat::Weekly);
332
assert_eq!(draft.local_start.to_string(), "2026-10-14 11:00:00");
333
assert_eq!(draft.local_end.to_string(), "2026-10-14 11:30:00");
334
assert_eq!(
335
draft.time_zone_name.as_deref(),
336
Some("W. Europe Standard Time")
337
);
299
338
}
300
339
301
340
#[test]
@@ -310,7 +349,7 @@ mod tests {
310
349
details: String::new(),
311
350
people: Vec::new(),
312
351
};
313
draft_from_form(values, &plus_two())
352
draft_from_form(values, &plus_two(), None)
314
353
};
315
354
assert_eq!(
316
355
build(" ", day(), time(9, 0), time(10, 0)).unwrap_err(),
@@ -25,6 +25,9 @@ pub struct MeetingDraft {25
pub subject: String,25
pub subject: String,26
pub start: DateTime<Utc>,26
pub start: DateTime<Utc>,27
pub end: DateTime<Utc>,27
pub end: DateTime<Utc>,28
pub local_start: NaiveDateTime,29
pub local_end: NaiveDateTime,30
pub time_zone_name: Option<String>,28
pub repeat: Repeat,31
pub repeat: Repeat,29
pub details: String,32
pub details: String,30
pub people: Vec<Invitee>,33
pub people: Vec<Invitee>,@@ -59,7 +62,11 @@ pub struct FormValues {59
pub people: Vec<Invitee>,62
pub people: Vec<Invitee>,60
}63
}61
64
62
pub fn draft_from_form(values: FormValues, zone: &impl TimeZone) -> Result<MeetingDraft, String> {65
pub fn draft_from_form(66
values: FormValues,67
zone: &impl TimeZone,68
time_zone_name: Option<String>,69
) -> Result<MeetingDraft, String> {63
let subject = values.subject.trim();70
let subject = values.subject.trim();64
if subject.is_empty() {71
if subject.is_empty() {65
return Err("Add a title.".to_owned());72
return Err("Add a title.".to_owned());@@ -76,6 +83,9 @@ pub fn draft_from_form(values: FormValues, zone: &impl TimeZone) -> Result<Meeti76
subject: subject.to_owned(),83
subject: subject.to_owned(),77
start,84
start,78
end,85
end,86
local_start: date.and_time(values.start),87
local_end: date.and_time(values.end),88
time_zone_name,79
repeat: values.repeat,89
repeat: values.repeat,80
details: values.details.trim().to_owned(),90
details: values.details.trim().to_owned(),81
people: values.people,91
people: values.people,@@ -121,6 +131,28 @@ fn demo_event(draft: &MeetingDraft, serial: u64) -> CalendarEvent {121
}131
}122
132
123
impl AppState {133
impl AppState {134
pub fn load_own_time_zone(&mut self, cx: &mut Context<Self>) {135
if self.own_time_zone.is_some() || self.own_time_zone_requested || self.own_email.is_empty()136
{137
return;138
}139
let Some(launcher) = self.call_launcher.clone() else {140
return;141
};142
self.own_time_zone_requested = true;143
let mail = self.own_email.clone();144
let looked_up = runtime::spawn(async move { launcher.time_zone_name(&mail).await });145
cx.spawn(async move |this, cx| {146
let time_zone_name = looked_up.await.ok().flatten();147
this.update(cx, |state, _| {148
state.own_time_zone_requested = time_zone_name.is_some();149
state.own_time_zone = time_zone_name;150
})151
.ok();152
})153
.detach();154
}155
124
pub fn create_meeting(156
pub fn create_meeting(125
&mut self,157
&mut self,126
draft: MeetingDraft,158
draft: MeetingDraft,@@ -283,6 +315,7 @@ mod tests {283
people: Vec::new(),315
people: Vec::new(),284
},316
},285
&plus_two(),317
&plus_two(),318
Some("W. Europe Standard Time".into()),286
)319
)287
.unwrap();320
.unwrap();288
assert_eq!(draft.subject, "Planning");321
assert_eq!(draft.subject, "Planning");@@ -296,6 +329,12 @@ mod tests {296
Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap()329
Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap()297
);330
);298
assert_eq!(draft.repeat, Repeat::Weekly);331
assert_eq!(draft.repeat, Repeat::Weekly);332
assert_eq!(draft.local_start.to_string(), "2026-10-14 11:00:00");333
assert_eq!(draft.local_end.to_string(), "2026-10-14 11:30:00");334
assert_eq!(335
draft.time_zone_name.as_deref(),336
Some("W. Europe Standard Time")337
);299
}338
}300
339
301
#[test]340
#[test]@@ -310,7 +349,7 @@ mod tests {310
details: String::new(),349
details: String::new(),311
people: Vec::new(),350
people: Vec::new(),312
};351
};313
draft_from_form(values, &plus_two())352
draft_from_form(values, &plus_two(), None)314
};353
};315
assert_eq!(354
assert_eq!(316
build(" ", day(), time(9, 0), time(10, 0)).unwrap_err(),355
build(" ", day(), time(9, 0), time(10, 0)).unwrap_err(),@@ -27,6 +27,7 @@ static SEARCHABLE_GIFS: OnceLock<Vec<Gif>> = OnceLock::new();
27
27
pub const DEMO_USER_ID: &str = "demo-me";
28
28
const DEMO_USER_NAME: &str = "Dana Demo";
29
29
const DEMO_USER_EMAIL: &str = "dana.demo@example.com";
30
const DEMO_TIME_ZONE: &str = "W. Europe Standard Time";
30
31
const PHOTO_SIZE: usize = 48;
31
32
const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard";
32
33
const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot";
@@ -824,6 +825,7 @@ fn photo(palette: &Palette) -> Arc<Image> {
824
825
pub fn seed_directory(state: &mut AppState) {
825
826
state.own_status = crate::own_status::demo_status();
826
827
state.own_email = DEMO_USER_EMAIL.to_owned();
828
state.own_time_zone = Some(DEMO_TIME_ZONE.to_owned());
827
829
state.translation.settings = Some(language_settings());
828
830
state.translation.languages = language_list();
829
831
let directory = &mut state.directory;
@@ -27,6 +27,7 @@ static SEARCHABLE_GIFS: OnceLock<Vec<Gif>> = OnceLock::new();27
pub const DEMO_USER_ID: &str = "demo-me";27
pub const DEMO_USER_ID: &str = "demo-me";28
const DEMO_USER_NAME: &str = "Dana Demo";28
const DEMO_USER_NAME: &str = "Dana Demo";29
const DEMO_USER_EMAIL: &str = "dana.demo@example.com";29
const DEMO_USER_EMAIL: &str = "dana.demo@example.com";30
const DEMO_TIME_ZONE: &str = "W. Europe Standard Time";30
const PHOTO_SIZE: usize = 48;31
const PHOTO_SIZE: usize = 48;31
const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard";32
const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard";32
const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot";33
const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot";@@ -824,6 +825,7 @@ fn photo(palette: &Palette) -> Arc<Image> {824
pub fn seed_directory(state: &mut AppState) {825
pub fn seed_directory(state: &mut AppState) {825
state.own_status = crate::own_status::demo_status();826
state.own_status = crate::own_status::demo_status();826
state.own_email = DEMO_USER_EMAIL.to_owned();827
state.own_email = DEMO_USER_EMAIL.to_owned();828
state.own_time_zone = Some(DEMO_TIME_ZONE.to_owned());827
state.translation.settings = Some(language_settings());829
state.translation.settings = Some(language_settings());828
state.translation.languages = language_list();830
state.translation.languages = language_list();829
let directory = &mut state.directory;831
let directory = &mut state.directory;@@ -111,7 +111,8 @@ impl MeetingForm {
111
111
details: self.details.read(cx).value().to_string(),
112
112
people,
113
113
};
114
let draft = match draft_from_form(values, &Local) {
114
let time_zone_name = self.state.read(cx).own_time_zone.clone();
115
let draft = match draft_from_form(values, &Local, time_zone_name) {
115
116
Ok(draft) => draft,
116
117
Err(message) => {
117
118
self.error = Some(message);
@@ -111,7 +111,8 @@ impl MeetingForm {111
details: self.details.read(cx).value().to_string(),111
details: self.details.read(cx).value().to_string(),112
people,112
people,113
};113
};114
let draft = match draft_from_form(values, &Local) {114
let time_zone_name = self.state.read(cx).own_time_zone.clone();115
let draft = match draft_from_form(values, &Local, time_zone_name) {115
Ok(draft) => draft,116
Ok(draft) => draft,116
Err(message) => {117
Err(message) => {117
self.error = Some(message);118
self.error = Some(message);@@ -2515,7 +2515,9 @@ mod tests {
2515
2515
let peer = factory.create_peer_connection(RtcConfiguration::default()).unwrap();
2516
2516
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);
2517
2517
let track = factory.create_audio_track("microphone", source);
2518
create_offer(&peer, &factory, &track, layout).await
2518
let offer = create_offer(&peer, &factory, &track, layout).await;
2519
peer.close();
2520
offer
2519
2521
}
2520
2522
2521
2523
#[tokio::test]
@@ -2536,7 +2538,9 @@ mod tests {
2536
2538
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);
2537
2539
let track = factory.create_audio_track("microphone", source);
2538
2540
let offer = include_str!("../tests/fixtures/teams_p2p_offer.sdp");
2539
if let Err(error) = answer_offer(&peer, Some(&track), offer, AnswerVideo::Receive).await {
2541
let answered = answer_offer(&peer, Some(&track), offer, AnswerVideo::Receive).await;
2542
peer.close();
2543
if let Err(error) = answered {
2540
2544
panic!("{error}");
2541
2545
}
2542
2546
}
@@ -2515,7 +2515,9 @@ mod tests {2515
let peer = factory.create_peer_connection(RtcConfiguration::default()).unwrap();2515
let peer = factory.create_peer_connection(RtcConfiguration::default()).unwrap();2516
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);2516
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);2517
let track = factory.create_audio_track("microphone", source);2517
let track = factory.create_audio_track("microphone", source);2518
create_offer(&peer, &factory, &track, layout).await2518
let offer = create_offer(&peer, &factory, &track, layout).await;2519
peer.close();2520
offer2519
}2521
}2520
2522
2521
#[tokio::test]2523
#[tokio::test]@@ -2536,7 +2538,9 @@ mod tests {2536
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);2538
let source = NativeAudioSource::new(AudioSourceOptions::default(), SAMPLE_RATE, 1, 100);2537
let track = factory.create_audio_track("microphone", source);2539
let track = factory.create_audio_track("microphone", source);2538
let offer = include_str!("../tests/fixtures/teams_p2p_offer.sdp");2540
let offer = include_str!("../tests/fixtures/teams_p2p_offer.sdp");2539
if let Err(error) = answer_offer(&peer, Some(&track), offer, AnswerVideo::Receive).await {2541
let answered = answer_offer(&peer, Some(&track), offer, AnswerVideo::Receive).await;2542
peer.close();2543
if let Err(error) = answered {2540
panic!("{error}");2544
panic!("{error}");2541
}2545
}2542
}2546
}@@ -1,4 +1,4 @@
1
use chrono::{DateTime, Datelike, Utc, Weekday};
1
use chrono::{DateTime, Datelike, NaiveDateTime, Utc, Weekday};
2
2
use serde_json::{Value, json};
3
3
use session::{GRAPH, Method, Request, Scope};
4
4
@@ -59,6 +59,9 @@ pub struct NewEvent {
59
59
pub subject: String,
60
60
pub start: DateTime<Utc>,
61
61
pub end: DateTime<Utc>,
62
pub local_start: NaiveDateTime,
63
pub local_end: NaiveDateTime,
64
pub time_zone_name: Option<String>,
62
65
pub attendees: Vec<Attendee>,
63
66
pub repeat: Repeat,
64
67
pub details: String,
@@ -99,14 +102,20 @@ fn day_name(weekday: Weekday) -> &'static str {
99
102
}
100
103
}
101
104
102
fn graph_time(time: DateTime<Utc>) -> Value {
103
json!({
104
"dateTime": time.format(GRAPH_TIME_FORMAT).to_string(),
105
"timeZone": TIME_ZONE,
106
})
105
fn graph_time(event: &NewEvent, local: NaiveDateTime, utc: DateTime<Utc>) -> Value {
106
match &event.time_zone_name {
107
Some(time_zone_name) => json!({
108
"dateTime": local.format(GRAPH_TIME_FORMAT).to_string(),
109
"timeZone": time_zone_name,
110
}),
111
None => json!({
112
"dateTime": utc.format(GRAPH_TIME_FORMAT).to_string(),
113
"timeZone": TIME_ZONE,
114
}),
115
}
107
116
}
108
117
109
fn recurrence(repeat: Repeat, start: DateTime<Utc>) -> Option<Value> {
118
fn recurrence(repeat: Repeat, start: NaiveDateTime) -> Option<Value> {
110
119
let (pattern_type, days): (&str, Vec<Weekday>) = match repeat {
111
120
Repeat::Never => return None,
112
121
Repeat::Daily => ("daily", Vec::new()),
@@ -130,8 +139,8 @@ fn recurrence(repeat: Repeat, start: DateTime<Utc>) -> Option<Value> {
130
139
pub fn new_event_body(event: &NewEvent) -> Value {
131
140
let mut body = json!({
132
141
"subject": event.subject,
133
"start": graph_time(event.start),
134
"end": graph_time(event.end),
142
"start": graph_time(event, event.local_start, event.start),
143
"end": graph_time(event, event.local_end, event.end),
135
144
"attendees": event.attendees.iter().map(|attendee| json!({
136
145
"type": "required",
137
146
"emailAddress": {"address": attendee.address, "name": attendee.name},
@@ -142,7 +151,7 @@ pub fn new_event_body(event: &NewEvent) -> Value {
142
151
if !event.details.trim().is_empty() {
143
152
body["body"] = json!({"contentType": "text", "content": event.details});
144
153
}
145
if let Some(recurrence) = recurrence(event.repeat, event.start) {
154
if let Some(recurrence) = recurrence(event.repeat, event.local_start) {
146
155
body["recurrence"] = recurrence;
147
156
}
148
157
body
@@ -287,11 +296,33 @@ mod tests {
287
296
}
288
297
}
289
298
299
fn local(year: i32, month: u32, day: u32, hour: u32, minute: u32) -> NaiveDateTime {
300
chrono::NaiveDate::from_ymd_opt(year, month, day)
301
.unwrap()
302
.and_hms_opt(hour, minute, 0)
303
.unwrap()
304
}
305
306
fn zoned(repeat: Repeat, local_start: NaiveDateTime, offset_hours: i64) -> NewEvent {
307
let utc_start = local_start.and_utc() - chrono::Duration::hours(offset_hours);
308
NewEvent {
309
start: utc_start,
310
end: utc_start + chrono::Duration::minutes(30),
311
local_start,
312
local_end: local_start + chrono::Duration::minutes(30),
313
time_zone_name: Some("W. Europe Standard Time".into()),
314
..event(repeat)
315
}
316
}
317
290
318
fn event(repeat: Repeat) -> NewEvent {
291
319
NewEvent {
292
320
subject: "Planning".into(),
293
321
start: Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap(),
294
322
end: Utc.with_ymd_and_hms(2026, 10, 14, 10, 0, 0).unwrap(),
323
local_start: local(2026, 10, 14, 11, 30),
324
local_end: local(2026, 10, 14, 12, 0),
325
time_zone_name: None,
295
326
attendees: vec![Attendee {
296
327
name: "Mara Lindqvist".into(),
297
328
address: "mara@example.invalid".into(),
@@ -319,6 +350,49 @@ mod tests {
319
350
assert!(body.get("body").is_none());
320
351
}
321
352
353
#[test]
354
fn summer_and_winter_series_keep_the_same_local_time() {
355
let summer = new_event_body(&zoned(Repeat::Weekly, local(2026, 7, 15, 9, 0), 2));
356
let winter = new_event_body(&zoned(Repeat::Weekly, local(2026, 12, 16, 9, 0), 1));
357
assert_eq!(summer["start"]["dateTime"], "2026-07-15T09:00:00");
358
assert_eq!(winter["start"]["dateTime"], "2026-12-16T09:00:00");
359
for body in [&summer, &winter] {
360
assert_eq!(body["start"]["timeZone"], "W. Europe Standard Time");
361
assert_eq!(body["end"]["dateTime"].as_str().unwrap()[11..], *"09:30:00");
362
assert_eq!(body["end"]["timeZone"], "W. Europe Standard Time");
363
}
364
}
365
366
#[test]
367
fn a_local_half_past_midnight_keeps_its_local_weekday_and_start_date() {
368
let body = new_event_body(&zoned(Repeat::Weekly, local(2026, 10, 14, 0, 30), 2));
369
assert_eq!(body["start"]["dateTime"], "2026-10-14T00:30:00");
370
assert_eq!(body["recurrence"]["range"]["startDate"], "2026-10-14");
371
assert_eq!(
372
body["recurrence"]["pattern"]["daysOfWeek"],
373
json!(["wednesday"])
374
);
375
}
376
377
#[test]
378
fn without_a_zone_name_times_stay_utc_but_the_date_is_local() {
379
let event = NewEvent {
380
start: Utc.with_ymd_and_hms(2026, 10, 13, 22, 30, 0).unwrap(),
381
end: Utc.with_ymd_and_hms(2026, 10, 13, 23, 0, 0).unwrap(),
382
local_start: local(2026, 10, 14, 0, 30),
383
local_end: local(2026, 10, 14, 1, 0),
384
..event(Repeat::Weekly)
385
};
386
let body = new_event_body(&event);
387
assert_eq!(body["start"]["dateTime"], "2026-10-13T22:30:00");
388
assert_eq!(body["start"]["timeZone"], "UTC");
389
assert_eq!(body["recurrence"]["range"]["startDate"], "2026-10-14");
390
assert_eq!(
391
body["recurrence"]["pattern"]["daysOfWeek"],
392
json!(["wednesday"])
393
);
394
}
395
322
396
#[test]
323
397
fn details_become_a_plain_text_body() {
324
398
let body = new_event_body(&NewEvent {
@@ -1,4 +1,4 @@1
use chrono::{DateTime, Datelike, Utc, Weekday};1
use chrono::{DateTime, Datelike, NaiveDateTime, Utc, Weekday};2
use serde_json::{Value, json};2
use serde_json::{Value, json};3
use session::{GRAPH, Method, Request, Scope};3
use session::{GRAPH, Method, Request, Scope};4
4
@@ -59,6 +59,9 @@ pub struct NewEvent {59
pub subject: String,59
pub subject: String,60
pub start: DateTime<Utc>,60
pub start: DateTime<Utc>,61
pub end: DateTime<Utc>,61
pub end: DateTime<Utc>,62
pub local_start: NaiveDateTime,63
pub local_end: NaiveDateTime,64
pub time_zone_name: Option<String>,62
pub attendees: Vec<Attendee>,65
pub attendees: Vec<Attendee>,63
pub repeat: Repeat,66
pub repeat: Repeat,64
pub details: String,67
pub details: String,@@ -99,14 +102,20 @@ fn day_name(weekday: Weekday) -> &'static str {99
}102
}100
}103
}101
104
102
fn graph_time(time: DateTime<Utc>) -> Value {105
fn graph_time(event: &NewEvent, local: NaiveDateTime, utc: DateTime<Utc>) -> Value {103
json!({106
match &event.time_zone_name {104
"dateTime": time.format(GRAPH_TIME_FORMAT).to_string(),107
Some(time_zone_name) => json!({105
"timeZone": TIME_ZONE,108
"dateTime": local.format(GRAPH_TIME_FORMAT).to_string(),106
})109
"timeZone": time_zone_name,110
}),111
None => json!({112
"dateTime": utc.format(GRAPH_TIME_FORMAT).to_string(),113
"timeZone": TIME_ZONE,114
}),115
}107
}116
}108
117
109
fn recurrence(repeat: Repeat, start: DateTime<Utc>) -> Option<Value> {118
fn recurrence(repeat: Repeat, start: NaiveDateTime) -> Option<Value> {110
let (pattern_type, days): (&str, Vec<Weekday>) = match repeat {119
let (pattern_type, days): (&str, Vec<Weekday>) = match repeat {111
Repeat::Never => return None,120
Repeat::Never => return None,112
Repeat::Daily => ("daily", Vec::new()),121
Repeat::Daily => ("daily", Vec::new()),@@ -130,8 +139,8 @@ fn recurrence(repeat: Repeat, start: DateTime<Utc>) -> Option<Value> {130
pub fn new_event_body(event: &NewEvent) -> Value {139
pub fn new_event_body(event: &NewEvent) -> Value {131
let mut body = json!({140
let mut body = json!({132
"subject": event.subject,141
"subject": event.subject,133
"start": graph_time(event.start),142
"start": graph_time(event, event.local_start, event.start),134
"end": graph_time(event.end),143
"end": graph_time(event, event.local_end, event.end),135
"attendees": event.attendees.iter().map(|attendee| json!({144
"attendees": event.attendees.iter().map(|attendee| json!({136
"type": "required",145
"type": "required",137
"emailAddress": {"address": attendee.address, "name": attendee.name},146
"emailAddress": {"address": attendee.address, "name": attendee.name},@@ -142,7 +151,7 @@ pub fn new_event_body(event: &NewEvent) -> Value {142
if !event.details.trim().is_empty() {151
if !event.details.trim().is_empty() {143
body["body"] = json!({"contentType": "text", "content": event.details});152
body["body"] = json!({"contentType": "text", "content": event.details});144
}153
}145
if let Some(recurrence) = recurrence(event.repeat, event.start) {154
if let Some(recurrence) = recurrence(event.repeat, event.local_start) {146
body["recurrence"] = recurrence;155
body["recurrence"] = recurrence;147
}156
}148
body157
body@@ -287,11 +296,33 @@ mod tests {287
}296
}288
}297
}289
298
299
fn local(year: i32, month: u32, day: u32, hour: u32, minute: u32) -> NaiveDateTime {300
chrono::NaiveDate::from_ymd_opt(year, month, day)301
.unwrap()302
.and_hms_opt(hour, minute, 0)303
.unwrap()304
}305
306
fn zoned(repeat: Repeat, local_start: NaiveDateTime, offset_hours: i64) -> NewEvent {307
let utc_start = local_start.and_utc() - chrono::Duration::hours(offset_hours);308
NewEvent {309
start: utc_start,310
end: utc_start + chrono::Duration::minutes(30),311
local_start,312
local_end: local_start + chrono::Duration::minutes(30),313
time_zone_name: Some("W. Europe Standard Time".into()),314
..event(repeat)315
}316
}317
290
fn event(repeat: Repeat) -> NewEvent {318
fn event(repeat: Repeat) -> NewEvent {291
NewEvent {319
NewEvent {292
subject: "Planning".into(),320
subject: "Planning".into(),293
start: Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap(),321
start: Utc.with_ymd_and_hms(2026, 10, 14, 9, 30, 0).unwrap(),294
end: Utc.with_ymd_and_hms(2026, 10, 14, 10, 0, 0).unwrap(),322
end: Utc.with_ymd_and_hms(2026, 10, 14, 10, 0, 0).unwrap(),323
local_start: local(2026, 10, 14, 11, 30),324
local_end: local(2026, 10, 14, 12, 0),325
time_zone_name: None,295
attendees: vec![Attendee {326
attendees: vec![Attendee {296
name: "Mara Lindqvist".into(),327
name: "Mara Lindqvist".into(),297
address: "mara@example.invalid".into(),328
address: "mara@example.invalid".into(),@@ -319,6 +350,49 @@ mod tests {319
assert!(body.get("body").is_none());350
assert!(body.get("body").is_none());320
}351
}321
352
353
#[test]354
fn summer_and_winter_series_keep_the_same_local_time() {355
let summer = new_event_body(&zoned(Repeat::Weekly, local(2026, 7, 15, 9, 0), 2));356
let winter = new_event_body(&zoned(Repeat::Weekly, local(2026, 12, 16, 9, 0), 1));357
assert_eq!(summer["start"]["dateTime"], "2026-07-15T09:00:00");358
assert_eq!(winter["start"]["dateTime"], "2026-12-16T09:00:00");359
for body in [&summer, &winter] {360
assert_eq!(body["start"]["timeZone"], "W. Europe Standard Time");361
assert_eq!(body["end"]["dateTime"].as_str().unwrap()[11..], *"09:30:00");362
assert_eq!(body["end"]["timeZone"], "W. Europe Standard Time");363
}364
}365
366
#[test]367
fn a_local_half_past_midnight_keeps_its_local_weekday_and_start_date() {368
let body = new_event_body(&zoned(Repeat::Weekly, local(2026, 10, 14, 0, 30), 2));369
assert_eq!(body["start"]["dateTime"], "2026-10-14T00:30:00");370
assert_eq!(body["recurrence"]["range"]["startDate"], "2026-10-14");371
assert_eq!(372
body["recurrence"]["pattern"]["daysOfWeek"],373
json!(["wednesday"])374
);375
}376
377
#[test]378
fn without_a_zone_name_times_stay_utc_but_the_date_is_local() {379
let event = NewEvent {380
start: Utc.with_ymd_and_hms(2026, 10, 13, 22, 30, 0).unwrap(),381
end: Utc.with_ymd_and_hms(2026, 10, 13, 23, 0, 0).unwrap(),382
local_start: local(2026, 10, 14, 0, 30),383
local_end: local(2026, 10, 14, 1, 0),384
..event(Repeat::Weekly)385
};386
let body = new_event_body(&event);387
assert_eq!(body["start"]["dateTime"], "2026-10-13T22:30:00");388
assert_eq!(body["start"]["timeZone"], "UTC");389
assert_eq!(body["recurrence"]["range"]["startDate"], "2026-10-14");390
assert_eq!(391
body["recurrence"]["pattern"]["daysOfWeek"],392
json!(["wednesday"])393
);394
}395
322
#[test]396
#[test]323
fn details_become_a_plain_text_body() {397
fn details_become_a_plain_text_body() {324
let body = new_event_body(&NewEvent {398
let body = new_event_body(&NewEvent {