Use activitystreams in the Inbox trait

This commit is contained in:
Bat 2018-05-18 09:04:40 +01:00
parent b6bd405f5f
commit 0e24ccbf29
4 changed files with 83 additions and 30 deletions

View file

@ -1,18 +1,19 @@
use activitystreams_traits::Actor;
use activitystreams_types::{ use activitystreams_types::{
actor::Person, actor::Person,
activity::{Create, Follow, Like, Undo}, activity::{Accept, Create, Follow, Like, Undo},
object::{Article, Note} object::{Article, Note}
}; };
use activitystreams::activity::Activity;
use diesel::PgConnection; use diesel::PgConnection;
use failure::Error; use failure::Error;
use serde_json; use serde_json;
// use activity_pub::broadcast; use activity_pub::{broadcast, IntoId};
use activity_pub::actor::Actor as APActor; use activity_pub::actor::Actor as APActor;
use activity_pub::sign::*; use activity_pub::sign::*;
use models::blogs::Blog; use models::blogs::Blog;
use models::comments::*; use models::comments::*;
use models::follows;
use models::likes; use models::likes;
use models::posts::*; use models::posts::*;
use models::users::User; use models::users::User;
@ -63,10 +64,10 @@ pub trait Inbox {
fn follow(&self, conn: &PgConnection, follow: Follow) -> Result<(), Error> { fn follow(&self, conn: &PgConnection, follow: Follow) -> Result<(), Error> {
let from = User::from_url(conn, follow.actor.as_str().unwrap().to_string()).unwrap(); let from = User::from_url(conn, follow.actor.as_str().unwrap().to_string()).unwrap();
match User::from_url(conn, follow.object.as_str().unwrap().to_string()) { match User::from_url(conn, follow.object.as_str().unwrap().to_string()) {
Some(u) => self.accept_follow(conn, &from, &u, &follow, from.id, u.id), Some(u) => self.accept_follow(conn, &from, &u, follow, from.id, u.id),
None => { None => {
let blog = Blog::from_url(conn, follow.object.as_str().unwrap().to_string()).unwrap(); let blog = Blog::from_url(conn, follow.object.as_str().unwrap().to_string()).unwrap();
self.accept_follow(conn, &from, &blog, &follow, from.id, blog.id) self.accept_follow(conn, &from, &blog, follow, from.id, blog.id)
} }
}; };
Ok(()) Ok(())
@ -117,22 +118,29 @@ pub trait Inbox {
} }
} }
fn accept_follow<A: Signer, B: Clone, T: Activity>( fn accept_follow<A: Signer + IntoId, B: Clone + WithInbox + Actor>(
&self, &self,
_conn: &PgConnection, conn: &PgConnection,
_from: &A, from: &A,
_target: &B, target: &B,
_follow: &T, follow: Follow,
_from_id: i32, from_id: i32,
_target_id: i32 target_id: i32
) { ) {
// TODO follows::Follow::insert(conn, follows::NewFollow {
//Follow::insert(conn, NewFollow { follower_id: from_id,
// follower_id: from_id, following_id: target_id
// following_id: target_id });
//});
//let accept = activity::Accept::new(target, follow, conn); let mut accept = Accept::default();//new(target, follow, conn);
//broadcast(conn, from, accept, vec![target.clone()]); accept.set_actor_link(from.into()).unwrap();
accept.set_object_object(follow).unwrap();
broadcast(conn, &*from, accept, vec![target.clone()]);
} }
} }
pub trait WithInbox {
fn get_inbox_url(&self) -> String;
fn get_shared_inbox_url(&self) -> Option<String>;
}

View file

@ -1,4 +1,4 @@
use activitystreams_traits::{Activity, Object, Link}; use activitystreams_traits::{Activity, Actor, Object, Link};
use array_tool::vec::Uniq; use array_tool::vec::Uniq;
use diesel::PgConnection; use diesel::PgConnection;
use reqwest::Client; use reqwest::Client;
@ -76,7 +76,7 @@ impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
} }
} }
pub fn broadcast<A: Activity + Clone, S: actor::Actor + sign::Signer, T: actor::Actor>(conn: &PgConnection, sender: &S, act: A, to: Vec<T>) { pub fn broadcast<A: Activity + Clone, S: sign::Signer, T: inbox::WithInbox + Actor>(conn: &PgConnection, 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>>()
@ -116,4 +116,8 @@ impl Id {
} }
} }
pub trait IntoId {
fn into(&self) -> Id;
}
impl Link for Id {} impl Link for Id {}

