2 activities::generate_activity_id,
3 activity_queue::{send_to_community, send_to_community_followers},
4 check_is_apub_id_valid,
5 extensions::context::lemmy_context,
6 fetcher::get_or_fetch_and_upsert_actor,
7 generate_moderators_url,
13 use activitystreams::{
34 base::{AnyBase, BaseExt, ExtendsExt},
39 use itertools::Itertools;
40 use lemmy_api_common::blocking;
41 use lemmy_db_queries::DbPool;
42 use lemmy_db_schema::source::{community::Community, person::Person};
43 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
44 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
45 use lemmy_websocket::LemmyContext;
48 impl ActorType for Community {
49 fn is_local(&self) -> bool {
52 fn actor_id(&self) -> Url {
53 self.actor_id.to_owned().into()
55 fn name(&self) -> String {
58 fn public_key(&self) -> Option<String> {
59 self.public_key.to_owned()
61 fn private_key(&self) -> Option<String> {
62 self.private_key.to_owned()
65 fn get_shared_inbox_or_inbox_url(&self) -> Url {
69 .unwrap_or_else(|| self.inbox_url.to_owned())
74 #[async_trait::async_trait(?Send)]
75 impl CommunityType for Community {
76 fn followers_url(&self) -> Url {
77 self.followers_url.clone().into()
80 /// If a remote community is updated by a local mod, send the updated info to the community's
82 async fn send_update(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
84 // Do nothing, other instances will automatically refetch the community
86 let mut update = Update::new(
88 AnyBase::from_arbitrary_json(self.to_apub(context.pool()).await?)?,
91 .set_many_contexts(lemmy_context())
92 .set_id(generate_activity_id(UpdateType::Update)?)
94 .set_many_ccs(vec![self.actor_id()]);
95 send_to_community(update, &mod_, self, None, context).await?;
100 /// If the creator of a community deletes the community, send this to all followers.
102 /// We need to handle deletion by a remote mod separately.
103 async fn send_delete(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
104 // Local mod, send directly from community to followers
106 let mut delete = Delete::new(self.actor_id(), self.actor_id());
108 .set_many_contexts(lemmy_context())
109 .set_id(generate_activity_id(DeleteType::Delete)?)
111 .set_many_ccs(vec![self.followers_url()]);
113 send_to_community_followers(delete, self, None, context).await?;
115 // Remote mod, send from mod to community
117 let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
119 .set_many_contexts(lemmy_context())
120 .set_id(generate_activity_id(DeleteType::Delete)?)
122 .set_many_ccs(vec![self.actor_id()]);
124 send_to_community(delete, &mod_, self, None, context).await?;
129 /// If the creator of a community reverts the deletion of a community, send this to all followers.
131 /// We need to handle undelete by a remote mod separately.
132 async fn send_undo_delete(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
133 // Local mod, send directly from community to followers
135 let mut delete = Delete::new(self.actor_id(), self.actor_id());
137 .set_many_contexts(lemmy_context())
138 .set_id(generate_activity_id(DeleteType::Delete)?)
140 .set_many_ccs(vec![self.followers_url()]);
142 let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?);
144 .set_many_contexts(lemmy_context())
145 .set_id(generate_activity_id(UndoType::Undo)?)
147 .set_many_ccs(vec![self.followers_url()]);
149 send_to_community_followers(undo, self, None, context).await?;
151 // Remote mod, send from mod to community
153 let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
155 .set_many_contexts(lemmy_context())
156 .set_id(generate_activity_id(DeleteType::Delete)?)
158 .set_many_ccs(vec![self.actor_id()]);
160 let mut undo = Undo::new(mod_.actor_id(), delete.into_any_base()?);
162 .set_many_contexts(lemmy_context())
163 .set_id(generate_activity_id(UndoType::Undo)?)
165 .set_many_ccs(vec![self.actor_id()]);
167 send_to_community(undo, &mod_, self, None, context).await?;
172 /// If an admin removes a community, send this to all followers.
173 async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
174 let mut remove = Remove::new(self.actor_id(), self.actor_id());
176 .set_many_contexts(lemmy_context())
177 .set_id(generate_activity_id(RemoveType::Remove)?)
179 .set_many_ccs(vec![self.followers_url()]);
181 send_to_community_followers(remove, self, None, context).await?;
185 /// If an admin reverts the removal of a community, send this to all followers.
186 async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
187 let mut remove = Remove::new(self.actor_id(), self.actor_id());
189 .set_many_contexts(lemmy_context())
190 .set_id(generate_activity_id(RemoveType::Remove)?)
192 .set_many_ccs(vec![self.followers_url()]);
194 // Undo that fake activity
195 let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?);
197 .set_many_contexts(lemmy_context())
198 .set_id(generate_activity_id(LikeType::Like)?)
200 .set_many_ccs(vec![self.followers_url()]);
202 send_to_community_followers(undo, self, None, context).await?;
206 /// Wraps an activity sent to the community in an announce, and then sends the announce to all
207 /// community followers.
209 /// If we are announcing a local activity, it hasn't been stored in the database yet, and we need
210 /// to do it here, so that it can be fetched by ID. Remote activities are inserted into DB in the
213 /// If the `object` of the announced activity is an actor, the actor ID needs to be passed as
214 /// `object_actor`, so that the announce can be delivered to that user.
215 async fn send_announce(
218 object_actor: Option<Url>,
219 context: &LemmyContext,
220 ) -> Result<(), LemmyError> {
221 let inner_id = activity.id().context(location_info!())?;
222 if inner_id.domain() == Some(&Settings::get().get_hostname_without_port()?) {
223 insert_activity(inner_id, activity.clone(), true, false, context.pool()).await?;
226 let mut ccs = vec![self.followers_url()];
227 let mut object_actor_inbox: Option<Url> = None;
228 if let Some(actor_id) = object_actor {
229 // Ignore errors, maybe its not actually an actor
230 // TODO: should pass the actual request counter in, but that seems complicated
231 let actor = get_or_fetch_and_upsert_actor(&actor_id, context, &mut 0)
234 if let Some(actor) = actor {
236 object_actor_inbox = Some(actor.get_shared_inbox_or_inbox_url());
239 let mut announce = Announce::new(self.actor_id(), activity);
241 .set_many_contexts(lemmy_context())
242 .set_id(generate_activity_id(AnnounceType::Announce)?)
246 send_to_community_followers(announce, self, object_actor_inbox, context).await?;
251 /// For a given community, returns the inboxes of all followers.
252 async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
255 let follows = blocking(pool, move |conn| {
256 CommunityFollowerView::for_community(conn, id)
259 let inboxes = follows
261 .filter(|f| !f.follower.local)
262 .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
263 .map(|i| i.into_inner())
265 // Don't send to blocked instances
266 .filter(|inbox| check_is_apub_id_valid(inbox, false).is_ok())
272 async fn send_add_mod(
276 context: &LemmyContext,
277 ) -> Result<(), LemmyError> {
278 let mut add = Add::new(actor.actor_id(), added_mod.actor_id());
280 .set_many_contexts(lemmy_context())
281 .set_id(generate_activity_id(AddType::Add)?)
283 .set_many_ccs(vec![self.actor_id()])
284 .set_target(generate_moderators_url(&self.actor_id)?.into_inner());
286 send_to_community(add, actor, self, Some(added_mod.actor_id()), context).await?;
290 async fn send_remove_mod(
294 context: &LemmyContext,
295 ) -> Result<(), LemmyError> {
296 let mut remove = Remove::new(actor.actor_id(), removed_mod.actor_id());
298 .set_many_contexts(lemmy_context())
299 .set_id(generate_activity_id(RemoveType::Remove)?)
301 .set_many_ccs(vec![self.actor_id()])
302 .set_target(generate_moderators_url(&self.actor_id)?.into_inner());
304 send_to_community(remove, actor, self, Some(removed_mod.actor_id()), context).await?;
308 async fn send_block_user(
311 blocked_user: Person,
312 context: &LemmyContext,
313 ) -> Result<(), LemmyError> {
314 let mut block = Block::new(actor.actor_id(), blocked_user.actor_id());
316 .set_many_contexts(lemmy_context())
317 .set_id(generate_activity_id(BlockType::Block)?)
319 .set_many_ccs(vec![self.actor_id()]);
321 send_to_community(block, actor, self, Some(blocked_user.actor_id()), context).await?;
325 async fn send_undo_block_user(
328 unblocked_user: Person,
329 context: &LemmyContext,
330 ) -> Result<(), LemmyError> {
331 let mut block = Block::new(actor.actor_id(), unblocked_user.actor_id());
333 .set_many_contexts(lemmy_context())
334 .set_id(generate_activity_id(BlockType::Block)?)
336 .set_many_ccs(vec![self.actor_id()]);
338 // Undo that fake activity
339 let mut undo = Undo::new(actor.actor_id(), block.into_any_base()?);
341 .set_many_contexts(lemmy_context())
342 .set_id(generate_activity_id(UndoType::Undo)?)
344 .set_many_ccs(vec![self.actor_id()]);
346 send_to_community(undo, actor, self, Some(unblocked_user.actor_id()), context).await?;