Compute IDs for activities

Fixes #7
This commit is contained in:
Bat 2018-05-03 16:22:40 +01:00
parent cf41ae5fda
commit 08a21c7a04
3 changed files with 35 additions and 4 deletions

View file

@ -9,6 +9,8 @@ use activity_pub::object::Object;
pub trait Activity {
fn get_id(&self) -> String;
fn get_type(&self) -> String;
fn serialize(&self) -> serde_json::Value;
// fn deserialize(serde_json::Value) -> Self;
@ -25,7 +27,7 @@ pub struct Accept {
impl Accept {
pub fn new<A: Activity, B: Actor>(who: &B, what: &A, conn: &PgConnection) -> Accept {
Accept {
id: "TODO".to_string(),
id: format!("{}/accept/{}/{}", who.compute_id(conn), what.get_type().to_lowercase(), what.get_id()),
actor: serde_json::Value::String(who.compute_id(conn)),
object: serde_json::Value::String(what.get_id()),
date: chrono::Utc::now()
@ -38,6 +40,10 @@ impl Activity for Accept {
self.id.clone()
}
fn get_type(&self) -> String {
"Accept".to_string()
}
fn serialize(&self) -> serde_json::Value {
json!({
"type": "Accept",
@ -59,7 +65,7 @@ pub struct Create {
impl Create {
pub fn new<A: Actor, B: Object>(actor: &A, obj: &B, conn: &PgConnection) -> Create {
Create {
id: "TODO".to_string(),
id: format!("{}/activity", obj.compute_id(conn)),
actor: serde_json::Value::String(actor.compute_id(conn)),
object: obj.serialize(conn),
date: chrono::Utc::now()
@ -72,6 +78,10 @@ impl Activity for Create {
self.id.clone()
}
fn get_type(&self) -> String {
"Create".to_string()
}
fn serialize(&self) -> serde_json::Value {
json!({
"type": "Create",
@ -93,7 +103,7 @@ pub struct Follow {
impl Follow {
pub fn new<A: Actor, B: Actor>(follower: &A, following: &B, conn: &PgConnection) -> Follow {
Follow {
id: "TODO".to_string(),
id: format!("{}/follow/{}", follower.compute_id(conn), following.compute_id(conn)),
actor: serde_json::Value::String(follower.compute_id(conn)),
object: serde_json::Value::String(following.compute_id(conn)),
date: chrono::Utc::now()
@ -119,6 +129,10 @@ impl Activity for Follow {
self.id.clone()
}
fn get_type(&self) -> String {
"Follow".to_string()
}
fn serialize(&self) -> serde_json::Value {
json!({
"type": "Follow",

View file

@ -5,6 +5,8 @@ use activity_pub::actor::Actor;
pub trait Object {
fn serialize(&self, conn: &PgConnection) -> serde_json::Value;
fn compute_id(&self, conn: &PgConnection) -> String;
}
pub trait Attribuable {

View file

@ -3,9 +3,11 @@ use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, Belon
use diesel::dsl::any;
use serde_json;
use activity_pub::PUBLIC_VISIBILTY;
use BASE_URL;
use activity_pub::{PUBLIC_VISIBILTY, ap_url};
use activity_pub::actor::Actor;
use activity_pub::object::Object;
use models::blogs::Blog;
use models::users::User;
use models::post_authors::PostAuthor;
use schema::posts;
@ -63,9 +65,22 @@ impl Post {
let author_list = PostAuthor::belonging_to(self).select(post_authors::author_id);
users::table.filter(users::id.eq(any(author_list))).load::<User>(conn).unwrap()
}
pub fn get_blog(&self, conn: &PgConnection) -> Blog {
use schema::blogs;
blogs::table.filter(blogs::id.eq(self.blog_id))
.limit(1)
.load::<Blog>(conn)
.expect("Couldn't load blog associted to post")
.into_iter().nth(0).unwrap()
}
}
impl Object for Post {
fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!("{}/{}/{}", BASE_URL.as_str(), self.get_blog(conn).actor_id, self.slug))
}
fn serialize(&self, conn: &PgConnection) -> serde_json::Value {
json!({
"type": "Article",