summaryrefslogtreecommitdiffstats
path: root/server/src/apub/user.rs
diff options
context:
space:
mode:
authorRiley <asonix@asonix.dog>2020-07-01 07:54:29 -0500
committerGitHub <noreply@github.com>2020-07-01 08:54:29 -0400
commita074564458b8a108b77d98e5e8ce24168656763a (patch)
tree8cfb4e463b6b2dbd3c4b3ac2f312a42542f38d64 /server/src/apub/user.rs
parent4c1cb5999cad496714cec67f101be38cd281d416 (diff)
Federation async (#848)
* Asyncify more * I guess these changed * Clean PR a bit * Convert more away from failure error * config changes for testing federation * It was DNS So actix-web's client relies on TRust DNS Resolver to figure out where to send data, but TRust DNS Resolver seems to not play nice with docker, which expressed itself as not resolving the name to an IP address _the first time_ when making a request. The fix was literally to make the request again (which I limited to 3 times total, and not exceeding the request timeout in total) * Only retry for connecterror Since TRust DNS Resolver was causing ConnectError::Timeout, this change limits the retry to only this error, returning immediately for any other error * Use http sig norm 0.4.0-alpha for actix-web 3.0 support * Blocking function, retry http requests * cargo +nightly fmt * Only create one pictrs dir * Don't yarn build * cargo +nightly fmt
Diffstat (limited to 'server/src/apub/user.rs')
-rw-r--r--server/src/apub/user.rs87
1 files changed, 65 insertions, 22 deletions
diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs
index c840cc22..51339ccf 100644
--- a/server/src/apub/user.rs
+++ b/server/src/apub/user.rs
@@ -8,6 +8,7 @@ use crate::{
PersonExt,
ToApub,
},
+ blocking,
convert_datetime,
db::{
activity::insert_activity,
@@ -15,6 +16,8 @@ use crate::{
},
naive_now,
routes::DbPoolParam,
+ DbPool,
+ LemmyError,
};
use activitystreams::{
actor::{properties::ApActorProperties, Person},
@@ -29,9 +32,7 @@ use activitystreams_new::{
object::Tombstone,
prelude::*,
};
-use actix_web::{body::Body, web::Path, HttpResponse, Result};
-use diesel::PgConnection;
-use failure::Error;
+use actix_web::{body::Body, client::Client, web, HttpResponse};
use serde::Deserialize;
#[derive(Deserialize)]
@@ -39,11 +40,12 @@ pub struct UserQuery {
user_name: String,
}
+#[async_trait::async_trait(?Send)]
impl ToApub for User_ {
type Response = PersonExt;
// Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
- fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
+ async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
// TODO go through all these to_string and to_owned()
let mut person = Person::default();
let oprops: &mut ObjectProperties = person.as_mut();
@@ -86,11 +88,12 @@ impl ToApub for User_ {
Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
}
- fn to_tombstone(&self) -> Result<Tombstone, Error> {
+ fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
unimplemented!()
}
}
+#[async_trait::async_trait(?Send)]
impl ActorType for User_ {
fn actor_id(&self) -> String {
self.actor_id.to_owned()
@@ -105,19 +108,29 @@ impl ActorType for User_ {
}
/// As a given local user, send out a follow request to a remote community.
- fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
+ async fn send_follow(
+ &self,
+ follow_actor_id: &str,
+ client: &Client,
+ pool: &DbPool,
+ ) -> Result<(), LemmyError> {
let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
follow.set_context(context()).set_id(id.parse()?);
let to = format!("{}/inbox", follow_actor_id);
- insert_activity(&conn, self.id, &follow, true)?;
+ insert_activity(self.id, follow.clone(), true, pool).await?;
- send_activity(&follow, self, vec![to])?;
+ send_activity(client, &follow, self, vec![to]).await?;
Ok(())
}
- fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
+ async fn send_unfollow(
+ &self,
+ follow_actor_id: &str,
+ client: &Client,
+ pool: &DbPool,
+ ) -> Result<(), LemmyError> {
let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
follow.set_context(context()).set_id(id.parse()?);
@@ -130,41 +143,67 @@ impl ActorType for User_ {
let mut undo = Undo::new(self.actor_id.parse::<XsdAnyUri>()?, follow.into_any_base()?);
undo.set_context(context()).set_id(undo_id.parse()?);
- insert_activity(&conn, self.id, &undo, true)?;
+ insert_activity(self.id, undo.clone(), true, pool).await?;
- send_activity(&undo, self, vec![to])?;
+ send_activity(client, &undo, self, vec![to]).await?;
Ok(())
}
- fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
+ async fn send_delete(
+ &self,
+ _creator: &User_,
+ _client: &Client,
+ _pool: &DbPool,
+ ) -> Result<(), LemmyError> {
unimplemented!()
}
- fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
+ async fn send_undo_delete(
+ &self,
+ _creator: &User_,
+ _client: &Client,
+ _pool: &DbPool,
+ ) -> Result<(), LemmyError> {
unimplemented!()
}
- fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
+ async fn send_remove(
+ &self,
+ _creator: &User_,
+ _client: &Client,
+ _pool: &DbPool,
+ ) -> Result<(), LemmyError> {
unimplemented!()
}
- fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
+ async fn send_undo_remove(
+ &self,
+ _creator: &User_,
+ _client: &Client,
+ _pool: &DbPool,
+ ) -> Result<(), LemmyError> {
unimplemented!()
}
- fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
+ async fn send_accept_follow(
+ &self,
+ _follow: &Follow,
+ _client: &Client,
+ _pool: &DbPool,
+ ) -> Result<(), LemmyError> {
unimplemented!()
}
- fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
+ async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
unimplemented!()
}
}
+#[async_trait::async_trait(?Send)]
impl FromApub for UserForm {
type ApubType = PersonExt;
/// Parse an ActivityPub person received from another instance into a Lemmy user.
- fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
+ async fn from_apub(person: &PersonExt, _: &Client, _: &DbPool) -> Result<Self, LemmyError> {
let oprops = &person.inner.object_props;
let aprops = &person.ext_one;
let public_key: &PublicKey = &person.ext_two.public_key;
@@ -210,10 +249,14 @@ impl FromApub for UserForm {
/// Return the user json over HTTP.
pub async fn get_apub_user_http(
- info: Path<UserQuery>,
+ info: web::Path<UserQuery>,
db: DbPoolParam,
-) -> Result<HttpResponse<Body>, Error> {
- let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
- let u = user.to_apub(&db.get().unwrap())?;
+) -> Result<HttpResponse<Body>, LemmyError> {
+ let user_name = info.into_inner().user_name;
+ let user = blocking(&db, move |conn| {
+ User_::find_by_email_or_username(conn, &user_name)
+ })
+ .await??;
+ let u = user.to_apub(&db).await?;
Ok(create_apub_response(&u))
}