]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/announce.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / apub / src / activities / community / announce.rs
1 use crate::{
2   activities::{
3     comment::create_or_update::CreateOrUpdateComment,
4     community::{
5       add_mod::AddMod,
6       block_user::BlockUserFromCommunity,
7       list_community_follower_inboxes,
8       undo_block_user::UndoBlockUserFromCommunity,
9     },
10     deletion::{delete::Delete, undo_delete::UndoDelete},
11     generate_activity_id,
12     post::create_or_update::CreateOrUpdatePost,
13     removal::{remove::RemoveMod, undo_remove::UndoRemovePostCommentOrCommunity},
14     verify_activity,
15     verify_community,
16     voting::{undo_vote::UndoVote, vote::Vote},
17   },
18   activity_queue::send_activity_new,
19   extensions::context::lemmy_context,
20   http::is_activity_already_known,
21   insert_activity,
22   ActorType,
23   CommunityType,
24 };
25 use activitystreams::activity::kind::AnnounceType;
26 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
27 use lemmy_db_schema::source::community::Community;
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::LemmyContext;
30 use serde::{Deserialize, Serialize};
31 use url::Url;
32
33 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
34 #[serde(untagged)]
35 pub enum AnnouncableActivities {
36   CreateOrUpdateComment(CreateOrUpdateComment),
37   CreateOrUpdatePost(Box<CreateOrUpdatePost>),
38   Vote(Vote),
39   UndoVote(UndoVote),
40   Delete(Delete),
41   UndoDelete(UndoDelete),
42   UndoRemovePostCommentOrCommunity(UndoRemovePostCommentOrCommunity),
43   BlockUserFromCommunity(BlockUserFromCommunity),
44   UndoBlockUserFromCommunity(UndoBlockUserFromCommunity),
45   AddMod(AddMod),
46   RemoveMod(RemoveMod),
47 }
48
49 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
50 #[serde(rename_all = "camelCase")]
51 pub struct AnnounceActivity {
52   to: PublicUrl,
53   object: AnnouncableActivities,
54   cc: Vec<Url>,
55   #[serde(rename = "type")]
56   kind: AnnounceType,
57   #[serde(flatten)]
58   common: ActivityCommonFields,
59 }
60
61 impl AnnounceActivity {
62   pub async fn send(
63     object: AnnouncableActivities,
64     community: &Community,
65     additional_inboxes: Vec<Url>,
66     context: &LemmyContext,
67   ) -> Result<(), LemmyError> {
68     let announce = AnnounceActivity {
69       to: PublicUrl::Public,
70       object,
71       cc: vec![community.followers_url()],
72       kind: AnnounceType::Announce,
73       common: ActivityCommonFields {
74         context: lemmy_context(),
75         id: generate_activity_id(&AnnounceType::Announce)?,
76         actor: community.actor_id(),
77         unparsed: Default::default(),
78       },
79     };
80     let inboxes = list_community_follower_inboxes(community, additional_inboxes, context).await?;
81     send_activity_new(
82       context,
83       &announce,
84       &announce.common.id,
85       community,
86       inboxes,
87       false,
88     )
89     .await
90   }
91 }
92
93 #[async_trait::async_trait(?Send)]
94 impl ActivityHandler for AnnounceActivity {
95   async fn verify(
96     &self,
97     context: &LemmyContext,
98     request_counter: &mut i32,
99   ) -> Result<(), LemmyError> {
100     verify_activity(self.common())?;
101     verify_community(&self.common.actor, context, request_counter).await?;
102     self.object.verify(context, request_counter).await?;
103     Ok(())
104   }
105
106   async fn receive(
107     self,
108     context: &LemmyContext,
109     request_counter: &mut i32,
110   ) -> Result<(), LemmyError> {
111     if is_activity_already_known(context.pool(), self.object.common().id_unchecked()).await? {
112       return Ok(());
113     }
114     insert_activity(
115       self.object.common().id_unchecked(),
116       self.object.clone(),
117       false,
118       true,
119       context.pool(),
120     )
121     .await?;
122     self.object.receive(context, request_counter).await
123   }
124
125   fn common(&self) -> &ActivityCommonFields {
126     &self.common
127   }
128 }