2 activities::send::generate_activity_id,
3 activity_queue::{send_activity_single_dest, send_to_community_followers},
4 check_is_apub_id_valid,
5 extensions::context::lemmy_context,
6 fetcher::user::get_or_fetch_and_upsert_user,
11 kind::{AcceptType, AnnounceType, DeleteType, LikeType, RemoveType, UndoType},
20 base::{AnyBase, BaseExt, ExtendsExt},
25 use itertools::Itertools;
26 use lemmy_api_structs::blocking;
27 use lemmy_db_queries::DbPool;
28 use lemmy_db_schema::source::community::Community;
29 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
30 use lemmy_utils::{location_info, LemmyError};
31 use lemmy_websocket::LemmyContext;
34 #[async_trait::async_trait(?Send)]
35 impl ActorType for Community {
36 fn is_local(&self) -> bool {
39 fn actor_id(&self) -> Url {
40 self.actor_id.to_owned().into_inner()
42 fn public_key(&self) -> Option<String> {
43 self.public_key.to_owned()
45 fn private_key(&self) -> Option<String> {
46 self.private_key.to_owned()
49 fn get_shared_inbox_or_inbox_url(&self) -> Url {
53 .unwrap_or_else(|| self.inbox_url.to_owned())
59 _follow_actor_id: &Url,
60 _context: &LemmyContext,
61 ) -> Result<(), LemmyError> {
65 async fn send_unfollow(
67 _follow_actor_id: &Url,
68 _context: &LemmyContext,
69 ) -> Result<(), LemmyError> {
73 /// As a local community, accept the follow request from a remote user.
74 async fn send_accept_follow(
77 context: &LemmyContext,
78 ) -> Result<(), LemmyError> {
79 let actor_uri = follow
81 .as_single_xsd_any_uri()
82 .context(location_info!())?;
83 let user = get_or_fetch_and_upsert_user(actor_uri, context, &mut 0).await?;
85 let mut accept = Accept::new(
86 self.actor_id.to_owned().into_inner(),
87 follow.into_any_base()?,
90 .set_many_contexts(lemmy_context()?)
91 .set_id(generate_activity_id(AcceptType::Accept)?)
92 .set_to(user.actor_id());
94 send_activity_single_dest(accept, self, user.inbox_url.into(), context).await?;
98 /// If the creator of a community deletes the community, send this to all followers.
99 async fn send_delete(&self, context: &LemmyContext) -> Result<(), LemmyError> {
100 let mut delete = Delete::new(self.actor_id(), self.actor_id());
102 .set_many_contexts(lemmy_context()?)
103 .set_id(generate_activity_id(DeleteType::Delete)?)
105 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
107 send_to_community_followers(delete, self, context).await?;
111 /// If the creator of a community reverts the deletion of a community, send this to all followers.
112 async fn send_undo_delete(&self, context: &LemmyContext) -> Result<(), LemmyError> {
113 let mut delete = Delete::new(self.actor_id(), self.actor_id());
115 .set_many_contexts(lemmy_context()?)
116 .set_id(generate_activity_id(DeleteType::Delete)?)
118 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
120 let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?);
122 .set_many_contexts(lemmy_context()?)
123 .set_id(generate_activity_id(UndoType::Undo)?)
125 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
127 send_to_community_followers(undo, self, context).await?;
131 /// If an admin removes a community, send this to all followers.
132 async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
133 let mut remove = Remove::new(self.actor_id(), self.actor_id());
135 .set_many_contexts(lemmy_context()?)
136 .set_id(generate_activity_id(RemoveType::Remove)?)
138 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
140 send_to_community_followers(remove, self, context).await?;
144 /// If an admin reverts the removal of a community, send this to all followers.
145 async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
146 let mut remove = Remove::new(self.actor_id(), self.actor_id());
148 .set_many_contexts(lemmy_context()?)
149 .set_id(generate_activity_id(RemoveType::Remove)?)
151 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
153 // Undo that fake activity
154 let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?);
156 .set_many_contexts(lemmy_context()?)
157 .set_id(generate_activity_id(LikeType::Like)?)
159 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
161 send_to_community_followers(undo, self, context).await?;
165 /// Wraps an activity sent to the community in an announce, and then sends the announce to all
166 /// community followers.
167 async fn send_announce(
170 context: &LemmyContext,
171 ) -> Result<(), LemmyError> {
172 let mut announce = Announce::new(self.actor_id.to_owned().into_inner(), activity);
174 .set_many_contexts(lemmy_context()?)
175 .set_id(generate_activity_id(AnnounceType::Announce)?)
177 .set_many_ccs(vec![self.followers_url.clone().into_inner()]);
179 send_to_community_followers(announce, self, context).await?;
184 /// For a given community, returns the inboxes of all followers.
185 async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
188 let follows = blocking(pool, move |conn| {
189 CommunityFollowerView::for_community(conn, id)
192 let inboxes = follows
194 .filter(|f| !f.follower.local)
195 .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
196 .map(|i| i.into_inner())
198 // Don't send to blocked instances
199 .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())