2 activities::{generate_activity_id, send_lemmy_activity, verify_is_public},
3 activity_lists::AnnouncableActivities,
5 objects::community::ApubCommunity,
7 activities::{community::announce::AnnounceActivity, CreateOrUpdateType},
12 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
13 use activitystreams_kinds::{activity::AnnounceType, public};
14 use lemmy_utils::error::LemmyError;
15 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 pub(crate) trait GetCommunity {
21 async fn get_community(
23 context: &LemmyContext,
24 request_counter: &mut i32,
25 ) -> Result<ApubCommunity, LemmyError>;
28 impl AnnounceActivity {
30 object: AnnouncableActivities,
31 community: &ApubCommunity,
32 context: &LemmyContext,
33 ) -> Result<AnnounceActivity, LemmyError> {
35 actor: ObjectId::new(community.actor_id()),
37 object: IdOrNestedObject::NestedObject(object),
38 cc: vec![community.followers_url.clone().into()],
39 kind: AnnounceType::Announce,
40 id: generate_activity_id(
41 &AnnounceType::Announce,
42 &context.settings().get_protocol_and_hostname(),
44 unparsed: Default::default(),
48 #[tracing::instrument(skip_all)]
50 object: AnnouncableActivities,
51 community: &ApubCommunity,
52 context: &LemmyContext,
53 ) -> Result<(), LemmyError> {
54 let announce = AnnounceActivity::new(object.clone(), community, context)?;
55 let inboxes = community.get_follower_inboxes(context).await?;
66 // Pleroma and Mastodon can't handle activities like Announce/Create/Page. So for
67 // compatibility, we also send Announce/Page so that they can follow Lemmy communities.
68 use AnnouncableActivities::*;
69 let object = match object {
70 CreateOrUpdatePost(c) if c.kind == CreateOrUpdateType::Create => Page(c.object),
73 let announce_compat = AnnounceActivity::new(object, community, context)?;
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for AnnounceActivity {
89 type DataType = LemmyContext;
90 type Error = LemmyError;
92 fn id(&self) -> &Url {
96 fn actor(&self) -> &Url {
100 #[tracing::instrument(skip_all)]
103 _context: &Data<LemmyContext>,
104 _request_counter: &mut i32,
105 ) -> Result<(), LemmyError> {
106 verify_is_public(&self.to, &self.cc)?;
110 #[tracing::instrument(skip_all)]
113 context: &Data<LemmyContext>,
114 request_counter: &mut i32,
115 ) -> Result<(), LemmyError> {
116 let object = self.object.object(context, request_counter).await?;
117 // we have to verify this here in order to avoid fetching the object twice over http
118 object.verify(context, request_counter).await?;
120 // TODO: this can probably be implemented in a cleaner way
122 // Dont insert these into activities table, as they are not activities.
123 AnnouncableActivities::Page(_) => {}
125 let object_value = serde_json::to_value(&object)?;
127 insert_activity(object.id(), object_value, false, true, context.pool()).await?;
130 "Received duplicate activity in announce {}",
131 object.id().to_string()
137 object.receive(context, request_counter).await