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
No known key found for this signature in database
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 activitypub::{actor::Group, collection::OrderedCollection, object::Image, CustomObject};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl}; use diesel::{self, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl, SaveChangesDsl};
use openssl::{ use openssl::{
hash::MessageDigest, hash::MessageDigest,
pkey::{PKey, Private}, pkey::{PKey, Private},
@ -195,14 +195,10 @@ impl Blog {
} }
} }
pub fn find_by_host(c: &PlumeRocket, host: Host) -> Result<Blog> { pub fn find_by_host(c: &PlumeRocket, host: Host) -> QueryResult<Blog> {
let from_db = blogs::table blogs::table
.filter(blogs::custom_domain.eq(host)) .filter(blogs::custom_domain.eq(host))
.limit(1) .first::<Blog>(&*c.conn)
.load::<Blog>(&*c.conn)?
.into_iter()
.next();
from_db.ok_or(Error::NotFound)
} }
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> { 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 { impl<'r> Responder<'r> for ErrorPage {
fn respond_to(self, req: &Request) -> response::Result<'r> { fn respond_to(self, req: &Request) -> response::Result<'r> {
let rockets = req.guard::<PlumeRocket>().unwrap(); let rockets = req.guard::<PlumeRocket>().unwrap();