2 activities::send::generate_activity_id,
3 activity_queue::{send_activity_single_dest, 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, person::get_or_fetch_and_upsert_person},
7 generate_moderators_url,
13 use activitystreams::{
38 base::{AnyBase, BaseExt, ExtendsExt},
43 use itertools::Itertools;
44 use lemmy_api_common::blocking;
45 use lemmy_db_queries::DbPool;
46 use lemmy_db_schema::source::{community::Community, person::Person};
47 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
48 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
49 use lemmy_websocket::LemmyContext;
52 #[async_trait::async_trait(?Send)]
53 impl ActorType for Community {
54 fn is_local(&self) -> bool {
57 fn actor_id(&self) -> Url {
58 self.actor_id.to_owned().into_inner()
60 fn public_key(&self) -> Option<String> {
61 self.public_key.to_owned()
63 fn private_key(&self) -> Option<String> {
64 self.private_key.to_owned()
67 fn get_shared_inbox_or_inbox_url(&self) -> Url {
71 .unwrap_or_else(|| self.inbox_url.to_owned())
76 #[async_trait::async_trait(?Send)]
77 impl CommunityType for Community {
78 fn followers_url(&self) -> Url {
79 self.followers_url.clone().into_inner()
82 /// As a local community, accept the follow request from a remote person.
83 async fn send_accept_follow(
86 context: &LemmyContext,
87 ) -> Result<(), LemmyError> {
88 let actor_uri = follow
90 .as_single_xsd_any_uri()
91 .context(location_info!())?;
92 let person = get_or_fetch_and_upsert_person(actor_uri, context, &mut 0).await?;
94 let mut accept = Accept::new(
95 self.actor_id.to_owned().into_inner(),
96 follow.into_any_base()?,
99 .set_many_contexts(lemmy_context()?)
100 .set_id(generate_activity_id(AcceptType::Accept)?)
101 .set_to(person.actor_id());
103 send_activity_single_dest(accept, self, person.inbox_url.into(), context).await?;
107 /// If a remote community is updated by a local mod, send the updated info to the community's
109 async fn send_update(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
111 // Do nothing, other instances will automatically refetch the community
113 let mut update = Update::new(
115 self.to_apub(context.pool()).await?.into_any_base()?,
118 .set_many_contexts(lemmy_context()?)
119 .set_id(generate_activity_id(UpdateType::Update)?)
121 .set_many_ccs(vec![self.actor_id()]);
122 send_to_community(update, &mod_, self, None, context).await?;
127 /// If the creator of a community deletes the community, send this to all followers.
129 /// We need to handle deletion by a remote mod separately.
130 async fn send_delete(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
131 // Local mod, send directly from community to followers
133 let mut delete = Delete::new(self.actor_id(), self.actor_id());
135 .set_many_contexts(lemmy_context()?)
136 .set_id(generate_activity_id(DeleteType::Delete)?)
138 .set_many_ccs(vec![self.followers_url()]);
140 send_to_community_followers(delete, self, None, context).await?;
142 // Remote mod, send from mod to community
144 let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
146 .set_many_contexts(lemmy_context()?)
147 .set_id(generate_activity_id(DeleteType::Delete)?)
149 .set_many_ccs(vec![self.actor_id()]);
151 send_to_community(delete, &mod_, self, None, context).await?;
156 /// If the creator of a community reverts the deletion of a community, send this to all followers.
158 /// We need to handle undelete by a remote mod separately.
159 async fn send_undo_delete(&self, mod_: Person, context: &LemmyContext) -> Result<(), LemmyError> {
160 // Local mod, send directly from community to followers
162 let mut delete = Delete::new(self.actor_id(), self.actor_id());
164 .set_many_contexts(lemmy_context()?)
165 .set_id(generate_activity_id(DeleteType::Delete)?)
167 .set_many_ccs(vec![self.followers_url()]);
169 let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?);
171 .set_many_contexts(lemmy_context()?)
172 .set_id(generate_activity_id(UndoType::Undo)?)
174 .set_many_ccs(vec![self.followers_url()]);
176 send_to_community_followers(undo, self, None, context).await?;
178 // Remote mod, send from mod to community
180 let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
182 .set_many_contexts(lemmy_context()?)
183 .set_id(generate_activity_id(DeleteType::Delete)?)
185 .set_many_ccs(vec![self.actor_id()]);
187 let mut undo = Undo::new(mod_.actor_id(), delete.into_any_base()?);
189 .set_many_contexts(lemmy_context()?)
190 .set_id(generate_activity_id(UndoType::Undo)?)
192 .set_many_ccs(vec![self.actor_id()]);
194 send_to_community(undo, &mod_, self, None, context).await?;
199 /// If an admin removes a community, send this to all followers.
200 async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
201 let mut remove = Remove::new(self.actor_id(), self.actor_id());
203 .set_many_contexts(lemmy_context()?)
204 .set_id(generate_activity_id(RemoveType::Remove)?)
206 .set_many_ccs(vec![self.followers_url()]);
208 send_to_community_followers(remove, self, None, context).await?;
212 /// If an admin reverts the removal of a community, send this to all followers.
213 async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
214 let mut remove = Remove::new(self.actor_id(), self.actor_id());
216 .set_many_contexts(lemmy_context()?)
217 .set_id(generate_activity_id(RemoveType::Remove)?)
219 .set_many_ccs(vec![self.followers_url()]);
221 // Undo that fake activity
222 let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?);
224 .set_many_contexts(lemmy_context()?)
225 .set_id(generate_activity_id(LikeType::Like)?)
227 .set_many_ccs(vec![self.followers_url()]);
229 send_to_community_followers(undo, self, None, context).await?;
233 /// Wraps an activity sent to the community in an announce, and then sends the announce to all
234 /// community followers.
236 /// If we are announcing a local activity, it hasn't been stored in the database yet, and we need
237 /// to do it here, so that it can be fetched by ID. Remote activities are inserted into DB in the
240 /// If the `object` of the announced activity is an actor, the actor ID needs to be passed as
241 /// `object_actor`, so that the announce can be delivered to that user.
242 async fn send_announce(
245 object_actor: Option<Url>,
246 context: &LemmyContext,
247 ) -> Result<(), LemmyError> {
248 let inner_id = activity.id().context(location_info!())?;
249 if inner_id.domain() == Some(&Settings::get().get_hostname_without_port()?) {
250 insert_activity(inner_id, activity.clone(), true, false, context.pool()).await?;
253 let mut ccs = vec![self.followers_url()];
254 let mut object_actor_inbox: Option<Url> = None;
255 if let Some(actor_id) = object_actor {
256 // Ignore errors, maybe its not actually an actor
257 // TODO: should pass the actual request counter in, but that seems complicated
258 let actor = get_or_fetch_and_upsert_actor(&actor_id, context, &mut 0)
261 if let Some(actor) = actor {
263 object_actor_inbox = Some(actor.get_shared_inbox_or_inbox_url());
266 let mut announce = Announce::new(self.actor_id(), activity);
268 .set_many_contexts(lemmy_context()?)
269 .set_id(generate_activity_id(AnnounceType::Announce)?)
273 send_to_community_followers(announce, self, object_actor_inbox, context).await?;
278 /// For a given community, returns the inboxes of all followers.
279 async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
282 let follows = blocking(pool, move |conn| {
283 CommunityFollowerView::for_community(conn, id)
286 let inboxes = follows
288 .filter(|f| !f.follower.local)
289 .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
290 .map(|i| i.into_inner())
292 // Don't send to blocked instances
293 .filter(|inbox| check_is_apub_id_valid(inbox, false).is_ok())
299 async fn send_add_mod(
303 context: &LemmyContext,
304 ) -> Result<(), LemmyError> {
305 let mut add = Add::new(actor.actor_id(), added_mod.actor_id());
307 .set_many_contexts(lemmy_context()?)
308 .set_id(generate_activity_id(AddType::Add)?)
310 .set_many_ccs(vec![self.actor_id()])
311 .set_target(generate_moderators_url(&self.actor_id)?.into_inner());
313 send_to_community(add, actor, self, Some(added_mod.actor_id()), context).await?;
317 async fn send_remove_mod(
321 context: &LemmyContext,
322 ) -> Result<(), LemmyError> {
323 let mut remove = Remove::new(actor.actor_id(), removed_mod.actor_id());
325 .set_many_contexts(lemmy_context()?)
326 .set_id(generate_activity_id(RemoveType::Remove)?)
328 .set_many_ccs(vec![self.actor_id()])
329 .set_target(generate_moderators_url(&self.actor_id)?.into_inner());
331 send_to_community(remove, &actor, self, Some(removed_mod.actor_id()), context).await?;
335 async fn send_block_user(
338 blocked_user: Person,
339 context: &LemmyContext,
340 ) -> Result<(), LemmyError> {
341 let mut block = Block::new(actor.actor_id(), blocked_user.actor_id());
343 .set_many_contexts(lemmy_context()?)
344 .set_id(generate_activity_id(BlockType::Block)?)
346 .set_many_ccs(vec![self.actor_id()]);
348 send_to_community(block, &actor, self, Some(blocked_user.actor_id()), context).await?;
352 async fn send_undo_block_user(
355 unblocked_user: Person,
356 context: &LemmyContext,
357 ) -> Result<(), LemmyError> {
358 let mut block = Block::new(actor.actor_id(), unblocked_user.actor_id());
360 .set_many_contexts(lemmy_context()?)
361 .set_id(generate_activity_id(BlockType::Block)?)
363 .set_many_ccs(vec![self.actor_id()]);
365 // Undo that fake activity
366 let mut undo = Undo::new(actor.actor_id(), block.into_any_base()?);
368 .set_many_contexts(lemmy_context()?)
369 .set_id(generate_activity_id(UndoType::Undo)?)
371 .set_many_ccs(vec![self.actor_id()]);
373 send_to_community(undo, &actor, self, Some(unblocked_user.actor_id()), context).await?;