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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;

mod action_cache;
mod database;
mod default_portal;
mod error;
pub mod feed_api;
mod feed_api_implementations;
pub mod models;
mod password_encryption;
mod schema;
pub mod util;

use crate::database::Database;
use crate::default_portal::DefaultPortal;
use chrono::{Duration, Utc};
use models::{ApiSecret, CategoryMapping, Thumbnail};
use parking_lot::{Mutex, RwLock};
use std::sync::Arc;

use crate::action_cache::ActionCache;
pub use crate::error::{NewsFlashError, NewsFlashErrorKind};
pub use crate::feed_api::error::{FeedApiError, FeedApiErrorKind};
use crate::feed_api::{FeedApi, Portal};
use crate::models::{
    Article, ArticleFilter, ArticleID, Category, CategoryID, CategoryType, Config, DatabaseSize, Enclosure, FatArticle, FavIcon, Feed, FeedID,
    FeedMapping, LoginData, Marked, PluginCapabilities, PluginID, PluginInfo, Read, Tag, TagID, Tagging, Url, NEWSFLASH_TOPLEVEL,
};
use crate::util::favicon_cache::FavIconCache;
pub use crate::util::feed_parser::{self, FeedParserError, ParsedUrl};
use crate::util::html2text::Html2Text;
use crate::util::opml;
use article_scraper::ArticleScraper;
use failure::{Fail, ResultExt};
use feed_api_implementations::FeedApiImplementations;
use log::{error, info};
use once_cell::sync::Lazy;
use reqwest::Client;
use std::collections::hash_map::HashMap;
use std::path::Path;

static SCRAPER_DATA_DIR: &str = "scraper_data";
type NewsFlashResult<T> = Result<T, NewsFlashError>;

pub struct NewsFlash {
    db: Arc<Database>,
    api: RwLock<Box<dyn FeedApi>>,
    config: RwLock<Config>,
    icons: FavIconCache,
    scraper: ArticleScraper,
    sync_cache: Mutex<ActionCache>,
    sync_ongoing: Arc<RwLock<bool>>,
}

impl NewsFlash {
    pub fn list_backends() -> HashMap<PluginID, PluginInfo> {
        let mut map: HashMap<PluginID, PluginInfo> = HashMap::new();
        for api_meta in FeedApiImplementations::list() {
            map.insert(api_meta.id(), api_meta.info().unwrap());
        }
        map
    }

    pub fn new(data_dir: &Path, config_dir: &Path, id: &PluginID, user_api_secret: Option<ApiSecret>) -> NewsFlashResult<Self> {
        // create data dir if it doesn't already exist
        std::fs::DirBuilder::new()
            .recursive(true)
            .create(&data_dir)
            .context(NewsFlashErrorKind::IO)?;
        std::fs::DirBuilder::new()
            .recursive(true)
            .create(&config_dir)
            .context(NewsFlashErrorKind::IO)?;

        let db = Database::new(data_dir).context(NewsFlashErrorKind::Database)?;
        let db = Arc::new(db);
        let api = NewsFlash::load_backend(id, config_dir, db.clone(), user_api_secret)?;
        let icons = FavIconCache::new(&db).context(NewsFlashErrorKind::IO)?;
        let scraper = ArticleScraper::new(data_dir.join(SCRAPER_DATA_DIR));
        let config = Config::open(config_dir).context(NewsFlashErrorKind::IO)?;
        let sync_cache = Mutex::new(ActionCache::new());
        let base = NewsFlash {
            db,
            api: RwLock::new(api),
            config: RwLock::new(config),
            icons,
            scraper,
            sync_cache,
            sync_ongoing: Arc::new(RwLock::new(false)),
        };

        Ok(base)
    }

    pub fn try_load(data_dir: &Path, config_dir: &Path) -> NewsFlashResult<Self> {
        let config = Config::open(config_dir).context(NewsFlashErrorKind::IO)?;
        let plugin_id = config.get_backend().ok_or(NewsFlashErrorKind::LoadBackend)?;
        Self::new(data_dir, config_dir, &plugin_id, None)
    }

    fn load_backend(
        backend_id: &PluginID,
        data_dir: &Path,
        db: Arc<Database>,
        user_api_secret: Option<ApiSecret>,
    ) -> NewsFlashResult<Box<dyn FeedApi>> {
        info!("Loading backend {}", backend_id);
        if let Some(meta_data) = FeedApiImplementations::get(backend_id) {
            let portal = NewsFlash::default_portal(db).context(NewsFlashErrorKind::Portal)?;
            let backend = meta_data
                .get_instance(data_dir, portal, user_api_secret)
                .context(NewsFlashErrorKind::LoadBackend)?;
            return Ok(backend);
        } else {
            error!("No meta object for id '{}' found", backend_id);
        }
        Err(NewsFlashErrorKind::LoadBackend.into())
    }

    pub fn id(&self) -> Option<PluginID> {
        self.config.read().get_backend()
    }

    pub fn user_name(&self) -> Option<String> {
        self.api.read().user_name()
    }

    pub fn features(&self) -> NewsFlashResult<PluginCapabilities> {
        Ok(self.api.read().features().context(NewsFlashErrorKind::API)?)
    }

