From 6a5d806b1d5bbd2417578b76c3e0ea04dbc5607c Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 22 Jun 2018 16:17:53 +0100 Subject: [PATCH] Various federation fixes --- src/activity_pub/mod.rs | 4 ---- src/models/follows.rs | 12 +++++++----- src/models/posts.rs | 6 ++++-- src/models/users.rs | 14 +++++++++----- src/routes/posts.rs | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 87fb579..5b35ff6 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -119,7 +119,6 @@ impl Link for Id {} #[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct ApSignature { - #[serde(skip_serializing_if = "Option::is_none")] #[activitystreams(concrete(PublicKey), functional)] pub public_key: Option } @@ -127,15 +126,12 @@ pub struct ApSignature { #[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct PublicKey { - #[serde(skip_serializing_if = "Option::is_none")] #[activitystreams(concrete(String), functional)] pub id: Option, - #[serde(skip_serializing_if = "Option::is_none")] #[activitystreams(concrete(String), functional)] pub owner: Option, - #[serde(skip_serializing_if = "Option::is_none")] #[activitystreams(concrete(String), functional)] pub public_key_pem: Option } diff --git a/src/models/follows.rs b/src/models/follows.rs index c7bffb5..4ad867c 100644 --- a/src/models/follows.rs +++ b/src/models/follows.rs @@ -28,10 +28,12 @@ impl Follow { insert!(follows, NewFollow); get!(follows); + /// from -> The one sending the follow request + /// target -> The target of the request, responding with Accept pub fn accept_follow( conn: &PgConnection, - from: &A, - target: &B, + from: &B, + target: &A, follow: FollowAct, from_id: i32, target_id: i32 @@ -42,9 +44,9 @@ impl Follow { }); let mut accept = Accept::default(); - accept.accept_props.set_actor_link::(from.clone().into_id()).unwrap(); + accept.accept_props.set_actor_link::(target.clone().into_id()).unwrap(); accept.accept_props.set_object_object(follow).unwrap(); - broadcast(&*from, accept, vec![target.clone()]); + broadcast(&*target, accept, vec![from.clone()]); res } } @@ -53,7 +55,7 @@ impl FromActivity for Follow { fn from_activity(conn: &PgConnection, follow: FollowAct, _actor: Id) -> Follow { let from = User::from_url(conn, follow.follow_props.actor.as_str().unwrap().to_string()).unwrap(); match User::from_url(conn, follow.follow_props.object.as_str().unwrap().to_string()) { - Some(u) => Follow::accept_follow(conn, &from, &u, follow, from.id, u.id), + Some(user) => Follow::accept_follow(conn, &from, &user, follow, from.id, user.id), None => { let blog = Blog::from_url(conn, follow.follow_props.object.as_str().unwrap().to_string()).unwrap(); Follow::accept_follow(conn, &from, &blog, follow, from.id, blog.id) diff --git a/src/models/posts.rs b/src/models/posts.rs index 9907ac5..d75888e 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -122,11 +122,13 @@ impl Post { .expect("Couldn't load reshares associted to post") } - pub fn update_ap_url(&self, conn: &PgConnection) { + pub fn update_ap_url(&self, conn: &PgConnection) -> Post { if self.ap_url.len() == 0 { diesel::update(self) .set(posts::ap_url.eq(self.compute_id(conn))) - .get_result::(conn).expect("Couldn't update AP URL"); + .get_result::(conn).expect("Couldn't update AP URL") + } else { + self.clone() } } diff --git a/src/models/users.rs b/src/models/users.rs index 9a3f776..4d036eb 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -159,7 +159,10 @@ impl User { .send(); match req { Ok(mut res) => { - let json: CustomPerson = serde_json::from_str(&res.text().unwrap()).unwrap(); + let text = &res.text().unwrap(); + let ap_sign: ApSignature = serde_json::from_str(text).unwrap(); + let mut json: CustomPerson = serde_json::from_str(text).unwrap(); + json.custom_props = ap_sign; // without this workaround, publicKey is not correctly deserialized Some(User::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string())) }, Err(_) => None @@ -177,6 +180,7 @@ impl User { }) } }; + println!("User from act : {:?}", acct.custom_props); User::insert(conn, NewUser { username: acct.object.ap_actor_props.preferred_username_string().expect("User::from_activity: preferredUsername error"), display_name: acct.object.object_props.name_string().expect("User::from_activity: name error"), @@ -330,11 +334,11 @@ impl User { actor.ap_actor_props.set_endpoints_endpoint(endpoints).expect("User::into_activity: endpoints error"); let mut public_key = PublicKey::default(); - public_key.set_id_string(format!("{}#main-key", self.ap_url)).expect("Blog::into_activity: publicKey.id error"); - public_key.set_owner_string(self.ap_url.clone()).expect("Blog::into_activity: publicKey.owner error"); - public_key.set_public_key_pem_string(self.public_key.clone()).expect("Blog::into_activity: publicKey.publicKeyPem error"); + public_key.set_id_string(format!("{}#main-key", self.ap_url)).expect("User::into_activity: publicKey.id error"); + public_key.set_owner_string(self.ap_url.clone()).expect("User::into_activity: publicKey.owner error"); + public_key.set_public_key_pem_string(self.public_key.clone()).expect("User::into_activity: publicKey.publicKeyPem error"); let mut ap_signature = ApSignature::default(); - ap_signature.set_public_key_publickey(public_key).expect("Blog::into_activity: publicKey error"); + ap_signature.set_public_key_publickey(public_key).expect("User::into_activity: publicKey error"); CustomPerson::new(actor, ap_signature) } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 7bb3841..e5217b6 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -107,7 +107,7 @@ fn create(blog_name: String, data: Form, user: User, conn: DbConn) license: form.license.to_string(), ap_url: "".to_string() }); - post.update_ap_url(&*conn); + let post = post.update_ap_url(&*conn); PostAuthor::insert(&*conn, NewPostAuthor { post_id: post.id, author_id: user.id