From bae49bcb47b86df9c7ae2ca28603afd7acb20146 Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 12 May 2018 22:34:13 +0100 Subject: [PATCH] Broadcast delete to AP --- src/activity_pub/activity.rs | 44 ++++++++++++++++++++++++++++++++++++ src/models/likes.rs | 19 ++++++++++++++++ src/routes/likes.rs | 4 ++-- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index b318b5b..50e38e0 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -95,6 +95,50 @@ impl Activity for Create { } } +#[derive(Clone)] +pub struct Delete { + id: String, + actor: serde_json::Value, + object: serde_json::Value, + date: chrono::DateTime +} + +impl Delete { + pub fn new(actor: &A, obj: &B, conn: &PgConnection) -> Delete { + Delete { + id: format!("{}#delete", obj.compute_id(conn)), + actor: serde_json::Value::String(actor.compute_id(conn)), + object: serde_json::Value::String(obj.compute_id(conn)), + date: chrono::Utc::now() + } + } +} + +impl Activity for Delete { + fn get_id(&self) -> String { + self.id.clone() + } + + fn get_type(&self) -> String { + "Delete".to_string() + } + + fn serialize(&self) -> serde_json::Value { + json!({ + "type": "Delete", + "id": self.id, + "actor": self.actor, + "object": { + "type": "Tombstone", + "id": self.object + }, + "published": self.date.to_rfc3339(), + "to": self.object["to"], + "cc": self.object["cc"] + }) + } +} + #[derive(Clone)] pub struct Follow { id: String, diff --git a/src/models/likes.rs b/src/models/likes.rs index 2591d58..c648d44 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -1,6 +1,9 @@ use chrono; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; +use serde_json; +use activity_pub::actor::Actor; +use activity_pub::object::Object; use models::posts::Post; use models::users::User; use schema::likes; @@ -49,3 +52,19 @@ impl Like { diesel::delete(self).execute(conn).unwrap(); } } + +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/routes/likes.rs b/src/routes/likes.rs index 22b8a36..599a075 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -1,6 +1,6 @@ use rocket::response::Redirect; -use activity_pub::activity::Like; +use activity_pub::activity::{Like, Delete}; use activity_pub::outbox::broadcast; use db_conn::DbConn; use models::likes; @@ -21,7 +21,7 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { } else { let like = likes::Like::for_user_on_post(&*conn, &user, &post).unwrap(); like.delete(&*conn); - // TODO: send Delete to AP + broadcast(&*conn, &user, Delete::new(&user, &like, &*conn), user.get_followers(&*conn)); } Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref())