Use Like::to_activity07() instead of to_activity()

This commit is contained in:
Kitaiti Makoto 2022-05-02 23:37:58 +09:00
parent b13444895f
commit 6ab1ecd57b
2 changed files with 8 additions and 49 deletions

View file

@ -2,9 +2,8 @@ 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::{ use activitystreams::{
activity::{ActorAndObjectRef, Like as Like07, Undo as Undo07}, activity::{ActorAndObjectRef, Like as Like07, Undo},
base::AnyBase, base::AnyBase,
iri_string::types::IriString, iri_string::types::IriString,
prelude::*, prelude::*,
@ -14,7 +13,7 @@ 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, PUBLIC_VISIBILITY,
}; };
#[derive(Clone, Queryable, Identifiable)] #[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_ap_url, ap_url as &str);
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> {
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<Like07> { pub fn to_activity07(&self, conn: &Connection) -> Result<Like07> {
let mut act = Like07::new( let mut act = Like07::new(
User::get(conn, self.user_id)?.ap_url.parse::<IriString>()?, User::get(conn, self.user_id)?.ap_url.parse::<IriString>()?,
@ -87,8 +70,8 @@ impl Like {
Ok(()) Ok(())
} }
pub fn build_undo07(&self, conn: &Connection) -> Result<Undo07> { pub fn build_undo07(&self, conn: &Connection) -> Result<Undo> {
let mut act = Undo07::new( let mut act = Undo::new(
User::get(conn, self.user_id)?.ap_url.parse::<IriString>()?, User::get(conn, self.user_id)?.ap_url.parse::<IriString>()?,
AnyBase::from_extended(self.to_activity07(conn)?)?, AnyBase::from_extended(self.to_activity07(conn)?)?,
); );
@ -171,7 +154,7 @@ impl FromId<DbConn> for Like {
} }
} }
impl AsObject<User, Undo07, &DbConn> for Like { impl AsObject<User, Undo, &DbConn> for Like {
type Error = Error; type Error = Error;
type Output = (); type Output = ();
@ -209,30 +192,6 @@ mod tests {
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use serde_json::{json, to_value}; 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] #[test]
fn to_activity07() { fn to_activity07() {
let conn = db(); let conn = db();

View file

@ -3,7 +3,7 @@ use rocket_i18n::I18n;
use crate::routes::errors::ErrorPage; use crate::routes::errors::ErrorPage;
use crate::utils::requires_login; use crate::utils::requires_login;
use plume_common::activity_pub::{broadcast, broadcast07}; use plume_common::activity_pub::broadcast07;
use plume_models::{ use plume_models::{
blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User, blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User,
Error, PlumeRocket, CONFIG, Error, PlumeRocket, CONFIG,
@ -27,10 +27,10 @@ pub fn create(
Timeline::add_to_all_timelines(&conn, &post, Kind::Like(&user))?; Timeline::add_to_all_timelines(&conn, &post, Kind::Like(&user))?;
let dest = User::one_by_instance(&*conn)?; let dest = User::one_by_instance(&*conn)?;
let act = like.to_activity(&*conn)?; let act = like.to_activity07(&*conn)?;
rockets rockets
.worker .worker
.execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned())); .execute(move || broadcast07(&user, act, dest, CONFIG.proxy().cloned()));
} else { } else {
let like = likes::Like::find_by_user_on_post(&conn, user.id, post.id)?; let like = likes::Like::find_by_user_on_post(&conn, user.id, post.id)?;
let delete_act = like.build_undo07(&conn)?; let delete_act = like.build_undo07(&conn)?;