Get rid of the legacy activity_pub::object module

This commit is contained in:
Bat 2018-06-20 10:01:25 +01:00
parent 65e819c425
commit ab7bef1490
7 changed files with 22 additions and 93 deletions

View File

@ -14,7 +14,6 @@ use self::sign::Signable;
pub mod actor;
pub mod inbox;
pub mod object;
pub mod request;
pub mod sign;

View File

@ -1,8 +0,0 @@
use diesel::PgConnection;
use serde_json;
pub trait Object {
fn serialize(&self, conn: &PgConnection) -> serde_json::Value;
fn compute_id(&self, conn: &PgConnection) -> String;
}

View File

@ -9,8 +9,7 @@ use serde_json;
use activity_pub::{
ap_url, Id, IntoId, PUBLIC_VISIBILTY,
actor::Actor,
inbox::{FromActivity, Notify},
object::Object
inbox::{FromActivity, Notify}
};
use models::{
instance::Instance,
@ -105,6 +104,10 @@ impl Comment {
json["author"] = self.get_author(conn).to_json(conn);
json
}
pub fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!("{}#comment-{}", self.get_post(conn).compute_id(conn), self.id))
}
}
impl FromActivity<Note> for Comment {
@ -146,31 +149,3 @@ impl Notify<Note> for Comment {
};
}
}
impl Object for Comment {
fn serialize(&self, conn: &PgConnection) -> serde_json::Value {
let mut to = self.get_author(conn).get_followers(conn).into_iter().map(|f| f.ap_url).collect::<Vec<String>>();
to.append(&mut self.get_post(conn).get_receivers_urls(conn));
to.push(PUBLIC_VISIBILTY.to_string());
json!({
"id": self.compute_id(conn),
"type": "Note",
"summary": self.spoiler_text,
"content": self.content,
"inReplyTo": self.in_response_to_id.map_or_else(|| self.get_post(conn).ap_url, |id| {
let comm = Comment::get(conn, id).unwrap();
comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
}),
"published": self.creation_date,
"attributedTo": self.get_author(conn).compute_id(conn),
"to": to,
"cc": [],
"sensitive": self.sensitive,
})
}
fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!("{}#comment-{}", self.get_post(conn).compute_id(conn), self.id))
}
}

View File

@ -1,14 +1,12 @@
use activitypub::activity;
use chrono;
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use serde_json;
use activity_pub::{
Id,
IntoId,
actor::Actor,
inbox::{FromActivity, Deletable, Notify},
object::Object
inbox::{FromActivity, Deletable, Notify}
};
use models::{
notifications::*,
@ -68,6 +66,14 @@ impl Like {
act
}
pub fn compute_id(&self, conn: &PgConnection) -> String {
format!(
"{}/like/{}",
User::get(conn, self.user_id).unwrap().compute_id(conn),
Post::get(conn, self.post_id).unwrap().compute_id(conn)
)
}
}
impl FromActivity<activity::Like> for Like {
@ -111,19 +117,3 @@ impl Deletable for Like {
}
}
}
impl Object for Like {
fn serialize(&self, conn: &PgConnection) -> serde_json::Value {
json!({
"id": self.compute_id(conn)
})
}
fn compute_id(&self, conn: &PgConnection) -> String {
format!(
"{}/like/{}",
User::get(conn, self.user_id).unwrap().compute_id(conn),
Post::get(conn, self.post_id).unwrap().compute_id(conn)
)
}
}

View File

@ -10,8 +10,7 @@ use BASE_URL;
use activity_pub::{
PUBLIC_VISIBILTY, ap_url, Id, IntoId,
actor::Actor,
inbox::FromActivity,
object::Object
inbox::FromActivity
};
use models::{
blogs::Blog,
@ -177,6 +176,10 @@ impl Post {
"date": self.creation_date.timestamp()
})
}
pub fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!("{}/~/{}/{}/", BASE_URL.as_str(), self.get_blog(conn).actor_id, self.slug))
}
}
impl FromActivity<Article> for Post {
@ -198,33 +201,3 @@ impl IntoId for Post {
Id::new(self.ap_url.clone())
}
}
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 {
let mut to = self.get_receivers_urls(conn);
to.push(PUBLIC_VISIBILTY.to_string());
json!({
"type": "Article",
"id": self.compute_id(conn),
"attributedTo": self.get_authors(conn)[0].compute_id(conn),
"name": self.title,
"content": self.content,
"actor": self.get_authors(conn)[0].compute_id(conn),
"published": self.creation_date,
// TODO: "image": "image",
// TODO: "preview": "preview",
// TODO: "replies": "replies",
// TODO: "summary": "summary",
"tag": [],
// TODO: "updated": "updated",
"url": self.compute_id(conn),
"to": to,
"cc": []
})
}
}

View File

@ -2,7 +2,7 @@ use activitypub::activity::{Announce, Undo};
use chrono::NaiveDateTime;
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use activity_pub::{Id, IntoId, actor::Actor, inbox::{FromActivity, Notify, Deletable}, object::Object};
use activity_pub::{Id, IntoId, actor::Actor, inbox::{FromActivity, Notify, Deletable}};
use models::{notifications::*, posts::Post, users::User};
use schema::reshares;

View File

@ -5,7 +5,7 @@ use rocket::response::{Redirect, Flash};
use rocket_contrib::Template;
use serde_json;
use activity_pub::{broadcast, context, activity_pub, ActivityPub, object::Object};
use activity_pub::{broadcast, context, activity_pub, ActivityPub};
use db_conn::DbConn;
use models::{
blogs::*,
@ -44,7 +44,7 @@ fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityPub {
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap();
let mut act = post.serialize(&*conn);
let mut act = serde_json::to_value(post.into_activity(&*conn)).unwrap();
act["@context"] = context();
activity_pub(act)
}