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
// backend module that links the api client to a local database and provides
// sync

mod db;

use std::cmp::Ordering::{Equal, Greater, Less};
use std::collections::HashSet;
use std::fmt;
use std::path::PathBuf;
use std::process::Command;

use failure::Fallible;
use serde_derive::{Deserialize, Serialize};
use serde_json;

use log::debug;

use wallabag_api::types::{
    Annotation, Config as APIConfig, Entries, EntriesFilter, Entry, NewEntry, Tags, ID,
};
use wallabag_api::Client;

use self::db::{NewUrl, DB};

#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
pub enum StringOrCmd {
    S(String),
    Cmd { cmd: Vec<String> },
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
    #[serde(default = "default_db_file")]
    pub db_file: PathBuf,
    pub client_id: StringOrCmd,
    pub client_secret: StringOrCmd,
    pub username: StringOrCmd,
    pub password: StringOrCmd,
    pub base_url: String,
}

fn default_db_file() -> PathBuf {
    "db.sqlite3".into()
}

#[derive(Debug)]
pub struct Backend {
    db: DB,
    client: Client,
}

#[derive(Debug)]
pub enum BackendError {
    EmptyCommand,
    FailedCommand,
}
impl fmt::Display for BackendError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}
impl std::error::Error for BackendError {}

/// Get a string from possibly a command to run. Used for evaluating config values which maybe be a
/// literal string, or could be a command to evaluate and use the output.
fn get_string(x: &StringOrCmd) -> Fallible<String> {
    match x {
        StringOrCmd::S(s) => Ok(s.clone()),
        StringOrCmd::Cmd { cmd } => {
            debug!("Running command {:?}", cmd);
            let mut args = cmd.iter();

            let executable = args.next().ok_or(BackendError::EmptyCommand)?;

            let output = Command::new(executable).args(args).output()?;
            if output.status.success() {
                let output = String::from_utf8(output.stdout)?;
                Ok(output)
            } else {
                debug!("Command failed with exit status {:?}", output.status.code());
                Err(BackendError::FailedCommand)?
            }
        }
    }
}

// TODO: add method to backend to get a reference to the client for lower level stuff if required
impl Backend {
    pub fn new_with_conf(conf: Config) -> Fallible<Self> {
        let backend = Self {
            db: DB::new(conf.db_file),
            client: Client::new(APIConfig {
                client_id: get_string(&conf.client_id)?,
                client_secret: get_string(&conf.client_secret)?,
                username: get_string(&conf.username)?,
                password: get_string(&conf.password)?,
                base_url: conf.base_url,
            }),
        };
        Ok(backend)
    }

    pub fn reset(&self) -> Fallible<()> {
        self.db.reset()?;
        debug!("DB reset success");

        Ok(())
    }

    pub fn init(&self) -> Fallible<()> {
        self.db.init()?;
        debug!("DB init success");

        Ok(())
    }

    /// Get a Vec of tags from the db.
    pub fn get_entry<T: Into<ID>>(&self, id: T) -> Fallible<Option<Entry>> {
        self.db.get_entry(id)
    }

    /// Get a Vec of tags from the db.
    pub fn tags(&self) -> Fallible<Tags> {
        self.db.get_tags()
    }

    /// Get a Vec of entries from the db.
    pub fn entries(&self) -> Fallible<Entries> {
        self.db.get_all_entries()
    }

    /// Add a new url and attempts to upload and create entry immediatedly. Fails if network
    /// connection down.
    pub fn add_url_online<T: AsRef<str>>(&mut self, url: T) -> Fallible<()> {
        let url = reqwest::Url::parse(url.as_ref())?;

        let new_entry = NewEntry::new_with_url(url.into_string());
        let entry = self.client.create_entry(&new_entry)?;

        self.pull_entry(&entry)
    }

    /// Add a new url. Does not attempt to upload immediately.
    pub fn add_url<T: AsRef<str>>(&self, url: T) -> Fallible<()> {
        let url = reqwest::Url::parse(url.as_ref())?;
        self.db.add_new_url(url.as_str())
    }

