Make Like follow activitystreams 0.7

This commit is contained in:
Kitaiti Makoto 2022-01-12 03:27:53 +09:00
parent 7a404c68df
commit ff26149b7c

View File

@ -2,13 +2,18 @@ use crate::{
db_conn::DbConn, instance::Instance, notifications::*, posts::Post, schema::likes, timeline::*, db_conn::DbConn, instance::Instance, notifications::*, posts::Post, schema::likes, timeline::*,
users::User, Connection, Error, Result, CONFIG, users::User, Connection, Error, Result, CONFIG,
}; };
use activitypub::activity; use activitystreams::{
activity::{self, ActivityExt, ActorAndObjectRefExt},
base::AnyBase,
primitives::OneOrMany,
uri as as_uri,
};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use plume_common::activity_pub::{ use plume_common::activity_pub::{
inbox::{AsActor, AsObject, FromId}, inbox::{AsActor, AsObject, FromId},
sign::Signer, sign::Signer,
Id, IntoId, PUBLIC_VISIBILITY,
}; };
#[derive(Clone, Queryable, Identifiable)] #[derive(Clone, Queryable, Identifiable)]
@ -35,17 +40,21 @@ impl Like {
find_by!(likes, find_by_user_on_post, user_id as i32, post_id as i32); find_by!(likes, find_by_user_on_post, user_id as i32, post_id as i32);
pub fn to_activity(&self, conn: &Connection) -> Result<activity::Like> { pub fn to_activity(&self, conn: &Connection) -> Result<activity::Like> {
let mut act = activity::Like::default(); let act = activity::Like::new(
act.like_props as_uri!(User::get(conn, self.user_id)?.ap_url), // FIXME: use to_activity()
.set_actor_link(User::get(conn, self.user_id)?.into_id())?; as_uri!(Post::get(conn, self.post_id)?.ap_url), // FIXME: use to_activity()
act.like_props );
.set_object_link(Post::get(conn, self.post_id)?.into_id())?; // let mut act = activity::Like::default();
act.object_props // act.like_props
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?; // .set_actor_link(User::get(conn, self.user_id)?.into_id())?;
act.object_props.set_cc_link_vec(vec![Id::new( // act.like_props
User::get(conn, self.user_id)?.followers_endpoint, // .set_object_link(Post::get(conn, self.post_id)?.into_id())?;
)])?; // act.object_props
act.object_props.set_id_string(self.ap_url.clone())?; // .set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
// act.object_props.set_cc_link_vec(vec![Id::new(
// User::get(conn, self.user_id)?.followers_endpoint,
// )])?;
// act.object_props.set_id_string(self.ap_url.clone())?;
Ok(act) Ok(act)
} }
@ -68,17 +77,27 @@ impl Like {
} }
pub fn build_undo(&self, conn: &Connection) -> Result<activity::Undo> { pub fn build_undo(&self, conn: &Connection) -> Result<activity::Undo> {
let mut act = activity::Undo::default(); let like = self.to_activity(conn)?;
act.undo_props let act = activity::Undo::new::<url::Url, OneOrMany<AnyBase>>(
.set_actor_link(User::get(conn, self.user_id)?.into_id())?; *like
act.undo_props.set_object_object(self.to_activity(conn)?)?; .actor()
act.object_props .expect("authorized domain")
.set_id_string(format!("{}#delete", self.ap_url))?; .as_single_id()
act.object_props .expect("exists and only"),
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?; *like.result().expect("exists"),
act.object_props.set_cc_link_vec(vec![Id::new( );
User::get(conn, self.user_id)?.followers_endpoint,
)])?; // let mut act = activity::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(
// User::get(conn, self.user_id)?.followers_endpoint,
// )])?;
Ok(act) Ok(act)
} }
@ -118,7 +137,7 @@ impl FromId<DbConn> for Like {
NewLike { NewLike {
post_id: Post::from_id( post_id: Post::from_id(
conn, conn,
&act.like_props.object_link::<Id>()?, &act.object().as_single_id().expect("exists").as_str(),
None, None,
CONFIG.proxy(), CONFIG.proxy(),
) )
@ -126,13 +145,17 @@ impl FromId<DbConn> for Like {
.id, .id,
user_id: User::from_id( user_id: User::from_id(
conn, conn,
&act.like_props.actor_link::<Id>()?, &act.actor()
.expect("authorized domain")
.as_single_id()
.expect("exists")
.as_str(),
None, None,
CONFIG.proxy(), CONFIG.proxy(),
) )
.map_err(|(_, e)| e)? .map_err(|(_, e)| e)?
.id, .id,
ap_url: act.object_props.id_string()?, ap_url: act.object().as_single_id().expect("exists").to_string(),
}, },
)?; )?;
res.notify(conn)?; res.notify(conn)?;