Refactor activity_pub::activity::Activity

I only had to wrap it in Arc… -_-
This commit is contained in:
Bat 2018-05-02 22:36:13 +01:00
parent afe98ab1c3
commit cf41ae5fda
5 changed files with 15 additions and 33 deletions

View File

@ -6,7 +6,7 @@ use std::str::FromStr;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::object::Object; use activity_pub::object::Object;
pub trait Activity: ActivityClone { pub trait Activity {
fn get_id(&self) -> String; fn get_id(&self) -> String;
fn serialize(&self) -> serde_json::Value; fn serialize(&self) -> serde_json::Value;
@ -14,26 +14,6 @@ pub trait Activity: ActivityClone {
// fn deserialize(serde_json::Value) -> Self; // fn deserialize(serde_json::Value) -> Self;
} }
trait ActivityClone {
fn clone_box(&self) -> Box<Activity>;
}
impl<T> ActivityClone for T
where
T: 'static + Activity + Clone,
{
fn clone_box(&self) -> Box<Activity> {
Box::new(self.clone())
}
}
// We can now implement Clone manually by forwarding to clone_box.
impl Clone for Box<Activity> {
fn clone(&self) -> Box<Activity> {
self.clone_box()
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Accept { pub struct Accept {
id: String, id: String,

View File

@ -3,19 +3,20 @@ use rocket::http::Status;
use rocket::response::{Response, Responder}; use rocket::response::{Response, Responder};
use rocket::request::Request; use rocket::request::Request;
use serde_json; use serde_json;
use std::sync::Arc;
use activity_pub::{activity_pub, ActivityPub, context}; use activity_pub::{activity_pub, ActivityPub, context};
use activity_pub::activity::Activity; use activity_pub::activity::Activity;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use models::users::User; use models::users::User;
pub struct Outbox<A> where A: Activity + Clone { pub struct Outbox {
id: String, id: String,
items: Vec<Box<A>> items: Vec<Arc<Activity>>
} }
impl<A: Activity + Clone + 'static> Outbox<A> { impl Outbox {
pub fn new(id: String, items: Vec<Box<A>>) -> Outbox<A> { pub fn new(id: String, items: Vec<Arc<Activity>>) -> Outbox {
Outbox { Outbox {
id: id, id: id,
items: items items: items
@ -34,7 +35,7 @@ impl<A: Activity + Clone + 'static> Outbox<A> {
} }
} }
impl<'r, A: Activity + Clone + 'static> Responder<'r> for Outbox<A> { impl<'r> Responder<'r> for Outbox {
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> { fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> {
self.serialize().respond_to(request) self.serialize().respond_to(request)
} }

View File

@ -1,5 +1,6 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use std::sync::Arc;
use activity_pub::activity::Activity; use activity_pub::activity::Activity;
use activity_pub::actor::{Actor, ActorType}; use activity_pub::actor::{Actor, ActorType};
@ -78,11 +79,11 @@ impl Blog {
} }
} }
pub fn outbox<A: Activity + Clone + 'static>(&self, conn: &PgConnection) -> Outbox<A> { pub fn outbox(&self, conn: &PgConnection) -> Outbox {
Outbox::new(self.compute_outbox(conn), self.get_activities(conn)) Outbox::new(self.compute_outbox(conn), self.get_activities(conn))
} }
fn get_activities<A: Activity + Clone>(&self, _conn: &PgConnection) -> Vec<Box<A>> { fn get_activities(&self, _conn: &PgConnection) -> Vec<Arc<Activity>> {
vec![] vec![]
} }
} }

View File

@ -8,6 +8,7 @@ use reqwest::mime::Mime;
use rocket::request::{self, FromRequest, Request}; use rocket::request::{self, FromRequest, Request};
use rocket::outcome::IntoOutcome; use rocket::outcome::IntoOutcome;
use serde_json; use serde_json;
use std::sync::Arc;
use url::Url; use url::Url;
use BASE_URL; use BASE_URL;
@ -184,16 +185,16 @@ impl User {
} }
} }
pub fn outbox<A: Activity + Clone + 'static>(&self, conn: &PgConnection) -> Outbox<A> { pub fn outbox(&self, conn: &PgConnection) -> Outbox {
Outbox::new(self.compute_outbox(conn), self.get_activities(conn)) Outbox::new(self.compute_outbox(conn), self.get_activities(conn))
} }
fn get_activities<A: Activity>(&self, conn: &PgConnection) -> Vec<Box<A>> { fn get_activities(&self, conn: &PgConnection) -> Vec<Arc<Activity>> {
use schema::posts; use schema::posts;
use schema::post_authors; use schema::post_authors;
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id); let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id);
let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::<Post>(conn).unwrap(); let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::<Post>(conn).unwrap();
posts.into_iter().map(|p| Box::new(Create::new(self, &p, conn)) as Box<A>).collect::<Vec<Box<A>>>() posts.into_iter().map(|p| Arc::new(Create::new(self, &p, conn)) as Arc<Activity>).collect::<Vec<Arc<Activity>>>()
} }
pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> { pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> {

View File

@ -4,7 +4,6 @@ use rocket_contrib::Template;
use std::collections::HashMap; use std::collections::HashMap;
use activity_pub::ActivityPub; use activity_pub::ActivityPub;
use activity_pub::activity::Activity;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::outbox::Outbox; use activity_pub::outbox::Outbox;
use db_conn::DbConn; use db_conn::DbConn;
@ -58,7 +57,7 @@ fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect {
} }
#[get("/~/<name>/outbox")] #[get("/~/<name>/outbox")]
fn outbox<A: Activity + Clone + 'static>(name: String, conn: DbConn) -> Outbox<A> { fn outbox(name: String, conn: DbConn) -> Outbox {
let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
blog.outbox(&*conn) blog.outbox(&*conn)
} }