Skip to main content

things3_cloud/store/
state.rs

1use crate::ids::ThingsId;
2use crate::store::entities::{
3    AreaStateProps, ChecklistItemStateProps, StateObject, StateProperties, TagStateProps,
4    TaskStateProps,
5};
6use crate::wire::area::AreaPatch;
7use crate::wire::checklist::ChecklistItemPatch;
8use crate::wire::tags::TagPatch;
9use crate::wire::task::TaskPatch;
10use crate::wire::wire_object::{OperationType, Properties, WireItem, WireObject};
11use std::collections::HashMap;
12
13pub type RawState = HashMap<ThingsId, StateObject>;
14
15fn apply_task_patch(task: &mut TaskStateProps, patch: TaskPatch) {
16    if let Some(title) = patch.title {
17        task.title = title;
18    }
19    if let Some(notes) = patch.notes {
20        task.notes = notes.to_plain_text();
21    }
22    if let Some(start_location) = patch.start_location {
23        task.start_location = start_location;
24    }
25    if let Some(scheduled_date) = patch.scheduled_date {
26        task.scheduled_date = scheduled_date.map(|v| v as f64);
27    }
28    if let Some(today_index_reference) = patch.today_index_reference {
29        task.today_index_reference = today_index_reference;
30    }
31    if let Some(parent_project_ids) = patch.parent_project_ids {
32        task.parent_project_ids = parent_project_ids;
33    }
34    if let Some(area_ids) = patch.area_ids {
35        task.area_ids = area_ids;
36    }
37    if let Some(action_group_ids) = patch.action_group_ids {
38        task.action_group_ids = action_group_ids;
39    }
40    if let Some(tag_ids) = patch.tag_ids {
41        task.tag_ids = tag_ids;
42    }
43    if let Some(evening_bit) = patch.evening_bit {
44        task.evening_bit = evening_bit;
45    }
46    if let Some(modification_date) = patch.modification_date {
47        task.modification_date = Some(modification_date);
48    }
49
50    if let Some(item_type) = patch.item_type {
51        task.item_type = item_type;
52    }
53    if let Some(status) = patch.status {
54        task.status = status;
55    }
56    if let Some(stop_date) = patch.stop_date {
57        task.stop_date = stop_date;
58    }
59    if let Some(deadline) = patch.deadline {
60        task.deadline = deadline;
61    }
62    if let Some(sort_index) = patch.sort_index {
63        task.sort_index = sort_index;
64    }
65    if let Some(today_sort_index) = patch.today_sort_index {
66        task.today_sort_index = today_sort_index;
67    }
68    if let Some(recurrence_rule) = patch.recurrence_rule {
69        task.recurrence_rule = recurrence_rule;
70    }
71    if let Some(recurrence_template_ids) = patch.recurrence_template_ids {
72        task.recurrence_template_ids = recurrence_template_ids;
73    }
74    if let Some(instance_creation_paused) = patch.instance_creation_paused {
75        task.instance_creation_paused = instance_creation_paused;
76    }
77    if let Some(leaves_tombstone) = patch.leaves_tombstone {
78        task.leaves_tombstone = leaves_tombstone;
79    }
80    if let Some(trashed) = patch.trashed {
81        task.trashed = trashed;
82    }
83    if let Some(creation_date) = patch.creation_date {
84        task.creation_date = creation_date;
85    }
86}
87
88fn apply_checklist_patch(item: &mut ChecklistItemStateProps, patch: ChecklistItemPatch) {
89    if let Some(title) = patch.title {
90        item.title = title;
91    }
92    if let Some(status) = patch.status {
93        item.status = status;
94    }
95    if let Some(task_ids) = patch.task_ids {
96        item.task_ids = task_ids;
97    }
98    if let Some(sort_index) = patch.sort_index {
99        item.sort_index = sort_index;
100    }
101}
102
103fn apply_area_patch(area: &mut AreaStateProps, patch: AreaPatch) {
104    if let Some(title) = patch.title {
105        area.title = title;
106    }
107    if let Some(tag_ids) = patch.tag_ids {
108        area.tag_ids = tag_ids;
109    }
110    if let Some(sort_index) = patch.sort_index {
111        area.sort_index = sort_index;
112    }
113}
114
115fn apply_tag_patch(tag: &mut TagStateProps, patch: TagPatch) {
116    if let Some(title) = patch.title {
117        tag.title = title;
118    }
119    if let Some(parent_ids) = patch.parent_ids {
120        tag.parent_ids = parent_ids;
121    }
122    if let Some(shortcut) = patch.shortcut {
123        tag.shortcut = shortcut;
124    }
125    if let Some(sort_index) = patch.sort_index {
126        tag.sort_index = sort_index;
127    }
128}
129
130fn wire_object_properties(obj: &WireObject) -> StateProperties {
131    match obj.properties() {
132        Ok(payload) => payload.into(),
133        Err(_) => StateProperties::Other,
134    }
135}
136
137fn insert_state_object(state: &mut RawState, uuid: ThingsId, obj: WireObject) {
138    let properties = wire_object_properties(&obj);
139    state.insert(
140        uuid,
141        StateObject {
142            entity_type: obj.entity_type,
143            properties,
144        },
145    );
146}
147
148fn apply_update_payload(existing: &mut StateObject, payload: Properties) {
149    match (&mut existing.properties, payload) {
150        (StateProperties::Task(task), Properties::TaskUpdate(patch)) => {
151            apply_task_patch(task, patch);
152        }
153        (StateProperties::ChecklistItem(item), Properties::ChecklistUpdate(patch)) => {
154            apply_checklist_patch(item, patch);
155        }
156        (StateProperties::Area(area), Properties::AreaUpdate(patch)) => {
157            apply_area_patch(area, patch);
158        }
159        (StateProperties::Tag(tag), Properties::TagUpdate(patch)) => {
160            apply_tag_patch(tag, patch);
161        }
162        (_, payload) => {
163            existing.properties = payload.into();
164        }
165    }
166}
167
168pub fn fold_item(item: WireItem, state: &mut RawState) {
169    for (uuid, obj) in item {
170        let uuid = ThingsId::from(uuid);
171        match obj.operation_type {
172            OperationType::Create => {
173                insert_state_object(state, uuid, obj);
174            }
175            OperationType::Update => {
176                if let Some(existing) = state.get_mut(&uuid) {
177                    match obj.properties() {
178                        Ok(payload) => apply_update_payload(existing, payload),
179                        Err(_) => existing.properties = StateProperties::Other,
180                    }
181                    if obj.entity_type.is_some() {
182                        existing.entity_type = obj.entity_type.clone();
183                    }
184                } else {
185                    insert_state_object(state, uuid, obj);
186                }
187            }
188            OperationType::Delete => {
189                state.remove(&uuid);
190            }
191            OperationType::Unknown(_) => {}
192        }
193    }
194}
195
196pub fn fold_items(items: impl IntoIterator<Item = WireItem>) -> RawState {
197    let mut state = RawState::new();
198    for item in items {
199        fold_item(item, &mut state);
200    }
201    state
202}