From 486e0ff5e486115098f57425961b222307a3ee35 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 21 Oct 2017 00:12:24 +0300 Subject: [PATCH] Now Upon feed addition only that feed is indexed/updated. --- hammond-data/src/index_feed.rs | 8 ++++---- hammond-gtk/src/headerbar.rs | 9 ++++----- hammond-gtk/src/main.rs | 2 ++ hammond-gtk/src/utils.rs | 31 +++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/hammond-data/src/index_feed.rs b/hammond-data/src/index_feed.rs index 542b81c..6489f2f 100644 --- a/hammond-data/src/index_feed.rs +++ b/hammond-data/src/index_feed.rs @@ -88,7 +88,7 @@ pub fn index_loop(db: Arc>, force: bool) -> Result<()> { Ok(()) } -fn complete_index_from_source( +pub fn complete_index_from_source( req: &mut reqwest::Response, source: &Source, mutex: Arc>, @@ -165,7 +165,7 @@ pub fn fetch_feeds( Ok(results) } -fn refresh_source( +pub fn refresh_source( connection: &SqliteConnection, feed: &mut Source, force: bool, @@ -188,11 +188,11 @@ fn refresh_source( // FIXME: I have fucked up somewhere here. // Getting back 200 codes even though I supposedly sent etags. - info!("Headers: {:?}", headers); + // info!("Headers: {:?}", headers); client.get(feed.uri()).headers(headers).send()? }; - info!("{}", req.status()); + info!("GET to {} , returned: {}", feed.uri(), req.status()); // TODO match on more stuff // 301: Permanent redirect of the url diff --git a/hammond-gtk/src/headerbar.rs b/hammond-gtk/src/headerbar.rs index 9fa4dcc..c367d22 100644 --- a/hammond-gtk/src/headerbar.rs +++ b/hammond-gtk/src/headerbar.rs @@ -36,15 +36,14 @@ pub fn get_headerbar(db: Arc>, stack: gtk::Stack) -> gtk let f = index_feed::insert_return_source(&tempdb, &url); drop(tempdb); info!("{:?} feed added", url); - if f.is_err() { + if let Ok(mut source) = f { + // update the db + utils::refresh_feed(db_clone.clone(), stack_clone.clone(), &mut source); + } else { error!("Expected Error, feed probably already exists."); error!("Error: {:?}", f.unwrap_err()); } - // update the db - // TODO: have it fettch only the added feed. - utils::refresh_db(db_clone.clone(), stack_clone.clone()); - // TODO: lock the button instead of hiding and add notification of feed added. // TODO: map the spinner add_popover_clone.hide(); diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index 36255a4..f8ab2c2 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -54,6 +54,8 @@ fn build_ui(app: >k::Application) { // Get the headerbar let header = headerbar::get_headerbar(db.clone(), stack.clone()); + // Uncomment this when etag implementation is fixed and refesh_db thread is non blocking. + // utils::refresh_db(db.clone(), stack.clone()); window.set_titlebar(&header); window.show_all(); diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 49bce42..52fff4d 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -4,6 +4,7 @@ use gtk; // use gtk::prelude::*; use hammond_data; +use hammond_data::models::Source; use diesel::prelude::SqliteConnection; use std::thread; @@ -25,8 +26,34 @@ pub fn refresh_db(db: Arc>, stack: gtk::Stack) { handle.join(); podcasts_view::update_podcasts_view(db.clone(), stack.clone()); - // let foo = stack.emit("foo", &[]); - // info!("{:?}", foo) +} + +pub fn refresh_feed(db: Arc>, stack: gtk::Stack, source: &mut Source) { + let db_clone = db.clone(); + let source_ = source.clone(); + // TODO: add timeout option and error reporting. + let handle = thread::spawn(move || { + let db_ = db_clone.lock().unwrap(); + let foo = hammond_data::index_feed::refresh_source(&db_, &mut source_.clone(), false); + drop(db_); + + if let Ok((mut req, s)) = foo { + let s = hammond_data::index_feed::complete_index_from_source( + &mut req, + &s, + db_clone.clone(), + ); + if s.is_err() { + error!("Error While trying to update the database."); + error!("Error msg: {}", s.unwrap_err()); + }; + }; + }); + // FIXME: atm freezing the ui till update is done. + // Make it instead emmit a signal on update completion. + handle.join(); + + podcasts_view::update_podcasts_view(db.clone(), stack.clone()); } // https://github.