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
use std::sync::RwLock;

use async_trait::async_trait;

use crate::repository::{
    self,
    subscription::{RepositoryResult, SubscriptionRepository},
};

pub struct MemoryRepository {
    feeds: RwLock<Vec<repository::types::FeedSubscription>>,
}

const TEST_DATA: &[&str] = &[
    "https://seanmonstar.com/rss",
    "https://thesquareplanet.com/feed.xml",
    "https://thiscute.world/en/index.xml",
    "https://blog.m-ou.se/index.xml",
    "https://keens.github.io/index.xml",
    "https://without.boats/index.xml",
    "https://blog.rust-lang.org/feed.xml",
    "https://blog.ymgyt.io/atom.xml",
    "https://this-week-in-rust.org/atom.xml",
    "https://blog.orhun.dev/rss.xml",
    "https://buttondown.email/o11y.news/rss",
    "https://fasterthanli.me/index.xml",
    "https://docs.aws.amazon.com/eks/latest/userguide/doc-history.rss",
    "https://kubernetes.io/feed.xml",
    "https://blog.guillaume-gomez.fr/atom",
    "https://sgued.fr/blog/atom.xml",
    "https://thiscute.world/en/index.xml",
    "https://blog-dry.com/feed",
];

impl MemoryRepository {
    #[must_use]
    pub fn new() -> Self {
        Self {
            feeds: RwLock::new(
                TEST_DATA
                    .iter()
                    .map(|feed| repository::types::FeedSubscription {
                        user_id: "me".into(),
                        url: (*feed).to_string(),
                    })
                    .collect(),
            ),
        }
    }
}

#[async_trait]
impl SubscriptionRepository for MemoryRepository {
    async fn put_feed_subscription(
        &self,
        feed: repository::types::FeedSubscription,
    ) -> RepositoryResult<()> {
        self.feeds.write().unwrap().push(feed);
        Ok(())
    }

    async fn delete_feed_subscription(
        &self,
        feed: repository::types::FeedSubscription,
    ) -> RepositoryResult<()> {
        let to_delete = feed.url;
        self.feeds
            .write()
            .unwrap()
            .retain(|sub| sub.url != to_delete);
        Ok(())
    }

    async fn fetch_subscribed_feed_urls(&self, _user_id: &str) -> RepositoryResult<Vec<String>> {
        Ok(self
            .feeds
            .read()
            .unwrap()
            .iter()
            .map(|feed| feed.url.clone())
            .collect())
    }
}