    /// Full sync. Can be slow if many articles. This will sync everything,
    /// including things that can't be synced with a quick/normal sync (eg. server-side deleted
    /// items)
    ///
    /// For entries and annotations existing in the database, object with latest
    /// updated_at value wins.
    ///
    /// Tags are tied to Entries, even though the tags will also have seperate db tables dedicated
    /// to them. An updated tag should mean an updated updated_at field in an entry.
    ///
    /// NOTE: changing tags on an entry touches updated_at. To add a tag
    /// locally, create a new tag object with an arbitrary `slug` and `id`,
    /// add it to the Entry object, and save to db. taglinks and tags table
    /// will be updated in next sync.
    ///
    /// NOTE: changing an annotation does not update entry updated_at
    pub fn full_sync(&mut self) -> Fallible<()> {
        // sync local deletes first otherwise entries will be re-created locally...
        self.sync_local_deletes()?;

        // get _all_ entries on the server
        let server_entries = self.client.get_entries()?;

        // used when syncing up locally updated entries/annotations to avoid syncing twice
        let seen_entries: HashSet<ID> = server_entries.iter().map(|e| e.id).collect();
        let tmp_empty_vec = vec![];
        let seen_annotations: HashSet<ID> = server_entries
            .iter()
            .flat_map(|e| {
                e.annotations
                    .as_ref()
                    .unwrap_or(&tmp_empty_vec)
                    .iter()
                    .map(|a| a.id)
            })
            .collect();

        for remote_entry in server_entries {
            // first check if existing entry with same id
            if let Some(saved_entry) = self.db.get_entry(remote_entry.id.as_int())? {
                match Ord::cmp(&saved_entry.updated_at, &remote_entry.updated_at) {
                    Less => {
                        // saved entry is older than pulled version; overwrite
                        self.pull_entry(&remote_entry)?;
                    }
                    Equal => {
                        // already synced and same version
                        // still need to sync annotations though
                        if let Some(ref anns) = remote_entry.annotations {
                            for ann in anns {
                                self.sync_annotation(ann, &remote_entry)?;
                            }
                        }
                    }
                    Greater => {
                        // local entry is newer, push to server
                        let updated_entry = self
                            .client
                            .update_entry(saved_entry.id, &(&saved_entry).into())?;
                        // run pull entry on the entry returned to sync any new tags
                        self.pull_entry(&updated_entry)?;
                    }
                }
            } else {
                self.pull_entry(&remote_entry)?;
            }
        }

        // delete all local entries that have been deleted on the server
        let local_entries: HashSet<ID> = self.db.get_all_entry_ids()?;
        let remotely_deleted_entries = HashSet::difference(&local_entries, &seen_entries);
        for entry_id in remotely_deleted_entries {
            self.db.delete_entry(*entry_id)?;
        }

        // delete all local annotations that have been deleted on the server
        let local_anns: HashSet<ID> = self.db.get_all_annotation_ids()?;
        let remotely_deleted_anns = HashSet::difference(&local_anns, &seen_annotations);
        for ann_id in remotely_deleted_anns {
            self.db.delete_annotation(*ann_id)?;
        }

        // finally push new things to the server
        for NewUrl { id, url } in self.db.get_new_urls()? {
            let new_entry = NewEntry::new_with_url(url);
            let entry = self.client.create_entry(&new_entry)?;
            self.pull_entry(&entry)?;
            self.db.remove_new_url(id)?;
        }

        for (entry_id, new_ann_id, new_ann) in self.db.get_new_annotations()? {
            let ann = self.client.create_annotation(entry_id, &new_ann)?;
            self.db.save_annotation(&ann, entry_id)?;
            self.db.remove_new_annotation(new_ann_id)?;
        }

        // last of all drop tags with no tag_links
        self.db.delete_unused_tags()?;

        // Touch the last sync time ready for next sync.
        // This must be done last to ensure the sync has successfully completed.
        self.db.touch_last_sync()?;

        Ok(())
    }

