2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
|
|
|
ap_url, notifications::*, schema::follows, users::User, Connection, Error, PlumeRocket, Result,
|
|
|
|
CONFIG,
|
|
|
|
};
|
2019-04-17 19:31:47 +02:00
|
|
|
use activitypub::activity::{Accept, Follow as FollowAct, Undo};
|
2019-03-04 21:35:03 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::{
|
|
|
|
broadcast,
|
2019-04-17 19:31:47 +02:00
|
|
|
inbox::{AsActor, AsObject, FromId},
|
2018-11-24 12:44:17 +01:00
|
|
|
sign::Signer,
|
2019-04-17 19:31:47 +02:00
|
|
|
Id, IntoId, PUBLIC_VISIBILITY,
|
2018-11-24 12:44:17 +01:00
|
|
|
};
|
2018-05-01 15:06:31 +02:00
|
|
|
|
2019-03-04 21:35:03 +01:00
|
|
|
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
|
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 {
|
2019-03-22 19:51:36 +01:00
|
|
|
insert!(
|
|
|
|
follows,
|
|
|
|
NewFollow,
|
|
|
|
|inserted, conn| if inserted.ap_url.is_empty() {
|
2019-03-21 10:30:33 +01:00
|
|
|
inserted.ap_url = ap_url(&format!("{}/follows/{}", CONFIG.base_url, inserted.id));
|
2019-03-04 21:35:03 +01:00
|
|
|
inserted.save_changes(conn).map_err(Error::from)
|
|
|
|
} else {
|
|
|
|
Ok(inserted)
|
|
|
|
}
|
2019-03-22 19:51:36 +01:00
|
|
|
);
|
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-12-29 09:36:07 +01:00
|
|
|
pub fn find(conn: &Connection, from: i32, to: i32) -> Result<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)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-09-04 12:37:58 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> Result<FollowAct> {
|
|
|
|
let user = User::get(conn, self.follower_id)?;
|
|
|
|
let target = User::get(conn, self.following_id)?;
|
2018-09-04 12:37:58 +02:00
|
|
|
|
|
|
|
let mut act = FollowAct::default();
|
2020-01-19 12:52:32 +01:00
|
|
|
act.follow_props.set_actor_link::<Id>(user.into_id())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
act.follow_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_object_link::<Id>(target.clone().into_id())?;
|
2019-03-20 17:56:17 +01:00
|
|
|
act.object_props.set_id_string(self.ap_url.clone())?;
|
2019-04-17 19:31:47 +02:00
|
|
|
act.object_props.set_to_link_vec(vec![target.into_id()])?;
|
|
|
|
act.object_props
|
|
|
|
.set_cc_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(act)
|
2018-09-04 12:37:58 +02:00
|
|
|
}
|
2018-06-12 21:10:08 +02:00
|
|
|
|
2019-05-04 17:15:41 +02:00
|
|
|
pub fn notify(&self, conn: &Connection) -> Result<()> {
|
|
|
|
if User::get(conn, self.following_id)?.is_local() {
|
|
|
|
Notification::insert(
|
|
|
|
conn,
|
|
|
|
NewNotification {
|
|
|
|
kind: notification_kind::FOLLOW.to_string(),
|
|
|
|
object_id: self.id,
|
|
|
|
user_id: self.following_id,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2019-04-17 19:31:47 +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
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
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-12-29 09:36:07 +01:00
|
|
|
) -> Result<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-29 09:36:07 +01:00
|
|
|
ap_url: follow.object_props.id_string()?,
|
2018-11-24 12:44:17 +01:00
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
res.notify(conn)?;
|
2018-06-12 21:10:08 +02:00
|
|
|
|
|
|
|
let mut accept = Accept::default();
|
2019-03-22 19:51:36 +01:00
|
|
|
let accept_id = ap_url(&format!(
|
|
|
|
"{}/follow/{}/accept",
|
|
|
|
CONFIG.base_url.as_str(),
|
|
|
|
&res.id
|
|
|
|
));
|
2019-03-20 17:56:17 +01:00
|
|
|
accept.object_props.set_id_string(accept_id)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
accept
|
|
|
|
.object_props
|
|
|
|
.set_to_link_vec(vec![from.clone().into_id()])?;
|
|
|
|
accept
|
|
|
|
.object_props
|
|
|
|
.set_cc_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
|
2018-11-24 12:44:17 +01:00
|
|
|
accept
|
|
|
|
.accept_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_actor_link::<Id>(target.clone().into_id())?;
|
2019-03-20 17:56:17 +01:00
|
|
|
accept.accept_props.set_object_object(follow)?;
|
2021-01-11 21:27:52 +01:00
|
|
|
broadcast(
|
|
|
|
&*target,
|
|
|
|
accept,
|
|
|
|
vec![from.clone()],
|
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
);
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(res)
|
2018-06-12 21:10:08 +02:00
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
pub fn build_undo(&self, conn: &Connection) -> Result<Undo> {
|
|
|
|
let mut undo = Undo::default();
|
|
|
|
undo.undo_props
|
|
|
|
.set_actor_link(User::get(conn, self.follower_id)?.into_id())?;
|
|
|
|
undo.object_props
|
|
|
|
.set_id_string(format!("{}/undo", self.ap_url))?;
|
|
|
|
undo.undo_props
|
|
|
|
.set_object_link::<Id>(self.clone().into_id())?;
|
|
|
|
undo.object_props
|
|
|
|
.set_to_link_vec(vec![User::get(conn, self.following_id)?.into_id()])?;
|
|
|
|
undo.object_props
|
|
|
|
.set_cc_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
|
|
|
|
Ok(undo)
|
|
|
|
}
|
2018-06-12 21:10:08 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl AsObject<User, FollowAct, &PlumeRocket> for User {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
2019-04-17 19:31:47 +02:00
|
|
|
type Output = Follow;
|
|
|
|
|
|
|
|
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Follow> {
|
|
|
|
// Mastodon (at least) requires the full Follow object when accepting it,
|
|
|
|
// so we rebuilt it here
|
|
|
|
let mut follow = FollowAct::default();
|
|
|
|
follow.object_props.set_id_string(id.to_string())?;
|
|
|
|
follow
|
2018-11-24 12:44:17 +01:00
|
|
|
.follow_props
|
2019-04-17 19:31:47 +02:00
|
|
|
.set_actor_link::<Id>(actor.clone().into_id())?;
|
|
|
|
Follow::accept_follow(&c.conn, &actor, &self, follow, actor.id, self.id)
|
2018-06-12 21:10:08 +02:00
|
|
|
}
|
2018-05-01 15:06:31 +02:00
|
|
|
}
|
2018-06-17 21:37:10 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl FromId<PlumeRocket> for Follow {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
2019-04-17 19:31:47 +02:00
|
|
|
type Object = FollowAct;
|
2018-12-29 09:36:07 +01:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
|
|
|
Follow::find_by_ap_url(&c.conn, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity(c: &PlumeRocket, follow: FollowAct) -> Result<Self> {
|
2021-01-11 21:27:52 +01:00
|
|
|
let actor = User::from_id(
|
|
|
|
c,
|
|
|
|
&follow.follow_props.actor_link::<Id>()?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
|
|
|
|
|
|
|
let target = User::from_id(
|
|
|
|
c,
|
|
|
|
&follow.follow_props.object_link::<Id>()?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
Follow::accept_follow(&c.conn, &actor, &target, follow, actor.id, target.id)
|
2018-06-17 21:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl AsObject<User, Undo, &PlumeRocket> for Follow {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
2019-04-17 19:31:47 +02:00
|
|
|
type Output = ();
|
2018-12-29 09:36:07 +01:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
|
|
|
let conn = &*c.conn;
|
|
|
|
if self.follower_id == actor.id {
|
|
|
|
diesel::delete(&self).execute(conn)?;
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
// delete associated notification if any
|
|
|
|
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
|
|
|
diesel::delete(¬if).execute(conn)?;
|
|
|
|
}
|
2018-09-04 12:37:58 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
2018-12-29 09:36:07 +01:00
|
|
|
} else {
|
|
|
|
Err(Error::Unauthorized)
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 21:35:03 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::{tests::db, users::tests as user_tests};
|
2019-03-20 17:56:17 +01:00
|
|
|
use diesel::Connection;
|
2019-03-04 21:35:03 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_id() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let users = user_tests::fill_database(&conn);
|
2019-03-20 17:56:17 +01:00
|
|
|
let follow = Follow::insert(
|
|
|
|
&conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: users[0].id,
|
|
|
|
following_id: users[1].id,
|
|
|
|
ap_url: String::new(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Couldn't insert new follow");
|
|
|
|
assert_eq!(
|
|
|
|
follow.ap_url,
|
2019-03-21 10:30:33 +01:00
|
|
|
format!("https://{}/follows/{}", CONFIG.base_url, follow.id)
|
2019-03-20 17:56:17 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let follow = Follow::insert(
|
|
|
|
&conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: users[1].id,
|
|
|
|
following_id: users[0].id,
|
|
|
|
ap_url: String::from("https://some.url/"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Couldn't insert new follow");
|
2019-03-04 21:35:03 +01:00
|
|
|
assert_eq!(follow.ap_url, String::from("https://some.url/"));
|
2019-10-07 19:08:20 +02:00
|
|
|
|
2019-03-04 21:35:03 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2019-03-04 21:35:03 +01:00
|
|
|
}
|
2019-03-20 17:56:17 +01:00
|
|
|
}
|