Make Reshare follow activitystreams 0.7

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

View File

@ -2,13 +2,17 @@ use crate::{
db_conn::DbConn, instance::Instance, notifications::*, posts::Post, schema::reshares,
timeline::*, users::User, Connection, Error, Result, CONFIG,
};
use activitypub::activity::{Announce, Undo};
use activitystreams::{
activity::{ActivityExt, ActorAndObjectRefExt, Announce, Undo},
base::AnyBase,
primitives::OneOrMany,
uri as as_uri,
};
use chrono::NaiveDateTime;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use plume_common::activity_pub::{
inbox::{AsActor, AsObject, FromId},
sign::Signer,
Id, IntoId, PUBLIC_VISIBILITY,
};
#[derive(Clone, Queryable, Identifiable)]
@ -61,16 +65,21 @@ impl Reshare {
}
pub fn to_activity(&self, conn: &Connection) -> Result<Announce> {
let mut act = Announce::default();
act.announce_props
.set_actor_link(User::get(conn, self.user_id)?.into_id())?;
act.announce_props
.set_object_link(Post::get(conn, self.post_id)?.into_id())?;
act.object_props.set_id_string(self.ap_url.clone())?;
act.object_props
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
act.object_props
.set_cc_link_vec(vec![Id::new(self.get_user(conn)?.followers_endpoint)])?;
let act = Announce::new(
as_uri!(User::get(conn, self.user_id)?.ap_url), // FIXME: Use to_activity()
as_uri!(Post::get(conn, self.post_id)?.ap_url), // FIXME: Use to_activity()
);
// let mut act = Announce::default();
// act.announce_props
// .set_actor_link(User::get(conn, self.user_id)?.into_id())?;
// act.announce_props
// .set_object_link(Post::get(conn, self.post_id)?.into_id())?;
// act.object_props.set_id_string(self.ap_url.clone())?;
// act.object_props
// .set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
// act.object_props
// .set_cc_link_vec(vec![Id::new(self.get_user(conn)?.followers_endpoint)])?;
Ok(act)
}
@ -93,16 +102,26 @@ impl Reshare {
}
pub fn build_undo(&self, conn: &Connection) -> Result<Undo> {
let mut act = Undo::default();
act.undo_props
.set_actor_link(User::get(conn, self.user_id)?.into_id())?;
act.undo_props.set_object_object(self.to_activity(conn)?)?;
act.object_props
.set_id_string(format!("{}#delete", self.ap_url))?;
act.object_props
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
act.object_props
.set_cc_link_vec(vec![Id::new(self.get_user(conn)?.followers_endpoint)])?;
let announce = self.to_activity(conn)?;
let act = Undo::new::<url::Url, OneOrMany<AnyBase>>(
*announce
.actor()
.expect("authorized domain")
.as_single_id()
.expect("exists and only"),
*announce.result().expect("exists"),
);
// let mut act = Undo::default();
// act.undo_props
// .set_actor_link(User::get(conn, self.user_id)?.into_id())?;
// act.undo_props.set_object_object(self.to_activity(conn)?)?;
// act.object_props
// .set_id_string(format!("{}#delete", self.ap_url))?;
// act.object_props
// .set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
// act.object_props
// .set_cc_link_vec(vec![Id::new(self.get_user(conn)?.followers_endpoint)])?;
Ok(act)
}
@ -143,7 +162,10 @@ impl FromId<DbConn> for Reshare {
NewReshare {
post_id: Post::from_id(
conn,
&act.announce_props.object_link::<Id>()?,
&act.object()
.as_single_id()
.expect("exists and only")
.as_str(),
None,
CONFIG.proxy(),
)
@ -151,13 +173,21 @@ impl FromId<DbConn> for Reshare {
.id,
user_id: User::from_id(
conn,
&act.announce_props.actor_link::<Id>()?,
&act.actor()
.expect("authorized domain")
.as_single_id()
.expect("exists")
.as_str(),
None,
CONFIG.proxy(),
)
.map_err(|(_, e)| e)?
.id,
ap_url: act.object_props.id_string()?,
ap_url: act
.object()
.as_single_id()
.expect("exists and only")
.to_string(),
},
)?;
res.notify(conn)?;