    /// Normal sync. Syncs everything changed since the last sync, with the
    /// exception of deleted entries and annotations (syncing deletes requires a
    /// full sweep through everything).
    ///
    /// For entries and annotations existing in the database, object with latest
    /// updated_at value wins.
    ///
    /// What this does _not_ sync:
    ///
    /// - Entries deleted server-side.
    /// - Annotations deleted server-side.
    /// - Annotations updated or created server-side that are not associated
    ///   with entries updated since previous sync. (ie. recently updated annotations on
    ///   non-recently updated entries)
    ///
    /// TODO: ignore errors relating to actions that have already been done - eg. 404 error on
    /// client delete entry.
    pub fn sync(&mut self) -> Fallible<()> {
        self.sync_local_deletes()?;

        // Sync entries recently updated server-side. Entries have tag links and annotations embedded.
        let mut filter = EntriesFilter::default();
        let since = self.db.get_last_sync()?;
        filter.since = since.timestamp();
        let entries = self.client.get_entries_with_filter(&filter)?;

        // used when syncing up locally updated entries/annotations to avoid syncing twice
        let seen_entries: HashSet<ID> = entries.iter().map(|e| e.id).collect();
        let tmp_empty_vec = vec![];
        let seen_annotations: HashSet<ID> = entries
            .iter()
            .flat_map(|e| {
                e.annotations
                    .as_ref()
                    .unwrap_or(&tmp_empty_vec)
                    .iter()
                    .map(|a| a.id)
            })
            .collect();

        // sync recently updated entries
        for remote_entry in entries {
            // first check if existing entry with same id
            if let Some(saved_entry) = self.db.get_entry(remote_entry.id.as_int())? {
                match Ord::cmp(&saved_entry.updated_at, &remote_entry.updated_at) {
                    Less => {
                        // saved entry is older than pulled version; overwrite
                        self.pull_entry(&remote_entry)?;
                    }
                    Equal => {
                        // already synced and same version
                        // still need to sync annotations though
                        if let Some(ref anns) = remote_entry.annotations {
                            for ann in anns {
                                self.sync_annotation(ann, &remote_entry)?;
                            }
                        }
                    }
                    Greater => {
                        // local entry is newer, push to server
                        let updated_entry = self
                            .client
                            .update_entry(saved_entry.id, &(&saved_entry).into())?;
                        // run pull entry on the entry returned to sync any new tags and
                        // update annotations
                        self.pull_entry(&updated_entry)?;
                    }
                }
            } else {
                self.pull_entry(&remote_entry)?;
            }
        }

        // Update all locally-recently-updated entries and annotations that weren't touched
        // previously.
        for entry in self.db.get_entries_since(since)? {
            if !seen_entries.contains(&entry.id) {
                self.client.update_entry(entry.id, &(&entry).into())?;
            }
        }

        for ann in self.db.get_annotations_since(since)? {
            if !seen_annotations.contains(&ann.id) {
                self.client.update_annotation(&ann)?;
            }
        }

        // finally push new things to the server
        for NewUrl { id, url } in self.db.get_new_urls()? {
            let new_entry = NewEntry::new_with_url(url);
            let entry = self.client.create_entry(&new_entry)?;
            self.pull_entry(&entry)?;
            self.db.remove_new_url(id)?;
        }

        for (entry_id, new_ann_id, new_ann) in self.db.get_new_annotations()? {
            let ann = self.client.create_annotation(entry_id, &new_ann)?;
            self.db.save_annotation(&ann, entry_id)?;
            self.db.remove_new_annotation(new_ann_id)?;
        }

        // last of all drop tags with no tag_links
        self.db.delete_unused_tags()?;

        // Touch the last sync time ready for next sync.
        // This must be done last to ensure the sync has successfully completed.
        self.db.touch_last_sync()?;

        Ok(())
    }

    /// save an entry to the database where the entry has been determined to be
    /// newer than any in the database, but still need to do bidirectional sync
    /// for associated annotations and tags
    fn pull_entry(&mut self, entry: &Entry) -> Fallible<()> {
        self.db.save_entry(entry)?;

        if let Some(ref anns) = entry.annotations {
            for ann in anns {
                self.sync_annotation(ann, entry)?;
            }
        }

        // rebuild tag links
        self.db.drop_tag_links_for_entry(entry)?;
        for tag in &entry.tags {
            self.db.save_tag(&tag)?;
            self.db.save_tag_link(entry, &tag)?;
        }

        Ok(())
    }

    /// Push up all local delete actions.
    fn sync_local_deletes(&mut self) -> Fallible<()> {
        // Track and sync client-side deletes. This needs to be done before pulling
        // entries/annotations otherwise they will simply be re-created.
        // Delete annotation deletes before entry deletes to avoid 404s.
        // TODO: ignore not found errors here
        for annotation_id in self.db.get_annotation_deletes()? {
            self.client.delete_annotation(annotation_id)?;
            self.db.remove_delete_annotation(annotation_id)?;
        }
        for entry_id in self.db.get_entry_deletes()? {
            self.client.delete_entry(entry_id)?;
            self.db.remove_delete_entry(entry_id)?;
        }

        Ok(())
    }

    /// sync an annotation given an annotation from the server.
    fn sync_annotation<T: Into<ID>>(&mut self, ann: &Annotation, entry_id: T) -> Fallible<()> {
        let entry_id = entry_id.into().as_int();
        if let Some(saved_ann) = self.db.get_annotation(ann.id.as_int())? {
            match Ord::cmp(&saved_ann.updated_at, &ann.updated_at) {
                Less => {
                    // saved annotation is older than pulled version; overwrite
                    self.db.save_annotation(ann, entry_id)?;
                }
                Equal => {
                    // noop; already synced and same version
                }
                Greater => {
                    // local annotation is newer, push to server
                    let updated_ann = self.client.update_annotation(&saved_ann)?;
                    self.db.save_annotation(&updated_ann, entry_id)?;
                }
            }
        } else {
            self.db.save_annotation(ann, entry_id)?;
        }

        Ok(())
    }
}