2 activities::send::generate_activity_id,
3 activity_queue::send_activity_single_dest,
4 extensions::context::lemmy_context,
10 kind::{FollowType, UndoType},
14 base::{BaseExt, ExtendsExt},
17 use lemmy_api_common::blocking;
18 use lemmy_db_queries::{ApubObject, Followable};
19 use lemmy_db_schema::source::{
20 community::{Community, CommunityFollower, CommunityFollowerForm},
23 use lemmy_utils::LemmyError;
24 use lemmy_websocket::LemmyContext;
27 #[async_trait::async_trait(?Send)]
28 impl ActorType for Person {
29 fn is_local(&self) -> bool {
32 fn actor_id(&self) -> Url {
33 self.actor_id.to_owned().into_inner()
36 fn public_key(&self) -> Option<String> {
37 self.public_key.to_owned()
40 fn private_key(&self) -> Option<String> {
41 self.private_key.to_owned()
44 fn get_shared_inbox_or_inbox_url(&self) -> Url {
48 .unwrap_or_else(|| self.inbox_url.to_owned())
53 #[async_trait::async_trait(?Send)]
54 impl UserType for Person {
55 /// As a given local person, send out a follow request to a remote community.
58 follow_actor_id: &Url,
59 context: &LemmyContext,
60 ) -> Result<(), LemmyError> {
61 let follow_actor_id = follow_actor_id.to_owned();
62 let community = blocking(context.pool(), move |conn| {
63 Community::read_from_apub_id(conn, &follow_actor_id.into())
67 let community_follower_form = CommunityFollowerForm {
68 community_id: community.id,
72 blocking(&context.pool(), move |conn| {
73 CommunityFollower::follow(conn, &community_follower_form).ok()
77 let mut follow = Follow::new(self.actor_id(), community.actor_id());
79 .set_many_contexts(lemmy_context()?)
80 .set_id(generate_activity_id(FollowType::Follow)?)
81 .set_to(community.actor_id());
83 send_activity_single_dest(follow, self, community.inbox_url.into(), context).await?;
87 async fn send_unfollow(
89 follow_actor_id: &Url,
90 context: &LemmyContext,
91 ) -> Result<(), LemmyError> {
92 let follow_actor_id = follow_actor_id.to_owned();
93 let community = blocking(context.pool(), move |conn| {
94 Community::read_from_apub_id(conn, &follow_actor_id.into())
98 let mut follow = Follow::new(self.actor_id(), community.actor_id());
100 .set_many_contexts(lemmy_context()?)
101 .set_id(generate_activity_id(FollowType::Follow)?)
102 .set_to(community.actor_id());
104 // Undo that fake activity
105 let mut undo = Undo::new(
106 self.actor_id.to_owned().into_inner(),
107 follow.into_any_base()?,
110 .set_many_contexts(lemmy_context()?)
111 .set_id(generate_activity_id(UndoType::Undo)?)
112 .set_to(community.actor_id());
114 send_activity_single_dest(undo, self, community.inbox_url.into(), context).await?;