From 8e6b1ab86e2d5fe8fb63f52b5204922264f9e5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Tue, 28 May 2019 11:14:41 +0200 Subject: [PATCH] simplify retrieval in find_by_host() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit we can use "first()" instead of limit(1).load().etc… since on a UNIQUE field, we only expect 1 result. first() returns QueryResult, which is Result, so we need to implement a converter for that error type. This commit addresses @fdb-hiroshima's review. --- plume-models/src/blogs.rs | 12 ++++-------- src/routes/errors.rs | 6 ++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index f8fcffb3..5e36331c 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -1,6 +1,6 @@ use activitypub::{actor::Group, collection::OrderedCollection, object::Image, CustomObject}; use chrono::NaiveDateTime; -use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl}; +use diesel::{self, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl, SaveChangesDsl}; use openssl::{ hash::MessageDigest, pkey::{PKey, Private}, @@ -195,14 +195,10 @@ impl Blog { } } - pub fn find_by_host(c: &PlumeRocket, host: Host) -> Result { - let from_db = blogs::table + pub fn find_by_host(c: &PlumeRocket, host: Host) -> QueryResult { + blogs::table .filter(blogs::custom_domain.eq(host)) - .limit(1) - .load::(&*c.conn)? - .into_iter() - .next(); - from_db.ok_or(Error::NotFound) + .first::(&*c.conn) } fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result { diff --git a/src/routes/errors.rs b/src/routes/errors.rs index 1320a068..23f38030 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -14,6 +14,12 @@ impl From for ErrorPage { } } +impl From for ErrorPage { + fn from(err: diesel::result::Error) -> ErrorPage { + ErrorPage(plume_models::Error::Db(err)) + } +} + impl<'r> Responder<'r> for ErrorPage { fn respond_to(self, req: &Request) -> response::Result<'r> { let rockets = req.guard::().unwrap();