From 6fe16c9f8465835835b46d4e3627e0566bd12955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Tue, 18 Feb 2020 16:35:25 +0100 Subject: [PATCH] =?UTF-8?q?upgrade=20and=20use=20futures=E2=80=A6=20then?= =?UTF-8?q?=20block=5Fon=20.await=20in=20a=20trait=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + plume-models/Cargo.toml | 1 + plume-models/src/blogs.rs | 9 +++++---- plume-models/src/lib.rs | 1 + plume-models/src/timeline/query.rs | 19 +++++++++++++++---- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a8256e5..783e5c71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2529,6 +2529,7 @@ dependencies = [ "diesel", "diesel-derive-newtype", "diesel_migrations", + "futures 0.3.4", "glob", "guid-create", "heck", diff --git a/plume-models/Cargo.toml b/plume-models/Cargo.toml index 1ee88cc8..fb49cfd6 100644 --- a/plume-models/Cargo.toml +++ b/plume-models/Cargo.toml @@ -10,6 +10,7 @@ ammonia = "2.1.1" askama_escape = "0.1" bcrypt = "0.5" guid-create = "0.1" +futures = "0.3" heck = "0.3.0" itertools = "0.8.0" lazy_static = "1.0" diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index cbef35c6..e4f15268 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -132,7 +132,7 @@ impl Blog { .map_err(Error::from) } - pub fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result { + pub async fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result { let from_db = blogs::table .filter(blogs::fqn.eq(fqn)) .first(&*c.conn) @@ -140,12 +140,13 @@ impl Blog { if let Some(from_db) = from_db { Ok(from_db) } else { - Blog::fetch_from_webfinger(c, fqn) + Blog::fetch_from_webfinger(c, fqn).await } } - fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result { - resolve_with_prefix(Prefix::Group, acct.to_owned(), true)? + async fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result { + resolve_with_prefix(Prefix::Group, acct.to_owned(), true) + .await? .links .into_iter() .find(|l| l.mime_type == Some(String::from("application/activity+json"))) diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index 2a56028e..f652e6df 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -4,6 +4,7 @@ #[macro_use] extern crate diesel; +extern crate futures; #[macro_use] extern crate lazy_static; #[macro_use] diff --git a/plume-models/src/timeline/query.rs b/plume-models/src/timeline/query.rs index 68cd42d5..7c0a249d 100644 --- a/plume-models/src/timeline/query.rs +++ b/plume-models/src/timeline/query.rs @@ -7,7 +7,9 @@ use crate::{ users::User, PlumeRocket, Result, }; +use futures::stream::{self, StreamExt}; use plume_common::activity_pub::inbox::AsActor; +use tokio::runtime::Runtime; use whatlang::{self, Lang}; #[derive(Debug, Clone, PartialEq)] @@ -295,10 +297,19 @@ impl WithList { } } List::Array(list) => match self { - WithList::Blog => Ok(list - .iter() - .filter_map(|b| Blog::find_by_fqn(rocket, b).ok()) - .any(|b| b.id == post.blog_id)), + WithList::Blog => { + let mut rt = Runtime::new().unwrap(); + rt.block_on(async move { + Ok(stream::iter(list) + .filter_map(|b| async move { + Some(Blog::find_by_fqn(rocket, b).await.ok().unwrap()) + }) + .collect::>() + .await + .into_iter() + .any(|b| b.id == post.blog_id)) + }) + } WithList::Author { boosts, likes } => match kind { Kind::Original => Ok(list .iter()