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

This commit is contained in:
Kitaiti Makoto 2022-05-03 00:47:46 +09:00
parent f8a0dff526
commit 595fa05660
2 changed files with 12 additions and 52 deletions

View File

@ -2,9 +2,8 @@ use crate::{
ap_url, db_conn::DbConn, instance::Instance, notifications::*, schema::follows, users::User, ap_url, db_conn::DbConn, instance::Instance, notifications::*, schema::follows, users::User,
Connection, Error, Result, CONFIG, Connection, Error, Result, CONFIG,
}; };
use activitypub::activity::Follow as FollowAct;
use activitystreams::{ use activitystreams::{
activity::{Accept, ActorAndObjectRef, Follow as FollowAct07, Undo}, activity::{Accept, ActorAndObjectRef, Follow as FollowAct, Undo},
base::AnyBase, base::AnyBase,
iri_string::types::IriString, iri_string::types::IriString,
prelude::*, prelude::*,
@ -56,27 +55,12 @@ impl Follow {
.map_err(Error::from) .map_err(Error::from)
} }
pub fn to_activity(&self, conn: &Connection) -> Result<FollowAct> { pub fn to_activity07(&self, conn: &Connection) -> Result<FollowAct> {
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())])?;
Ok(act)
}
pub fn to_activity07(&self, conn: &Connection) -> Result<FollowAct07> {
let user = User::get(conn, self.follower_id)?; let user = User::get(conn, self.follower_id)?;
let target = User::get(conn, self.following_id)?; let target = User::get(conn, self.following_id)?;
let target_id = target.ap_url.parse::<IriString>()?; let target_id = target.ap_url.parse::<IriString>()?;
let mut act = FollowAct07::new(user.ap_url.parse::<IriString>()?, target_id.clone()); let mut act = FollowAct::new(user.ap_url.parse::<IriString>()?, target_id.clone());
act.set_id(self.ap_url.parse::<IriString>()?); act.set_id(self.ap_url.parse::<IriString>()?);
act.set_many_tos(vec![target_id]); act.set_many_tos(vec![target_id]);
act.set_many_ccs(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]); act.set_many_ccs(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]);
@ -104,7 +88,7 @@ impl Follow {
conn: &Connection, conn: &Connection,
from: &B, from: &B,
target: &A, target: &A,
follow: FollowAct07, follow: FollowAct,
from_id: i32, from_id: i32,
target_id: i32, target_id: i32,
) -> Result<Follow> { ) -> Result<Follow> {
@ -135,7 +119,7 @@ impl Follow {
&self, &self,
from: &B, from: &B,
target: &A, target: &A,
follow: FollowAct07, follow: FollowAct,
) -> Result<Accept> { ) -> Result<Accept> {
let mut accept = Accept::new( let mut accept = Accept::new(
target.clone().into_id().parse::<IriString>()?, target.clone().into_id().parse::<IriString>()?,
@ -170,7 +154,7 @@ impl Follow {
} }
} }
impl AsObject<User, FollowAct07, &DbConn> for User { impl AsObject<User, FollowAct, &DbConn> for User {
type Error = Error; type Error = Error;
type Output = Follow; type Output = Follow;
@ -178,7 +162,7 @@ impl AsObject<User, FollowAct07, &DbConn> for User {
// Mastodon (at least) requires the full Follow object when accepting it, // Mastodon (at least) requires the full Follow object when accepting it,
// so we rebuilt it here // so we rebuilt it here
let mut follow = let mut follow =
FollowAct07::new(id.parse::<IriString>()?, actor.ap_url.parse::<IriString>()?); FollowAct::new(id.parse::<IriString>()?, actor.ap_url.parse::<IriString>()?);
follow.set_id(id.parse::<IriString>()?); follow.set_id(id.parse::<IriString>()?);
Follow::accept_follow07(conn, &actor, &self, follow, actor.id, self.id) Follow::accept_follow07(conn, &actor, &self, follow, actor.id, self.id)
} }
@ -186,13 +170,13 @@ impl AsObject<User, FollowAct07, &DbConn> for User {
impl FromId<DbConn> for Follow { impl FromId<DbConn> for Follow {
type Error = Error; type Error = Error;
type Object = FollowAct07; type Object = FollowAct;
fn from_db07(conn: &DbConn, id: &str) -> Result<Self> { fn from_db07(conn: &DbConn, id: &str) -> Result<Self> {
Follow::find_by_ap_url(conn, id) Follow::find_by_ap_url(conn, id)
} }
fn from_activity07(conn: &DbConn, follow: FollowAct07) -> Result<Self> { fn from_activity07(conn: &DbConn, follow: FollowAct) -> Result<Self> {
let actor = User::from_id( let actor = User::from_id(
conn, conn,
follow follow
@ -312,28 +296,6 @@ mod tests {
}) })
} }
#[test]
fn to_activity() {
let conn = db();
conn.test_transaction::<_, Error, _>(|| {
let (follow, _following, _follower, _users) = prepare_activity(&conn);
let act = follow.to_activity(&conn)?;
let expected = json!({
"actor": "https://plu.me/@/other/",
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
"id": format!("https://plu.me/follows/{}", follow.id),
"object": "https://plu.me/@/user/",
"to": ["https://plu.me/@/user/"],
"type": "Follow"
});
assert_json_eq!(to_value(act)?, expected);
Ok(())
});
}
#[test] #[test]
fn to_activity07() { fn to_activity07() {
let conn = db(); let conn = db();

View File

@ -18,9 +18,7 @@ use crate::routes::{
}; };
use crate::template_utils::{IntoContext, Ructe}; use crate::template_utils::{IntoContext, Ructe};
use crate::utils::requires_login; use crate::utils::requires_login;
use plume_common::activity_pub::{ use plume_common::activity_pub::{broadcast07, ActivityStream, ApRequest, CustomPerson, Id};
broadcast, broadcast07, ActivityStream, ApRequest, CustomPerson, Id,
};
use plume_common::utils::md_to_html; use plume_common::utils::md_to_html;
use plume_models::{ use plume_models::{
blogs::Blog, blogs::Blog,
@ -127,11 +125,11 @@ pub fn follow(
)?; )?;
f.notify(&conn)?; f.notify(&conn)?;
let act = f.to_activity(&conn)?; let act = f.to_activity07(&conn)?;
let msg = i18n!(rockets.intl.catalog, "You are now following {}."; target.name()); let msg = i18n!(rockets.intl.catalog, "You are now following {}."; target.name());
rockets rockets
.worker .worker
.execute(move || broadcast(&user, act, vec![target], CONFIG.proxy().cloned())); .execute(move || broadcast07(&user, act, vec![target], CONFIG.proxy().cloned()));
msg msg
}; };
Ok(Flash::success( Ok(Flash::success(