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