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