various small cleanups

This commit is contained in:
ranfdev
2023-10-27 11:30:24 +02:00
parent 04c3f86a4b
commit d93356905c
6 changed files with 54 additions and 77 deletions

View File

@ -9,7 +9,7 @@ use glib::Properties;
use gtk::{gio, glib};
use ntfy_daemon::models;
use ntfy_daemon::ntfy_capnp::{output_channel, subscription, watch_handle, Status};
use tracing::{debug, debug_span, error, instrument};
use tracing::{debug, error, instrument};
struct TopicWatcher {
sub: glib::WeakRef<Subscription>,
@ -217,8 +217,7 @@ impl Subscription {
val.set_display_name(&*imp.display_name.borrow());
val.set_read_until(imp.read_until.get());
Promise::from_future(async move {
let _span = debug_span!("send_updated_info").entered();
debug!("sending");
debug!("sending update_info");
req.send().promise.await?;
Ok(())
})
@ -233,12 +232,8 @@ impl Subscription {
}
fn update_unread_count(&self) {
let imp = self.imp();
if let Some(last) = Self::last_message(&imp.messages) {
if last.time > imp.read_until.get() {
imp.unread_count.set(1);
} else {
imp.unread_count.set(0);
}
if Self::last_message(&imp.messages).map(|last| last.time) > Some(imp.read_until.get()) {
imp.unread_count.set(1);
} else {
imp.unread_count.set(0);
}
@ -256,13 +251,12 @@ impl Subscription {
}
pub fn flag_all_as_read(&self) -> Promise<(), capnp::Error> {
let imp = self.imp();
let Some(last) = Self::last_message(&imp.messages) else {
let Some(value) = Self::last_message(&imp.messages)
.map(|last| last.time)
.filter(|time| *time > self.imp().read_until.get())
else {
return Promise::ok(());
};
let value = last.time;
if self.imp().read_until.get() == value {
return Promise::ok(());
}
let this = self.clone();
Promise::from_future(async move {
@ -276,14 +270,15 @@ impl Subscription {
}
pub fn publish_msg(&self, mut msg: models::Message) -> Promise<(), capnp::Error> {
let imp = self.imp();
let json = {
msg.topic = self.topic();
serde_json::to_string(&msg).map_err(|e| capnp::Error::failed(e.to_string()))
};
let mut req = imp.client.get().unwrap().publish_request();
msg.topic = self.topic();
let json = serde_json::to_string(&msg).map_err(|e| capnp::Error::failed(e.to_string()));
req.get().set_message(&pry!(json));
Promise::from_future(async move {
let _span = debug_span!("publish").entered();
debug!("sending");
debug!("sending publish");
req.send().promise.await?;
Ok(())
})
@ -294,8 +289,7 @@ impl Subscription {
let req = imp.client.get().unwrap().clear_notifications_request();
let this = self.clone();
Promise::from_future(async move {
let _span = debug_span!("clear_notifications").entered();
debug!("sending");
debug!("sending clear_notifications");
req.send().promise.await?;
this.imp().messages.remove_all();
Ok(())

View File

@ -145,39 +145,29 @@ impl MessageRow {
self.attach(&tags, 0, row, 3, 1);
}
}
fn fetch_image_bytes(url: &str) -> anyhow::Result<Vec<u8>> {
let path = glib::user_cache_dir().join("com.ranfdev.Notify").join(&url);
let bytes = if path.exists() {
std::fs::read(&path)?
} else {
let mut bytes = vec![];
ureq::get(&url)
.call()?
.into_reader()
.take(5 * 1_000_000) // 5 MB
.read_to_end(&mut bytes)?;
bytes
};
Ok(bytes)
}
fn build_image(&self, url: String) -> gtk::Picture {
let (tx, rx) = glib::MainContext::channel(Default::default());
gio::spawn_blocking(move || {
let path = glib::user_cache_dir().join("com.ranfdev.Notify").join(&url);
let bytes = if path.exists() {
match std::fs::read(&path) {
Ok(v) => v,
Err(e) => {
error!(error = %e, path = %path.display(), "reading image from disk");
return glib::ControlFlow::Break;
}
}
} else {
let res = match ureq::get(&url).call() {
Ok(res) => res,
Err(e) => {
error!(error = %e, "fetching image");
return glib::ControlFlow::Break;
}
};
let mut bytes = vec![];
if let Err(e) = res
.into_reader()
.take(5 * 1_000_000) // 5 MB
.read_to_end(&mut bytes)
{
error!(error = %e, "reading image data");
return glib::ControlFlow::Break;
}
bytes
};
tx.send(glib::Bytes::from_owned(bytes)).unwrap();
if let Err(e) =
Self::fetch_image_bytes(&url).map(|bytes| tx.send(glib::Bytes::from_owned(bytes)))
{
error!(error = %e)
}
glib::ControlFlow::Break
});
let picture = gtk::Picture::new();

View File

@ -346,7 +346,7 @@ impl NotifyWindow {
.bind_model(Some(&imp.subscription_list_model), |obj| {
let sub = obj.downcast_ref::<Subscription>().unwrap();
Self::build_subscription_ui(&sub).upcast()
Self::build_subscription_row(&sub).upcast()
});
let this = self.clone();
@ -445,7 +445,7 @@ impl NotifyWindow {
chip
}
fn build_subscription_ui(sub: &Subscription) -> impl glib::IsA<gtk::Widget> {
fn build_subscription_row(sub: &Subscription) -> impl glib::IsA<gtk::Widget> {
let b = gtk::Box::builder().spacing(4).build();
let label = gtk::Label::builder()