Actually start playing with ActivityPub
And Rust
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
use activity_pub::actor::Actor;
|
||||
use activity_pub::object::Object;
|
||||
|
||||
pub struct Create<'a, T, U> where T: Actor + 'static, U: Object {
|
||||
by: &'a T,
|
||||
object: U
|
||||
}
|
||||
|
||||
impl<'a, T: Actor, U: Object> Create<'a, T, U> {
|
||||
pub fn new(by: &T, obj: U) -> Create<T, U> {
|
||||
Create {
|
||||
by: by,
|
||||
object: obj
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use diesel::PgConnection;
|
||||
|
||||
use activity_pub::{activity, Activity, context};
|
||||
use activity_pub::{activity_pub, ActivityPub, context};
|
||||
use activity_pub::activity::Create;
|
||||
use activity_pub::object::{Attribuable, Object};
|
||||
use models::instance::Instance;
|
||||
|
||||
pub enum ActorType {
|
||||
@@ -26,8 +28,8 @@ pub trait Actor {
|
||||
|
||||
fn get_actor_type() -> ActorType;
|
||||
|
||||
fn as_activity_pub (&self, conn: &PgConnection) -> Activity {
|
||||
activity(json!({
|
||||
fn as_activity_pub (&self, conn: &PgConnection) -> ActivityPub {
|
||||
activity_pub(json!({
|
||||
"@context": context(),
|
||||
"id": self.compute_id(conn),
|
||||
"type": Self::get_actor_type().to_string(),
|
||||
@@ -63,4 +65,9 @@ pub trait Actor {
|
||||
user = self.get_actor_id()
|
||||
)
|
||||
}
|
||||
|
||||
fn create<T>(&self, obj: T) -> Create<Self, T> where T: Object + Attribuable, Self: Actor + Sized {
|
||||
obj.set_attribution::<Self>(self);
|
||||
Create::<Self, T>::new(self, obj)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ use rocket::response::Content;
|
||||
use rocket_contrib::Json;
|
||||
use serde_json;
|
||||
|
||||
pub mod activity;
|
||||
pub mod actor;
|
||||
pub mod object;
|
||||
pub mod webfinger;
|
||||
|
||||
pub type Activity = Content<Json>;
|
||||
pub type ActivityPub = Content<Json>;
|
||||
|
||||
pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams";
|
||||
|
||||
pub fn context() -> serde_json::Value {
|
||||
json!([
|
||||
@@ -32,6 +36,6 @@ pub fn context() -> serde_json::Value {
|
||||
])
|
||||
}
|
||||
|
||||
pub fn activity(json: serde_json::Value) -> Activity {
|
||||
pub fn activity_pub(json: serde_json::Value) -> ActivityPub {
|
||||
Content(ContentType::new("application", "activity+json"), Json(json))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
use activity_pub::actor::Actor;
|
||||
|
||||
pub trait Object {}
|
||||
|
||||
pub trait Attribuable {
|
||||
fn set_attribution<T>(&self, by: &T) where T: Actor;
|
||||
}
|
||||
@@ -8,6 +8,10 @@ extern crate diesel;
|
||||
extern crate dotenv;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
#[feature(custom_attribute)]
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use activity_pub::Activity;
|
||||
use activity_pub::ActivityPub;
|
||||
use activity_pub::actor::Actor;
|
||||
use db_conn::DbConn;
|
||||
use models::blog_authors::*;
|
||||
@@ -18,7 +18,7 @@ fn details(name: String) -> String {
|
||||
}
|
||||
|
||||
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
|
||||
fn activity_details(name: String, conn: DbConn) -> Activity {
|
||||
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
|
||||
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
|
||||
blog.as_activity_pub(&*conn)
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use activity_pub::Activity;
|
||||
use activity_pub::ActivityPub;
|
||||
use activity_pub::actor::Actor;
|
||||
use db_conn::DbConn;
|
||||
use models::instance::Instance;
|
||||
@@ -20,7 +20,7 @@ fn details(name: String) -> String {
|
||||
}
|
||||
|
||||
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
|
||||
fn activity_details(name: String, conn: DbConn) -> Activity {
|
||||
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
|
||||
let user = User::find_by_name(&*conn, name).unwrap();
|
||||
user.as_activity_pub(&*conn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user