upgrade and use futures… then block_on .await in a trait?

This commit is contained in:
Igor Galić 2020-02-18 16:35:25 +01:00
parent 43cb9f700c
commit 6fe16c9f84
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
5 changed files with 23 additions and 8 deletions

1
Cargo.lock generated
View File

@ -2529,6 +2529,7 @@ dependencies = [
"diesel", "diesel",
"diesel-derive-newtype", "diesel-derive-newtype",
"diesel_migrations", "diesel_migrations",
"futures 0.3.4",
"glob", "glob",
"guid-create", "guid-create",
"heck", "heck",

View File

@ -10,6 +10,7 @@ ammonia = "2.1.1"
askama_escape = "0.1" askama_escape = "0.1"
bcrypt = "0.5" bcrypt = "0.5"
guid-create = "0.1" guid-create = "0.1"
futures = "0.3"
heck = "0.3.0" heck = "0.3.0"
itertools = "0.8.0" itertools = "0.8.0"
lazy_static = "1.0" lazy_static = "1.0"

View File

@ -132,7 +132,7 @@ impl Blog {
.map_err(Error::from) .map_err(Error::from)
} }
pub fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result<Blog> { pub async fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result<Blog> {
let from_db = blogs::table let from_db = blogs::table
.filter(blogs::fqn.eq(fqn)) .filter(blogs::fqn.eq(fqn))
.first(&*c.conn) .first(&*c.conn)
@ -140,12 +140,13 @@ impl Blog {
if let Some(from_db) = from_db { if let Some(from_db) = from_db {
Ok(from_db) Ok(from_db)
} else { } else {
Blog::fetch_from_webfinger(c, fqn) Blog::fetch_from_webfinger(c, fqn).await
} }
} }
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> { async fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> {
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)? resolve_with_prefix(Prefix::Group, acct.to_owned(), true)
.await?
.links .links
.into_iter() .into_iter()
.find(|l| l.mime_type == Some(String::from("application/activity+json"))) .find(|l| l.mime_type == Some(String::from("application/activity+json")))

View File

@ -4,6 +4,7 @@
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
extern crate futures;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]

View File

@ -7,7 +7,9 @@ use crate::{
users::User, users::User,
PlumeRocket, Result, PlumeRocket, Result,
}; };
use futures::stream::{self, StreamExt};
use plume_common::activity_pub::inbox::AsActor; use plume_common::activity_pub::inbox::AsActor;
use tokio::runtime::Runtime;
use whatlang::{self, Lang}; use whatlang::{self, Lang};
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -295,10 +297,19 @@ impl WithList {
} }
} }
List::Array(list) => match self { List::Array(list) => match self {
WithList::Blog => Ok(list WithList::Blog => {
.iter() let mut rt = Runtime::new().unwrap();
.filter_map(|b| Blog::find_by_fqn(rocket, b).ok()) rt.block_on(async move {
.any(|b| b.id == post.blog_id)), Ok(stream::iter(list)
.filter_map(|b| async move {
Some(Blog::find_by_fqn(rocket, b).await.ok().unwrap())
})
.collect::<Vec<_>>()
.await
.into_iter()
.any(|b| b.id == post.blog_id))
})
}
WithList::Author { boosts, likes } => match kind { WithList::Author { boosts, likes } => match kind {
Kind::Original => Ok(list Kind::Original => Ok(list
.iter() .iter()