Make Reshare follow activitystreams 0.7

This commit is contained in:
Kitaiti Makoto 2022-01-12 03:28:18 +09:00
parent fad7c7279b
commit fff1b8396b

View File

@ -2,13 +2,18 @@ use crate::{
ap_url, db_conn::DbConn, instance::Instance, notifications::*, schema::follows, users::User,
Connection, Error, Result, CONFIG,
};
use activitypub::activity::{Accept, Follow as FollowAct, Undo};
use activitystreams::{
activity::{Accept, ActivityExt, ActorAndObjectRefExt, AsActivity, Follow as FollowAct, Undo},
base::AnyBase,
primitives::OneOrMany,
uri as as_uri,
};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
use plume_common::activity_pub::{
broadcast,
inbox::{AsActor, AsObject, FromId},
sign::Signer,
Id, IntoId, PUBLIC_VISIBILITY,
Id, IntoId,
};
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
@ -54,14 +59,19 @@ impl Follow {
let user = User::get(conn, self.follower_id)?;
let target = User::get(conn, self.following_id)?;
let mut act = FollowAct::default();
act.follow_props.set_actor_link::<Id>(user.into_id())?;
act.follow_props
.set_object_link::<Id>(target.clone().into_id())?;
act.object_props.set_id_string(self.ap_url.clone())?;
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())])?;
let act = FollowAct::new(
as_uri!(user.ap_url), // FIXME: Use to_activity()
as_uri!(target.ap_url), // FIXME: Use to_activity()
);
// let mut act = FollowAct::default();
// act.follow_props.set_actor_link::<Id>(user.into_id())?;
// act.follow_props
// .set_object_link::<Id>(target.clone().into_id())?;
// act.object_props.set_id_string(self.ap_url.clone())?;
// 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())])?;
Ok(act)
}
@ -94,7 +104,11 @@ impl Follow {
NewFollow {
follower_id: from_id,
following_id: target_id,
ap_url: follow.object_props.id_string()?,
ap_url: follow
.object()
.as_single_id()
.expect("exists and only")
.to_string(),
},
)?;
res.notify(conn)?;
@ -102,7 +116,7 @@ impl Follow {
let accept = res.build_accept(from, target, follow)?;
broadcast(
&*target,
accept,
*accept.activity_ref(),
vec![from.clone()],
CONFIG.proxy().cloned(),
);
@ -115,39 +129,53 @@ impl Follow {
target: &A,
follow: FollowAct,
) -> Result<Accept> {
let mut accept = Accept::default();
let accept_id = ap_url(&format!(
"{}/follows/{}/accept",
CONFIG.base_url.as_str(),
self.id
));
accept.object_props.set_id_string(accept_id)?;
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())])?;
accept
.accept_props
.set_actor_link::<Id>(target.clone().into_id())?;
accept.accept_props.set_object_object(follow)?;
let accept = Accept::new(
as_uri!(target.into_id()), // FIXME: Use to_activity()
as_uri!(from.into_id()), // FIXME: Use to_activity()
);
// let mut accept = Accept::default();
// let accept_id = ap_url(&format!(
// "{}/follows/{}/accept",
// CONFIG.base_url.as_str(),
// self.id
// ));
// accept.object_props.set_id_string(accept_id)?;
// 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())])?;
// accept
// .accept_props
// .set_actor_link::<Id>(target.clone().into_id())?;
// accept.accept_props.set_object_object(follow)?;
Ok(accept)
}
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())])?;
let follow = self.to_activity(conn)?;
let undo = Undo::new::<url::Url, OneOrMany<AnyBase>>(
*follow
.actor()
.expect("authorized domain")
.as_single_id()
.expect("exists and only"),
*follow.result().expect("exists"),
);
// 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)
}
}
@ -159,11 +187,12 @@ impl AsObject<User, FollowAct, &DbConn> for User {
fn activity(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 mut follow = FollowAct::default();
follow.object_props.set_id_string(id.to_string())?;
follow
.follow_props
.set_actor_link::<Id>(actor.clone().into_id())?;
let follow = FollowAct::new(as_uri!(actor.ap_url), as_uri!(id));
// let mut follow = FollowAct::default();
// follow.object_props.set_id_string(id.to_string())?;
// follow
// .follow_props
// .set_actor_link::<Id>(actor.clone().into_id())?;
Follow::accept_follow(conn, &actor, &self, follow, actor.id, self.id)
}
}
@ -179,7 +208,12 @@ impl FromId<DbConn> for Follow {
fn from_activity(conn: &DbConn, follow: FollowAct) -> Result<Self> {
let actor = User::from_id(
conn,
&follow.follow_props.actor_link::<Id>()?,
&follow
.actor()
.expect("authorized domain")
.as_single_id()
.expect("exists and only")
.as_str(),
None,
CONFIG.proxy(),
)
@ -187,7 +221,11 @@ impl FromId<DbConn> for Follow {
let target = User::from_id(
conn,
&follow.follow_props.object_link::<Id>()?,
&follow
.object()
.as_single_id()
.expect("exists and only")
.as_str(),
None,
CONFIG.proxy(),
)