diff --git a/Cargo.toml b/Cargo.toml index 894d9672..fac4e204 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] activitypub = "0.1.3" askama_escape = "0.1" +async-trait = "*" atom_syndication = "0.6" clap = "2.33" colored = "1.8" @@ -32,7 +33,7 @@ syntect = "3.3" tokio = "0.2" validator = "0.8" validator_derive = "0.8" -webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "update-deps" } +webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "4e8f12810c4a7ba7a07bbcb722cd265fdff512b6", features = ["async"] } [[bin]] name = "plume" diff --git a/src/routes/comments.rs b/src/routes/comments.rs index eaf27751..99ef6e57 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -36,8 +36,12 @@ pub fn create( rockets: PlumeRocket, ) -> Result, Ructe> { let conn = &*rockets.conn; - let blog = Blog::find_by_fqn(&rockets, &blog_name).expect("comments::create: blog error"); - let post = Post::find_by_slug(&*conn, &slug, blog.id).expect("comments::create: post error"); + let blog = Blog::find_by_fqn(&rockets, &blog_name) + .await + .expect("comments::create: blog error"); + let post = Post::find_by_slug(&*conn, &slug, blog.id) + .await + .expect("comments::create: post error"); form.validate() .map(|_| { let (html, mentions, _hashtags) = utils::md_to_html( diff --git a/src/routes/user.rs b/src/routes/user.rs index e024d7e4..2a43289e 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -42,7 +42,7 @@ pub fn me(user: Option) -> RespondOrRedirect { } #[get("/@/", rank = 2)] -pub fn details( +pub async fn details( name: String, rockets: PlumeRocket, fetch_rockets: PlumeRocket, @@ -61,6 +61,7 @@ pub fn details( worker.execute(move || { for create_act in user_clone .fetch_outbox::() + .await .expect("Remote user: outbox couldn't be fetched") { match create_act.create_props.object_object::() { @@ -79,6 +80,7 @@ pub fn details( worker.execute(move || { for user_id in user_clone .fetch_followers_ids() + .await .expect("Remote user: fetching followers error") { let follower = User::from_id(&fetch_followers_rockets, &user_id, None) @@ -101,6 +103,7 @@ pub fn details( worker.execute(move || { user_clone .refetch(&*update_conn) + .await .expect("Couldn't update user info"); }); } @@ -152,7 +155,7 @@ pub fn follow( rockets: PlumeRocket, ) -> Result, ErrorPage> { let conn = &*rockets.conn; - let target = User::find_by_fqn(&rockets, &name)?; + let target = User::find_by_fqn(&rockets, &name).await?; let message = if let Ok(follow) = follows::Follow::find(&*conn, user.id, target.id) { let delete_act = follow.build_undo(&*conn)?; local_inbox( @@ -196,9 +199,10 @@ pub fn follow_not_connected( remote_form: Option>, i18n: I18n, ) -> Result { - let target = User::find_by_fqn(&rockets, &name)?; + let target = User::find_by_fqn(&rockets, &name).await?; if let Some(remote_form) = remote_form { if let Some(uri) = User::fetch_remote_interact_uri(&remote_form) + .await .ok() .and_then(|uri| { uri.replace( @@ -265,15 +269,15 @@ pub fn follow_auth(name: String, i18n: I18n) -> Flash { } #[get("/@//followers?", rank = 2)] -pub fn followers( - name: String, +pub async fn followers( + String, page: Option, rockets: PlumeRocket, ) -> Result { let conn = &*rockets.conn; let page = page.unwrap_or_default(); - let user = User::find_by_fqn(&rockets, &name)?; - let followers_count = user.count_followers(&*conn)?; + let user = User::find_by_fqn(&rockets, &name).await?; + let followers_count = user.count_followers(&*; Ok(render!(users::followers( &rockets.to_context(), @@ -299,7 +303,7 @@ pub fn followed( ) -> Result { let conn = &*rockets.conn; let page = page.unwrap_or_default(); - let user = User::find_by_fqn(&rockets, &name)?; + let user = User::find_by_fqn(&rockets, &name).await?; let followed_count = user.count_followed(conn)?; Ok(render!(users::followed( @@ -319,13 +323,13 @@ pub fn followed( } #[get("/@/", rank = 1)] -pub fn activity_details( +pub async fn activity_details( name: String, rockets: PlumeRocket, _ap: ApRequest, ) -> Option> { - let user = User::find_by_fqn(&rockets, &name).ok()?; - Some(ActivityStream::new(user.to_activity(&*rockets.conn).ok()?)) + let user = User::find_by_fqn(&rockets, &name).await?.ok()?; + Some(ActivityStream::new(user.to_activity(&*roonn).ok()?)) } #[get("/users/new")] @@ -411,39 +415,38 @@ pub fn update( } #[post("/@//delete")] -pub fn delete( +pub async fn delete( name: String, user: User, mut cookies: Cookies<'_>, rockets: PlumeRocket, ) -> Result, ErrorPage> { - let account = User::find_by_fqn(&rockets, &name)?; - if user.id == account.id { - account.delete(&*rockets.conn, &rockets.searcher)?; - - let target = User::one_by_instance(&*rockets.conn)?; - let delete_act = account.delete_activity(&*rockets.conn)?; - rockets - .worker - .execute(move || broadcast(&account, delete_act, target)); - - if let Some(cookie) = cookies.get_private(AUTH_COOKIE) { - cookies.remove_private(cookie); - } - - Ok(Flash::success( - Redirect::to(uri!(super::instance::index)), - i18n!(rockets.intl.catalog, "Your account has been deleted."), - )) - } else { - Ok(Flash::error( + let account = User::find_by_fqn(&rockets, &name).await?; + if user.id != account.id { + return Ok(Flash::error( Redirect::to(uri!(edit: name = name)), i18n!( rockets.intl.catalog, "You can't delete someone else's account." ), - )) + )); } + account.delete(&*rockets.conn, &rockets.searcher)?; + + let target = User::one_by_instance(&*rockets.conn)?; + let delete_act = account.delete_activity(&*rockets.conn)?; + rockets + .worker + .execute(move || broadcast(&account, delete_act, target)); + + if let Some(cookie) = cookies.get_private(AUTH_COOKIE) { + cookies.remove_private(cookie); + } + + Ok(Flash::success( + Redirect::to(uri!(super::instance::index)), + i18n!(rockets.intl.catalog, "Your account has been deleted."), + )) } #[derive(Default, FromForm, Validate)] @@ -564,37 +567,44 @@ pub fn create( } #[get("/@//outbox")] -pub fn outbox(name: String, rockets: PlumeRocket) -> Option> { - let user = User::find_by_fqn(&rockets, &name).ok()?; +pub async fn outbox( + name: String, + rockets: PlumeRocket, +) -> Option> { + let user = User::find_by_fqn(&rockets, &name).await.ok()?; user.outbox(&*rockets.conn).ok() } + #[get("/@//outbox?")] -pub fn outbox_page( +pub async fn outbox_page( name: String, page: Page, rockets: PlumeRocket, ) -> Option> { - let user = User::find_by_fqn(&rockets, &name).ok()?; + let user = User::find_by_fqn(&rockets, &name).await.ok()?; user.outbox_page(&*rockets.conn, page.limits()).ok() } + #[post("/@//inbox", data = "")] -pub fn inbox( +pub async fn inbox( name: String, data: inbox::SignedJson, headers: Headers<'_>, rockets: PlumeRocket, ) -> Result> { - User::find_by_fqn(&rockets, &name).map_err(|_| status::BadRequest(Some("User not found")))?; + User::find_by_fqn(&rockets, &name) + .await + .map_err(|_| status::BadRequest(Some("User not found")))?; inbox::handle_incoming(rockets, data, headers) } #[get("/@//followers", rank = 1)] -pub fn ap_followers( +pub async fn ap_followers( name: String, rockets: PlumeRocket, _ap: ApRequest, ) -> Option> { - let user = User::find_by_fqn(&rockets, &name).ok()?; + let user = User::find_by_fqn(&rockets, &name).await?.ok()?; let followers = user .get_followers(&*rockets.conn) .ok()? @@ -614,9 +624,9 @@ pub fn ap_followers( } #[get("/@//atom.xml")] -pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option> { +pub async fn atom_feed(name: String, rockets: PlumeRocket) -> Option> { let conn = &*rockets.conn; - let author = User::find_by_fqn(&rockets, &name).ok()?; + let author = User::find_by_fqn(&rockets, &name).await?.ok()?; let entries = Post::get_recents_for_author(conn, &author, 15).ok()?; let uri = Instance::get_local() .ok()?