acter_core/models/tasks/
task.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
use matrix_sdk_base::ruma::{
    events::OriginalMessageLikeEvent, EventId, OwnedUserId, RoomId, UserId,
};
use serde::{Deserialize, Serialize};
use std::ops::Deref;

use super::{
    super::{default_model_execute, ActerModel, AnyActerModel, Capability, EventMeta, Store},
    KEYS,
};
use crate::{
    events::tasks::{
        TaskEventContent, TaskSelfAssignEventContent, TaskSelfUnassignEventContent,
        TaskUpdateBuilder, TaskUpdateEventContent,
    },
    Result,
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Task {
    inner: TaskEventContent,
    pub meta: EventMeta,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    assignees: Vec<OwnedUserId>,
}

impl Deref for Task {
    type Target = TaskEventContent;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl Task {
    pub fn title(&self) -> &String {
        &self.inner.title
    }

    pub fn assignees(&self) -> Vec<OwnedUserId> {
        self.assignees.clone()
    }

    pub fn room_id(&self) -> &RoomId {
        &self.meta.room_id
    }

    pub fn sender(&self) -> &UserId {
        &self.meta.sender
    }

    pub fn is_done(&self) -> bool {
        self.inner
            .progress_percent
            .map(|u| u >= 100)
            .unwrap_or_default()
    }

    pub fn is_assigned(&self, user_id: &UserId) -> bool {
        self.assignees.iter().any(|o| o == user_id)
    }

    pub fn percent(&self) -> Option<u8> {
        self.inner.progress_percent
    }

    pub fn due_date(&self) -> Option<String> {
        self.inner
            .due_date
            .map(|d| d.format("%Y-%m-%d").to_string())
    }

    pub fn utc_due_time_of_day(&self) -> Option<i32> {
        self.inner.utc_due_time_of_day
    }

    pub fn utc_start_rfc3339(&self) -> Option<String> {
        self.inner
            .utc_start
            .as_ref()
            .map(|d| d.to_rfc3339_opts(chrono::SecondsFormat::Secs, true))
    }

    pub fn updater(&self) -> TaskUpdateBuilder {
        TaskUpdateBuilder::default()
            .task(self.meta.event_id.clone())
            .to_owned()
    }

    pub fn self_assign_event_content(&self) -> TaskSelfAssignEventContent {
        TaskSelfAssignEventContent {
            task: self.meta.event_id.clone().into(),
        }
    }

    pub fn self_unassign_event_content(&self) -> TaskSelfUnassignEventContent {
        TaskSelfUnassignEventContent {
            task: self.meta.event_id.clone().into(),
        }
    }
}

impl ActerModel for Task {
    fn indizes(&self, user_id: &UserId) -> Vec<String> {
        let tasks_key = KEYS::TASKS;
        let task_list_id_idx = format!("{}::{tasks_key}", self.inner.task_list_id.event_id);
        if self.is_assigned(user_id) {
            if self.is_done() {
                return vec![KEYS::MY_DONE_TASKS.to_owned(), task_list_id_idx];
            } else {
                return vec![KEYS::MY_OPEN_TASKS.to_owned(), task_list_id_idx];
            }
        }
        // not mine
        vec![task_list_id_idx]
    }

    fn event_id(&self) -> &EventId {
        &self.meta.event_id
    }
    fn room_id(&self) -> &RoomId {
        &self.meta.room_id
    }

    fn capabilities(&self) -> &[Capability] {
        &[
            Capability::Commentable,
            Capability::Attachmentable,
            Capability::Reactable,
        ]
    }

    async fn execute(self, store: &Store) -> Result<Vec<String>> {
        default_model_execute(store, self.into()).await
    }

    fn belongs_to(&self) -> Option<Vec<String>> {
        Some(vec![self.inner.task_list_id.event_id.to_string()])
    }

    fn transition(&mut self, model: &AnyActerModel) -> Result<bool> {
        match model {
            AnyActerModel::TaskUpdate(update) => update.apply(&mut self.inner),
            AnyActerModel::TaskSelfAssign(update) => update.apply(self),
            AnyActerModel::TaskSelfUnassign(update) => update.apply(self),
            _ => Ok(false),
        }
    }
}

impl From<OriginalMessageLikeEvent<TaskEventContent>> for Task {
    fn from(outer: OriginalMessageLikeEvent<TaskEventContent>) -> Self {
        let OriginalMessageLikeEvent {
            content,
            room_id,
            event_id,
            sender,
            origin_server_ts,
            ..
        } = outer;
        Task {
            inner: content,
            assignees: Vec::with_capacity(0),
            meta: EventMeta {
                room_id,
                event_id,
                sender,
                origin_server_ts,
                redacted: None,
            },
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskUpdate {
    inner: TaskUpdateEventContent,
    meta: EventMeta,
}

impl ActerModel for TaskUpdate {
    fn indizes(&self, _user_id: &UserId) -> Vec<String> {
        vec![format!("{:}::history", self.inner.task.event_id)]
    }

    fn event_id(&self) -> &EventId {
        &self.meta.event_id
    }
    fn room_id(&self) -> &RoomId {
        &self.meta.room_id
    }

    async fn execute(self, store: &Store) -> Result<Vec<String>> {
        default_model_execute(store, self.into()).await
    }

    fn belongs_to(&self) -> Option<Vec<String>> {
        Some(vec![self.inner.task.event_id.to_string()])
    }
}

impl Deref for TaskUpdate {
    type Target = TaskUpdateEventContent;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl From<OriginalMessageLikeEvent<TaskUpdateEventContent>> for TaskUpdate {
    fn from(outer: OriginalMessageLikeEvent<TaskUpdateEventContent>) -> Self {
        let OriginalMessageLikeEvent {
            content,
            room_id,
            event_id,
            sender,
            origin_server_ts,
            ..
        } = outer;
        TaskUpdate {
            inner: content,
            meta: EventMeta {
                room_id,
                event_id,
                sender,
                origin_server_ts,
                redacted: None,
            },
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskSelfAssign {
    inner: TaskSelfAssignEventContent,
    meta: EventMeta,
}

impl TaskSelfAssign {
    fn apply(&self, task: &mut Task) -> Result<bool> {
        let new_user_id = self.meta.sender.clone();
        // remove any existing instance of the user in the list.
        task.assignees.retain(|u| u != &new_user_id);
        // add it at the new first entry;
        task.assignees.insert(0, new_user_id);
        Ok(true)
    }
}

impl ActerModel for TaskSelfAssign {
    fn indizes(&self, _user_id: &UserId) -> Vec<String> {
        vec![format!("{:}::history", self.inner.task.event_id)]
    }

    fn event_id(&self) -> &EventId {
        &self.meta.event_id
    }
    fn room_id(&self) -> &RoomId {
        &self.meta.room_id
    }

    async fn execute(self, store: &Store) -> Result<Vec<String>> {
        default_model_execute(store, self.into()).await
    }

    fn belongs_to(&self) -> Option<Vec<String>> {
        Some(vec![self.inner.task.event_id.to_string()])
    }
}

impl From<OriginalMessageLikeEvent<TaskSelfAssignEventContent>> for TaskSelfAssign {
    fn from(outer: OriginalMessageLikeEvent<TaskSelfAssignEventContent>) -> Self {
        let OriginalMessageLikeEvent {
            content,
            room_id,
            event_id,
            sender,
            origin_server_ts,
            ..
        } = outer;
        TaskSelfAssign {
            inner: content,
            meta: EventMeta {
                room_id,
                event_id,
                sender,
                origin_server_ts,
                redacted: None,
            },
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskSelfUnassign {
    inner: TaskSelfUnassignEventContent,
    meta: EventMeta,
}

impl TaskSelfUnassign {
    fn apply(&self, task: &mut Task) -> Result<bool> {
        let new_user_id = self.meta.sender.clone();
        // remove the user from the list.
        task.assignees.retain(|u| u != &new_user_id);
        Ok(true)
    }
}

impl ActerModel for TaskSelfUnassign {
    fn indizes(&self, _user_id: &UserId) -> Vec<String> {
        vec![format!("{:}::history", self.inner.task.event_id)]
    }

    fn event_id(&self) -> &EventId {
        &self.meta.event_id
    }
    fn room_id(&self) -> &RoomId {
        &self.meta.room_id
    }

    async fn execute(self, store: &Store) -> Result<Vec<String>> {
        default_model_execute(store, self.into()).await
    }

    fn belongs_to(&self) -> Option<Vec<String>> {
        Some(vec![self.inner.task.event_id.to_string()])
    }
}

impl From<OriginalMessageLikeEvent<TaskSelfUnassignEventContent>> for TaskSelfUnassign {
    fn from(outer: OriginalMessageLikeEvent<TaskSelfUnassignEventContent>) -> Self {
        let OriginalMessageLikeEvent {
            content,
            room_id,
            event_id,
            sender,
            origin_server_ts,
            ..
        } = outer;
        TaskSelfUnassign {
            inner: content,
            meta: EventMeta {
                room_id,
                event_id,
                sender,
                origin_server_ts,
                redacted: None,
            },
        }
    }
}