    pub fn get_login_data(&self) -> Option<LoginData> {
        self.api.read().get_login_data()
    }

    pub fn is_sync_ongoing(&self) -> bool {
        *self.sync_ongoing.read()
    }

    pub fn is_database_empty(&self) -> NewsFlashResult<bool> {
        let is_empty = self.db.is_empty().context(NewsFlashErrorKind::Database)?;
        Ok(is_empty)
    }

    fn default_portal(db: Arc<Database>) -> NewsFlashResult<Box<dyn Portal>> {
        let portal = DefaultPortal::new(db);
        let portal = Box::new(portal);
        Ok(portal)
    }

    pub fn parse_error(error: &dyn Fail) -> Option<String> {
        for api_meta in FeedApiImplementations::list() {
            if let Some(error) = api_meta.parse_error(error) {
                return Some(error);
            }
        }
        None
    }

    pub fn set_keep_articles_duration(&self, keep_articles: Option<chrono::Duration>) -> NewsFlashResult<()> {
        self.config
            .write()
            .set_keep_articles_duration(keep_articles)
            .context(NewsFlashErrorKind::Config)?;
        if let Some(keep_articles) = keep_articles {
            self.db.drop_old_articles(keep_articles).context(NewsFlashErrorKind::Database)?;
        }

        Ok(())
    }

    pub fn get_keep_articles_duration(&self) -> Option<chrono::Duration> {
        self.config.read().get_keep_articles_duration()
    }

    pub fn error_login_related(error: &NewsFlashError) -> bool {
        match error.kind() {
            NewsFlashErrorKind::LoadBackend | NewsFlashErrorKind::NotLoggedIn | NewsFlashErrorKind::Login | NewsFlashErrorKind::API => true,

            NewsFlashErrorKind::Database
            | NewsFlashErrorKind::GrabContent
            | NewsFlashErrorKind::Icon
            | NewsFlashErrorKind::Thumbnail
            | NewsFlashErrorKind::ImageDownload
            | NewsFlashErrorKind::IO
            | NewsFlashErrorKind::Portal
            | NewsFlashErrorKind::OPML
            | NewsFlashErrorKind::Syncing
            | NewsFlashErrorKind::Unknown
            | NewsFlashErrorKind::Config => false,
        }
    }

    pub async fn get_icon_info(&self, feed_id: &FeedID, client: &Lazy<Client>) -> NewsFlashResult<FavIcon> {
        let info = self.icons.get_icon(feed_id, &self.api, client).await.context(NewsFlashErrorKind::Icon)?;
        Ok(info)
    }

    pub async fn get_article_thumbnail(&self, article_id: &ArticleID, client: &Lazy<Client>) -> NewsFlashResult<Thumbnail> {
        if let Ok(thumbnail) = self.db.read_thumbnail(article_id) {
            if thumbnail.data.is_some() {
                return Ok(thumbnail);
            }

            if Utc::now().naive_utc() - thumbnail.last_try > Duration::days(4) {
                self.download_thumbnail(article_id, client).await?;
            } else {
                log::debug!(
                    "Tried to download thumbnail for '{}' recently, will not attemt to download again.",
                    article_id
                );
            }
        }

        self.download_thumbnail(article_id, client).await
    }

    async fn download_thumbnail(&self, article_id: &ArticleID, client: &Lazy<Client>) -> NewsFlashResult<Thumbnail> {
        if let Ok(article) = self.db.read_article(article_id) {
            if let Some(thumb_url) = article.thumbnail_url {
                if let Ok(thumbnail) = Thumbnail::from_url(&thumb_url, article_id, client).await {
                    self.db.insert_thumbnail(&thumbnail).context(NewsFlashErrorKind::Database)?;
                    return Ok(thumbnail);
                } else {
                    log::warn!("downloading thumbnail '{}' failed", thumb_url);
                }
            } else {
                log::debug!("Couldn't download thumbnail: article '{}' doesn't specify thumbnail url", article_id);
            }
        } else {
            log::warn!("Couldn't download thumbnail: article with ID '{}' not found", article_id);
        }

        Err(NewsFlashErrorKind::Thumbnail.into())
    }

    pub fn database_size(&self) -> NewsFlashResult<DatabaseSize> {
        let size = self.db.size().context(NewsFlashErrorKind::Database)?;
        Ok(size)
    }

    pub async fn login(&self, data: LoginData, client: &Client) -> NewsFlashResult<()> {
        let id = data.id();
        self.api.write().login(data, client).await.context(NewsFlashErrorKind::Login)?;
        self.config.write().set_backend(Some(&id)).context(NewsFlashErrorKind::Login)?;
        Ok(())
    }

    pub async fn logout(&self, client: &Client) -> NewsFlashResult<()> {
        self.config.write().set_backend(None).context(NewsFlashErrorKind::Config)?;
        self.api.write().logout(client).await.context(NewsFlashErrorKind::API)?;
        self.db.reset().context(NewsFlashErrorKind::Database)?;
        Ok(())
    }

    pub async fn initial_sync(&self, client: &Client) -> NewsFlashResult<i64> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            *self.sync_ongoing.write() = true;

            let now = chrono::Utc::now();

