]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/receive/community.rs
Rename `lemmy_structs` to `lemmy_api_structs`
[lemmy.git] / crates / apub / src / activities / receive / community.rs
1 use crate::{activities::receive::verify_activity_domains_valid, inbox::is_addressed_to_public};
2 use activitystreams::{
3   activity::{ActorAndObjectRefExt, Delete, Remove, Undo},
4   base::{AnyBase, ExtendsExt},
5 };
6 use anyhow::Context;
7 use lemmy_api_structs::{blocking, community::CommunityResponse};
8 use lemmy_db_queries::{source::community::Community_, ApubObject};
9 use lemmy_db_schema::source::community::Community;
10 use lemmy_db_views_actor::community_view::CommunityView;
11 use lemmy_utils::{location_info, LemmyError};
12 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
13 use url::Url;
14
15 pub(crate) async fn receive_delete_community(
16   context: &LemmyContext,
17   community: Community,
18 ) -> Result<(), LemmyError> {
19   let deleted_community = blocking(context.pool(), move |conn| {
20     Community::update_deleted(conn, community.id, true)
21   })
22   .await??;
23
24   let community_id = deleted_community.id;
25   let res = CommunityResponse {
26     community_view: blocking(context.pool(), move |conn| {
27       CommunityView::read(conn, community_id, None)
28     })
29     .await??,
30   };
31
32   let community_id = res.community_view.community.id;
33   context.chat_server().do_send(SendCommunityRoomMessage {
34     op: UserOperation::EditCommunity,
35     response: res,
36     community_id,
37     websocket_id: None,
38   });
39
40   Ok(())
41 }
42
43 pub(crate) async fn receive_remove_community(
44   context: &LemmyContext,
45   activity: AnyBase,
46   expected_domain: &Url,
47 ) -> Result<(), LemmyError> {
48   let remove = Remove::from_any_base(activity)?.context(location_info!())?;
49   verify_activity_domains_valid(&remove, expected_domain, true)?;
50   is_addressed_to_public(&remove)?;
51
52   let community_uri = remove
53     .object()
54     .to_owned()
55     .single_xsd_any_uri()
56     .context(location_info!())?;
57   let community = blocking(context.pool(), move |conn| {
58     Community::read_from_apub_id(conn, &community_uri.into())
59   })
60   .await??;
61
62   let removed_community = blocking(context.pool(), move |conn| {
63     Community::update_removed(conn, community.id, true)
64   })
65   .await??;
66
67   let community_id = removed_community.id;
68   let res = CommunityResponse {
69     community_view: blocking(context.pool(), move |conn| {
70       CommunityView::read(conn, community_id, None)
71     })
72     .await??,
73   };
74
75   let community_id = res.community_view.community.id;
76   context.chat_server().do_send(SendCommunityRoomMessage {
77     op: UserOperation::EditCommunity,
78     response: res,
79     community_id,
80     websocket_id: None,
81   });
82
83   Ok(())
84 }
85
86 pub(crate) async fn receive_undo_delete_community(
87   context: &LemmyContext,
88   undo: Undo,
89   community: Community,
90   expected_domain: &Url,
91 ) -> Result<(), LemmyError> {
92   is_addressed_to_public(&undo)?;
93   let inner = undo.object().to_owned().one().context(location_info!())?;
94   let delete = Delete::from_any_base(inner)?.context(location_info!())?;
95   verify_activity_domains_valid(&delete, expected_domain, true)?;
96   is_addressed_to_public(&delete)?;
97
98   let deleted_community = blocking(context.pool(), move |conn| {
99     Community::update_deleted(conn, community.id, false)
100   })
101   .await??;
102
103   let community_id = deleted_community.id;
104   let res = CommunityResponse {
105     community_view: blocking(context.pool(), move |conn| {
106       CommunityView::read(conn, community_id, None)
107     })
108     .await??,
109   };
110
111   let community_id = res.community_view.community.id;
112   context.chat_server().do_send(SendCommunityRoomMessage {
113     op: UserOperation::EditCommunity,
114     response: res,
115     community_id,
116     websocket_id: None,
117   });
118
119   Ok(())
120 }
121
122 pub(crate) async fn receive_undo_remove_community(
123   context: &LemmyContext,
124   undo: Undo,
125   expected_domain: &Url,
126 ) -> Result<(), LemmyError> {
127   is_addressed_to_public(&undo)?;
128
129   let inner = undo.object().to_owned().one().context(location_info!())?;
130   let remove = Remove::from_any_base(inner)?.context(location_info!())?;
131   verify_activity_domains_valid(&remove, &expected_domain, true)?;
132   is_addressed_to_public(&remove)?;
133
134   let community_uri = remove
135     .object()
136     .to_owned()
137     .single_xsd_any_uri()
138     .context(location_info!())?;
139   let community = blocking(context.pool(), move |conn| {
140     Community::read_from_apub_id(conn, &community_uri.into())
141   })
142   .await??;
143
144   let removed_community = blocking(context.pool(), move |conn| {
145     Community::update_removed(conn, community.id, false)
146   })
147   .await??;
148
149   let community_id = removed_community.id;
150   let res = CommunityResponse {
151     community_view: blocking(context.pool(), move |conn| {
152       CommunityView::read(conn, community_id, None)
153     })
154     .await??,
155   };
156
157   let community_id = res.community_view.community.id;
158
159   context.chat_server().do_send(SendCommunityRoomMessage {
160     op: UserOperation::EditCommunity,
161     response: res,
162     community_id,
163     websocket_id: None,
164   });
165
166   Ok(())
167 }