acter/api/
bookmarks.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
use acter_core::events::bookmarks::BookmarksEventContent;
use anyhow::Result;
use matrix_sdk::Account;

use crate::RUNTIME;

pub struct Bookmarks {
    inner: BookmarksEventContent,
    account: Account,
}

impl Bookmarks {
    pub fn entries(&self, key: String) -> Vec<String> {
        let lowered = key.to_lowercase();
        match lowered.as_str() {
            "pins" => self.inner.pins.clone(),
            "news" => self.inner.news.clone(),
            "events" => self.inner.events.clone(),
            "tasks" => self.inner.tasks.clone(),
            "task_lists" => self.inner.task_lists.clone(),
            _ => self.inner.other.get(&lowered).cloned().unwrap_or_default(),
        }
    }

    pub async fn add(&self, key: String, entry: String) -> Result<bool> {
        let mut inner = self.inner.clone();
        let lowered = key.to_lowercase();
        match lowered.as_str() {
            "pins" => {
                inner.pins.push(entry);
                inner.pins.dedup();
            }
            "news" => {
                inner.news.push(entry);
                inner.news.dedup();
            }
            "events" => {
                inner.events.push(entry);
                inner.events.dedup();
            }
            "tasks" => {
                inner.tasks.push(entry);
                inner.tasks.dedup();
            }
            "task_lists" => {
                inner.task_lists.push(entry);
                inner.task_lists.dedup();
            }
            _ => {
                inner
                    .other
                    .entry(lowered)
                    .and_modify(|entries| {
                        entries.push(entry.clone());
                        entries.dedup();
                    })
                    .or_insert_with(|| vec![entry]);
            }
        };

        self.submit(inner).await?;

        Ok(true)
    }

    pub async fn remove(&self, key: String, entry: String) -> Result<bool> {
        let mut inner = self.inner.clone();
        let lowered = key.to_lowercase();
        let has_updated = match lowered.as_str() {
            "pins" => {
                if let Some(pos) = inner.pins.iter().position(|e| e == &entry) {
                    inner.pins.remove(pos);
                    true
                } else {
                    false
                }
            }
            "news" => {
                if let Some(pos) = inner.news.iter().position(|e| e == &entry) {
                    inner.news.remove(pos);
                    true
                } else {
                    false
                }
            }
            "events" => {
                if let Some(pos) = inner.events.iter().position(|e| e == &entry) {
                    inner.events.remove(pos);
                    true
                } else {
                    false
                }
            }
            "tasks" => {
                if let Some(pos) = inner.tasks.iter().position(|e| e == &entry) {
                    inner.tasks.remove(pos);
                    true
                } else {
                    false
                }
            }
            "task_lists" => {
                if let Some(pos) = inner.task_lists.iter().position(|e| e == &entry) {
                    inner.task_lists.remove(pos);
                    true
                } else {
                    false
                }
            }
            _ => {
                if let Some(entries) = inner.other.get_mut(&lowered) {
                    if let Some(pos) = entries.iter().position(|e| e == &entry) {
                        entries.remove(pos);
                        true
                    } else {
                        false
                    }
                } else {
                    false
                }
            }
        };
        if (has_updated) {
            self.submit(inner).await?;
        }

        Ok(has_updated)
    }

    async fn submit(&self, content: BookmarksEventContent) -> Result<()> {
        let account = self.account.clone();
        let resp = RUNTIME
            .spawn(async move { account.set_account_data(content).await })
            .await??;
        Ok(())
    }
}

impl crate::Account {
    pub async fn bookmarks(&self) -> Result<Bookmarks> {
        let account = self.account.clone();
        let inner = RUNTIME
            .spawn(async move {
                let raw = account.account_data::<BookmarksEventContent>().await?;

                anyhow::Ok(if let Some(o) = raw {
                    o.deserialize().map(Option::Some)?
                } else {
                    None
                })
            })
            .await??
            .unwrap_or_default();

        Ok(Bookmarks {
            inner,
            account: self.account.clone(),
        })
    }
}