            let result = self
                .api
                .read()
                .initial_sync(client)
                .await
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::API)?;
            // Filter out old articles that would be deleted right away afterwards
            let result = result.remove_old_articles(self.config.read().get_keep_articles_duration());
            let result = result.generate_tag_colors(&[]);
            // Modify result with all changes that happened during sync
            let result = self.sync_cache.lock().process_sync_result(result);
            // push all changes that happend druing sync to the backend
            self.sync_cache
                .lock()
                .execute_api_actions(&self.api, &self.config, client)
                .await
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::API)?;
            // reset the sync_cache for next sync
            self.sync_cache.lock().reset();

            let new_article_count = self
                .db
                .write_sync_result(result, self.config.read().get_keep_articles_duration())
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::Database)?;

            self.config
                .write()
                .set_last_sync(now)
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::Config)?;

            *self.sync_ongoing.write() = false;
            return Ok(new_article_count);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn sync(&self, client: &Client) -> NewsFlashResult<i64> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            *self.sync_ongoing.write() = true;

            let now = chrono::Utc::now();
            let max_count = self.config.read().get_sync_amount();
            let last_sync = self.config.read().get_last_sync();

            let result = self
                .api
                .read()
                .sync(max_count, last_sync, client)
                .await
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::API)?;
            // Filter out old articles that would be deleted right away afterwards
            let result = result.remove_old_articles(self.config.read().get_keep_articles_duration());

            let tags = self.db.read_tags().context(NewsFlashErrorKind::Database)?;
            let result = result.generate_tag_colors(&tags);
            // Modify result with all changes that happened during sync
            let result = self.sync_cache.lock().process_sync_result(result);
            // push all changes that happend druing sync to the backend
            self.sync_cache
                .lock()
                .execute_api_actions(&self.api, &self.config, client)
                .await
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::API)?;
            // reset the sync_cache for next sync
            self.sync_cache.lock().reset();

            let new_article_count = self
                .db
                .write_sync_result(result, self.config.read().get_keep_articles_duration())
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::Database)?;
            self.config
                .write()
                .set_last_sync(now)
                .map_err(|error| {
                    *self.sync_ongoing.write() = false;
                    error
                })
                .context(NewsFlashErrorKind::Config)?;

            *self.sync_ongoing.write() = false;
            return Ok(new_article_count);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn set_article_read(&self, articles: &[ArticleID], read: Read, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                for article_id in articles {
                    match read {
                        Read::Read => self.sync_cache.lock().add_article_marked_read(article_id),
                        Read::Unread => self.sync_cache.lock().add_article_marked_unread(article_id),
                    }
                }
                self.db.set_article_read(articles, read).context(NewsFlashErrorKind::Database)?;
            } else {
                let articles_before = self
                    .db
                    .read_articles(ArticleFilter::read_ids(articles, read.invert()))
                    .context(NewsFlashErrorKind::Database)?;
                self.db.set_article_read(articles, read).context(NewsFlashErrorKind::Database)?;

                let api_result = self.api.read().set_article_read(articles, read, client).await;

                // in case of error, reset read state to what it was before
                if api_result.is_err() {
                    let ids_before = articles_before.iter().map(|a| a.article_id.clone()).collect::<Vec<_>>();
                    self.db
                        .set_article_read(&ids_before, read.invert())
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn set_article_marked(&self, articles: &[ArticleID], marked: Marked, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                for article_id in articles {
                    match marked {
                        Marked::Marked => self.sync_cache.lock().add_article_mark(article_id),
                        Marked::Unmarked => self.sync_cache.lock().add_article_unmark(article_id),
                    }
                }

                self.db.set_article_marked(articles, marked).context(NewsFlashErrorKind::Database)?;
            } else {
                let articles_before = self
                    .db
                    .read_articles(ArticleFilter::marked_ids(articles, marked.invert()))
                    .context(NewsFlashErrorKind::Database)?;
                self.db.set_article_marked(articles, marked).context(NewsFlashErrorKind::Database)?;

                let article_ids_to_update = articles_before.iter().map(|a| &a.article_id).cloned().collect::<Vec<_>>();
                let api_result = self.api.read().set_article_marked(&article_ids_to_update, marked, client).await;

                // in case of error, reset marked state to what it was before
                if api_result.is_err() {
                    let ids_before = articles_before.iter().map(|a| a.article_id.clone()).collect::<Vec<_>>();
                    self.db
                        .set_article_marked(&ids_before, marked.invert())
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn set_feed_read(&self, feeds: &[FeedID], client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                for feed_id in feeds {
                    self.sync_cache.lock().add_feed_mark_read(feed_id);
                }
                self.db.set_feed_read(feeds).context(NewsFlashErrorKind::Database)?;
            } else {
                let mut unread_articles_before: Vec<ArticleID> = Vec::new();
                for feed_id in feeds {
                    let articles_before = self
                        .db
                        .read_articles(ArticleFilter::feed_unread(feed_id))
                        .context(NewsFlashErrorKind::Database)?;
                    unread_articles_before.append(&mut articles_before.into_iter().map(|a| a.article_id).collect());
                }

                self.db.set_feed_read(feeds).context(NewsFlashErrorKind::Database)?;

                let last_sync = self.config.read().get_last_sync();
                let api_result = self.api.read().set_feed_read(feeds, &unread_articles_before, last_sync, client).await;

                // in case of error, reset read state to what it was before
                if api_result.is_err() {
                    self.db
                        .set_article_read(&unread_articles_before, Read::Unread)
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn set_category_read(&self, categories: &[CategoryID], client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                for category_id in categories {
                    self.sync_cache.lock().add_category_mark_read(category_id);
                }
                self.db.set_category_read(categories).context(NewsFlashErrorKind::Database)?;
            } else {
                let mut unread_articles_before: Vec<ArticleID> = Vec::new();
                for category_id in categories {
                    let articles_before = self
                        .db
                        .read_articles(ArticleFilter::category_unread(category_id))
                        .context(NewsFlashErrorKind::Database)?;
                    unread_articles_before.append(&mut articles_before.into_iter().map(|a| a.article_id).collect());
                }

                self.db.set_category_read(categories).context(NewsFlashErrorKind::Database)?;
                let last_sync = self.config.read().get_last_sync();
                let api_result = self
                    .api
                    .read()
                    .set_category_read(categories, &unread_articles_before, last_sync, client)
                    .await;

                // in case of error, reset read state to what it was before
                if api_result.is_err() {
                    self.db
                        .set_article_read(&unread_articles_before, Read::Unread)
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn set_tag_read(&self, tags: &[TagID], client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                for tag_id in tags {
                    self.sync_cache.lock().add_tag_mark_read(tag_id);
                }
                self.db.set_tag_read(tags).context(NewsFlashErrorKind::Database)?;
            } else {
                let mut unread_articles_before: Vec<ArticleID> = Vec::new();
                for tag_id in tags {
                    let articles_before = self
                        .db
                        .read_articles(ArticleFilter::tag_unread(tag_id))
                        .context(NewsFlashErrorKind::Database)?;
                    unread_articles_before.append(&mut articles_before.into_iter().map(|a| a.article_id).collect());
                }

                self.db.set_tag_read(tags).context(NewsFlashErrorKind::Database)?;
                let last_sync = self.config.read().get_last_sync();
                let api_result = self.api.read().set_tag_read(tags, &unread_articles_before, last_sync, client).await;

                // in case of error, reset read state to what it was before
                if api_result.is_err() {
                    self.db
                        .set_article_read(&unread_articles_before, Read::Unread)
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn set_all_read(&self, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                let categories = self.db.read_categories().context(NewsFlashErrorKind::Database)?;
                for category in categories {
                    self.sync_cache.lock().add_category_mark_read(&category.category_id);
                }
                self.db.set_all_read().context(NewsFlashErrorKind::Database)?;
            } else {
                let unread_articles_before = self
                    .db
                    .read_articles(ArticleFilter::all_unread())
                    .context(NewsFlashErrorKind::Database)?
                    .into_iter()
                    .map(|a| a.article_id)
                    .collect::<Vec<_>>();
                self.db.set_all_read().context(NewsFlashErrorKind::Database)?;

                let last_sync = self.config.read().get_last_sync();
                let api_result = self.api.read().set_all_read(&unread_articles_before, last_sync, client).await;

                // in case of error, reset read state to what it was before
                if api_result.is_err() {
                    self.db
                        .set_article_read(&unread_articles_before, Read::Unread)
                        .context(NewsFlashErrorKind::Database)?;
                }

                api_result.context(NewsFlashErrorKind::API)?;
            }

            Ok(())
        } else {
            Err(NewsFlashErrorKind::NotLoggedIn.into())
        }
    }

    pub async fn add_feed(
        &self,
        url: &Url,
        title: Option<String>,
        category_id: Option<CategoryID>,
        client: &Client,
    ) -> NewsFlashResult<(Feed, FeedMapping, Option<Category>, Option<CategoryMapping>)> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let (feed, category) = self
                .api
                .read()
                .add_feed(url, title, category_id.clone(), client)
                .await
                .context(NewsFlashErrorKind::API)?;

            self.db.insert_feed(&feed).context(NewsFlashErrorKind::Database)?;

            let category_mapping = if let Some(category) = &category {
                let category_mapping = CategoryMapping {
                    parent_id: NEWSFLASH_TOPLEVEL.clone(),
                    category_id: category.category_id.clone(),
                    sort_index: None,
                };
                self.db.insert_category(category).context(NewsFlashErrorKind::Database)?;
                self.db.insert_category_mapping(&category_mapping).context(NewsFlashErrorKind::Database)?;
                Some(category_mapping)
            } else {
                None
            };

            let category_id = match category_id {
                Some(category_id) => Some(category_id),
                None => category.as_ref().map(|c| c.category_id.clone()),
            };

            let feed_mapping = if let Some(category_id) = category_id {
                let mapping = FeedMapping {
                    feed_id: feed.feed_id.clone(),
                    category_id,
                    sort_index: None,
                };
                self.db.insert_feed_mapping(&mapping).context(NewsFlashErrorKind::Database)?;
                mapping
            } else {
                FeedMapping {
                    feed_id: feed.feed_id.clone(),
                    category_id: NEWSFLASH_TOPLEVEL.clone(),
                    sort_index: None,
                }
            };

            return Ok((feed, feed_mapping, category, category_mapping));
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn remove_feed(&self, feed: &Feed, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }
            self.api
                .read()
                .remove_feed(&feed.feed_id, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            // remove feed from db
            self.db.drop_feed(&feed.feed_id).context(NewsFlashErrorKind::Database)?;
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn move_feed(&self, from: &FeedMapping, to: &FeedMapping, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            self.api
                .read()
                .move_feed(&from.feed_id, &from.category_id, &to.category_id, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            // drop mapping 'from'
            self.db.drop_feed_mapping(from).context(NewsFlashErrorKind::Database)?;

            // add mapping 'to'
            self.db.insert_feed_mapping(to).context(NewsFlashErrorKind::Database)?;
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn rename_feed(&self, feed: &Feed, new_title: &str, client: &Client) -> NewsFlashResult<Feed> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let new_id = self
                .api
                .read()
                .rename_feed(&feed.feed_id, new_title, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            let mut modified_feed = feed.clone();
            modified_feed.label = new_title.to_owned();
            modified_feed.feed_id = new_id.clone();

            self.db.insert_feed(&modified_feed).context(NewsFlashErrorKind::Database)?;

            if new_id != feed.feed_id {
                self.db.drop_feed(&feed.feed_id).context(NewsFlashErrorKind::Database)?;

                // fix mappings
                let mappings = self
                    .db
                    .read_feed_mappings(Some(&feed.feed_id), None)
                    .context(NewsFlashErrorKind::Database)?;
                let modified_mappings: Vec<FeedMapping> = mappings
                    .into_iter()
                    .map(|mut mapping| {
                        mapping.feed_id = new_id.clone();
                        mapping
                    })
                    .collect();
                self.db.drop_mapping_of_feed(&feed.feed_id).context(NewsFlashErrorKind::Database)?;
                self.db.write_feed_mappings(&modified_mappings).context(NewsFlashErrorKind::Database)?;

                // fix articles
                let articles = self
                    .db
                    .read_articles(ArticleFilter {
                        limit: None,
                        offset: None,
                        order: None,
                        unread: None,
                        marked: None,
                        feed: Some(feed.feed_id.clone()),
                        feed_blacklist: None,
                        category: None,
                        category_blacklist: None,
                        tag: None,
                        ids: None,
                        newer_than: None,
                        older_than: None,
                        search_term: None,
                    })
                    .context(NewsFlashErrorKind::Database)?;
                let mut modified_ids: Vec<ArticleID> = Vec::new();
                let modified_articles: Vec<Article> = articles
                    .into_iter()
                    .map(|mut article| {
                        modified_ids.push(article.article_id.clone());
                        article.feed_id = new_id.clone();
                        article
                    })
                    .collect();
                self.db.drop_articles(&modified_ids).context(NewsFlashErrorKind::Database)?;
                self.db.write_articles(&modified_articles).context(NewsFlashErrorKind::Database)?;
            }
            return Ok(modified_feed);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn add_category(
        &self,
        title: &str,
        parent: Option<&CategoryID>,
        sort_index: Option<i32>,
        client: &Client,
    ) -> NewsFlashResult<(Category, CategoryMapping)> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let category_id = self
                .api
                .read()
                .add_category(title, parent, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            let category = Category {
                category_id: category_id.clone(),
                label: title.to_owned(),
                category_type: CategoryType::Default,
            };

            let category_mapping = CategoryMapping {
                parent_id: match parent {
                    Some(parent) => parent.clone(),
                    None => NEWSFLASH_TOPLEVEL.clone(),
                },
                category_id,
                sort_index,
            };

            self.db.insert_category(&category).context(NewsFlashErrorKind::Database)?;
            self.db.insert_category_mapping(&category_mapping).context(NewsFlashErrorKind::Database)?;

            return Ok((category, category_mapping));
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn remove_category(&self, category: &Category, remove_children: bool, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            self.api
                .read()
                .remove_category(&category.category_id, remove_children, client)
                .await
                .context(NewsFlashErrorKind::API)?;
        } else {
            return Err(NewsFlashErrorKind::NotLoggedIn.into());
        }

        if remove_children {
            self.remove_category_from_db_recurse(category)?;
        } else {
            self.remove_category_from_db_move_children_up(category)?;
        }

        Ok(())
    }

    fn remove_category_from_db_move_children_up(&self, category: &Category) -> NewsFlashResult<()> {
        let parent_id = self
            .db
            .read_category_mappings(None, Some(&category.category_id))
            .context(NewsFlashErrorKind::Database)?
            .first()
            .map(|m| m.parent_id.clone())
            .unwrap_or_else(|| NEWSFLASH_TOPLEVEL.clone());

        // map feeds of category as children of parent
        let feed_mappings = self
            .db
            .read_feed_mappings(None, Some(&category.category_id))
            .context(NewsFlashErrorKind::Database)?;
        for mut mapping in feed_mappings {
            self.db.drop_feed_mapping(&mapping).context(NewsFlashErrorKind::Database)?;
            mapping.category_id = parent_id.clone();
            self.db.insert_feed_mapping(&mapping).context(NewsFlashErrorKind::Database)?;
        }

        // map child categories of category as children of parent
        let category_mappings = self
            .db
            .read_category_mappings(None, Some(&category.category_id))
            .context(NewsFlashErrorKind::Database)?;
        for mut mapping in category_mappings {
            self.db.drop_category_mapping(&mapping).context(NewsFlashErrorKind::Database)?;
            mapping.parent_id = parent_id.clone();
            self.db.insert_category_mapping(&mapping).context(NewsFlashErrorKind::Database)?;
        }

        Ok(())
    }

    fn remove_category_from_db_recurse(&self, category: &Category) -> NewsFlashResult<()> {
        // remove childen feeds
        let mappings = self
            .db
            .read_feed_mappings(None, Some(&category.category_id))
            .context(NewsFlashErrorKind::Database)?;
        for mapping in mappings {
            self.db.drop_feed(&mapping.feed_id).context(NewsFlashErrorKind::Database)?;
        }

        // remove category
        self.db.drop_category(category).context(NewsFlashErrorKind::Database)?;

        // look for children categories and recurse
        let mappings = self
            .db
            .read_category_mappings(None, Some(&category.category_id))
            .context(NewsFlashErrorKind::Database)?;
        let categories = self.db.read_categories().context(NewsFlashErrorKind::Database)?;

        let child_categories: Vec<Category> = categories
            .into_iter()
            .filter(|category| mappings.iter().any(|mapping| mapping.category_id == category.category_id))
            .collect();
        for child_category in child_categories {
            self.remove_category_from_db_recurse(&child_category)?;
        }

        Ok(())
    }

    pub async fn rename_category(&self, category: &Category, new_title: &str, client: &Client) -> NewsFlashResult<Category> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let mut modified_category_mappings = self
                .db
                .read_category_mappings(None, Some(&category.category_id))
                .context(NewsFlashErrorKind::Database)?;
            let mut modified_feed_mappings = self
                .db
                .read_feed_mappings(None, Some(&category.category_id))
                .context(NewsFlashErrorKind::Database)?;

            let new_id = self
                .api
                .read()
                .rename_category(&category.category_id, new_title, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            let mut modified_category = category.clone();
            modified_category.label = new_title.to_owned();

            if new_id != category.category_id {
                self.db.drop_category(category).context(NewsFlashErrorKind::Database)?;
                self.db
                    .drop_feed_mappings_of_category(&category.category_id)
                    .context(NewsFlashErrorKind::Database)?;
                modified_category.category_id = new_id.clone();

                // fix mappings
                modified_feed_mappings = modified_feed_mappings
                    .into_iter()
                    .map(|mut mapping| {
                        mapping.category_id = new_id.clone();
                        mapping
                    })
                    .collect();

                modified_category_mappings = modified_category_mappings
                    .into_iter()
                    .map(|mut m| {
                        m.category_id = new_id.clone();
                        m
                    })
                    .collect();
            }

            self.db.insert_category(&modified_category).context(NewsFlashErrorKind::Database)?;
            self.db
                .insert_category_mappings(&modified_category_mappings)
                .context(NewsFlashErrorKind::Database)?;
            self.db
                .insert_feed_mappings(&modified_feed_mappings)
                .context(NewsFlashErrorKind::Database)?;
            return Ok(modified_category);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn move_category(&self, category_id: &CategoryID, parent: &CategoryID, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            self.api
                .read()
                .move_category(category_id, parent, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            let category_mapping = CategoryMapping {
                parent_id: parent.clone(),
                category_id: category_id.clone(),
                sort_index: None,
            };

            self.db
                .drop_feed_mappings_of_category(category_id)
                .context(NewsFlashErrorKind::Database)?;
            self.db.insert_category_mapping(&category_mapping).context(NewsFlashErrorKind::Database)?;
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn add_tag(&self, title: &str, color: Option<String>, sort_index: Option<i32>, client: &Client) -> NewsFlashResult<Tag> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let tag_id = self.api.read().add_tag(title, client).await.context(NewsFlashErrorKind::API)?;
            let tag = Tag {
                tag_id,
                label: title.to_owned(),
                color,
                sort_index,
            };

            self.db.insert_tag(&tag).context(NewsFlashErrorKind::Database)?;
            return Ok(tag);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn remove_tag(&self, tag: &Tag, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            self.api.read().remove_tag(&tag.tag_id, client).await.context(NewsFlashErrorKind::API)?;

            self.db.drop_tag(tag).context(NewsFlashErrorKind::Database)?;
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn edit_tag(&self, tag: &Tag, new_title: &str, new_color: &Option<String>, client: &Client) -> NewsFlashResult<Tag> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            let new_id = self
                .api
                .read()
                .rename_tag(&tag.tag_id, new_title, client)
                .await
                .context(NewsFlashErrorKind::API)?;

            let taggings = self.db.read_taggings(None, Some(&tag.tag_id)).context(NewsFlashErrorKind::Database)?;
            self.db.drop_tag(tag).context(NewsFlashErrorKind::Database)?;
            let mutated_tag = Tag {
                tag_id: new_id.clone(),
                label: new_title.to_owned(),
                color: new_color.clone(),
                sort_index: tag.sort_index,
            };
            self.db.insert_tag(&mutated_tag).context(NewsFlashErrorKind::Database)?;
            self.db.insert_taggings(&taggings).context(NewsFlashErrorKind::Database)?;

            let taggings = if new_id != tag.tag_id {
                taggings
                    .into_iter()
                    .map(|mut tagging| {
                        tagging.tag_id = new_id.clone();
                        tagging
                    })
                    .collect()
            } else {
                taggings
            };
            self.db.insert_taggings(&taggings).context(NewsFlashErrorKind::Database)?;

            return Ok(mutated_tag);
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn tag_article(&self, article: &Article, tag: &Tag, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                self.sync_cache.lock().add_article_tagged(&article.article_id, &tag.tag_id);
            } else {
                self.api
                    .read()
                    .tag_article(&article.article_id, &tag.tag_id, client)
                    .await
                    .context(NewsFlashErrorKind::API)?;
                let tagging = Tagging {
                    article_id: article.article_id.clone(),
                    tag_id: tag.tag_id.clone(),
                };

                self.db.insert_tagging(&tagging).context(NewsFlashErrorKind::Database)?;
            }
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn untag_article(&self, article: &Article, tag: &Tag, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                self.sync_cache.lock().add_article_untagged(&article.article_id, &tag.tag_id);
            } else {
                self.api
                    .read()
                    .untag_article(&article.article_id, &tag.tag_id, client)
                    .await
                    .context(NewsFlashErrorKind::API)?;
                let tagging = Tagging {
                    article_id: article.article_id.clone(),
                    tag_id: tag.tag_id.clone(),
                };

                self.db.drop_tagging(&tagging).context(NewsFlashErrorKind::Database)?;
            }
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub async fn import_opml(&self, opml: &str, parse_all_feeds: bool, client: &Client) -> NewsFlashResult<()> {
        if self.api.read().is_logged_in(client).await.context(NewsFlashErrorKind::API)? {
            if *self.sync_ongoing.read() {
                return Err(NewsFlashErrorKind::Syncing.into());
            }

            self.api.read().import_opml(opml, client).await.context(NewsFlashErrorKind::API)?;
            let opml_result = opml::parse_opml(opml, parse_all_feeds, client).await.context(NewsFlashErrorKind::OPML)?;

            self.db.insert_categories(&opml_result.categories).context(NewsFlashErrorKind::Database)?;
            self.db.insert_feeds(&opml_result.feeds).context(NewsFlashErrorKind::Database)?;
            self.db
                .insert_feed_mappings(&opml_result.feed_mappings)
                .context(NewsFlashErrorKind::Database)?;
            self.db
                .insert_category_mappings(&opml_result.category_mappings)
                .context(NewsFlashErrorKind::Database)?;
            return Ok(());
        }
        Err(NewsFlashErrorKind::NotLoggedIn.into())
    }

    pub fn export_opml(&self) -> NewsFlashResult<String> {
        if *self.sync_ongoing.read() {
            return Err(NewsFlashErrorKind::Syncing.into());
        }

        let categories = self.db.read_categories().context(NewsFlashErrorKind::Database)?;
        let category_mappings = self.db.read_category_mappings(None, None).context(NewsFlashErrorKind::Database)?;
        let feeds = self.db.read_feeds().context(NewsFlashErrorKind::Database)?;
        let feed_mappings = self.db.read_feed_mappings(None, None).context(NewsFlashErrorKind::Database)?;

        let opml_string = opml::generate_opml(&categories, &category_mappings, &feeds, &feed_mappings).context(NewsFlashErrorKind::OPML)?;
        Ok(opml_string)
    }

    pub fn get_categories(&self) -> NewsFlashResult<(Vec<Category>, Vec<CategoryMapping>)> {
        let categories = self.db.read_categories().context(NewsFlashErrorKind::Database)?;
        let category_mappings = self.db.read_category_mappings(None, None).context(NewsFlashErrorKind::Database)?;
        Ok((categories, category_mappings))
    }

    pub fn unread_count_category(&self, category: &CategoryID) -> NewsFlashResult<i64> {
        let count = self.db.unread_count_category(category).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn marked_count_category(&self, category: &CategoryID) -> NewsFlashResult<i64> {
        let count = self.db.marked_count_category(category).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn get_feeds(&self) -> NewsFlashResult<(Vec<Feed>, Vec<FeedMapping>)> {
        let feeds = self.db.read_feeds().context(NewsFlashErrorKind::Database)?;
        let mappings = self.db.read_feed_mappings(None, None).context(NewsFlashErrorKind::Database)?;
        Ok((feeds, mappings))
    }

    pub fn unread_count_feed(&self, feed: &FeedID) -> NewsFlashResult<i64> {
        let count = self.db.unread_count_feed(feed).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn marked_count_feed(&self, feed: &FeedID) -> NewsFlashResult<i64> {
        let count = self.db.marked_count_feed(feed).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn unread_count_feed_map(&self, exclude_future: bool) -> NewsFlashResult<HashMap<FeedID, i64>> {
        let mut count_vec = self.db.unread_count_feed_map(exclude_future).context(NewsFlashErrorKind::Database)?;
        let mut map: HashMap<FeedID, i64> = HashMap::new();
        count_vec.drain(..).for_each(|c| {
            map.insert(c.feed_id, c.count);
        });
        Ok(map)
    }

    pub fn marked_count_feed_map(&self) -> NewsFlashResult<HashMap<FeedID, i64>> {
        let mut count_vec = self.db.marked_count_feed_map().context(NewsFlashErrorKind::Database)?;
        let mut map: HashMap<FeedID, i64> = HashMap::new();
        count_vec.drain(..).for_each(|c| {
            map.insert(c.feed_id, c.count);
        });
        Ok(map)
    }

    pub fn get_tags(&self) -> NewsFlashResult<(Vec<Tag>, Vec<Tagging>)> {
        let tags = self.db.read_tags().context(NewsFlashErrorKind::Database)?;
        let taggings = self.db.read_taggings(None, None).context(NewsFlashErrorKind::Database)?;
        Ok((tags, taggings))
    }

    pub fn get_tags_of_article(&self, article_id: &ArticleID) -> NewsFlashResult<Vec<Tag>> {
        let tags = self.db.read_tags_for_article(article_id).context(NewsFlashErrorKind::Database)?;
        Ok(tags)
    }

    pub fn unread_count_tag(&self, tag: &TagID) -> NewsFlashResult<i64> {
        let count = self.db.unread_count_tag(tag).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn marked_count_tag(&self, tag: &TagID) -> NewsFlashResult<i64> {
        let count = self.db.marked_count_tag(tag).context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn unread_count_all(&self) -> NewsFlashResult<i64> {
        let count = self.db.unread_count_all().context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn marked_count_all(&self) -> NewsFlashResult<i64> {
        let count = self.db.marked_count_all().context(NewsFlashErrorKind::Database)?;
        Ok(count)
    }

    pub fn get_articles(&self, filter: ArticleFilter) -> NewsFlashResult<Vec<Article>> {
        let articles = self.db.read_articles(filter).context(NewsFlashErrorKind::Database)?;
        Ok(articles)
    }

    pub fn get_article(&self, id: &ArticleID) -> NewsFlashResult<Article> {
        let article = self.db.read_article(id).context(NewsFlashErrorKind::Database)?;
        Ok(article)
    }

    pub fn get_fat_articles(&self, filter: ArticleFilter) -> NewsFlashResult<Vec<FatArticle>> {
        let articles = self.db.read_fat_articles(filter).context(NewsFlashErrorKind::Database)?;
        Ok(articles)
    }

    pub fn get_fat_article(&self, id: &ArticleID) -> NewsFlashResult<FatArticle> {
        let article = self.db.read_fat_article(id).context(NewsFlashErrorKind::Database)?;
        Ok(article)
    }

    pub fn get_enclosures(&self, id: &ArticleID) -> NewsFlashResult<Vec<Enclosure>> {
        let enclosures = self.db.read_enclosures(id).context(NewsFlashErrorKind::Database)?;
        Ok(enclosures)
    }

    pub async fn article_download_images(&self, id: &ArticleID, client: &Client) -> NewsFlashResult<FatArticle> {
        let mut article = self.get_fat_article(id)?;

        if let Some(scraped_content) = article.scraped_content {
            let processed_scraped_content = self
                .scraper
                .image_downloader
                .download_images_from_string(&scraped_content, client)
                .await
                .context(NewsFlashErrorKind::ImageDownload)?;
            article.scraped_content = Some(processed_scraped_content);
        } else if let Some(html) = article.html {
            let processed_html = self
                .scraper
                .image_downloader
                .download_images_from_string(&html, client)
                .await
                .context(NewsFlashErrorKind::ImageDownload)?;
            article.html = Some(processed_html);
        }

        self.db.update_article_grabbed_content(&article).context(NewsFlashErrorKind::Database)?;

        Ok(article)
    }

    pub async fn article_scrap_content(&self, id: &ArticleID, client: &Client) -> NewsFlashResult<FatArticle> {
        let mut article = self.get_fat_article(id)?;

        if let Some(url) = &article.url {
            let processed_article = self
                .scraper
                .parse(url, false, client)
                .await
                .map_err(|e| {
                    error!("Internal scraper: '{}' ({})", e, url);
                    e
                })
                .context(NewsFlashErrorKind::GrabContent)?;

            info!("Internal scraper: successfully scraped: '{}'", url);
            if let Some(html) = processed_article.html {
                article.plain_text = Html2Text::process(&html);
                article.scraped_content = Some(html);
            }
            if let Some(title) = processed_article.title {
                if article.title.is_none() {
                    article.title = Some(title);
                }
            }
            if let Some(author) = processed_article.author {
                if article.author.is_none() {
                    article.author = Some(author);
                }
            }

            self.db.update_article_grabbed_content(&article).context(NewsFlashErrorKind::Database)?;
            Ok(article)
        } else {
            error!("Article doesn't contain source URL");
            Err(NewsFlashErrorKind::GrabContent.into())
        }
    }

    pub fn update_external_scraped_content(&self, article: &FatArticle) -> NewsFlashResult<()> {
        self.db.update_article_grabbed_content(article).context(NewsFlashErrorKind::Database)?;
        Ok(())
    }
}