From 6ab1ecd57b29350405168e9bdb437e5eb9397f65 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 2 May 2022 23:37:58 +0900 Subject: [PATCH] Use Like::to_activity07() instead of to_activity() --- plume-models/src/likes.rs | 51 ++++----------------------------------- src/routes/likes.rs | 6 ++--- 2 files changed, 8 insertions(+), 49 deletions(-) diff --git a/plume-models/src/likes.rs b/plume-models/src/likes.rs index 38eb0f0f..0218c8ee 100644 --- a/plume-models/src/likes.rs +++ b/plume-models/src/likes.rs @@ -2,9 +2,8 @@ use crate::{ db_conn::DbConn, instance::Instance, notifications::*, posts::Post, schema::likes, timeline::*, users::User, Connection, Error, Result, CONFIG, }; -use activitypub::activity; use activitystreams::{ - activity::{ActorAndObjectRef, Like as Like07, Undo as Undo07}, + activity::{ActorAndObjectRef, Like as Like07, Undo}, base::AnyBase, iri_string::types::IriString, prelude::*, @@ -14,7 +13,7 @@ use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use plume_common::activity_pub::{ inbox::{AsActor, AsObject, FromId}, sign::Signer, - Id, IntoId, PUBLIC_VISIBILITY, + PUBLIC_VISIBILITY, }; #[derive(Clone, Queryable, Identifiable)] @@ -40,22 +39,6 @@ impl Like { find_by!(likes, find_by_ap_url, ap_url as &str); find_by!(likes, find_by_user_on_post, user_id as i32, post_id as i32); - pub fn to_activity(&self, conn: &Connection) -> Result { - let mut act = activity::Like::default(); - act.like_props - .set_actor_link(User::get(conn, self.user_id)?.into_id())?; - act.like_props - .set_object_link(Post::get(conn, self.post_id)?.into_id())?; - 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, - )])?; - act.object_props.set_id_string(self.ap_url.clone())?; - - Ok(act) - } - pub fn to_activity07(&self, conn: &Connection) -> Result { let mut act = Like07::new( User::get(conn, self.user_id)?.ap_url.parse::()?, @@ -87,8 +70,8 @@ impl Like { Ok(()) } - pub fn build_undo07(&self, conn: &Connection) -> Result { - let mut act = Undo07::new( + pub fn build_undo07(&self, conn: &Connection) -> Result { + let mut act = Undo::new( User::get(conn, self.user_id)?.ap_url.parse::()?, AnyBase::from_extended(self.to_activity07(conn)?)?, ); @@ -171,7 +154,7 @@ impl FromId for Like { } } -impl AsObject for Like { +impl AsObject for Like { type Error = Error; type Output = (); @@ -209,30 +192,6 @@ mod tests { use assert_json_diff::assert_json_eq; use serde_json::{json, to_value}; - #[test] - fn to_activity() { - let conn = db(); - conn.test_transaction::<_, Error, _>(|| { - let (posts, _users, _blogs) = fill_database(&conn); - let post = &posts[0]; - let user = &post.get_authors(&conn)?[0]; - let like = Like::insert(&*conn, NewLike::new(post, user))?; - let act = like.to_activity(&conn).unwrap(); - - let expected = json!({ - "actor": "https://plu.me/@/admin/", - "cc": ["https://plu.me/@/admin/followers"], - "id": "https://plu.me/@/admin/like/https://plu.me/~/BlogName/testing", - "object": "https://plu.me/~/BlogName/testing", - "to": ["https://www.w3.org/ns/activitystreams#Public"], - "type": "Like", - }); - assert_json_eq!(to_value(act)?, expected); - - Ok(()) - }); - } - #[test] fn to_activity07() { let conn = db(); diff --git a/src/routes/likes.rs b/src/routes/likes.rs index cda2dbfd..6ef57225 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -3,7 +3,7 @@ use rocket_i18n::I18n; use crate::routes::errors::ErrorPage; use crate::utils::requires_login; -use plume_common::activity_pub::{broadcast, broadcast07}; +use plume_common::activity_pub::broadcast07; use plume_models::{ blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User, Error, PlumeRocket, CONFIG, @@ -27,10 +27,10 @@ pub fn create( Timeline::add_to_all_timelines(&conn, &post, Kind::Like(&user))?; let dest = User::one_by_instance(&*conn)?; - let act = like.to_activity(&*conn)?; + let act = like.to_activity07(&*conn)?; rockets .worker - .execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned())); + .execute(move || broadcast07(&user, act, dest, CONFIG.proxy().cloned())); } else { let like = likes::Like::find_by_user_on_post(&conn, user.id, post.id)?; let delete_act = like.build_undo07(&conn)?;