Remove PgConnection when we don't need it
Massive simplification in the ActivityPub module!
This commit is contained in:
		
							parent
							
								
									7ddad12420
								
							
						
					
					
						commit
						2217ec0d56
					
				| @ -1,6 +1,5 @@ | ||||
| use activitypub::{Activity, Actor, Object, Link}; | ||||
| use array_tool::vec::Uniq; | ||||
| use diesel::PgConnection; | ||||
| use reqwest::Client; | ||||
| use rocket::{ | ||||
|     http::{ContentType, Status}, | ||||
| @ -74,7 +73,7 @@ impl<'r, O: Object> Responder<'r> for ActivityStream<O> { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub fn broadcast<A: Activity, S: sign::Signer, T: inbox::WithInbox + Actor>(conn: &PgConnection, sender: &S, act: A, to: Vec<T>) { | ||||
| pub fn broadcast<A: Activity, S: sign::Signer, T: inbox::WithInbox + Actor>(sender: &S, act: A, to: Vec<T>) { | ||||
|     let boxes = to.into_iter() | ||||
|         .map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url())) | ||||
|         .collect::<Vec<String>>() | ||||
| @ -82,14 +81,14 @@ pub fn broadcast<A: Activity, S: sign::Signer, T: inbox::WithInbox + Actor>(conn | ||||
| 
 | ||||
|     let mut act = serde_json::to_value(act).unwrap(); | ||||
|     act["@context"] = context(); | ||||
|     let signed = act.sign(sender, conn); | ||||
|     let signed = act.sign(sender); | ||||
| 
 | ||||
|     for inbox in boxes { | ||||
|         // TODO: run it in Sidekiq or something like that
 | ||||
|         let res = Client::new() | ||||
|             .post(&inbox[..]) | ||||
|             .headers(request::headers()) | ||||
|             .header(request::signature(sender, request::headers(), conn)) | ||||
|             .header(request::signature(sender, request::headers())) | ||||
|             .header(request::digest(signed.to_string())) | ||||
|             .body(signed.to_string()) | ||||
|             .send(); | ||||
|  | ||||
| @ -1,5 +1,4 @@ | ||||
| use base64; | ||||
| use diesel::PgConnection; | ||||
| use openssl::hash::{Hasher, MessageDigest}; | ||||
| use reqwest::header::{Date, Headers, UserAgent}; | ||||
| use std::time::SystemTime; | ||||
| @ -23,7 +22,7 @@ pub fn headers() -> Headers { | ||||
|     headers | ||||
| } | ||||
| 
 | ||||
| pub fn signature<S: Signer>(signer: &S, headers: Headers, conn: &PgConnection) -> Signature { | ||||
| pub fn signature<S: Signer>(signer: &S, headers: Headers) -> Signature { | ||||
|     let signed_string = headers.iter().map(|h| format!("{}: {}", h.name().to_lowercase(), h.value_string())).collect::<Vec<String>>().join("\n"); | ||||
|     let signed_headers = headers.iter().map(|h| h.name().to_string()).collect::<Vec<String>>().join(" ").to_lowercase(); | ||||
|     
 | ||||
| @ -32,7 +31,7 @@ pub fn signature<S: Signer>(signer: &S, headers: Headers, conn: &PgConnection) - | ||||
| 
 | ||||
|     Signature(format!( | ||||
|         "keyId=\"{key_id}\",algorithm=\"rsa-sha256\",headers=\"{signed_headers}\",signature=\"{signature}\"", | ||||
|         key_id = signer.get_key_id(conn), | ||||
|         key_id = signer.get_key_id(), | ||||
|         signed_headers = signed_headers, | ||||
|         signature = sign | ||||
|     )) | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| use base64; | ||||
| use chrono::Utc; | ||||
| use diesel::PgConnection; | ||||
| use hex; | ||||
| use openssl::{ | ||||
|     pkey::PKey, | ||||
| @ -24,7 +23,7 @@ pub trait Signer { | ||||
| } | ||||
| 
 | ||||
| pub trait Signable { | ||||
|     fn sign<T>(&mut self, creator: &T, conn: &PgConnection) -> &mut Self where T: Signer; | ||||
|     fn sign<T>(&mut self, creator: &T) -> &mut Self where T: Signer; | ||||
| 
 | ||||
|     fn hash(data: String) -> String { | ||||
|         let bytes = data.into_bytes(); | ||||
| @ -33,11 +32,11 @@ pub trait Signable { | ||||
| } | ||||
| 
 | ||||
| impl Signable for serde_json::Value { | ||||
|     fn sign<T: Signer>(&mut self, creator: &T, conn: &PgConnection) -> &mut serde_json::Value { | ||||
|     fn sign<T: Signer>(&mut self, creator: &T) -> &mut serde_json::Value { | ||||
|         let creation_date = Utc::now().to_rfc3339(); | ||||
|         let mut options = json!({ | ||||
|             "type": "RsaSignature2017", | ||||
|             "creator": creator.get_key_id(conn), | ||||
|             "creator": creator.get_key_id(), | ||||
|             "created": creation_date | ||||
|         }); | ||||
| 
 | ||||
|  | ||||
| @ -44,7 +44,7 @@ impl Follow { | ||||
|         let mut accept = Accept::default(); | ||||
|         accept.accept_props.set_actor_link::<Id>(from.clone().into_id()).unwrap(); | ||||
|         accept.accept_props.set_object_object(follow).unwrap(); | ||||
|         broadcast(conn, &*from, accept, vec![target.clone()]); | ||||
|         broadcast(&*from, accept, vec![target.clone()]); | ||||
|         res | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -401,7 +401,7 @@ impl APActor for User { | ||||
|     fn custom_props(&self, conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> { | ||||
|         let mut res = serde_json::Map::new(); | ||||
|         res.insert("publicKey".to_string(), json!({ | ||||
|             "id": self.get_key_id(conn), | ||||
|             "id": self.get_key_id(), | ||||
|             "owner": self.ap_url, | ||||
|             "publicKeyPem": self.public_key | ||||
|         })); | ||||
|  | ||||
| @ -45,7 +45,7 @@ fn create_response(blog_name: String, slug: String, query: Option<CommentQuery>, | ||||
| 
 | ||||
|     let instance = Instance::get_local(&*conn).unwrap(); | ||||
|     instance.received(&*conn, serde_json::to_value(new_comment.clone()).expect("JSON serialization error")); | ||||
|     broadcast(&*conn, &user, new_comment, user.get_followers(&*conn)); | ||||
|     broadcast(&user, new_comment, user.get_followers(&*conn)); | ||||
| 
 | ||||
|     Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id)) | ||||
| } | ||||
|  | ||||
| @ -25,11 +25,11 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { | ||||
|         like.update_ap_url(&*conn); | ||||
|         like.notify(&*conn); | ||||
| 
 | ||||
|         broadcast(&*conn, &user, like.into_activity(&*conn), user.get_followers(&*conn)); | ||||
|         broadcast(&user, like.into_activity(&*conn), user.get_followers(&*conn)); | ||||
|     } else { | ||||
|         let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); | ||||
|         let delete_act = like.delete(&*conn); | ||||
|         broadcast(&*conn, &user, delete_act, user.get_followers(&*conn)); | ||||
|         broadcast(&user, delete_act, user.get_followers(&*conn)); | ||||
|     } | ||||
| 
 | ||||
|     Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) | ||||
|  | ||||
| @ -119,7 +119,7 @@ fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) | ||||
|             } | ||||
| 
 | ||||
|             let act = post.create_activity(&*conn); | ||||
|             broadcast(&*conn, &user, act, user.get_followers(&*conn)); | ||||
|             broadcast(&user, act, user.get_followers(&*conn)); | ||||
| 
 | ||||
|             Redirect::to(uri!(details: blog = blog_name, slug = slug)) | ||||
|         } | ||||
|  | ||||
| @ -25,11 +25,11 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { | ||||
|         reshare.update_ap_url(&*conn); | ||||
|         reshare.notify(&*conn); | ||||
| 
 | ||||
|         broadcast(&*conn, &user, reshare.into_activity(&*conn), user.get_followers(&*conn)); | ||||
|         broadcast(&user, reshare.into_activity(&*conn), user.get_followers(&*conn)); | ||||
|     } else { | ||||
|         let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); | ||||
|         let delete_act = reshare.delete(&*conn); | ||||
|         broadcast(&*conn, &user, delete_act, user.get_followers(&*conn)); | ||||
|         broadcast(&user, delete_act, user.get_followers(&*conn)); | ||||
|     } | ||||
| 
 | ||||
|     Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) | ||||
|  | ||||
| @ -82,7 +82,7 @@ fn follow(name: String, conn: DbConn, user: User) -> Redirect { | ||||
|     act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap(); | ||||
|     act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap(); | ||||
| 
 | ||||
|     broadcast(&*conn, &user, act, vec![target]); | ||||
|     broadcast(&user, act, vec![target]); | ||||
|     Redirect::to(uri!(details: name = name)) | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user