@ -5,9 +5,10 @@ use crate::{
verify_person ,
verify_person_in_community ,
} ,
fetcher ::user_or_community ::UserOrCommunity ,
local_instance ,
objects ::{ community ::ApubCommunity , person ::ApubPerson } ,
protocol ::activities ::following ::{ accept ::AcceptFollow Community , follow ::Follow Community } ,
protocol ::activities ::following ::{ accept ::AcceptFollow , follow ::Follow } ,
ActorType ,
} ;
use activitypub_federation ::{
@ -17,20 +18,23 @@ use activitypub_federation::{
} ;
use activitystreams_kinds ::activity ::FollowType ;
use lemmy_db_schema ::{
source ::community ::{ CommunityFollower , CommunityFollowerForm } ,
source ::{
community ::{ CommunityFollower , CommunityFollowerForm } ,
person ::{ PersonFollower , PersonFollowerForm } ,
} ,
traits ::Followable ,
} ;
use lemmy_utils ::error ::LemmyError ;
use lemmy_websocket ::LemmyContext ;
use url ::Url ;
impl Follow Community {
impl Follow {
pub ( in crate ::activities ::following ) fn new (
actor : & ApubPerson ,
community : & ApubCommunity ,
context : & LemmyContext ,
) -> Result < Follow Community , LemmyError > {
Ok ( Follow Community {
) -> Result < Follow , LemmyError > {
Ok ( Follow {
actor : ObjectId ::new ( actor . actor_id ( ) ) ,
object : ObjectId ::new ( community . actor_id ( ) ) ,
kind : FollowType ::Follow ,
@ -56,14 +60,14 @@ impl FollowCommunity {
. await
. ok ( ) ;
let follow = Follow Community ::new ( actor , community , context ) ? ;
let follow = Follow ::new ( actor , community , context ) ? ;
let inbox = vec! [ community . shared_inbox_or_inbox ( ) ] ;
send_lemmy_activity ( context , follow , actor , inbox , true ) . await
}
}
#[ async_trait::async_trait(?Send) ]
impl ActivityHandler for Follow Community {
impl ActivityHandler for Follow {
type DataType = LemmyContext ;
type Error = LemmyError ;
@ -82,11 +86,13 @@ impl ActivityHandler for FollowCommunity {
request_counter : & mut i32 ,
) -> Result < ( ) , LemmyError > {
verify_person ( & self . actor , context , request_counter ) . await ? ;
let community = self
let object = self
. object
. dereference ( context , local_instance ( context ) . await , request_counter )
. await ? ;
verify_person_in_community ( & self . actor , & community , context , request_counter ) . await ? ;
if let UserOrCommunity ::Community ( c ) = object {
verify_person_in_community ( & self . actor , & c , context , request_counter ) . await ? ;
}
Ok ( ( ) )
}
@ -96,25 +102,33 @@ impl ActivityHandler for FollowCommunity {
context : & Data < LemmyContext > ,
request_counter : & mut i32 ,
) -> Result < ( ) , LemmyError > {
let person = self
let actor = self
. actor
. dereference ( context , local_instance ( context ) . await , request_counter )
. await ? ;
let community = self
let object = self
. object
. dereference ( context , local_instance ( context ) . await , request_counter )
. await ? ;
let community_follower_form = CommunityFollowerForm {
community_id : community . id ,
person_id : person . id ,
pending : false ,
} ;
// This will fail if they're already a follower, but ignore the error.
CommunityFollower ::follow ( context . pool ( ) , & community_follower_form )
. await
. ok ( ) ;
match object {
UserOrCommunity ::User ( u ) = > {
let form = PersonFollowerForm {
person_id : u . id ,
follower_id : actor . id ,
pending : false ,
} ;
PersonFollower ::follow ( context . pool ( ) , & form ) . await ? ;
}
UserOrCommunity ::Community ( c ) = > {
let form = CommunityFollowerForm {
community_id : c . id ,
person_id : actor . id ,
pending : false ,
} ;
CommunityFollower ::follow ( context . pool ( ) , & form ) . await ? ;
}
}
AcceptFollowCommunity ::send ( self , context , request_counter ) . await
AcceptFollow ::send ( self , context , request_counter ) . await
}
}