Remove PgConnection when we don't need it

Massive simplification in the ActivityPub module!
This commit is contained in:
Bat 2018-06-21 16:31:42 +01:00
parent 7ddad12420
commit 2217ec0d56
10 changed files with 17 additions and 20 deletions

View File

@ -1,6 +1,5 @@
use activitypub::{Activity, Actor, Object, Link}; use activitypub::{Activity, Actor, Object, Link};
use array_tool::vec::Uniq; use array_tool::vec::Uniq;
use diesel::PgConnection;
use reqwest::Client; use reqwest::Client;
use rocket::{ use rocket::{
http::{ContentType, Status}, 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() let boxes = to.into_iter()
.map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url())) .map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url()))
.collect::<Vec<String>>() .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(); let mut act = serde_json::to_value(act).unwrap();
act["@context"] = context(); act["@context"] = context();
let signed = act.sign(sender, conn); let signed = act.sign(sender);
for inbox in boxes { for inbox in boxes {
// TODO: run it in Sidekiq or something like that // TODO: run it in Sidekiq or something like that
let res = Client::new() let res = Client::new()
.post(&inbox[..]) .post(&inbox[..])
.headers(request::headers()) .headers(request::headers())
.header(request::signature(sender, request::headers(), conn)) .header(request::signature(sender, request::headers()))
.header(request::digest(signed.to_string())) .header(request::digest(signed.to_string()))
.body(signed.to_string()) .body(signed.to_string())
.send(); .send();

View File

@ -1,5 +1,4 @@
use base64; use base64;
use diesel::PgConnection;
use openssl::hash::{Hasher, MessageDigest}; use openssl::hash::{Hasher, MessageDigest};
use reqwest::header::{Date, Headers, UserAgent}; use reqwest::header::{Date, Headers, UserAgent};
use std::time::SystemTime; use std::time::SystemTime;
@ -23,7 +22,7 @@ pub fn headers() -> 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_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(); 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!( Signature(format!(
"keyId=\"{key_id}\",algorithm=\"rsa-sha256\",headers=\"{signed_headers}\",signature=\"{signature}\"", "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, signed_headers = signed_headers,
signature = sign signature = sign
)) ))

View File

@ -1,6 +1,5 @@
use base64; use base64;
use chrono::Utc; use chrono::Utc;
use diesel::PgConnection;
use hex; use hex;
use openssl::{ use openssl::{
pkey::PKey, pkey::PKey,
@ -24,7 +23,7 @@ pub trait Signer {
} }
pub trait Signable { 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 { fn hash(data: String) -> String {
let bytes = data.into_bytes(); let bytes = data.into_bytes();
@ -33,11 +32,11 @@ pub trait Signable {
} }
impl Signable for serde_json::Value { 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 creation_date = Utc::now().to_rfc3339();
let mut options = json!({ let mut options = json!({
"type": "RsaSignature2017", "type": "RsaSignature2017",
"creator": creator.get_key_id(conn), "creator": creator.get_key_id(),
"created": creation_date "created": creation_date
}); });

View File

@ -44,7 +44,7 @@ impl Follow {
let mut accept = Accept::default(); let mut accept = Accept::default();
accept.accept_props.set_actor_link::<Id>(from.clone().into_id()).unwrap(); accept.accept_props.set_actor_link::<Id>(from.clone().into_id()).unwrap();
accept.accept_props.set_object_object(follow).unwrap(); accept.accept_props.set_object_object(follow).unwrap();
broadcast(conn, &*from, accept, vec![target.clone()]); broadcast(&*from, accept, vec![target.clone()]);
res res
} }
} }

View File

@ -401,7 +401,7 @@ impl APActor for User {
fn custom_props(&self, conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> { fn custom_props(&self, conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> {
let mut res = serde_json::Map::new(); let mut res = serde_json::Map::new();
res.insert("publicKey".to_string(), json!({ res.insert("publicKey".to_string(), json!({
"id": self.get_key_id(conn), "id": self.get_key_id(),
"owner": self.ap_url, "owner": self.ap_url,
"publicKeyPem": self.public_key "publicKeyPem": self.public_key
})); }));

View File

@ -45,7 +45,7 @@ fn create_response(blog_name: String, slug: String, query: Option<CommentQuery>,
let instance = Instance::get_local(&*conn).unwrap(); let instance = Instance::get_local(&*conn).unwrap();
instance.received(&*conn, serde_json::to_value(new_comment.clone()).expect("JSON serialization error")); 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)) Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id))
} }

View File

@ -25,11 +25,11 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
like.update_ap_url(&*conn); like.update_ap_url(&*conn);
like.notify(&*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 { } else {
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
let delete_act = like.delete(&*conn); 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)) Redirect::to(uri!(super::posts::details: blog = blog, slug = slug))

View File

@ -119,7 +119,7 @@ fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn)
} }
let act = post.create_activity(&*conn); 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)) Redirect::to(uri!(details: blog = blog_name, slug = slug))
} }

View File

@ -25,11 +25,11 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
reshare.update_ap_url(&*conn); reshare.update_ap_url(&*conn);
reshare.notify(&*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 { } else {
let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
let delete_act = reshare.delete(&*conn); 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)) Redirect::to(uri!(super::posts::details: blog = blog, slug = slug))

View File

@ -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.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(); 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)) Redirect::to(uri!(details: name = name))
} }