diff --git a/src/feeds.rs b/src/feeds.rs index 29ea447..c95cab6 100644 --- a/src/feeds.rs +++ b/src/feeds.rs @@ -43,6 +43,30 @@ pub struct NewFeed { pub categorization: Vec, } +#[derive(Debug)] +struct FeedError; + +async fn fetch_feed(url: &Url) -> Result { + // Fetch the feed content + let response = reqwest::get(url.as_ref()).await.map_err(|e| { + eprintln!("Failed to fetch feed: {}", e); + FeedError + })?; + + let content = response.text().await.map_err(|e| { + eprintln!("Failed to read response body: {}", e); + FeedError + })?; + + // Parse the feed + let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| { + eprintln!("Failed to parse feed content: {}", e); + FeedError + })?; + + Ok(feed_data) +} + #[post("/feeds", data = "")] pub async fn create_feed( mut db: Connection, @@ -56,28 +80,15 @@ pub async fn create_feed( return Err(Status::UnprocessableEntity); } - // Fetch the feed content - let response = reqwest::get(new_feed.url.as_ref()).await.map_err(|e| { - eprintln!("Failed to fetch feed: {}", e); - Status::UnprocessableEntity - })?; - - let content = response.text().await.map_err(|e| { - eprintln!("Failed to read response body: {}", e); - Status::UnprocessableEntity - })?; - - // Parse the feed - let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| { - eprintln!("Failed to parse feed content: {}", e); - Status::UnprocessableEntity - })?; + let feed_data = fetch_feed(&new_feed.url) + .await + .map_err(|_| Status::UnprocessableEntity)?; // Use the feed title as the name, or URL if no title is available let name = feed_data .title .map(|t| t.content) - .unwrap_or_else(|| new_feed.url.host_str().unwrap_or("Unknown").to_string()); + .unwrap_or_else(|| new_feed.url.host_str().unwrap_or("").to_string()); let mut feed = Feed::new(name, new_feed.url, user.user_id); feed.categorization = new_feed.categorization; @@ -156,10 +167,11 @@ pub async fn list_feeds( })?; // Parse categorization from JSON value - let categorization: Vec = serde::json::from_value(row.categorization).map_err(|e| { - eprintln!("Failed to parse categorization: {}", e); - Status::InternalServerError - })?; + let categorization: Vec = + serde::json::from_value(row.categorization).map_err(|e| { + eprintln!("Failed to parse categorization: {}", e); + Status::InternalServerError + })?; Ok(Feed { feed_id: Uuid::parse_str(&row.feed_id).unwrap(),