]> Untitled Git - lemmy.git/blob - src/apub/activities.rs
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / src / apub / activities.rs
1 use crate::{
2   apub::{activity_queue::send_activity, community::do_announce, insert_activity},
3   LemmyContext,
4 };
5 use activitystreams::{
6   base::{Extends, ExtendsExt},
7   object::AsObject,
8 };
9 use lemmy_db::{community::Community, user::User_};
10 use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
11 use serde::{export::fmt::Debug, Serialize};
12 use url::{ParseError, Url};
13 use uuid::Uuid;
14
15 pub async fn send_activity_to_community<T, Kind>(
16   creator: &User_,
17   community: &Community,
18   to: Vec<Url>,
19   activity: T,
20   context: &LemmyContext,
21 ) -> Result<(), LemmyError>
22 where
23   T: AsObject<Kind> + Extends<Kind> + Serialize + Debug + Send + Clone + 'static,
24   Kind: Serialize,
25   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
26 {
27   // TODO: looks like call this sometimes with activity, and sometimes with any_base
28   insert_activity(creator.id, activity.clone(), true, context.pool()).await?;
29
30   // if this is a local community, we need to do an announce from the community instead
31   if community.local {
32     do_announce(activity.into_any_base()?, &community, creator, context).await?;
33   } else {
34     send_activity(context.activity_queue(), activity, creator, to)?;
35   }
36
37   Ok(())
38 }
39
40 pub(in crate::apub) fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError>
41 where
42   T: ToString,
43 {
44   let id = format!(
45     "{}://{}/activities/{}/{}",
46     get_apub_protocol_string(),
47     Settings::get().hostname,
48     kind.to_string().to_lowercase(),
49     Uuid::new_v4()
50   );
51   Url::parse(&id)
52 }