]> Untitled Git - lemmy.git/blob - crates/apub/src/activity_lists.rs
Reduce stack memory usage in apub code
[lemmy.git] / crates / apub / src / activity_lists.rs
1 use crate::{
2   activities::community::announce::GetCommunity,
3   objects::community::ApubCommunity,
4   protocol::activities::{
5     community::{
6       add_mod::AddMod,
7       announce::AnnounceActivity,
8       block_user::BlockUserFromCommunity,
9       remove_mod::RemoveMod,
10       report::Report,
11       undo_block_user::UndoBlockUserFromCommunity,
12       update::UpdateCommunity,
13     },
14     create_or_update::{comment::CreateOrUpdateComment, post::CreateOrUpdatePost},
15     deletion::{delete::Delete, undo_delete::UndoDelete},
16     following::{
17       accept::AcceptFollowCommunity,
18       follow::FollowCommunity,
19       undo_follow::UndoFollowCommunity,
20     },
21     private_message::{
22       create_or_update::CreateOrUpdatePrivateMessage,
23       delete::DeletePrivateMessage,
24       undo_delete::UndoDeletePrivateMessage,
25     },
26     voting::{undo_vote::UndoVote, vote::Vote},
27   },
28 };
29 use lemmy_apub_lib::traits::ActivityHandler;
30 use lemmy_utils::LemmyError;
31 use lemmy_websocket::LemmyContext;
32 use serde::{Deserialize, Serialize};
33
34 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
35 #[serde(untagged)]
36 #[activity_handler(LemmyContext)]
37 pub enum SharedInboxActivities {
38   GroupInboxActivities(GroupInboxActivities),
39   // Note, pm activities need to be at the end, otherwise comments will end up here. We can probably
40   // avoid this problem by replacing createpm.object with our own struct, instead of NoteExt.
41   PersonInboxActivities(Box<PersonInboxActivities>),
42 }
43
44 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
45 #[serde(untagged)]
46 #[activity_handler(LemmyContext)]
47 pub enum GroupInboxActivities {
48   FollowCommunity(FollowCommunity),
49   UndoFollowCommunity(UndoFollowCommunity),
50   AnnouncableActivities(Box<AnnouncableActivities>),
51   Report(Report),
52 }
53
54 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
55 #[serde(untagged)]
56 #[activity_handler(LemmyContext)]
57 pub enum PersonInboxActivities {
58   AcceptFollowCommunity(AcceptFollowCommunity),
59   /// Some activities can also be sent from user to user, eg a comment with mentions
60   AnnouncableActivities(AnnouncableActivities),
61   CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
62   DeletePrivateMessage(DeletePrivateMessage),
63   UndoDeletePrivateMessage(UndoDeletePrivateMessage),
64   AnnounceActivity(AnnounceActivity),
65 }
66
67 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
68 #[serde(untagged)]
69 #[activity_handler(LemmyContext)]
70 pub enum AnnouncableActivities {
71   CreateOrUpdateComment(CreateOrUpdateComment),
72   CreateOrUpdatePost(CreateOrUpdatePost),
73   Vote(Vote),
74   UndoVote(UndoVote),
75   Delete(Delete),
76   UndoDelete(UndoDelete),
77   UpdateCommunity(UpdateCommunity),
78   BlockUserFromCommunity(BlockUserFromCommunity),
79   UndoBlockUserFromCommunity(UndoBlockUserFromCommunity),
80   AddMod(AddMod),
81   RemoveMod(RemoveMod),
82 }
83
84 #[async_trait::async_trait(?Send)]
85 impl GetCommunity for AnnouncableActivities {
86   async fn get_community(
87     &self,
88     context: &LemmyContext,
89     request_counter: &mut i32,
90   ) -> Result<ApubCommunity, LemmyError> {
91     use AnnouncableActivities::*;
92     let community = match self {
93       CreateOrUpdateComment(a) => a.get_community(context, request_counter).await?,
94       CreateOrUpdatePost(a) => a.get_community(context, request_counter).await?,
95       Vote(a) => a.get_community(context, request_counter).await?,
96       UndoVote(a) => a.get_community(context, request_counter).await?,
97       Delete(a) => a.get_community(context, request_counter).await?,
98       UndoDelete(a) => a.get_community(context, request_counter).await?,
99       UpdateCommunity(a) => a.get_community(context, request_counter).await?,
100       BlockUserFromCommunity(a) => a.get_community(context, request_counter).await?,
101       UndoBlockUserFromCommunity(a) => a.get_community(context, request_counter).await?,
102       AddMod(a) => a.get_community(context, request_counter).await?,
103       RemoveMod(a) => a.get_community(context, request_counter).await?,
104     };
105     Ok(community)
106   }
107 }