2018-11-24 12:44:17 +01:00
|
|
|
use activitypub::{
|
|
|
|
activity::{Accept, Follow as FollowAct, Undo},
|
|
|
|
actor::Person,
|
|
|
|
Actor,
|
|
|
|
};
|
2018-09-27 23:06:40 +02:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-05-01 15:06:31 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use blogs::Blog;
|
|
|
|
use notifications::*;
|
2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::{
|
|
|
|
broadcast,
|
|
|
|
inbox::{Deletable, FromActivity, Notify, WithInbox},
|
|
|
|
sign::Signer,
|
|
|
|
Id, IntoId,
|
|
|
|
};
|
2018-05-01 15:06:31 +02:00
|
|
|
use schema::follows;
|
2018-11-24 12:44:17 +01:00
|
|
|
use users::User;
|
|
|
|
use {ap_url, Connection, BASE_URL};
|
2018-05-01 15:06:31 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
#[derive(Clone, Queryable, Identifiable, Associations)]
|
2018-05-01 15:23:23 +02:00
|
|
|
#[belongs_to(User, foreign_key = "following_id")]
|
2018-05-01 15:06:31 +02:00
|
|
|
pub struct Follow {
|
|
|
|
pub id: i32,
|
|
|
|
pub follower_id: i32,
|
2018-09-04 12:37:58 +02:00
|
|
|
pub following_id: i32,
|
|
|
|
pub ap_url: String,
|
2018-05-01 15:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "follows"]
|
|
|
|
pub struct NewFollow {
|
|
|
|
pub follower_id: i32,
|
2018-09-04 12:37:58 +02:00
|
|
|
pub following_id: i32,
|
|
|
|
pub ap_url: String,
|
2018-05-01 15:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Follow {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(follows, NewFollow);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(follows);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(follows, find_by_ap_url, ap_url as &str);
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn find(conn: &Connection, from: i32, to: i32) -> Option<Follow> {
|
2018-11-24 12:44:17 +01:00
|
|
|
follows::table
|
|
|
|
.filter(follows::follower_id.eq(from))
|
2018-09-04 12:37:58 +02:00
|
|
|
.filter(follows::following_id.eq(to))
|
|
|
|
.get_result(conn)
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> FollowAct {
|
2018-11-24 12:44:17 +01:00
|
|
|
let user = User::get(conn, self.follower_id)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: actor not found error");
|
2018-11-24 12:44:17 +01:00
|
|
|
let target = User::get(conn, self.following_id)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: target not found error");
|
2018-09-04 12:37:58 +02:00
|
|
|
|
|
|
|
let mut act = FollowAct::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
act.follow_props
|
|
|
|
.set_actor_link::<Id>(user.clone().into_id())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: actor error");
|
2018-11-24 12:44:17 +01:00
|
|
|
act.follow_props
|
2018-12-23 11:12:15 +01:00
|
|
|
.set_object_link::<Id>(target.clone().into_id())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: object error");
|
2018-11-24 12:44:17 +01:00
|
|
|
act.object_props
|
|
|
|
.set_id_string(self.ap_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: id error");
|
2018-11-24 12:44:17 +01:00
|
|
|
act.object_props
|
2018-12-23 11:12:15 +01:00
|
|
|
.set_to_link(target.into_id())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: target error");
|
2018-11-24 12:44:17 +01:00
|
|
|
act.object_props
|
|
|
|
.set_cc_link_vec::<Id>(vec![])
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::to_activity: cc error");
|
2018-09-04 12:37:58 +02:00
|
|
|
act
|
|
|
|
}
|
2018-06-12 21:10:08 +02:00
|
|
|
|
2018-06-22 17:17:53 +02:00
|
|
|
/// from -> The one sending the follow request
|
|
|
|
/// target -> The target of the request, responding with Accept
|
2018-06-23 13:50:14 +02:00
|
|
|
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor + IntoId>(
|
2018-09-26 17:22:42 +02:00
|
|
|
conn: &Connection,
|
2018-06-22 17:17:53 +02:00
|
|
|
from: &B,
|
|
|
|
target: &A,
|
2018-06-12 21:10:08 +02:00
|
|
|
follow: FollowAct,
|
|
|
|
from_id: i32,
|
2018-11-24 12:44:17 +01:00
|
|
|
target_id: i32,
|
2018-06-12 21:10:08 +02:00
|
|
|
) -> Follow {
|
2018-11-24 12:44:17 +01:00
|
|
|
let res = Follow::insert(
|
|
|
|
conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: from_id,
|
|
|
|
following_id: target_id,
|
2018-12-23 11:12:15 +01:00
|
|
|
ap_url: follow.object_props.id_string().expect("Follow::accept_follow: get id error"),
|
2018-11-24 12:44:17 +01:00
|
|
|
},
|
|
|
|
);
|
2018-06-12 21:10:08 +02:00
|
|
|
|
|
|
|
let mut accept = Accept::default();
|
2018-11-26 10:21:52 +01:00
|
|
|
let accept_id = ap_url(&format!("{}/follow/{}/accept", BASE_URL.as_str(), &res.id));
|
2018-11-24 12:44:17 +01:00
|
|
|
accept
|
|
|
|
.object_props
|
|
|
|
.set_id_string(accept_id)
|
2018-12-23 11:12:15 +01:00
|
|
|
.expect("Follow::accept_follow: set id error");
|
2018-11-24 12:44:17 +01:00
|
|
|
accept
|
|
|
|
.object_props
|
|
|
|
.set_to_link(from.clone().into_id())
|
|
|
|
.expect("Follow::accept_follow: to error");
|
|
|
|
accept
|
|
|
|
.object_props
|
|
|
|
.set_cc_link_vec::<Id>(vec![])
|
|
|
|
.expect("Follow::accept_follow: cc error");
|
|
|
|
accept
|
|
|
|
.accept_props
|
|
|
|
.set_actor_link::<Id>(target.clone().into_id())
|
|
|
|
.expect("Follow::accept_follow: actor error");
|
|
|
|
accept
|
|
|
|
.accept_props
|
|
|
|
.set_object_object(follow)
|
|
|
|
.expect("Follow::accept_follow: object error");
|
2018-06-22 17:17:53 +02:00
|
|
|
broadcast(&*target, accept, vec![from.clone()]);
|
2018-06-12 21:10:08 +02:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl FromActivity<FollowAct, Connection> for Follow {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn from_activity(conn: &Connection, follow: FollowAct, _actor: Id) -> Follow {
|
2018-11-24 12:44:17 +01:00
|
|
|
let from_id = follow
|
|
|
|
.follow_props
|
|
|
|
.actor_link::<Id>()
|
|
|
|
.map(|l| l.into())
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
follow
|
|
|
|
.follow_props
|
|
|
|
.actor_object::<Person>()
|
|
|
|
.expect("Follow::from_activity: actor not found error")
|
|
|
|
.object_props
|
|
|
|
.id_string()
|
|
|
|
.expect("Follow::from_activity: actor not found error")
|
|
|
|
});
|
|
|
|
let from =
|
2018-11-26 10:21:52 +01:00
|
|
|
User::from_url(conn, &from_id).expect("Follow::from_activity: actor not found error");
|
2018-11-24 12:44:17 +01:00
|
|
|
match User::from_url(
|
|
|
|
conn,
|
|
|
|
follow
|
|
|
|
.follow_props
|
|
|
|
.object
|
|
|
|
.as_str()
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::from_activity: target url parsing error"),
|
2018-11-24 12:44:17 +01:00
|
|
|
) {
|
2018-06-22 17:17:53 +02:00
|
|
|
Some(user) => Follow::accept_follow(conn, &from, &user, follow, from.id, user.id),
|
2018-06-12 21:10:08 +02:00
|
|
|
None => {
|
2018-11-24 12:44:17 +01:00
|
|
|
let blog = Blog::from_url(
|
|
|
|
conn,
|
|
|
|
follow
|
|
|
|
.follow_props
|
|
|
|
.object
|
|
|
|
.as_str()
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Follow::from_activity: target url parsing error"),
|
2018-11-24 12:44:17 +01:00
|
|
|
).expect("Follow::from_activity: target not found error");
|
2018-06-12 21:10:08 +02:00
|
|
|
Follow::accept_follow(conn, &from, &blog, follow, from.id, blog.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 15:06:31 +02:00
|
|
|
}
|
2018-06-17 21:37:10 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl Notify<Connection> for Follow {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn notify(&self, conn: &Connection) {
|
2018-11-24 12:44:17 +01:00
|
|
|
Notification::insert(
|
|
|
|
conn,
|
|
|
|
NewNotification {
|
|
|
|
kind: notification_kind::FOLLOW.to_string(),
|
|
|
|
object_id: self.id,
|
|
|
|
user_id: self.following_id,
|
|
|
|
},
|
|
|
|
);
|
2018-06-17 21:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl Deletable<Connection, Undo> for Follow {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn delete(&self, conn: &Connection) -> Undo {
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Follow::delete: follow deletion error");
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2018-09-08 00:29:50 +02:00
|
|
|
// delete associated notification if any
|
|
|
|
if let Some(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(¬if)
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Follow::delete: notification deletion error");
|
2018-09-08 00:29:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:37:58 +02:00
|
|
|
let mut undo = Undo::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
undo.undo_props
|
|
|
|
.set_actor_link(
|
|
|
|
User::get(conn, self.follower_id)
|
|
|
|
.expect("Follow::delete: actor error")
|
|
|
|
.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
|
2018-12-23 11:12:15 +01:00
|
|
|
.set_object_link::<Id>(self.clone().into_id())
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("Follow::delete: object error");
|
2018-09-04 12:37:58 +02:00
|
|
|
undo
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
fn delete_id(id: &str, actor_id: &str, conn: &Connection) {
|
2018-09-04 12:37:58 +02:00
|
|
|
if let Some(follow) = Follow::find_by_ap_url(conn, id) {
|
2018-10-22 17:29:25 +02:00
|
|
|
if let Some(user) = User::find_by_ap_url(conn, actor_id) {
|
|
|
|
if user.id == follow.follower_id {
|
|
|
|
follow.delete(conn);
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 12:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 11:12:15 +01:00
|
|
|
|
|
|
|
impl IntoId for Follow {
|
|
|
|
fn into_id(self) -> Id {
|
|
|
|
Id::new(self.ap_url)
|
|
|
|
}
|
|
|
|
}
|