Big refactoring of the Inbox (#443)
* Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
This commit is contained in:
+95
-72
@@ -1,20 +1,16 @@
|
||||
use activitypub::{
|
||||
activity::{Accept, Follow as FollowAct, Undo},
|
||||
actor::Person,
|
||||
};
|
||||
use activitypub::activity::{Accept, Follow as FollowAct, Undo};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
|
||||
use blogs::Blog;
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::{
|
||||
broadcast,
|
||||
inbox::{Deletable, FromActivity, Notify, WithInbox},
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
sign::Signer,
|
||||
Id, IntoId,
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
};
|
||||
use schema::follows;
|
||||
use users::User;
|
||||
use {ap_url, Connection, Error, Result, CONFIG};
|
||||
use {ap_url, Connection, Error, PlumeRocket, Result, CONFIG};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
|
||||
#[belongs_to(User, foreign_key = "following_id")]
|
||||
@@ -65,14 +61,26 @@ impl Follow {
|
||||
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(target.into_id())?;
|
||||
act.object_props.set_cc_link_vec::<Id>(vec![])?;
|
||||
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 notify(&self, conn: &Connection) -> Result<Notification> {
|
||||
Notification::insert(
|
||||
conn,
|
||||
NewNotification {
|
||||
kind: notification_kind::FOLLOW.to_string(),
|
||||
object_id: self.id,
|
||||
user_id: self.following_id,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// from -> The one sending the follow request
|
||||
/// target -> The target of the request, responding with Accept
|
||||
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + IntoId>(
|
||||
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
|
||||
conn: &Connection,
|
||||
from: &B,
|
||||
target: &A,
|
||||
@@ -88,6 +96,7 @@ impl Follow {
|
||||
ap_url: follow.object_props.id_string()?,
|
||||
},
|
||||
)?;
|
||||
res.notify(conn)?;
|
||||
|
||||
let mut accept = Accept::default();
|
||||
let accept_id = ap_url(&format!(
|
||||
@@ -96,8 +105,12 @@ impl Follow {
|
||||
&res.id
|
||||
));
|
||||
accept.object_props.set_id_string(accept_id)?;
|
||||
accept.object_props.set_to_link(from.clone().into_id())?;
|
||||
accept.object_props.set_cc_link_vec::<Id>(vec![])?;
|
||||
accept
|
||||
.object_props
|
||||
.set_to_link_vec(vec![from.clone().into_id()])?;
|
||||
accept
|
||||
.object_props
|
||||
.set_cc_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
|
||||
accept
|
||||
.accept_props
|
||||
.set_actor_link::<Id>(target.clone().into_id())?;
|
||||
@@ -105,61 +118,8 @@ impl Follow {
|
||||
broadcast(&*target, accept, vec![from.clone()]);
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromActivity<FollowAct, Connection> for Follow {
|
||||
type Error = Error;
|
||||
|
||||
fn from_activity(conn: &Connection, follow: FollowAct, _actor: Id) -> Result<Follow> {
|
||||
let from_id = follow
|
||||
.follow_props
|
||||
.actor_link::<Id>()
|
||||
.map(Into::into)
|
||||
.or_else(|_| {
|
||||
Ok(follow
|
||||
.follow_props
|
||||
.actor_object::<Person>()?
|
||||
.object_props
|
||||
.id_string()?) as Result<String>
|
||||
})?;
|
||||
let from = User::from_url(conn, &from_id)?;
|
||||
match User::from_url(conn, follow.follow_props.object.as_str()?) {
|
||||
Ok(user) => Follow::accept_follow(conn, &from, &user, follow, from.id, user.id),
|
||||
Err(_) => {
|
||||
let blog = Blog::from_url(conn, follow.follow_props.object.as_str()?)?;
|
||||
Follow::accept_follow(conn, &from, &blog, follow, from.id, blog.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Notify<Connection> for Follow {
|
||||
type Error = Error;
|
||||
|
||||
fn notify(&self, conn: &Connection) -> Result<()> {
|
||||
Notification::insert(
|
||||
conn,
|
||||
NewNotification {
|
||||
kind: notification_kind::FOLLOW.to_string(),
|
||||
object_id: self.id,
|
||||
user_id: self.following_id,
|
||||
},
|
||||
)
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
impl Deletable<Connection, Undo> for Follow {
|
||||
type Error = Error;
|
||||
|
||||
fn delete(&self, conn: &Connection) -> Result<Undo> {
|
||||
diesel::delete(self).execute(conn)?;
|
||||
|
||||
// delete associated notification if any
|
||||
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
||||
diesel::delete(¬if).execute(conn)?;
|
||||
}
|
||||
|
||||
pub fn build_undo(&self, conn: &Connection) -> Result<Undo> {
|
||||
let mut undo = Undo::default();
|
||||
undo.undo_props
|
||||
.set_actor_link(User::get(conn, self.follower_id)?.into_id())?;
|
||||
@@ -167,14 +127,77 @@ impl Deletable<Connection, Undo> for Follow {
|
||||
.set_id_string(format!("{}/undo", self.ap_url))?;
|
||||
undo.undo_props
|
||||
.set_object_link::<Id>(self.clone().into_id())?;
|
||||
undo.object_props
|
||||
.set_to_link_vec(vec![User::get(conn, self.following_id)?.into_id()])?;
|
||||
undo.object_props
|
||||
.set_cc_link_vec(vec![Id::new(PUBLIC_VISIBILITY.to_string())])?;
|
||||
Ok(undo)
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_id(id: &str, actor_id: &str, conn: &Connection) -> Result<Undo> {
|
||||
let follow = Follow::find_by_ap_url(conn, id)?;
|
||||
let user = User::find_by_ap_url(conn, actor_id)?;
|
||||
if user.id == follow.follower_id {
|
||||
follow.delete(conn)
|
||||
impl AsObject<User, FollowAct, &PlumeRocket> for User {
|
||||
type Error = Error;
|
||||
type Output = Follow;
|
||||
|
||||
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Follow> {
|
||||
// Mastodon (at least) requires the full Follow object when accepting it,
|
||||
// so we rebuilt it here
|
||||
let mut follow = FollowAct::default();
|
||||
follow.object_props.set_id_string(id.to_string())?;
|
||||
follow
|
||||
.follow_props
|
||||
.set_actor_link::<Id>(actor.clone().into_id())?;
|
||||
Follow::accept_follow(&c.conn, &actor, &self, follow, actor.id, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromId<PlumeRocket> for Follow {
|
||||
type Error = Error;
|
||||
type Object = FollowAct;
|
||||
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Follow::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &PlumeRocket, follow: FollowAct) -> Result<Self> {
|
||||
let actor = User::from_id(
|
||||
c,
|
||||
&{
|
||||
let res: String = follow.follow_props.actor_link::<Id>()?.into();
|
||||
res
|
||||
},
|
||||
None,
|
||||
)
|
||||
.map_err(|(_, e)| e)?;
|
||||
|
||||
let target = User::from_id(
|
||||
c,
|
||||
&{
|
||||
let res: String = follow.follow_props.object_link::<Id>()?.into();
|
||||
res
|
||||
},
|
||||
None,
|
||||
)
|
||||
.map_err(|(_, e)| e)?;
|
||||
Follow::accept_follow(&c.conn, &actor, &target, follow, actor.id, target.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Undo, &PlumeRocket> for Follow {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let conn = &*c.conn;
|
||||
if self.follower_id == actor.id {
|
||||
diesel::delete(&self).execute(conn)?;
|
||||
|
||||
// delete associated notification if any
|
||||
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
||||
diesel::delete(¬if).execute(conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::Unauthorized)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user