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

@ -26,11 +26,9 @@ impl Db {
Ok(this)
}
fn migrate(&mut self) -> Result<()> {
{
self.conn
.borrow()
.execute_batch(include_str!("./migrations/00.sql"))?
};
self.conn
.borrow()
.execute_batch(include_str!("./migrations/00.sql"))?;
Ok(())
}
fn get_or_insert_server(&mut self, server: &str) -> Result<i64> {
@ -90,9 +88,9 @@ impl Db {
",
)?;
let msgs: Result<Vec<String>, _> = stmt
.query_map(params![server, topic, since], |row| Ok(row.get(0)?))?
.query_map(params![server, topic, since], |row| row.get(0))?
.collect();
Ok(msgs?)
msgs
}
pub fn insert_subscription(&mut self, sub: models::Subscription) -> Result<(), Error> {
let server_id = self.get_or_insert_server(&sub.server)?;
@ -116,7 +114,7 @@ impl Db {
WHERE server = ?1 AND topic = ?2",
params![server_id, topic],
)?;
if res <= 0 {
if res == 0 {
return Err(Error::SubscriptionNotFound("removing subscription".into()));
}
Ok(())
@ -162,7 +160,7 @@ impl Db {
sub.topic,
],
)?;
if res <= 0 {
if res == 0 {
return Err(Error::SubscriptionNotFound("updating subscription".into()));
}
info!(info = ?sub, "stored subscription info");
@ -184,7 +182,7 @@ impl Db {
",
params![server_id, topic, value],
)?;
if res <= 0 {
if res == 0 {
return Err(Error::SubscriptionNotFound("updating read_until".into()));
}
Ok(())
@ -198,7 +196,7 @@ impl Db {
",
params![server_id, topic],
)?;
if res <= 0 {
if res == 0 {
return Err(Error::SubscriptionNotFound("deleting messages".into()));
}
Ok(())

View File

@ -148,7 +148,7 @@ impl Subscription {
let mut url = url::Url::parse(server)?;
url.path_segments_mut()
.map_err(|_| url::ParseError::RelativeUrlWithCannotBeABaseBase)?
.push(&topic)
.push(topic)
.push("json");
url.query_pairs_mut()
.append_pair("since", &since.to_string());
@ -162,7 +162,7 @@ impl Subscription {
if let Err(e) = Self::build_url(&self.server, &self.topic, 0) {
errs.push(e);
};
if errs.len() > 0 {
if !errs.is_empty() {
return Err(errs);
}
Ok(self)

View File

@ -63,7 +63,7 @@ impl output_channel::Server for NotifyForwarder {
let already_stored: bool = {
// If this fails parsing, the message is not valid at all.
// The server is probably misbehaving.
let min_message: MinMessage = pry!(serde_json::from_str(&message)
let min_message: MinMessage = pry!(serde_json::from_str(message)
.map_err(|e| Error::InvalidMinMessage(message.to_string(), e)));
let model = self.model.borrow();
match self.env.db.insert_message(&model.server, message) {
@ -83,20 +83,15 @@ impl output_channel::Server for NotifyForwarder {
// Show notification
// Our priority is to show notifications. If anything fails, panic.
if !{ self.model.borrow().muted } {
let msg: Message = pry!(serde_json::from_str(&message)
let msg: Message = pry!(serde_json::from_str(message)
.map_err(|e| Error::InvalidMessage(message.to_string(), e)));
let np = self.env.proxy.clone();
let title = { msg.notification_title(&*self.model.borrow()) };
let title = { msg.notification_title(&self.model.borrow()) };
let n = models::Notification {
title: title.to_string(),
body: msg
.display_message()
.as_ref()
.map(|x| x.as_str())
.unwrap_or("")
.to_string(),
title,
body: msg.display_message().as_deref().unwrap_or("").to_string(),
actions: msg.actions,
};
@ -380,7 +375,7 @@ impl SystemNotifier {
pub fn watch_subscribed(&mut self) -> Promise<(), capnp::Error> {
let f: Vec<_> = pry!(self.env.db.list_subscriptions())
.into_iter()
.map(|m| self.watch(m.clone()))
.map(|m| self.watch(m))
.collect();
Promise::from_future(async move {
join_all(f.into_iter().map(|x| async move {
@ -434,7 +429,7 @@ impl system_notifier::Server for SystemNotifier {
pry!(self
.env
.db
.remove_subscription(&server, &topic)
.remove_subscription(server, topic)
.map_err(|e| capnp::Error::failed(e.to_string())));
info!(server, topic, "Unsubscribed");
}