diff --git a/migrations/2018-09-04-103017_follows_add_ap_url/down.sql b/migrations/2018-09-04-103017_follows_add_ap_url/down.sql new file mode 100644 index 00000000..a1d36e37 --- /dev/null +++ b/migrations/2018-09-04-103017_follows_add_ap_url/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE follows DROP COLUMN ap_url; diff --git a/migrations/2018-09-04-103017_follows_add_ap_url/up.sql b/migrations/2018-09-04-103017_follows_add_ap_url/up.sql new file mode 100644 index 00000000..e4c7d949 --- /dev/null +++ b/migrations/2018-09-04-103017_follows_add_ap_url/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE follows ADD COLUMN ap_url TEXT NOT NULL DEFAULT ''; diff --git a/plume-models/src/follows.rs b/plume-models/src/follows.rs index 57e26fa4..eca39d3d 100644 --- a/plume-models/src/follows.rs +++ b/plume-models/src/follows.rs @@ -1,7 +1,7 @@ -use activitypub::{Actor, activity::{Accept, Follow as FollowAct}, actor::Person}; +use activitypub::{Actor, activity::{Accept, Follow as FollowAct, Undo}, actor::Person}; use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl}; -use plume_common::activity_pub::{broadcast, Id, IntoId, inbox::{FromActivity, Notify, WithInbox}, sign::Signer}; +use plume_common::activity_pub::{broadcast, Id, IntoId, inbox::{FromActivity, Notify, WithInbox, Deletable}, sign::Signer}; use blogs::Blog; use notifications::*; use users::User; @@ -12,19 +12,42 @@ use schema::follows; pub struct Follow { pub id: i32, pub follower_id: i32, - pub following_id: i32 + pub following_id: i32, + pub ap_url: String, } #[derive(Insertable)] #[table_name = "follows"] pub struct NewFollow { pub follower_id: i32, - pub following_id: i32 + pub following_id: i32, + pub ap_url: String, } impl Follow { insert!(follows, NewFollow); get!(follows); + find_by!(follows, find_by_ap_url, ap_url as String); + + pub fn find(conn: &PgConnection, from: i32, to: i32) -> Option { + follows::table.filter(follows::follower_id.eq(from)) + .filter(follows::following_id.eq(to)) + .get_result(conn) + .ok() + } + + pub fn into_activity(&self, conn: &PgConnection) -> FollowAct { + let user = User::get(conn, self.follower_id).unwrap(); + let target = User::get(conn, self.following_id).unwrap(); + + let mut act = FollowAct::default(); + act.follow_props.set_actor_link::(user.clone().into_id()).expect("Follow::into_activity: actor error"); + act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap(); + act.object_props.set_id_string(self.ap_url.clone()).unwrap(); + act.object_props.set_to_link(target.clone().into_id()).expect("New Follow error while setting 'to'"); + act.object_props.set_cc_link_vec::(vec![]).expect("New Follow error while setting 'cc'"); + act + } /// from -> The one sending the follow request /// target -> The target of the request, responding with Accept @@ -36,9 +59,12 @@ impl Follow { from_id: i32, target_id: i32 ) -> Follow { + let from_url: String = from.clone().into_id().into(); + let target_url: String = target.clone().into_id().into(); let res = Follow::insert(conn, NewFollow { follower_id: from_id, - following_id: target_id + following_id: target_id, + ap_url: format!("{}/follow/{}", from_url, target_url), }); let mut accept = Accept::default(); @@ -77,3 +103,21 @@ impl Notify for Follow { }); } } + +impl Deletable for Follow { + fn delete(&self, conn: &PgConnection) -> Undo { + diesel::delete(self).execute(conn).expect("Coudn't delete follow"); + + let mut undo = Undo::default(); + undo.undo_props.set_actor_link(User::get(conn, self.follower_id).unwrap().into_id()).expect("Follow::delete: actor error"); + undo.object_props.set_id_string(format!("{}/undo", self.ap_url)).expect("Follow::delete: id error"); + undo.undo_props.set_object_object(self.into_activity(conn)).expect("Follow::delete: object error"); + undo + } + + fn delete_id(id: String, conn: &PgConnection) { + if let Some(follow) = Follow::find_by_ap_url(conn, id) { + follow.delete(conn); + } + } +} diff --git a/plume-models/src/schema.rs b/plume-models/src/schema.rs index 6a625bb8..1671bf72 100644 --- a/plume-models/src/schema.rs +++ b/plume-models/src/schema.rs @@ -42,6 +42,7 @@ table! { id -> Int4, follower_id -> Int4, following_id -> Int4, + ap_url -> Text, } } diff --git a/src/routes/user.rs b/src/routes/user.rs index ae3eb6d1..cd50ffc5 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -116,20 +116,20 @@ fn dashboard_auth() -> Flash { #[get("/@//follow")] fn follow(name: String, conn: DbConn, user: User, worker: Worker) -> Redirect { let target = User::find_by_fqn(&*conn, name.clone()).unwrap(); - let f = follows::Follow::insert(&*conn, follows::NewFollow { - follower_id: user.id, - following_id: target.id - }); - f.notify(&*conn); - - let mut act = Follow::default(); - act.follow_props.set_actor_link::(user.clone().into_id()).unwrap(); - act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap(); - act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap(); - act.object_props.set_to_link(target.clone().into_id()).expect("New Follow error while setting 'to'"); - act.object_props.set_cc_link_vec::(vec![]).expect("New Follow error while setting 'cc'"); - - worker.execute(Thunk::of(move || broadcast(&user, act, vec![target]))); + if let Some(follow) = follows::Follow::find(&*conn, user.id, target.id) { + let delete_act = follow.delete(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, delete_act, vec![target]))); + } else { + let f = follows::Follow::insert(&*conn, follows::NewFollow { + follower_id: user.id, + following_id: target.id, + ap_url: format!("{}/follow/{}", user.ap_url, target.ap_url), + }); + f.notify(&*conn); + + let act = f.into_activity(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, act, vec![target]))); + } Redirect::to(uri!(details: name = name)) }