]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/announce.rs
Merge apub, apub_receive crates (fixes #1621)
[lemmy.git] / crates / apub / src / activities / community / announce.rs
1 use crate::{
2   activities::{
3     comment::{create::CreateComment, update::UpdateComment},
4     community::{
5       add_mod::AddMod,
6       block_user::BlockUserFromCommunity,
7       undo_block_user::UndoBlockUserFromCommunity,
8     },
9     deletion::{
10       delete::DeletePostCommentOrCommunity,
11       undo_delete::UndoDeletePostCommentOrCommunity,
12     },
13     post::{create::CreatePost, update::UpdatePost},
14     removal::{
15       remove::RemovePostCommentCommunityOrMod,
16       undo_remove::UndoRemovePostCommentOrCommunity,
17     },
18     verify_activity,
19     verify_community,
20     voting::{
21       dislike::DislikePostOrComment,
22       like::LikePostOrComment,
23       undo_dislike::UndoDislikePostOrComment,
24       undo_like::UndoLikePostOrComment,
25     },
26   },
27   http::is_activity_already_known,
28   insert_activity,
29 };
30 use activitystreams::activity::kind::AnnounceType;
31 use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::LemmyContext;
34 use serde::{Deserialize, Serialize};
35 use url::Url;
36
37 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
38 #[serde(untagged)]
39 pub enum AnnouncableActivities {
40   CreateComment(CreateComment),
41   UpdateComment(UpdateComment),
42   CreatePost(CreatePost),
43   UpdatePost(UpdatePost),
44   LikePostOrComment(LikePostOrComment),
45   DislikePostOrComment(DislikePostOrComment),
46   UndoLikePostOrComment(UndoLikePostOrComment),
47   UndoDislikePostOrComment(UndoDislikePostOrComment),
48   DeletePostCommentOrCommunity(DeletePostCommentOrCommunity),
49   UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity),
50   RemovePostCommentCommunityOrMod(RemovePostCommentCommunityOrMod),
51   UndoRemovePostCommentOrCommunity(UndoRemovePostCommentOrCommunity),
52   BlockUserFromCommunity(BlockUserFromCommunity),
53   UndoBlockUserFromCommunity(UndoBlockUserFromCommunity),
54   AddMod(AddMod),
55 }
56
57 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
58 #[serde(rename_all = "camelCase")]
59 pub struct AnnounceActivity {
60   to: PublicUrl,
61   object: AnnouncableActivities,
62   cc: Vec<Url>,
63   #[serde(rename = "type")]
64   kind: AnnounceType,
65   #[serde(flatten)]
66   common: ActivityCommonFields,
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for AnnounceActivity {
71   async fn verify(
72     &self,
73     context: &LemmyContext,
74     request_counter: &mut i32,
75   ) -> Result<(), LemmyError> {
76     verify_activity(self.common())?;
77     verify_community(&self.common.actor, context, request_counter).await?;
78     self.object.verify(context, request_counter).await?;
79     Ok(())
80   }
81
82   async fn receive(
83     &self,
84     context: &LemmyContext,
85     request_counter: &mut i32,
86   ) -> Result<(), LemmyError> {
87     if is_activity_already_known(context.pool(), self.object.common().id_unchecked()).await? {
88       return Ok(());
89     }
90     insert_activity(
91       self.object.common().id_unchecked(),
92       self.object.clone(),
93       false,
94       true,
95       context.pool(),
96     )
97     .await?;
98     self.object.receive(context, request_counter).await
99   }
100
101   fn common(&self) -> &ActivityCommonFields {
102     &self.common
103   }
104 }