2020-01-21 06:02:03 +00:00
|
|
|
use crate::{
|
2021-11-24 13:50:16 +00:00
|
|
|
ap_url, db_conn::DbConn, instance::Instance, notifications::*, schema::follows, users::User,
|
|
|
|
Connection, Error, Result, CONFIG,
|
2020-01-21 06:02:03 +00:00
|
|
|
};
|
2019-04-17 17:31:47 +00:00
|
|
|
use activitypub::activity::{Accept, Follow as FollowAct, Undo};
|
2022-04-23 18:38:24 +00:00
|
|
|
use activitystreams::{
|
2022-04-23 21:09:00 +00:00
|
|
|
activity::{Accept as Accept07, ActorAndObjectRef, Follow as FollowAct07, Undo as Undo07},
|
2022-04-23 18:38:24 +00:00
|
|
|
base::AnyBase,
|
|
|
|
iri_string::types::IriString,
|
|
|
|
prelude::*,
|
|
|
|
};
|
2019-03-04 20:35:03 +00:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
2018-11-24 11:44:17 +00:00
|
|
|
use plume_common::activity_pub::{
|
2022-04-23 20:50:45 +00:00
|
|
|
broadcast, broadcast07,
|
2022-04-23 21:09:00 +00:00
|
|
|
inbox::{AsActor, AsObject, AsObject07, FromId, FromId07},
|
2018-11-24 11:44:17 +00:00
|
|
|
sign::Signer,
|
2019-04-17 17:31:47 +00:00
|
|
|
Id, IntoId, PUBLIC_VISIBILITY,
|
2018-11-24 11:44:17 +00:00
|
|
|
};
|
2018-05-01 13:06:31 +00:00
|
|
|
|
2019-03-04 20:35:03 +00:00
|
|
|
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
|
2018-05-01 13:23:23 +00:00
|
|
|
#[belongs_to(User, foreign_key = "following_id")]
|
2018-05-01 13:06:31 +00:00
|
|
|
pub struct Follow {
|
|
|
|
pub id: i32,
|
|
|
|
pub follower_id: i32,
|
2018-09-04 10:37:58 +00:00
|
|
|
pub following_id: i32,
|
|
|
|
pub ap_url: String,
|
2018-05-01 13:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "follows"]
|
|
|
|
pub struct NewFollow {
|
|
|
|
pub follower_id: i32,
|
2018-09-04 10:37:58 +00:00
|
|
|
pub following_id: i32,
|
|
|
|
pub ap_url: String,
|
2018-05-01 13:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Follow {
|
2019-03-22 18:51:36 +00:00
|
|
|
insert!(
|
|
|
|
follows,
|
|
|
|
NewFollow,
|
|
|
|
|inserted, conn| if inserted.ap_url.is_empty() {
|
2019-03-21 09:30:33 +00:00
|
|
|
inserted.ap_url = ap_url(&format!("{}/follows/{}", CONFIG.base_url, inserted.id));
|
2019-03-04 20:35:03 +00:00
|
|
|
inserted.save_changes(conn).map_err(Error::from)
|
|
|
|
} else {
|
|
|
|
Ok(inserted)
|
|
|
|
}
|
2019-03-22 18:51:36 +00:00
|
|
|
);
|
2018-06-18 13:44:23 +00:00
|
|
|
get!(follows);
|
2018-11-26 09:21:52 +00:00
|
|
|
find_by!(follows, find_by_ap_url, ap_url as &str);
|
2018-09-04 10:37:58 +00:00
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn find(conn: &Connection, from: i32, to: i32) -> Result<Follow> {
|
2018-11-24 11:44:17 +00:00
|
|
|
follows::table
|
|
|
|
.filter(follows::follower_id.eq(from))
|
2018-09-04 10:37:58 +00:00
|
|
|
.filter(follows::following_id.eq(to))
|
|
|
|
.get_result(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
.map_err(Error::from)
|
2018-09-04 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 08:36:07 +00: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 10:37:58 +00:00
|
|
|
|
|
|
|
let mut act = FollowAct::default();
|
2020-01-19 11:52:32 +00:00
|
|
|
act.follow_props.set_actor_link::<Id>(user.into_id())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
act.follow_props
|
2018-12-29 08:36:07 +00:00
|
|
|
.set_object_link::<Id>(target.clone().into_id())?;
|
2019-03-20 16:56:17 +00:00
|
|
|
act.object_props.set_id_string(self.ap_url.clone())?;
|
2019-04-17 17:31:47 +00: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 08:36:07 +00:00
|
|
|
Ok(act)
|
2018-09-04 10:37:58 +00:00
|
|
|
}
|
2018-06-12 19:10:08 +00:00
|
|
|
|
2022-04-23 18:38:36 +00:00
|
|
|
pub fn to_activity07(&self, conn: &Connection) -> Result<FollowAct07> {
|
|
|
|
let user = User::get(conn, self.follower_id)?;
|
|
|
|
let target = User::get(conn, self.following_id)?;
|
|
|
|
let target_id = target.ap_url.parse::<IriString>()?;
|
|
|
|
|
|
|
|
let mut act = FollowAct07::new(user.ap_url.parse::<IriString>()?, target_id.clone());
|
|
|
|
act.set_id(self.ap_url.parse::<IriString>()?);
|
|
|
|
act.set_many_tos(vec![target_id]);
|
|
|
|
act.set_many_ccs(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]);
|
|
|
|
|
|
|
|
Ok(act)
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:15:41 +00: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 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 15:17:53 +00:00
|
|
|
/// from -> The one sending the follow request
|
|
|
|
/// target -> The target of the request, responding with Accept
|
2019-04-17 17:31:47 +00:00
|
|
|
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
2018-09-26 15:22:42 +00:00
|
|
|
conn: &Connection,
|
2018-06-22 15:17:53 +00:00
|
|
|
from: &B,
|
|
|
|
target: &A,
|
2018-06-12 19:10:08 +00:00
|
|
|
follow: FollowAct,
|
|
|
|
from_id: i32,
|
2018-11-24 11:44:17 +00:00
|
|
|
target_id: i32,
|
2018-12-29 08:36:07 +00:00
|
|
|
) -> Result<Follow> {
|
2018-11-24 11:44:17 +00:00
|
|
|
let res = Follow::insert(
|
|
|
|
conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: from_id,
|
|
|
|
following_id: target_id,
|
2018-12-29 08:36:07 +00:00
|
|
|
ap_url: follow.object_props.id_string()?,
|
2018-11-24 11:44:17 +00:00
|
|
|
},
|
2018-12-29 08:36:07 +00:00
|
|
|
)?;
|
2019-04-17 17:31:47 +00:00
|
|
|
res.notify(conn)?;
|
2018-06-12 19:10:08 +00:00
|
|
|
|
2022-01-08 23:29:02 +00:00
|
|
|
let accept = res.build_accept(from, target, follow)?;
|
|
|
|
broadcast(
|
|
|
|
&*target,
|
|
|
|
accept,
|
|
|
|
vec![from.clone()],
|
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
);
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2022-04-23 20:50:45 +00:00
|
|
|
/// from -> The one sending the follow request
|
|
|
|
/// target -> The target of the request, responding with Accept
|
|
|
|
pub fn accept_follow07<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
|
|
|
conn: &Connection,
|
|
|
|
from: &B,
|
|
|
|
target: &A,
|
|
|
|
follow: FollowAct07,
|
|
|
|
from_id: i32,
|
|
|
|
target_id: i32,
|
|
|
|
) -> Result<Follow> {
|
|
|
|
let res = Follow::insert(
|
|
|
|
conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: from_id,
|
|
|
|
following_id: target_id,
|
|
|
|
ap_url: follow
|
|
|
|
.id_unchecked()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.to_string(),
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
res.notify(conn)?;
|
|
|
|
|
|
|
|
let accept = res.build_accept07(from, target, follow)?;
|
|
|
|
broadcast07(
|
|
|
|
&*target,
|
|
|
|
accept,
|
|
|
|
vec![from.clone()],
|
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
);
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2022-01-08 23:29:02 +00:00
|
|
|
pub fn build_accept<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
|
|
|
&self,
|
|
|
|
from: &B,
|
|
|
|
target: &A,
|
|
|
|
follow: FollowAct,
|
|
|
|
) -> Result<Accept> {
|
2018-06-12 19:10:08 +00:00
|
|
|
let mut accept = Accept::default();
|
2019-03-22 18:51:36 +00:00
|
|
|
let accept_id = ap_url(&format!(
|
2022-01-09 00:12:29 +00:00
|
|
|
"{}/follows/{}/accept",
|
2019-03-22 18:51:36 +00:00
|
|
|
CONFIG.base_url.as_str(),
|
2022-01-08 23:29:02 +00:00
|
|
|
self.id
|
2019-03-22 18:51:36 +00:00
|
|
|
));
|
2019-03-20 16:56:17 +00:00
|
|
|
accept.object_props.set_id_string(accept_id)?;
|
2019-04-17 17:31:47 +00: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 11:44:17 +00:00
|
|
|
accept
|
|
|
|
.accept_props
|
2018-12-29 08:36:07 +00:00
|
|
|
.set_actor_link::<Id>(target.clone().into_id())?;
|
2019-03-20 16:56:17 +00:00
|
|
|
accept.accept_props.set_object_object(follow)?;
|
2022-01-08 23:29:02 +00:00
|
|
|
|
2022-04-23 18:39:08 +00:00
|
|
|
Ok(accept)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_accept07<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
|
|
|
&self,
|
|
|
|
from: &B,
|
|
|
|
target: &A,
|
|
|
|
follow: FollowAct07,
|
|
|
|
) -> Result<Accept07> {
|
|
|
|
let mut accept = Accept07::new(
|
|
|
|
target.clone().into_id().parse::<IriString>()?,
|
|
|
|
AnyBase::from_extended(follow)?,
|
|
|
|
);
|
|
|
|
let accept_id = ap_url(&format!(
|
|
|
|
"{}/follows/{}/accept",
|
|
|
|
CONFIG.base_url.as_str(),
|
|
|
|
self.id
|
|
|
|
));
|
|
|
|
accept.set_id(accept_id.parse::<IriString>()?);
|
|
|
|
accept.set_many_tos(vec![from.clone().into_id().parse::<IriString>()?]);
|
|
|
|
accept.set_many_ccs(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]);
|
|
|
|
|
2022-01-08 23:29:02 +00:00
|
|
|
Ok(accept)
|
2018-06-12 19:10:08 +00:00
|
|
|
}
|
2019-04-17 17:31:47 +00: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)
|
|
|
|
}
|
2022-04-23 18:45:22 +00:00
|
|
|
|
|
|
|
pub fn build_undo07(&self, conn: &Connection) -> Result<Undo07> {
|
|
|
|
let mut undo = Undo07::new(
|
|
|
|
User::get(conn, self.follower_id)?
|
|
|
|
.ap_url
|
|
|
|
.parse::<IriString>()?,
|
|
|
|
self.ap_url.parse::<IriString>()?,
|
|
|
|
);
|
|
|
|
undo.set_id(format!("{}/undo", self.ap_url).parse::<IriString>()?);
|
|
|
|
undo.set_many_tos(vec![User::get(conn, self.following_id)?
|
|
|
|
.ap_url
|
|
|
|
.parse::<IriString>()?]);
|
|
|
|
undo.set_many_ccs(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]);
|
|
|
|
|
|
|
|
Ok(undo)
|
|
|
|
}
|
2018-06-12 19:10:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
impl AsObject<User, FollowAct, &DbConn> for User {
|
2018-12-29 08:36:07 +00:00
|
|
|
type Error = Error;
|
2019-04-17 17:31:47 +00:00
|
|
|
type Output = Follow;
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
fn activity(self, conn: &DbConn, actor: User, id: &str) -> Result<Follow> {
|
2019-04-17 17:31:47 +00:00
|
|
|
// 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 11:44:17 +00:00
|
|
|
.follow_props
|
2019-04-17 17:31:47 +00:00
|
|
|
.set_actor_link::<Id>(actor.clone().into_id())?;
|
2021-01-30 10:24:16 +00:00
|
|
|
Follow::accept_follow(conn, &actor, &self, follow, actor.id, self.id)
|
2018-06-12 19:10:08 +00:00
|
|
|
}
|
2018-05-01 13:06:31 +00:00
|
|
|
}
|
2018-06-17 19:37:10 +00:00
|
|
|
|
2022-04-23 20:51:24 +00:00
|
|
|
impl AsObject07<User, FollowAct07, &DbConn> for User {
|
|
|
|
type Error = Error;
|
|
|
|
type Output = Follow;
|
|
|
|
|
|
|
|
fn activity07(self, conn: &DbConn, actor: User, id: &str) -> Result<Follow> {
|
|
|
|
// Mastodon (at least) requires the full Follow object when accepting it,
|
|
|
|
// so we rebuilt it here
|
|
|
|
let follow = FollowAct07::new(id.parse::<IriString>()?, actor.ap_url.parse::<IriString>()?);
|
|
|
|
Follow::accept_follow07(conn, &actor, &self, follow, actor.id, self.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
impl FromId<DbConn> for Follow {
|
2018-12-29 08:36:07 +00:00
|
|
|
type Error = Error;
|
2019-04-17 17:31:47 +00:00
|
|
|
type Object = FollowAct;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
fn from_db(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
Follow::find_by_ap_url(conn, id)
|
2019-04-17 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
fn from_activity(conn: &DbConn, follow: FollowAct) -> Result<Self> {
|
2021-01-11 20:27:52 +00:00
|
|
|
let actor = User::from_id(
|
2021-01-30 10:24:16 +00:00
|
|
|
conn,
|
2021-01-11 20:27:52 +00:00
|
|
|
&follow.follow_props.actor_link::<Id>()?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
|
|
|
|
|
|
|
let target = User::from_id(
|
2021-01-30 10:24:16 +00:00
|
|
|
conn,
|
2021-01-11 20:27:52 +00:00
|
|
|
&follow.follow_props.object_link::<Id>()?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
2021-01-30 10:24:16 +00:00
|
|
|
Follow::accept_follow(conn, &actor, &target, follow, actor.id, target.id)
|
2018-06-17 19:37:10 +00:00
|
|
|
}
|
2021-11-24 13:50:16 +00:00
|
|
|
|
|
|
|
fn get_sender() -> &'static dyn Signer {
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
}
|
2018-06-17 19:37:10 +00:00
|
|
|
}
|
2018-09-04 10:37:58 +00:00
|
|
|
|
2022-04-23 21:09:00 +00:00
|
|
|
impl FromId07<DbConn> for Follow {
|
|
|
|
type Error = Error;
|
|
|
|
type Object = FollowAct07;
|
|
|
|
|
|
|
|
fn from_db07(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
Follow::find_by_ap_url(conn, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity07(conn: &DbConn, follow: FollowAct07) -> Result<Self> {
|
|
|
|
let actor = User::from_id07(
|
|
|
|
conn,
|
|
|
|
follow
|
|
|
|
.actor_field_ref()
|
|
|
|
.as_single_id()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.as_str(),
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
|
|
|
|
|
|
|
let target = User::from_id07(
|
|
|
|
conn,
|
|
|
|
follow
|
|
|
|
.object_field_ref()
|
|
|
|
.as_single_id()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.as_str(),
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?;
|
|
|
|
Follow::accept_follow07(conn, &actor, &target, follow, actor.id, target.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sender07() -> &'static dyn Signer {
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
impl AsObject<User, Undo, &DbConn> for Follow {
|
2018-12-29 08:36:07 +00:00
|
|
|
type Error = Error;
|
2019-04-17 17:31:47 +00:00
|
|
|
type Output = ();
|
2018-12-29 08:36:07 +00:00
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
fn activity(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> {
|
|
|
|
let conn = conn;
|
2019-04-17 17:31:47 +00:00
|
|
|
if self.follower_id == actor.id {
|
2021-01-30 10:24:16 +00:00
|
|
|
diesel::delete(&self).execute(&**conn)?;
|
2018-09-04 10:37:58 +00:00
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
// delete associated notification if any
|
2021-11-27 22:53:13 +00:00
|
|
|
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
2021-01-30 10:24:16 +00:00
|
|
|
diesel::delete(¬if).execute(&**conn)?;
|
2019-04-17 17:31:47 +00:00
|
|
|
}
|
2018-09-04 10:37:58 +00:00
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
Ok(())
|
2018-12-29 08:36:07 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::Unauthorized)
|
2018-09-04 10:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 10:12:15 +00:00
|
|
|
|
2022-04-23 21:11:54 +00:00
|
|
|
impl AsObject07<User, Undo07, &DbConn> for Follow {
|
|
|
|
type Error = Error;
|
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
fn activity07(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> {
|
|
|
|
let conn = conn;
|
|
|
|
if self.follower_id == actor.id {
|
|
|
|
diesel::delete(&self).execute(&**conn)?;
|
|
|
|
|
|
|
|
// delete associated notification if any
|
|
|
|
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
|
|
|
diesel::delete(¬if).execute(&**conn)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::Unauthorized)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 10:12:15 +00:00
|
|
|
impl IntoId for Follow {
|
|
|
|
fn into_id(self) -> Id {
|
|
|
|
Id::new(self.ap_url)
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 20:35:03 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-01-08 23:19:32 +00:00
|
|
|
use crate::{tests::db, users::tests as user_tests, users::tests::fill_database};
|
|
|
|
use assert_json_diff::assert_json_eq;
|
2019-03-20 16:56:17 +00:00
|
|
|
use diesel::Connection;
|
2022-01-08 23:19:32 +00:00
|
|
|
use serde_json::{json, to_value};
|
2019-03-04 20:35:03 +00:00
|
|
|
|
2022-01-09 00:12:29 +00:00
|
|
|
fn prepare_activity(conn: &DbConn) -> (Follow, User, User, Vec<User>) {
|
|
|
|
let users = fill_database(conn);
|
|
|
|
let following = &users[1];
|
|
|
|
let follower = &users[2];
|
|
|
|
let mut follow = Follow::insert(
|
|
|
|
conn,
|
|
|
|
NewFollow {
|
|
|
|
follower_id: follower.id,
|
|
|
|
following_id: following.id,
|
|
|
|
ap_url: "".into(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
// following.ap_url = format!("https://plu.me/follows/{}", follow.id);
|
|
|
|
follow.ap_url = format!("https://plu.me/follows/{}", follow.id);
|
|
|
|
|
|
|
|
(follow, following.to_owned(), follower.to_owned(), users)
|
|
|
|
}
|
|
|
|
|
2019-03-04 20:35:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_id() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let users = user_tests::fill_database(&conn);
|
2019-03-20 16:56:17 +00: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 09:30:33 +00:00
|
|
|
format!("https://{}/follows/{}", CONFIG.base_url, follow.id)
|
2019-03-20 16:56:17 +00: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 20:35:03 +00:00
|
|
|
assert_eq!(follow.ap_url, String::from("https://some.url/"));
|
2019-10-07 17:08:20 +00:00
|
|
|
|
2019-03-04 20:35:03 +00:00
|
|
|
Ok(())
|
2019-10-07 17:08:20 +00:00
|
|
|
})
|
2019-03-04 20:35:03 +00:00
|
|
|
}
|
2022-01-08 23:19:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_activity() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
2022-01-09 00:12:29 +00:00
|
|
|
let (follow, _following, _follower, _users) = prepare_activity(&conn);
|
2022-01-08 23:19:32 +00:00
|
|
|
let act = follow.to_activity(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"object": "https://plu.me/@/user/",
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
2022-04-23 18:38:24 +00:00
|
|
|
"type": "Follow"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_activity07() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (follow, _following, _follower, _users) = prepare_activity(&conn);
|
|
|
|
let act = follow.to_activity07(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"object": "https://plu.me/@/user/",
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
2022-01-08 23:19:32 +00:00
|
|
|
"type": "Follow"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-01-09 00:12:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_accept() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (follow, following, follower, _users) = prepare_activity(&conn);
|
|
|
|
let act = follow.build_accept(&follower, &following, follow.to_activity(&conn)?)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/user/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://127.0.0.1:7878/follows/{}/accept", follow.id),
|
|
|
|
"object": {
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"object": "https://plu.me/@/user/",
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
2022-04-23 18:38:54 +00:00
|
|
|
"type": "Follow"
|
|
|
|
},
|
|
|
|
"to": ["https://plu.me/@/other/"],
|
|
|
|
"type": "Accept"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_accept07() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (follow, following, follower, _users) = prepare_activity(&conn);
|
|
|
|
let act = follow.build_accept07(&follower, &following, follow.to_activity07(&conn)?)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/user/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://127.0.0.1:7878/follows/{}/accept", follow.id),
|
|
|
|
"object": {
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"object": "https://plu.me/@/user/",
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
2022-01-09 00:12:29 +00:00
|
|
|
"type": "Follow"
|
|
|
|
},
|
|
|
|
"to": ["https://plu.me/@/other/"],
|
|
|
|
"type": "Accept"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_undo() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (follow, _following, _follower, _users) = prepare_activity(&conn);
|
|
|
|
let act = follow.build_undo(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}/undo", follow.id),
|
|
|
|
"object": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
|
|
|
"type": "Undo"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-04-23 18:45:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_undo07() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (follow, _following, _follower, _users) = prepare_activity(&conn);
|
|
|
|
let act = follow.build_undo07(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/other/",
|
|
|
|
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"id": format!("https://plu.me/follows/{}/undo", follow.id),
|
|
|
|
"object": format!("https://plu.me/follows/{}", follow.id),
|
|
|
|
"to": ["https://plu.me/@/user/"],
|
|
|
|
"type": "Undo"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2019-03-20 16:56:17 +00:00
|
|
|
}
|