acter_core/events/
tasks.rs

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use chrono_tz::Tz;
use core::result::Result as CoreResult;
use derive_builder::Builder;
use derive_getters::Getters;
use matrix_sdk_base::ruma::events::{macros::EventContent, room::message::TextMessageEventContent};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use tracing::trace;

/// ToDo Lists and Task Items management
/// modeled after [JMAP Tasks](https://jmap.io/spec-tasks.html), extensions to
/// [ietf rfc8984](https://www.rfc-editor.org/rfc/rfc8984.html#name-task).
///
use super::{BelongsTo, Date, Display, Update, UtcDateTime};
use crate::{util::deserialize_some, Result as ActerResult};

#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum SpecialTaskListRole {
    Inbox,
    Trash,
}

#[derive(Clone, Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug)]
#[repr(u8)]
/// Implementing Priority according to
/// <https://www.rfc-editor.org/rfc/rfc8984.html#section-4.4.1>
#[derive(Default)]
pub enum Priority {
    #[default]
    Undefined = 0,
    Highest = 1,
    SecondHighest = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Seven = 7,
    SecondLowest = 8,
    Lowest = 9,
}

impl Priority {
    fn is_undefinied(&self) -> bool {
        matches!(self, Priority::Undefined)
    }
}

/// The TaskList Event
///
/// modeled after [JMAP TaskList](https://jmap.io/spec-tasks.html#tasklists)
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.tasklist", kind = MessageLike)]
#[builder(name = "TaskListBuilder", derive(Debug))]
pub struct TaskListEventContent {
    pub name: String,

    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<SpecialTaskListRole>,

    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<TextMessageEventContent>,

    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display: Option<Display>,

    #[builder(default)]
    #[serde(default)]
    pub sort_order: u32,

    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub time_zone: Option<Tz>,

    // 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>,
}

/// The TaskList Event
///
/// modeled after [JMAP TaskList](https://jmap.io/spec-tasks.html#tasklists)
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder)]
#[ruma_event(type = "global.acter.dev.tasklist.update", kind = MessageLike)]
#[builder(name = "TaskListUpdateBuilder", derive(Debug))]
pub struct TaskListUpdateEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub task_list: Update,

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

    #[builder(setter(into), default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub role: Option<Option<SpecialTaskListRole>>,

    #[builder(setter(into), default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub description: Option<Option<TextMessageEventContent>>,

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

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

    #[builder(setter(into), default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub time_zone: Option<Option<Tz>>,

    // 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 = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub keywords: Option<Vec<String>>,

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

impl TaskListUpdateEventContent {
    pub fn apply(&self, task_list: &mut TaskListEventContent) -> ActerResult<bool> {
        let mut updated = false;
        if let Some(name) = &self.name {
            task_list.name.clone_from(name);
            updated = true;
        }
        if let Some(role) = &self.role {
            task_list.role.clone_from(role);
            updated = true;
        }
        if let Some(description) = &self.description {
            task_list.description.clone_from(description);
            updated = true;
        }
        if let Some(display) = &self.display {
            task_list.display.clone_from(display);
            updated = true;
        }
        if let Some(sort_order) = &self.sort_order {
            task_list.sort_order = *sort_order;
            updated = true;
        }
        if let Some(time_zone) = &self.time_zone {
            task_list.time_zone = *time_zone;
            updated = true;
        }
        if let Some(keywords) = &self.keywords {
            task_list.keywords.clone_from(keywords);
            updated = true;
        }
        if let Some(categories) = &self.categories {
            task_list.categories.clone_from(categories);
            updated = true;
        }

        trace!(update = ?self, ?updated, ?task_list, "TaskList updated");

        Ok(updated)
    }
}

/// The Task Event
///
/// modeled after [JMAP Task](https://jmap.io/spec-tasks.html#tasks)
/// see also the [IETF Task](https://www.rfc-editor.org/rfc/rfc8984.html#name-task)
/// but all timezones have been dumbed down to UTC-only.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.task", kind = MessageLike)]
#[builder(
    name = "TaskBuilder",
    build_fn(validate = "Self::validate"),
    derive(Debug)
)]
pub struct TaskEventContent {
    /// The title of the Task
    pub title: String,

