news_flash/
default_portal.rs

1use crate::database::Database;
2use crate::feed_api::portal::{Portal, PortalErrorKind, PortalResult};
3use crate::models::{Article, ArticleFilter, ArticleID, Category, CategoryID, CategoryMapping, Feed, FeedID, FeedMapping, Headline, Tag};
4use failure::ResultExt;
5use std::sync::Arc;
6
7pub struct DefaultPortal {
8    db: Arc<Database>,
9}
10
11unsafe impl Send for DefaultPortal {}
12unsafe impl Sync for DefaultPortal {}
13
14impl DefaultPortal {
15    pub fn new(db: Arc<Database>) -> DefaultPortal {
16        DefaultPortal { db }
17    }
18}
19
20impl Portal for DefaultPortal {
21    fn get_headlines(&self, ids: &[ArticleID]) -> PortalResult<Vec<Headline>> {
22        let articles = self
23            .db
24            .read_articles(ArticleFilter {
25                limit: None,
26                offset: Some(ids.len() as i64),
27                order: None,
28                unread: None,
29                marked: None,
30                feed: None,
31                feed_blacklist: None,
32                category: None,
33                category_blacklist: None,
34                tag: None,
35                ids: Some(ids),
36                newer_than: None,
37                older_than: None,
38                search_term: None,
39            })
40            .context(PortalErrorKind::DB)?;
41        let headlines = articles.iter().map(Headline::from_article).collect();
42        Ok(headlines)
43    }
44
45    fn get_articles(&self, ids: &[ArticleID]) -> PortalResult<Vec<Article>> {
46        let articles = self
47            .db
48            .read_articles(ArticleFilter {
49                limit: Some(ids.len() as i64),
50                offset: None,
51                order: None,
52                unread: None,
53                marked: None,
54                feed: None,
55                feed_blacklist: None,
56                category: None,
57                category_blacklist: None,
58                tag: None,
59                ids: Some(ids),
60                newer_than: None,
61                older_than: None,
62                search_term: None,
63            })
64            .context(PortalErrorKind::DB)?;
65        Ok(articles)
66    }
67
68    fn get_article_exists(&self, id: &ArticleID) -> PortalResult<bool> {
69        let exists = self.db.article_exists(id).context(PortalErrorKind::DB)?;
70        Ok(exists)
71    }
72
73    fn get_article_ids_unread_feed(&self, feed_id: &FeedID) -> PortalResult<Vec<ArticleID>> {
74        let articles = self.db.read_articles(ArticleFilter::feed_unread(feed_id)).context(PortalErrorKind::DB)?;
75
76        Ok(articles.iter().map(|article| article.article_id.clone()).collect())
77    }
78
79    fn get_article_ids_unread_category(&self, category_id: &CategoryID) -> PortalResult<Vec<ArticleID>> {
80        let articles = self
81            .db
82            .read_articles(ArticleFilter::category_unread(category_id))
83            .context(PortalErrorKind::DB)?;
84
85        Ok(articles.iter().map(|article| article.article_id.clone()).collect())
86    }
87
88    fn get_article_ids_unread_all(&self) -> PortalResult<Vec<ArticleID>> {
89        let articles = self.db.read_articles(ArticleFilter::all_unread()).context(PortalErrorKind::DB)?;
90
91        Ok(articles.iter().map(|article| article.article_id.clone()).collect())
92    }
93
94    fn get_article_ids_marked_all(&self) -> PortalResult<Vec<ArticleID>> {
95        let articles = self.db.read_articles(ArticleFilter::all_marked()).context(PortalErrorKind::DB)?;
96
97        Ok(articles.iter().map(|article| article.article_id.clone()).collect())
98    }
99
100    fn get_feeds(&self) -> PortalResult<Vec<Feed>> {
101        let feeds = self.db.read_feeds().context(PortalErrorKind::DB)?;
102        Ok(feeds)
103    }
104
105    fn get_categories(&self) -> PortalResult<Vec<Category>> {
106        let categories = self.db.read_categories().context(PortalErrorKind::DB)?;
107        Ok(categories)
108    }
109
110    fn get_feed_mappings(&self) -> PortalResult<Vec<FeedMapping>> {
111        let mappings = self.db.read_feed_mappings(None, None).context(PortalErrorKind::DB)?;
112        Ok(mappings)
113    }
114
115    fn get_category_mappings(&self) -> PortalResult<Vec<CategoryMapping>> {
116        let mappings = self.db.read_category_mappings(None, None).context(PortalErrorKind::DB)?;
117        Ok(mappings)
118    }
119
120    fn get_tags(&self) -> PortalResult<Vec<Tag>> {
121        let tags = self.db.read_tags().context(PortalErrorKind::DB)?;
122        Ok(tags)
123    }
124}