From ab7bef1490dae4656f44c0aaf0fddfd6151a1a12 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 20 Jun 2018 10:01:25 +0100 Subject: [PATCH] Get rid of the legacy activity_pub::object module --- src/activity_pub/mod.rs | 1 - src/activity_pub/object.rs | 8 -------- src/models/comments.rs | 35 +++++------------------------------ src/models/likes.rs | 28 +++++++++------------------- src/models/posts.rs | 37 +++++-------------------------------- src/models/reshares.rs | 2 +- src/routes/posts.rs | 4 ++-- 7 files changed, 22 insertions(+), 93 deletions(-) delete mode 100644 src/activity_pub/object.rs diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index f4c4a06a..4943f58e 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -14,7 +14,6 @@ use self::sign::Signable; pub mod actor; pub mod inbox; -pub mod object; pub mod request; pub mod sign; diff --git a/src/activity_pub/object.rs b/src/activity_pub/object.rs deleted file mode 100644 index a06c671e..00000000 --- a/src/activity_pub/object.rs +++ /dev/null @@ -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; -} diff --git a/src/models/comments.rs b/src/models/comments.rs index 79848332..5926170a 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -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 for Comment { @@ -146,31 +149,3 @@ impl Notify 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::>(); - 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)) - } -} diff --git a/src/models/likes.rs b/src/models/likes.rs index 58aba083..3efe9f76 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -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 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) - ) - } -} diff --git a/src/models/posts.rs b/src/models/posts.rs index b084dd4b..b0915a63 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -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
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": [] - }) - } -} diff --git a/src/models/reshares.rs b/src/models/reshares.rs index 09e75bfa..f1858cea 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -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; diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 616160f5..5cc3262e 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -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) }