    /// Every tasks belongs to a tasklist
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub task_list_id: BelongsTo,

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

    /// Which day is this task due
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub due_date: Option<Date>,

    /// Any particular time this task is due as seconds since/to midnight UTC
    /// make sure to include any
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub utc_due_time_of_day: Option<i32>,

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

    /// How far along is this task in percent (everything > 100: = 100)
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub progress_percent: Option<u8>,

    /// Sort order within the TaskList
    #[builder(default)]
    #[serde(default)]
    pub sort_order: u32,

    /// the priority of the Task
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Priority::is_undefinied")]
    pub priority: Priority,

    /// Display this task
    #[builder(setter(into), default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display: Option<Display>,

    // 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 TaskBuilder {
    fn validate(&self) -> CoreResult<(), String> {
        if let Some(Some(percent)) = &self.progress_percent {
            if *percent > 100 {
                return Err("Progress percent can’t be higher than 100".to_string());
            }
        }
        Ok(())
    }
}

/// The Task Update Event
///
/// modeled after [JMAP Task](https://jmap.io/spec-tasks.html#tasks)
/// see also the [IETF Task](https://www.rfc-editor.org/rfc/rfc8984.html#name-task)
/// but all timezones have been dumbed down to UTC-only.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder)]
#[ruma_event(type = "global.acter.dev.task.update", kind = MessageLike)]
#[builder(name = "TaskUpdateBuilder", derive(Debug))]
pub struct TaskUpdateEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub task: Update,

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

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

    /// Day when is this task due
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub due_date: Option<Option<Date>>,

    /// Specific time on the day is this task due
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub utc_due_time_of_day: Option<Option<i32>>,

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

    /// How far along is this task in percent (everything > 100: = 100)
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub progress_percent: Option<Option<u8>>,

    /// Sort order within the TaskList
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub sort_order: Option<u32>,

    /// the priority of the Task
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub priority: Option<Priority>,

    /// Display this task
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub display: Option<Option<Display>>,

    // 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>>,
}

impl TaskUpdateEventContent {
    pub fn apply(&self, task: &mut TaskEventContent) -> ActerResult<bool> {
        let mut updated = false;
        if let Some(title) = &self.title {
            task.title.clone_from(title);
            updated = true;
        }
        if let Some(description) = &self.description {
            task.description.clone_from(description);
            updated = true;
        }
        if let Some(due_date) = &self.due_date {
            task.due_date = *due_date;
            updated = true;
        }
        if let Some(utc_due_time_of_day) = &self.utc_due_time_of_day {
            task.utc_due_time_of_day = *utc_due_time_of_day;
            updated = true;
        }
        if let Some(utc_start) = &self.utc_start {
            task.utc_start = *utc_start;
            updated = true;
        }
        if let Some(progress_percent) = &self.progress_percent {
            task.progress_percent = *progress_percent;
            updated = true;
        }
        if let Some(sort_order) = &self.sort_order {
            task.sort_order = *sort_order;
            updated = true;
        }
        if let Some(priority) = &self.priority {
            task.priority = priority.clone();
            updated = true;
        }
        if let Some(display) = &self.display {
            task.display.clone_from(display);
            updated = true;
        }
        if let Some(keywords) = &self.keywords {
            task.keywords.clone_from(keywords);
            updated = true;
        }
        if let Some(categories) = &self.categories {
            task.categories.clone_from(categories);
            updated = true;
        }

        trace!(update = ?self, ?updated, ?task, "Task updated");

        Ok(updated)
    }
}

/// TaskSelfAssign Event
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.task.self_assign", kind = MessageLike)]
#[builder(name = "TaskSelfAssignBuilder", derive(Debug))]
pub struct TaskSelfAssignEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub task: BelongsTo,
}

/// TaskSelfUnassign Event
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.task.self_unassign", kind = MessageLike)]
#[builder(name = "TaskSelfUnassignBuilder", derive(Debug))]
pub struct TaskSelfUnassignEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub task: BelongsTo,
}