View file

@ -1,3 +1,4 @@
use activitystreams_traits::{Actor, Object};
use activitystreams_types::collection::OrderedCollection; use activitystreams_types::collection::OrderedCollection;
use reqwest::Client; use reqwest::Client;
use reqwest::header::{Accept, qitem}; use reqwest::header::{Accept, qitem};
@ -11,15 +12,16 @@ use openssl::pkey::{PKey, Private};
use openssl::rsa::Rsa; use openssl::rsa::Rsa;
use openssl::sign::Signer; use openssl::sign::Signer;
use activity_pub::ActivityStream; use activity_pub::{ActivityStream, Id};
use activity_pub::actor::{Actor, ActorType}; use activity_pub::actor::{Actor as APActor, ActorType};
use activity_pub::inbox::WithInbox;
use activity_pub::sign; use activity_pub::sign;
use activity_pub::webfinger::*; use activity_pub::webfinger::*;
use models::instance::Instance; use models::instance::Instance;
use schema::blogs; use schema::blogs;
#[derive(Queryable, Identifiable, Serialize, Clone)] #[derive(Queryable, Identifiable, Serialize, Deserialize, Clone)]
pub struct Blog { pub struct Blog {
pub id: i32, pub id: i32,
pub actor_id: String, pub actor_id: String,
@ -173,7 +175,26 @@ impl Blog {
} }
} }
impl Actor for Blog { impl Into<Id> for Blog {
fn into(self) -> Id {
Id::new(self.ap_url)
}
}
impl Object for Blog {}
impl Actor for Blog {}
impl WithInbox for Blog {
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}
fn get_shared_inbox_url(&self) -> Option<String> {
None
}
}
impl APActor for Blog {
fn get_box_prefix() -> &'static str { fn get_box_prefix() -> &'static str {
"~" "~"
} }

View file

@ -1,3 +1,4 @@
use activitystreams_traits::{Actor, Object};
use activitystreams_types::{ use activitystreams_types::{
activity::Create, activity::Create,
collection::OrderedCollection collection::OrderedCollection
@ -19,9 +20,9 @@ use serde_json;
use url::Url; use url::Url;
use BASE_URL; use BASE_URL;
use activity_pub::{ap_url, ActivityStream}; use activity_pub::{ap_url, ActivityStream, Id, IntoId};
use activity_pub::actor::{ActorType, Actor}; use activity_pub::actor::{ActorType, Actor as APActor};
use activity_pub::inbox::Inbox; use activity_pub::inbox::{Inbox, WithInbox};
use activity_pub::sign::{Signer, gen_keypair}; use activity_pub::sign::{Signer, gen_keypair};
use activity_pub::webfinger::{Webfinger, resolve}; use activity_pub::webfinger::{Webfinger, resolve};
use db_conn::DbConn; use db_conn::DbConn;
@ -35,7 +36,7 @@ use schema::users;
pub const AUTH_COOKIE: &'static str = "user_id"; pub const AUTH_COOKIE: &'static str = "user_id";
#[derive(Queryable, Identifiable, Serialize, Clone)] #[derive(Queryable, Identifiable, Serialize, Deserialize, Clone)]
pub struct User { pub struct User {
pub id: i32, pub id: i32,
pub username: String, pub username: String,
@ -292,7 +293,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
} }
} }
impl Actor for User { impl APActor for User {
fn get_box_prefix() -> &'static str { fn get_box_prefix() -> &'static str {
"@" "@"
} }
@ -357,6 +358,25 @@ impl Actor for User {
} }
} }
impl IntoId for User {
fn into(&self) -> Id {
Id::new(self.ap_url.clone())
}
}
impl Object for User {}
impl Actor for User {}
impl WithInbox for User {
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}
fn get_shared_inbox_url(&self) -> Option<String> {
self.shared_inbox_url.clone()
}
}
impl Inbox for User { impl Inbox for User {
fn received(&self, conn: &PgConnection, act: serde_json::Value) { fn received(&self, conn: &PgConnection, act: serde_json::Value) {
self.save(conn, act.clone()).unwrap(); self.save(conn, act.clone()).unwrap();