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