Drop activity_pub::activitypub and activity_pub::ActivityPub and only use the ActivityStream responder

This commit is contained in:
Bat 2018-06-21 18:09:18 +01:00
parent 6df4b70318
commit b2e8d54161
6 changed files with 29 additions and 57 deletions

View File

@ -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")
}

View File

@ -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<Json<serde_json::Value>>;
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> (T);
impl<T> ActivityStream<T> {
@ -69,7 +62,7 @@ impl<T> ActivityStream<T> {
impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
fn respond_to(self, request: &Request) -> Result<Response<'r>, 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")

View File

@ -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)

View File

@ -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<User>) -> Template {
}
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
fn activity_details(name: String, conn: DbConn) -> ActivityStream<Group> {
let blog = Blog::find_local(&*conn, name).unwrap();
blog.as_activity_pub(&*conn)
ActivityStream::new(blog.into_activity(&*conn))
}
#[get("/blogs/new")]

View File

@ -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<User>
}
#[get("/~/<blog>/<slug>", 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<Article> {
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("/~/<blog>/new", rank = 2)]

View File

@ -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<User>) -> Template {
}
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
fn activity_details(name: String, conn: DbConn) -> ActivityStream<Person> {
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("/@/<name>/followers", format = "application/activity+json")]
fn ap_followers(name: String, conn: DbConn) -> ActivityPub {
fn ap_followers(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
let user = User::find_local(&*conn, name).unwrap();
let followers = user.get_followers(&*conn).into_iter().map(|f| f.ap_url).collect::<Vec<String>>();
let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::<Vec<Id>>();
let json = json!({
"@context": context(),
"id": user.compute_box(&*conn, "followers"),
"type": "OrderedCollection",
"totalItems": followers.len(),
"orderedItems": followers
});
activity_pub(json)
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)
}