]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/receive/community.rs
48f6b295dd6fda6e0c656df1e9c4fd2be2459d05
[lemmy.git] / crates / apub / src / activities / receive / community.rs
1 use lemmy_api_structs::{blocking, community::CommunityResponse};
2 use lemmy_db_queries::source::community::Community_;
3 use lemmy_db_schema::source::community::Community;
4 use lemmy_db_views_actor::community_view::CommunityView;
5 use lemmy_utils::LemmyError;
6 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
7
8 pub(crate) async fn receive_delete_community(
9   context: &LemmyContext,
10   community: Community,
11 ) -> Result<(), LemmyError> {
12   let deleted_community = blocking(context.pool(), move |conn| {
13     Community::update_deleted(conn, community.id, true)
14   })
15   .await??;
16
17   let community_id = deleted_community.id;
18   let res = CommunityResponse {
19     community_view: blocking(context.pool(), move |conn| {
20       CommunityView::read(conn, community_id, None)
21     })
22     .await??,
23   };
24
25   let community_id = res.community_view.community.id;
26   context.chat_server().do_send(SendCommunityRoomMessage {
27     op: UserOperation::EditCommunity,
28     response: res,
29     community_id,
30     websocket_id: None,
31   });
32
33   Ok(())
34 }
35
36 pub(crate) async fn receive_remove_community(
37   context: &LemmyContext,
38   community: Community,
39 ) -> Result<(), LemmyError> {
40   let removed_community = blocking(context.pool(), move |conn| {
41     Community::update_removed(conn, community.id, true)
42   })
43   .await??;
44
45   let community_id = removed_community.id;
46   let res = CommunityResponse {
47     community_view: blocking(context.pool(), move |conn| {
48       CommunityView::read(conn, community_id, None)
49     })
50     .await??,
51   };
52
53   let community_id = res.community_view.community.id;
54   context.chat_server().do_send(SendCommunityRoomMessage {
55     op: UserOperation::EditCommunity,
56     response: res,
57     community_id,
58     websocket_id: None,
59   });
60
61   Ok(())
62 }
63
64 pub(crate) async fn receive_undo_delete_community(
65   context: &LemmyContext,
66   community: Community,
67 ) -> Result<(), LemmyError> {
68   let deleted_community = blocking(context.pool(), move |conn| {
69     Community::update_deleted(conn, community.id, false)
70   })
71   .await??;
72
73   let community_id = deleted_community.id;
74   let res = CommunityResponse {
75     community_view: blocking(context.pool(), move |conn| {
76       CommunityView::read(conn, community_id, None)
77     })
78     .await??,
79   };
80
81   let community_id = res.community_view.community.id;
82   context.chat_server().do_send(SendCommunityRoomMessage {
83     op: UserOperation::EditCommunity,
84     response: res,
85     community_id,
86     websocket_id: None,
87   });
88
89   Ok(())
90 }
91
92 pub(crate) async fn receive_undo_remove_community(
93   context: &LemmyContext,
94   community: Community,
95 ) -> Result<(), LemmyError> {
96   let removed_community = blocking(context.pool(), move |conn| {
97     Community::update_removed(conn, community.id, false)
98   })
99   .await??;
100
101   let community_id = removed_community.id;
102   let res = CommunityResponse {
103     community_view: blocking(context.pool(), move |conn| {
104       CommunityView::read(conn, community_id, None)
105     })
106     .await??,
107   };
108
109   let community_id = res.community_view.community.id;
110
111   context.chat_server().do_send(SendCommunityRoomMessage {
112     op: UserOperation::EditCommunity,
113     response: res,
114     community_id,
115     websocket_id: None,
116   });
117
118   Ok(())
119 }