1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use derive_builder::Builder;
use derive_getters::Getters;
use matrix_sdk_base::ruma::events::{macros::EventContent, room::message::TextMessageEventContent};
use serde::{Deserialize, Serialize};
use tracing::trace;

use crate::{models::TextMessageContent, util::deserialize_some, Result};

/// Calendar Events
/// modeled after [JMAP Calendar Events](https://jmap.io/spec-calendars.html#calendar-events), extensions to
/// [ietf rfc8984](https://www.rfc-editor.org/rfc/rfc8984.html#name-event).
///
use super::{Display, Icon, Update, UtcDateTime};

/// Event Location
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum EventLocation {
    Physical {
        /// Optional name of this location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,

        /// further description to this location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        description: Option<TextMessageEventContent>,

        /// Alternative Icon to show with this location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        icon: Option<Icon>,

        /// A `geo:` URI [RFC5870] for the location.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        coordinates: Option<String>,

        /// further Link
        #[serde(default, skip_serializing_if = "Option::is_none")]
        uri: Option<String>,
    },
    Virtual {
        /// URI to this virtual location
        uri: String,

        /// Optional name of this virtual location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,

        /// further description for virtual location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        description: Option<TextMessageEventContent>,

        /// Alternative Icon to show with this location
        #[serde(default, skip_serializing_if = "Option::is_none")]
        icon: Option<Icon>,
    },
}

pub struct EventLocationInfo {
    pub inner: EventLocation,
}

impl EventLocationInfo {
    pub fn new(location: &EventLocation) -> Self {
        match location {
            EventLocation::Physical {
                name,
                description,
                icon,
                coordinates,
                uri,
            } => EventLocationInfo {
                inner: EventLocation::Physical {
                    name: name.clone(),
                    description: description.clone(),
                    icon: icon.clone(),
                    coordinates: coordinates.clone(),
                    uri: uri.clone(),
                },
            },
            EventLocation::Virtual {
                uri,
                name,
                description,
                icon,
            } => EventLocationInfo {
                inner: EventLocation::Virtual {
                    uri: uri.clone(),
                    name: name.clone(),
                    description: description.clone(),
                    icon: icon.clone(),
                },
            },
        }
    }

    pub fn location_type(&self) -> String {
        match &self.inner {
            EventLocation::Physical { .. } => "Physical".to_string(),
            EventLocation::Virtual { .. } => "Virtual".to_string(),
        }
    }

    pub fn name(&self) -> Option<String> {
        match &self.inner {
            EventLocation::Physical { name, .. } => name.clone(),
            EventLocation::Virtual { name, .. } => name.clone(),
        }
    }

    pub fn description(&self) -> Option<TextMessageContent> {
        match &self.inner {
            EventLocation::Physical { description, .. } => {
                description.clone().map(TextMessageContent::from)
            }
            EventLocation::Virtual { description, .. } => {
                description.clone().map(TextMessageContent::from)
            }
        }
    }

    pub fn icon(&self) -> Option<Icon> {
        match &self.inner {
            EventLocation::Physical { icon, .. } => icon.clone(),
            EventLocation::Virtual { icon, .. } => icon.clone(),
        }
    }

    pub fn coordinates(&self) -> Option<String> {
        match &self.inner {
            EventLocation::Physical { coordinates, .. } => coordinates.clone(),
            _ => None,
        }
    }

    /// always available for virtual location
    pub fn uri(&self) -> Option<String> {
        match &self.inner {
            EventLocation::Physical { uri, .. } => uri.clone(),
            EventLocation::Virtual { uri, .. } => Some(uri.clone()),
        }
    }
}

/// The Calendar Event
///
/// modeled after [JMAP Calendar Events](https://jmap.io/spec-calendars.html#calendar-events)
/// see also the [IETF CalendarEvent](https://www.rfc-editor.org/rfc/rfc8984.html#name-event)
/// but all timezones have been dumbed down to UTC-only.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.calendar_event", kind = MessageLike)]
#[builder(name = "CalendarEventBuilder", derive(Debug))]
pub struct CalendarEventEventContent {
    /// The title of the CalendarEvent
    pub title: String,

    /// Further information describing the calendar_event
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<TextMessageEventContent>,

    /// Further information describing the calendar_event
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display: Option<Display>,

    /// When will this event start?
    #[builder(setter(into))]
    pub utc_start: UtcDateTime,

    /// When will this event end?
    #[builder(setter(into))]
    pub utc_end: UtcDateTime,

    /// Should this event been shown without the time?
    #[builder(default)]
    #[serde(default)]
    pub show_without_time: bool,

    /// Where is this event happening?
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub locations: Vec<EventLocation>,

    // FIXME: manage through `label` as in [MSC2326](https://github.com/matrix-org/matrix-doc/pull/2326)
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keywords: Vec<String>,

    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub categories: Vec<String>,
}

impl CalendarEventBuilder {
    pub fn into_event_loc(&mut self, loc_info: &EventLocationInfo) -> Self {
        let event_loc = loc_info.inner.clone();
        self.locations
            .as_mut()
            .expect("we have growable list")
            .push(event_loc);
        self.clone()
    }
}

/// The CalendarEvent Update Event
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder)]
#[ruma_event(type = "global.acter.dev.calendar_event.update", kind = MessageLike)]
#[builder(name = "CalendarEventUpdateBuilder", derive(Debug))]
pub struct CalendarEventUpdateEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub calendar_event: Update,

    /// The title of the CalendarEvent
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub title: Option<String>,

    /// Every calendar_events belongs to a calendar_eventlist
    /// Further information describing the calendar_event
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub description: Option<Option<TextMessageEventContent>>,

    /// When was this calendar_event started?
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub utc_start: Option<UtcDateTime>,

    /// When was this calendar_event started?
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub utc_end: Option<UtcDateTime>,

    /// Should this event been shown without the time?
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub show_without_time: Option<bool>,

    /// Where is this event happening?
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub locations: Option<Vec<EventLocation>>,

    // FIXME: manage through `label` as in [MSC2326](https://github.com/matrix-org/matrix-doc/pull/2326)
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub keywords: Option<Vec<String>>,

    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub categories: Option<Vec<String>>,

    /// Optionally some displaying parameters
    #[builder(setter(into), default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub display: Option<Option<Display>>,
}

impl CalendarEventUpdateEventContent {
    pub fn apply(&self, calendar_event: &mut CalendarEventEventContent) -> Result<bool> {
        let mut updated = false;
        if let Some(title) = &self.title {
            calendar_event.title.clone_from(title);
            updated = true;
        }

        if let Some(description) = &self.description {
            calendar_event.description.clone_from(description);
            updated = true;
        }

        if let Some(utc_start) = &self.utc_start {
            calendar_event.utc_start = *utc_start;
            updated = true;
        }

        if let Some(utc_end) = &self.utc_end {
            calendar_event.utc_end = *utc_end;
            updated = true;
        }

        if let Some(locations) = &self.locations {
            calendar_event.locations.clone_from(locations);
            updated = true;
        }

        if let Some(show_without_time) = &self.show_without_time {
            calendar_event.show_without_time = *show_without_time;
            updated = true;
        }

        if let Some(display) = &self.display {
            calendar_event.display.clone_from(display);
            updated = true;
        }

        if let Some(keywords) = &self.keywords {
            calendar_event.keywords.clone_from(keywords);
            updated = true;
        }

        if let Some(categories) = &self.categories {
            calendar_event.categories.clone_from(categories);
            updated = true;
        }

        trace!(update = ?self, ?updated, ?calendar_event, "CalendarEvent updated");

        Ok(updated)
    }
}