fix some, break some compiling by adding async/await in front of it

i forgot that we can't `Send` diesel connections over threads, so this
is going to require some rethinking.
This commit is contained in:
Igor Galić 2020-05-16 12:06:58 +02:00
parent 3c830ab0ce
commit 44ebce516c
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
5 changed files with 39 additions and 29 deletions

View File

@ -71,7 +71,8 @@ impl Blog {
insert!(blogs, NewBlog, |inserted, conn| {
let instance = inserted.get_instance(conn)?;
if inserted.outbox_url.is_empty() {
inserted.outbox_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "outbox");
inserted.outbox_url =
instance.compute_box(BLOG_PREFIX, &inserted.actor_id, r#"outbox"#);
}
if inserted.inbox_url.is_empty() {

View File

@ -66,14 +66,14 @@ pub struct OAuthRequest {
}
#[get("/oauth2?<query..>")]
pub fn oauth(
pub async fn oauth(
query: Form<OAuthRequest>,
rockets: PlumeRocket,
) -> Result<Json<serde_json::Value>, ApiError> {
let conn = &*rockets.conn;
let app = App::find_by_client_id(conn, &query.client_id)?;
if app.client_secret == query.client_secret {
if let Outcome::Success(user) = User::find_by_fqn(&rockets, &query.username) {
if let Ok(user) = User::find_by_fqn(&rockets, &query.username).await {
if user.auth(&query.password) {
let token = ApiToken::insert(
conn,

View File

@ -98,7 +98,7 @@ pub fn list(
}
#[post("/posts", data = "<payload>")]
pub fn create(
pub async fn create(
auth: Authorization<Write, Post>,
payload: Json<NewPostData>,
rockets: PlumeRocket,
@ -192,7 +192,7 @@ pub fn create(
for m in mentions.into_iter() {
Mention::from_activity(
&*conn,
&Mention::build_activity(&rockets, &m)?,
&Mention::build_activity(&rockets, &m).await?,
post.id,
true,
true,

View File

@ -38,12 +38,12 @@ pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result
}
#[get("/~/<name>", rank = 1)]
pub fn activity_details(
pub async fn activity_details(
name: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<CustomGroup>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
let blog = Blog::find_by_fqn(&rockets, &name).await?.ok()?;
Some(ActivityStream::new(blog.to_activity(&*rockets.conn).ok()?))
}
@ -93,7 +93,7 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
Ok(_) => ValidationErrors::new(),
Err(e) => e,
};
if Blog::find_by_fqn(&rockets, &slug).is_ok() {
if Blog::find_by_fqn(&rockets, &slug).await.is_ok() {
errors.add(
"title",
ValidationError {
@ -184,9 +184,9 @@ pub struct EditForm {
}
#[get("/~/<name>/edit")]
pub fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
pub async fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let blog = Blog::find_by_fqn(&rockets, &name)?;
let blog = Blog::find_by_fqn(&rockets, &name).await?;
if rockets
.user
.clone()
@ -233,14 +233,16 @@ fn check_media(conn: &Connection, id: i32, user: &User) -> bool {
}
#[put("/~/<name>/edit", data = "<form>")]
pub fn update(
pub async fn update(
name: String,
form: LenientForm<EditForm>,
rockets: PlumeRocket,
) -> RespondOrRedirect {
let conn = &*rockets.conn;
let intl = &rockets.intl.catalog;
let mut blog = Blog::find_by_fqn(&rockets, &name).expect("blog::update: blog not found");
let mut blog = Blog::find_by_fqn(&rockets, &name)
.await
.expect("blog::update: blog not found");
if !rockets
.user
.clone()
@ -342,23 +344,26 @@ pub fn update(
}
#[get("/~/<name>/outbox")]
pub fn outbox(name: String, rockets: PlumeRocket) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
pub async fn outbox(
name: String,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_by_fqn(&rockets, &name).await?.ok()?;
Some(blog.outbox(&*rockets.conn).ok()?)
}
#[allow(unused_variables)]
#[get("/~/<name>/outbox?<page>")]
pub fn outbox_page(
pub async fn outbox_page(
name: String,
page: Page,
rockets: PlumeRocket,
) -> Option<ActivityStream<OrderedCollectionPage>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
let blog = Blog::find_by_fqn(&rockets, &name).await?.ok()?;
Some(blog.outbox_page(&*rockets.conn, page.limits()).ok()?)
}
#[get("/~/<name>/atom.xml")]
pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
pub async fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
let blog = Blog::find_by_fqn(&rockets, &name).await?.ok()?;
let conn = &*rockets.conn;
let entries = Post::get_recents_for_blog(&*conn, &blog, 15).ok()?;
let uri = Instance::get_local()

View File

@ -162,7 +162,7 @@ pub fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result<Ructe,
}
#[get("/~/<blog>/<slug>/edit")]
pub fn edit(
pub async fn edit(
blog: String,
slug: String,
cl: ContentLen,
@ -170,7 +170,7 @@ pub fn edit(
) -> Result<Ructe, ErrorPage> {
let conn = &*rockets.conn;
let intl = &rockets.intl.catalog;
let b = Blog::find_by_fqn(&rockets, &blog)?;
let b = Blog::find_by_fqn(&rockets, &blog).await?;
let post = Post::find_by_slug(&*conn, &slug, b.id)?;
let user = rockets.user.clone().unwrap();
@ -216,7 +216,7 @@ pub fn edit(
}
#[post("/~/<blog>/<slug>/edit", data = "<form>")]
pub fn update(
pub async fn update(
blog: String,
slug: String,
cl: ContentLen,
@ -224,7 +224,9 @@ pub fn update(
rockets: PlumeRocket,
) -> RespondOrRedirect {
let conn = &*rockets.conn;
let b = Blog::find_by_fqn(&rockets, &blog).expect("post::update: blog error");
let b = Blog::find_by_fqn(&rockets, &blog)
.await
.expect("post::update: blog error");
let mut post =
Post::find_by_slug(&*conn, &slug, b.id).expect("post::update: find by slug error");
let user = rockets.user.clone().unwrap();
@ -302,11 +304,11 @@ pub fn update(
if post.published {
post.update_mentions(
&conn,
mentions
.into_iter()
.filter_map(|m| Mention::build_activity(&rockets, &m).ok())
.filter_map(|m| Mention::build_activity(&rockets, &m).await.ok())
.collect(),
&conn,
)
.expect("post::update: mentions error");
}
@ -321,7 +323,7 @@ pub fn update(
.filter_map(|t| Tag::build_activity(t).ok())
.collect::<Vec<_>>();
post.update_tags(&conn, tags)
.expect("post::update: tags error");
.expect(r#"post::update: tags error"#);
let hashtags = hashtags
.into_iter()
@ -615,12 +617,13 @@ pub fn delete(
}
#[get("/~/<blog_name>/<slug>/remote_interact")]
pub fn remote_interact(
pub async fn remote_interact(
rockets: PlumeRocket,
blog_name: String,
slug: String,
) -> Result<Ructe, ErrorPage> {
let target = Blog::find_by_fqn(&rockets, &blog_name)
.await
.and_then(|blog| Post::find_by_slug(&rockets.conn, &slug, blog.id))?;
Ok(render!(posts::remote_interact(
&rockets.to_context(),
@ -633,17 +636,18 @@ pub fn remote_interact(
}
#[post("/~/<blog_name>/<slug>/remote_interact", data = "<remote>")]
pub fn remote_interact_post(
pub async fn remote_interact_post(
rockets: PlumeRocket,
blog_name: String,
slug: String,
remote: LenientForm<RemoteForm>,
) -> Result<RespondOrRedirect, ErrorPage> {
let target = Blog::find_by_fqn(&rockets, &blog_name)
.await
.and_then(|blog| Post::find_by_slug(&rockets.conn, &slug, blog.id))?;
if let Some(uri) = User::fetch_remote_interact_uri(&remote.remote)
.ok()
.and_then(|uri| uri.replace("{uri}", format!("{}", target.ap_url)).ok())
.await
.map(|uri| uri.replace("{uri}", format!("{}", target.ap_url)).ok())
{
Ok(Redirect::to(uri).into())
} else {