handle duplicate feeds

This commit is contained in:
Gabriel 2025-07-25 18:40:50 -04:00
parent 5563a293f5
commit 8183feb229

View file

@ -25,7 +25,7 @@ const FEEDS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
'title' TEXT NOT NULL, 'title' TEXT NOT NULL,
'description' TEXT, 'description' TEXT,
'icon' BLOB, 'icon' BLOB,
'url' text, 'url' text not null unique,
'subscribed' INTEGER NOT NULL default 0, 'subscribed' INTEGER NOT NULL default 0,
'last_updated' TEXT , 'last_updated' TEXT ,
PRIMARY KEY('feedID') PRIMARY KEY('feedID')
@ -86,22 +86,31 @@ pub fn initialize() {
} }
pub fn add_feed(url: &str) { pub fn add_feed(url: &str) -> Option<usize> {
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
let feed = load_rss(url).unwrap(); let feed = load_rss(url).unwrap();
let new_feed = feed.clone(); let new_feed = feed.clone();
let time = Utc::now().to_rfc2822(); let time = Utc::now().to_rfc2822();
conn.execute( match conn.execute(
"insert into feeds(title,url,description,last_updated) values(?1,?2,?3,?4)", "insert into feeds(title,url,description,last_updated) values(?1,?2,?3,?4)",
[feed.title, url.to_string(), feed.description, time], [feed.title, url.to_string(), feed.description, time],
) ) {
.unwrap(); Ok(_) => {
}
Err(e) => {
println!("Couldn't add feed:{}\nError:{}",url,e);
return None;
}
}
let mut stmt = conn.prepare("select feedID from feeds where url=?1").unwrap(); let mut stmt = conn.prepare("select feedID from feeds where url=?1").unwrap();
let id: usize = stmt.query_row([url],|row| { let id: usize = stmt.query_row([url],|row| {
row.get(0) row.get(0)
}).unwrap(); }).unwrap();
//need to get the feed_id from the DB and then make sure items are mapped to feed //need to get the feed_id from the DB and then make sure items are mapped to feed
store_items(new_feed,id); store_items(new_feed,id);
Some(id)
} }
pub fn store_items(feed: rss::Channel,feed_id: usize) { pub fn store_items(feed: rss::Channel,feed_id: usize) {