Add Actor ActivityPub representation

This commit is contained in:
Bat 2018-04-23 17:26:01 +01:00
parent 656b201244
commit e9bcaf4dd9
5 changed files with 69 additions and 9 deletions

View File

@ -2,6 +2,20 @@ use models::instance::Instance;
use diesel::PgConnection; use diesel::PgConnection;
use serde_json::Value; use serde_json::Value;
pub enum ActorType {
Person,
Blog
}
impl ToString for ActorType {
fn to_string(&self) -> String {
String::from(match self {
ActorType::Person => "Person",
ActorType::Blog => "Blog"
})
}
}
pub trait Actor { pub trait Actor {
fn get_box_prefix() -> &'static str; fn get_box_prefix() -> &'static str;
@ -9,8 +23,43 @@ pub trait Actor {
fn get_instance(&self, conn: &PgConnection) -> Instance; fn get_instance(&self, conn: &PgConnection) -> Instance;
fn as_activity_pub (&self) -> Value { fn get_actor_type() -> ActorType;
json!({})
fn as_activity_pub (&self, conn: &PgConnection) -> Value {
json!({
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"sensitive": "as:sensitive",
"movedTo": "as:movedTo",
"Hashtag": "as:Hashtag",
"ostatus":"http://ostatus.org#",
"atomUri":"ostatus:atomUri",
"inReplyToAtomUri":"ostatus:inReplyToAtomUri",
"conversation":"ostatus:conversation",
"toot":"http://joinmastodon.org/ns#",
"Emoji":"toot:Emoji",
"focalPoint": {
"@container":"@list",
"@id":"toot:focalPoint"
},
"featured":"toot:featured"
}
],
"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": "",
"summary": "",
"url": self.compute_id(conn),
"endpoints": {
"sharedInbox": "https://plu.me/inbox"
}
})
} }
fn compute_outbox(&self, conn: &PgConnection) -> String { fn compute_outbox(&self, conn: &PgConnection) -> String {
@ -22,12 +71,15 @@ pub trait Actor {
} }
fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String { fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String {
format!("{id}/{name}", id = self.compute_id(conn), name = box_name)
}
fn compute_id(&self, conn: &PgConnection) -> String {
format!( format!(
"https://{instance}/{prefix}/{user}/{name}", "https://{instance}/{prefix}/{user}",
instance = self.get_instance(conn).public_domain, instance = self.get_instance(conn).public_domain,
prefix = Self::get_box_prefix(), prefix = Self::get_box_prefix(),
user = self.get_actor_id(), user = self.get_actor_id()
name = box_name
) )
} }
} }

View File

@ -1,7 +1,7 @@
use diesel; use diesel;
use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use schema::blogs; use schema::blogs;
use activity_pub::Actor; use activity_pub::{Actor, ActorType};
use models::instance::Instance; use models::instance::Instance;
#[derive(Queryable, Identifiable)] #[derive(Queryable, Identifiable)]
@ -77,6 +77,10 @@ impl Actor for Blog {
fn get_instance(&self, conn: &PgConnection) -> Instance { fn get_instance(&self, conn: &PgConnection) -> Instance {
Instance::get(conn, self.instance_id).unwrap() Instance::get(conn, self.instance_id).unwrap()
} }
fn get_actor_type () -> ActorType {
ActorType::Blog
}
} }
impl NewBlog { impl NewBlog {

View File

@ -5,7 +5,7 @@ use diesel;
use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use schema::users; use schema::users;
use db_conn::DbConn; use db_conn::DbConn;
use activity_pub::Actor; use activity_pub::{ActorType, Actor};
use models::instance::Instance; use models::instance::Instance;
use bcrypt; use bcrypt;
@ -121,6 +121,10 @@ impl Actor for User {
fn get_instance(&self, conn: &PgConnection) -> Instance { fn get_instance(&self, conn: &PgConnection) -> Instance {
Instance::get(conn, self.instance_id).unwrap() Instance::get(conn, self.instance_id).unwrap()
} }
fn get_actor_type() -> ActorType {
ActorType::Person
}
} }
impl NewUser { impl NewUser {

View File

@ -19,7 +19,7 @@ fn details(name: String) -> String {
#[get("/~/<name>", format = "application/activity+json", rank = 1)] #[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity(name: String, conn: DbConn) -> Json { fn activity(name: String, conn: DbConn) -> Json {
let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
Json(blog.as_activity_pub()) Json(blog.as_activity_pub(&*conn))
} }
#[get("/blogs/new")] #[get("/blogs/new")]

View File

@ -21,7 +21,7 @@ fn details(name: String) -> String {
#[get("/@/<name>", format = "application/activity+json", rank = 1)] #[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity(name: String, conn: DbConn) -> Json { fn activity(name: String, conn: DbConn) -> Json {
let user = User::find_by_name(&*conn, name).unwrap(); let user = User::find_by_name(&*conn, name).unwrap();
Json(user.as_activity_pub()) Json(user.as_activity_pub(&*conn))
} }
#[get("/users/new")] #[get("/users/new")]