Skip to main content

rss_proxy/plugin/builtin/
cleaner.rs

1use crate::model::FeedItem;
2use crate::plugin::{FilterDecision, Plugin};
3use async_trait::async_trait;
4
5pub struct FeedCleanerPlugin;
6
7impl FeedCleanerPlugin {
8    pub fn new() -> Self {
9        Self
10    }
11}
12
13impl Default for FeedCleanerPlugin {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19#[async_trait]
20impl Plugin for FeedCleanerPlugin {
21    fn name(&self) -> &str {
22        "FeedCleanerPlugin"
23    }
24
25    async fn filter_item(&self, item: &FeedItem) -> anyhow::Result<FilterDecision> {
26        let title_empty = item.title.trim().is_empty() || item.title == "Untitled";
27        let link_empty = item.link.trim().is_empty();
28        let desc_empty = item.description.trim().is_empty();
29
30        // Drop item if it has no title, no link, and no description (completely empty/corrupt entry)
31        if title_empty && link_empty && desc_empty {
32            return Ok(FilterDecision::Drop);
33        }
34
35        // Drop entries that are typical error pages from cloud providers (e.g. Cloudflare / Cloud Functions)
36        let lower_title = item.title.to_lowercase();
37        if lower_title.contains("403 forbidden")
38            || lower_title.contains("502 bad gateway")
39            || lower_title.contains("error 404")
40        {
41            return Ok(FilterDecision::Drop);
42        }
43
44        Ok(FilterDecision::Keep)
45    }
46
47    async fn transform_item(&self, item: &mut FeedItem) -> anyhow::Result<()> {
48        // Clean and trim extra whitespace in titles
49        item.title = item.title.split_whitespace().collect::<Vec<_>>().join(" ");
50        item.description = item.description.trim().to_string();
51
52        Ok(())
53    }
54}