diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index ab5f6c5e..25ea3d27 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -1,8 +1,7 @@ use diesel::PgConnection; use serde_json; -use BASE_URL; -use activity_pub::{activity_pub, ActivityPub, context, ap_url}; +use activity_pub::ap_url; use models::instance::Instance; pub enum ActorType { @@ -40,27 +39,6 @@ pub trait Actor: Sized { serde_json::Map::new() } - fn as_activity_pub (&self, conn: &PgConnection) -> ActivityPub { - let mut repr = json!({ - "@context": context(), - "id": self.compute_id(conn), - "type": Self::get_actor_type().to_string(), - "inbox": self.compute_inbox(conn), - "outbox": self.compute_outbox(conn), - "preferredUsername": self.get_actor_id(), - "name": self.get_display_name(), - "summary": self.get_summary(), - "url": self.compute_id(conn), - "endpoints": { - "sharedInbox": ap_url(format!("{}/inbox", BASE_URL.as_str())) - } - }); - - self.custom_props(conn).iter().for_each(|p| repr[p.0] = p.1.clone()); - - activity_pub(repr) - } - fn compute_outbox(&self, conn: &PgConnection) -> String { self.compute_box(conn, "outbox") } diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index eff1e421..9903ac88 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -2,11 +2,10 @@ use activitypub::{Activity, Actor, Object, Link}; use array_tool::vec::Uniq; use reqwest::Client; use rocket::{ - http::{ContentType, Status}, - response::{Response, Responder, Content}, + http::Status, + response::{Response, Responder}, request::Request }; -use rocket_contrib::Json; use serde_json; use self::sign::Signable; @@ -16,8 +15,6 @@ pub mod inbox; pub mod request; pub mod sign; -pub type ActivityPub = Content>; - pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; pub const PUBLIC_VISIBILTY: &'static str = "https://www.w3.org/ns/activitystreams#Public"; @@ -55,10 +52,6 @@ pub fn context() -> serde_json::Value { ]) } -pub fn activity_pub(json: serde_json::Value) -> ActivityPub { - Content(ContentType::new("application", "activity+json"), Json(json)) -} - pub struct ActivityStream (T); impl ActivityStream { @@ -69,7 +62,7 @@ impl ActivityStream { impl<'r, O: Object> Responder<'r> for ActivityStream { fn respond_to(self, request: &Request) -> Result, Status> { - let mut json = serde_json::to_value(&self.0).map_err(|e| Status::InternalServerError)?; + let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?; json["@context"] = context(); serde_json::to_string(&json).respond_to(request).map(|r| Response::build_from(r) .raw_header("Content-Type", "application/activity+json") diff --git a/src/models/blogs.rs b/src/models/blogs.rs index ea3d1beb..12cd9c93 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -1,4 +1,4 @@ -use activitypub::{Actor, Object, collection::OrderedCollection}; +use activitypub::{Actor, Object, actor::Group, collection::OrderedCollection}; use reqwest::{ Client, header::{Accept, qitem}, @@ -137,6 +137,10 @@ impl Blog { }) } + pub fn into_activity(&self, _conn: &PgConnection) -> Group { + Group::default() // TODO + } + pub fn update_boxes(&self, conn: &PgConnection) { if self.outbox_url.len() == 0 { diesel::update(self) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index ce53a647..6b091941 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -1,4 +1,4 @@ -use activitypub::collection::OrderedCollection; +use activitypub::{actor::Group, collection::OrderedCollection}; use rocket::{ request::Form, response::{Redirect, Flash} @@ -6,7 +6,7 @@ use rocket::{ use rocket_contrib::Template; use serde_json; -use activity_pub::{ActivityStream, ActivityPub, actor::Actor}; +use activity_pub::ActivityStream; use db_conn::DbConn; use models::{ blog_authors::*, @@ -32,9 +32,9 @@ fn details(name: String, conn: DbConn, user: Option) -> Template { } #[get("/~/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> ActivityPub { +fn activity_details(name: String, conn: DbConn) -> ActivityStream { let blog = Blog::find_local(&*conn, name).unwrap(); - blog.as_activity_pub(&*conn) + ActivityStream::new(blog.into_activity(&*conn)) } #[get("/blogs/new")] diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 38d55633..79e0a8fe 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -1,10 +1,11 @@ +use activitypub::object::Article; use heck::KebabCase; use rocket::request::Form; use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use serde_json; -use activity_pub::{broadcast, context, activity_pub, ActivityPub}; +use activity_pub::{broadcast, ActivityStream}; use db_conn::DbConn; use models::{ blogs::*, @@ -49,13 +50,11 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option } #[get("/~//", rank = 3, format = "application/activity+json")] -fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityPub { +fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityStream
{ let blog = Blog::find_by_fqn(&*conn, blog).unwrap(); let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap(); - let mut act = serde_json::to_value(post.into_activity(&*conn)).unwrap(); - act["@context"] = context(); - activity_pub(act) + ActivityStream::new(post.into_activity(&*conn)) } #[get("/~//new", rank = 2)] diff --git a/src/routes/user.rs b/src/routes/user.rs index a6d00527..cfc416f8 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -1,5 +1,6 @@ use activitypub::{ activity::Follow, + actor::Person, collection::OrderedCollection }; use rocket::{request::Form, @@ -9,7 +10,7 @@ use rocket_contrib::Template; use serde_json; use activity_pub::{ - activity_pub, ActivityPub, ActivityStream, context, broadcast, Id, IntoId, + ActivityStream, broadcast, Id, IntoId, inbox::{Inbox, Notify}, actor::Actor }; @@ -110,9 +111,9 @@ fn followers(name: String, conn: DbConn, account: Option) -> Template { } #[get("/@/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> ActivityPub { +fn activity_details(name: String, conn: DbConn) -> ActivityStream { let user = User::find_local(&*conn, name).unwrap(); - user.as_activity_pub(&*conn) + ActivityStream::new(user.into_activity(&*conn)) } #[get("/users/new")] @@ -209,16 +210,13 @@ fn inbox(name: String, conn: DbConn, data: String) -> String { } #[get("/@//followers", format = "application/activity+json")] -fn ap_followers(name: String, conn: DbConn) -> ActivityPub { +fn ap_followers(name: String, conn: DbConn) -> ActivityStream { let user = User::find_local(&*conn, name).unwrap(); - let followers = user.get_followers(&*conn).into_iter().map(|f| f.ap_url).collect::>(); - - let json = json!({ - "@context": context(), - "id": user.compute_box(&*conn, "followers"), - "type": "OrderedCollection", - "totalItems": followers.len(), - "orderedItems": followers - }); - activity_pub(json) + let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::>(); + + let mut coll = OrderedCollection::default(); + coll.object_props.set_id_string(format!("{}/followers", user.ap_url)).expect("Follower collection: id error"); + coll.collection_props.set_total_items_u64(followers.len() as u64).expect("Follower collection: totalItems error"); + coll.collection_props.set_items_link_vec(followers).expect("Follower collection: items error"); + ActivityStream::new(coll) }