Skip to main content

rss_proxy/plugin/builtin/
link_cleaner.rs

1use crate::model::FeedItem;
2use crate::plugin::Plugin;
3use async_trait::async_trait;
4use url::Url;
5
6pub struct LinkCleanerPlugin {
7    tracking_params: Vec<&'static str>,
8}
9
10impl LinkCleanerPlugin {
11    pub fn new() -> Self {
12        Self {
13            tracking_params: vec![
14                "utm_source",
15                "utm_medium",
16                "utm_campaign",
17                "utm_term",
18                "utm_content",
19                "utm_id",
20                "fbclid",
21                "gclid",
22                "mc_cid",
23                "mc_eid",
24                "_ga",
25                "_hsenc",
26                "_hsmi",
27            ],
28        }
29    }
30
31    fn clean_url_string(&self, raw_url: &str) -> Option<String> {
32        let mut parsed_url = Url::parse(raw_url).ok()?;
33        let filtered_query: Vec<(String, String)> = parsed_url
34            .query_pairs()
35            .filter(|(key, _)| {
36                let k = key.to_lowercase();
37                !self.tracking_params.iter().any(|&param| k == param)
38            })
39            .map(|(k, v)| (k.into_owned(), v.into_owned()))
40            .collect();
41
42        if filtered_query.is_empty() {
43            parsed_url.set_query(None);
44        } else {
45            let mut serializer = url::form_urlencoded::Serializer::new(String::new());
46            for (k, v) in filtered_query {
47                serializer.append_pair(&k, &v);
48            }
49            parsed_url.set_query(Some(&serializer.finish()));
50        }
51
52        Some(parsed_url.to_string())
53    }
54}
55
56impl Default for LinkCleanerPlugin {
57    fn default() -> Self {
58        Self::new()
59    }
60}
61
62#[async_trait]
63impl Plugin for LinkCleanerPlugin {
64    fn name(&self) -> &str {
65        "LinkCleanerPlugin"
66    }
67
68    async fn transform_item(&self, item: &mut FeedItem) -> anyhow::Result<()> {
69        if let Some(cleaned) = self.clean_url_string(&item.link) {
70            item.link = cleaned;
71        }
72
73        if let Some(cleaned_id) = self.clean_url_string(&item.id) {
74            item.id = cleaned_id;
75        }
76
77        Ok(())
78    }
79}