use crate::{ Result, AdapterPool, db::{ ChannelId, ItemId, }, fetch::FetchedRSSItem, }; use chrono::{DateTime, Utc}; pub struct UnparsedItem { pub id: i64, pub channel_id: i64, pub fetched_at: Option, pub title: Option, pub description: Option, pub content: Option, } impl UnparsedItem { pub fn parse(self) -> Result { Ok(Item { id: ItemId(self.id), channel_id: ChannelId(self.channel_id), fetched_at: match self.fetched_at { Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)? .with_timezone(&Utc)), None => None, }, title: self.title, description: self.description, content: self.content, }) } } pub struct Item { id: ItemId, channel_id: ChannelId, #[allow(dead_code)] // TODO: Use for score decay calculations later fetched_at: Option>, title: Option, description: Option, content: Option, } impl Item { pub fn id(&self) -> ItemId { self.id } pub fn channel(&self) -> ChannelId { self.channel_id } pub fn title(&self) -> Option<&str> { self.title.as_deref() } pub fn description(&self) -> Option<&str> { self.description.as_deref() } pub fn content(&self) -> Option<&str> { self.content.as_deref() } pub async fn get_or_create( pool: &AdapterPool, from_channel: ChannelId, guid: &str ) -> Result { let int_channel_id = i64::from(from_channel); let item = sqlx::query_as!( UnparsedItem, "INSERT INTO items (channel_id, guid) VALUES (?, ?) ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id RETURNING id as `id!`, channel_id, fetched_at, title, description, content", int_channel_id, guid ).fetch_one(&pool.0).await?.parse(); item } pub async fn update_content( &self, pool: &AdapterPool, fetched: &FetchedRSSItem, fetched_at: &DateTime ) -> Result<()> { let title = fetched.title(); let description = fetched.description(); let content = fetched.content(); let string_fetched_at = fetched_at.to_rfc2822(); sqlx::query!( "UPDATE items SET title = ?, description = ?, content = ?, fetched_at = ? WHERE id = ?", title, description, content, string_fetched_at, self.id.0 ).execute(&pool.0).await?; Ok(()) } } #[cfg(test)] mod tests { use super::*; use reqwest::Url; use crate::{ Adapter, AdapterBuilder, db::{ Channel, } }; const FEED1: &str = "https://example.com/feed"; const ITEM_GUID: &str = "item-guid"; async fn setup_adapter() -> Adapter { AdapterBuilder::new() .database_url("sqlite::memory:") .create().await.unwrap() } async fn setup_channel(pool: &AdapterPool) -> Channel { let url = Url::parse(FEED1).unwrap(); Channel::get_or_create(pool, url).await.unwrap() } // Item Tests #[tokio::test] pub async fn get_or_create_duplicate() { let adapter = setup_adapter().await; let pool = adapter.get_pool(); let channel = setup_channel(pool).await; let item1 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap(); let item2 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap(); assert_eq!(item1.id(), item2.id()); } }