]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
Adding admin purging of DB items and pictures. #904 #1331 (#1809)
[lemmy.git] / crates / api / src / site / mod_log.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   site::{GetModlog, GetModlogResponse},
5   utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
6 };
7 use lemmy_db_views_moderator::structs::{
8   AdminPurgeCommentView,
9   AdminPurgeCommunityView,
10   AdminPurgePersonView,
11   AdminPurgePostView,
12   ModAddCommunityView,
13   ModAddView,
14   ModBanFromCommunityView,
15   ModBanView,
16   ModHideCommunityView,
17   ModLockPostView,
18   ModRemoveCommentView,
19   ModRemoveCommunityView,
20   ModRemovePostView,
21   ModStickyPostView,
22   ModTransferCommunityView,
23 };
24 use lemmy_utils::{error::LemmyError, ConnectionId};
25 use lemmy_websocket::LemmyContext;
26
27 #[async_trait::async_trait(?Send)]
28 impl Perform for GetModlog {
29   type Response = GetModlogResponse;
30
31   #[tracing::instrument(skip(context, _websocket_id))]
32   async fn perform(
33     &self,
34     context: &Data<LemmyContext>,
35     _websocket_id: Option<ConnectionId>,
36   ) -> Result<GetModlogResponse, LemmyError> {
37     let data: &GetModlog = self;
38
39     let local_user_view =
40       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
41         .await?;
42
43     check_private_instance(&local_user_view, context.pool()).await?;
44
45     let community_id = data.community_id;
46     let mod_person_id = data.mod_person_id;
47     let page = data.page;
48     let limit = data.limit;
49     let removed_posts = blocking(context.pool(), move |conn| {
50       ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
51     })
52     .await??;
53
54     let locked_posts = blocking(context.pool(), move |conn| {
55       ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
56     })
57     .await??;
58
59     let stickied_posts = blocking(context.pool(), move |conn| {
60       ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
61     })
62     .await??;
63
64     let removed_comments = blocking(context.pool(), move |conn| {
65       ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
66     })
67     .await??;
68
69     let banned_from_community = blocking(context.pool(), move |conn| {
70       ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
71     })
72     .await??;
73
74     let added_to_community = blocking(context.pool(), move |conn| {
75       ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
76     })
77     .await??;
78
79     let transferred_to_community = blocking(context.pool(), move |conn| {
80       ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
81     })
82     .await??;
83
84     let hidden_communities = blocking(context.pool(), move |conn| {
85       ModHideCommunityView::list(conn, community_id, mod_person_id, page, limit)
86     })
87     .await??;
88
89     // These arrays are only for the full modlog, when a community isn't given
90     let (
91       removed_communities,
92       banned,
93       added,
94       admin_purged_persons,
95       admin_purged_communities,
96       admin_purged_posts,
97       admin_purged_comments,
98     ) = if data.community_id.is_none() {
99       blocking(context.pool(), move |conn| {
100         Ok((
101           ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
102           ModBanView::list(conn, mod_person_id, page, limit)?,
103           ModAddView::list(conn, mod_person_id, page, limit)?,
104           AdminPurgePersonView::list(conn, mod_person_id, page, limit)?,
105           AdminPurgeCommunityView::list(conn, mod_person_id, page, limit)?,
106           AdminPurgePostView::list(conn, mod_person_id, page, limit)?,
107           AdminPurgeCommentView::list(conn, mod_person_id, page, limit)?,
108         )) as Result<_, LemmyError>
109       })
110       .await??
111     } else {
112       Default::default()
113     };
114
115     // Return the jwt
116     Ok(GetModlogResponse {
117       removed_posts,
118       locked_posts,
119       stickied_posts,
120       removed_comments,
121       removed_communities,
122       banned_from_community,
123       banned,
124       added_to_community,
125       added,
126       transferred_to_community,
127       admin_purged_persons,
128       admin_purged_communities,
129       admin_purged_posts,
130       admin_purged_comments,
131       hidden_communities,
132     })
133   }
134 }