6 verify_person_in_community,
8 activity_lists::AnnouncableActivities,
10 objects::community::ApubCommunity,
12 activities::community::announce::{AnnounceActivity, RawAnnouncableActivities},
18 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
19 use activitystreams_kinds::{activity::AnnounceType, public};
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
22 use serde_json::Value;
26 #[async_trait::async_trait(?Send)]
27 impl ActivityHandler for RawAnnouncableActivities {
28 type DataType = LemmyContext;
29 type Error = LemmyError;
31 fn id(&self) -> &Url {
35 fn actor(&self) -> &Url {
39 #[tracing::instrument(skip_all)]
42 _data: &Data<Self::DataType>,
43 _request_counter: &mut i32,
44 ) -> Result<(), Self::Error> {
48 #[tracing::instrument(skip_all)]
51 data: &Data<Self::DataType>,
52 request_counter: &mut i32,
53 ) -> Result<(), Self::Error> {
54 let activity: AnnouncableActivities = self.clone().try_into()?;
55 // This is only for sending, not receiving so we reject it.
56 if let AnnouncableActivities::Page(_) = activity {
57 return Err(LemmyError::from_message("Cant receive page"));
59 let community = activity.get_community(data, &mut 0).await?;
60 let actor_id = ObjectId::new(activity.actor().clone());
62 // verify and receive activity
63 activity.verify(data, request_counter).await?;
64 activity.receive(data, request_counter).await?;
66 // send to community followers
68 verify_person_in_community(&actor_id, &community, data, &mut 0).await?;
69 AnnounceActivity::send(self, &community, data).await?;
75 #[async_trait::async_trait(?Send)]
76 pub(crate) trait GetCommunity {
77 async fn get_community(
79 context: &LemmyContext,
80 request_counter: &mut i32,
81 ) -> Result<ApubCommunity, LemmyError>;
84 impl AnnounceActivity {
86 object: RawAnnouncableActivities,
87 community: &ApubCommunity,
88 context: &LemmyContext,
89 ) -> Result<AnnounceActivity, LemmyError> {
91 actor: ObjectId::new(community.actor_id()),
93 object: IdOrNestedObject::NestedObject(object),
94 cc: vec![community.followers_url.clone().into()],
95 kind: AnnounceType::Announce,
96 id: generate_activity_id(
97 &AnnounceType::Announce,
98 &context.settings().get_protocol_and_hostname(),
103 #[tracing::instrument(skip_all)]
105 object: RawAnnouncableActivities,
106 community: &ApubCommunity,
107 context: &LemmyContext,
108 ) -> Result<(), LemmyError> {
109 let announce = AnnounceActivity::new(object.clone(), community, context)?;
110 let inboxes = community.get_follower_inboxes(context).await?;
111 send_lemmy_activity(context, announce, community, inboxes.clone(), false).await?;
113 // Pleroma and Mastodon can't handle activities like Announce/Create/Page. So for
114 // compatibility, we also send Announce/Page so that they can follow Lemmy communities.
115 let object_parsed = object.try_into()?;
116 if let AnnouncableActivities::CreateOrUpdatePost(c) = object_parsed {
117 // Hack: need to convert Page into a format which can be sent as activity, which requires
118 // adding actor field.
119 let announcable_page = RawAnnouncableActivities {
120 id: c.object.id.clone().into_inner(),
121 actor: c.actor.clone().into_inner(),
122 other: serde_json::to_value(c.object)?.as_object().unwrap().clone(),
124 let announce_compat = AnnounceActivity::new(announcable_page, community, context)?;
125 send_lemmy_activity(context, announce_compat, community, inboxes, false).await?;
131 #[async_trait::async_trait(?Send)]
132 impl ActivityHandler for AnnounceActivity {
133 type DataType = LemmyContext;
134 type Error = LemmyError;
136 fn id(&self) -> &Url {
140 fn actor(&self) -> &Url {
144 #[tracing::instrument(skip_all)]
147 _context: &Data<LemmyContext>,
148 _request_counter: &mut i32,
149 ) -> Result<(), LemmyError> {
150 verify_is_public(&self.to, &self.cc)?;
154 #[tracing::instrument(skip_all)]
157 context: &Data<LemmyContext>,
158 request_counter: &mut i32,
159 ) -> Result<(), LemmyError> {
160 let object: AnnouncableActivities = self
162 .object(context, request_counter)
165 // This is only for sending, not receiving so we reject it.
166 if let AnnouncableActivities::Page(_) = object {
167 return Err(LemmyError::from_message("Cant receive page"));
170 // we have to verify this here in order to avoid fetching the object twice over http
171 object.verify(context, request_counter).await?;
173 let object_value = serde_json::to_value(&object)?;
174 let insert = insert_activity(object.id(), object_value, false, true, context.pool()).await?;
177 "Received duplicate activity in announce {}",
178 object.id().to_string()
182 object.receive(context, request_counter).await
186 impl Id for RawAnnouncableActivities {
187 fn object_id(&self) -> &Url {
188 ActivityHandler::id(self)
192 impl TryFrom<RawAnnouncableActivities> for AnnouncableActivities {
193 type Error = serde_json::error::Error;
195 fn try_from(value: RawAnnouncableActivities) -> Result<Self, Self::Error> {
196 let mut map = value.other.clone();
197 map.insert("id".to_string(), Value::String(value.id.to_string()));
198 map.insert("actor".to_string(), Value::String(value.actor.to_string()));
199 serde_json::from_value(Value::Object(map))
203 impl TryFrom<AnnouncableActivities> for RawAnnouncableActivities {
204 type Error = serde_json::error::Error;
206 fn try_from(value: AnnouncableActivities) -> Result<Self, Self::Error> {
207 serde_json::from_value(serde_json::to_value(value)?)