simplify retrieval in find_by_host()

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<T, diesel::Error>, so we
need to implement a converter for that error type.

This commit addresses @fdb-hiroshima's review.
This commit is contained in:
Igor Galić 2019-05-28 11:14:41 +02:00 committed by Igor Galić
parent 1c34ac38f7
commit 8e6b1ab86e
Signed by untrusted user: igalic
GPG key ID: ACFEFF7F6A123A86
2 changed files with 10 additions and 8 deletions

View file

@ -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<Blog> {
let from_db = blogs::table
pub fn find_by_host(c: &PlumeRocket, host: Host) -> QueryResult<Blog> {
blogs::table
.filter(blogs::custom_domain.eq(host))
.limit(1)
.load::<Blog>(&*c.conn)?
.into_iter()
.next();
from_db.ok_or(Error::NotFound)
.first::<Blog>(&*c.conn)
}
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> {

View file

@ -14,6 +14,12 @@ impl From<Error> for ErrorPage {
}
}
impl From<diesel::result::Error> 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::<PlumeRocket>().unwrap();