]> Untitled Git - lemmy.git/blob - crates/api/src/site/mod_log.rs
Split apart api files (#2216)
[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   blocking,
5   check_private_instance,
6   get_local_user_view_from_jwt_opt,
7   site::{GetModlog, GetModlogResponse},
8 };
9 use lemmy_db_views_moderator::{
10   mod_add_community_view::ModAddCommunityView,
11   mod_add_view::ModAddView,
12   mod_ban_from_community_view::ModBanFromCommunityView,
13   mod_ban_view::ModBanView,
14   mod_hide_community_view::ModHideCommunityView,
15   mod_lock_post_view::ModLockPostView,
16   mod_remove_comment_view::ModRemoveCommentView,
17   mod_remove_community_view::ModRemoveCommunityView,
18   mod_remove_post_view::ModRemovePostView,
19   mod_sticky_post_view::ModStickyPostView,
20   mod_transfer_community_view::ModTransferCommunityView,
21 };
22 use lemmy_utils::{ConnectionId, LemmyError};
23 use lemmy_websocket::LemmyContext;
24
25 #[async_trait::async_trait(?Send)]
26 impl Perform for GetModlog {
27   type Response = GetModlogResponse;
28
29   #[tracing::instrument(skip(context, _websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     _websocket_id: Option<ConnectionId>,
34   ) -> Result<GetModlogResponse, LemmyError> {
35     let data: &GetModlog = self;
36
37     let local_user_view =
38       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
39         .await?;
40
41     check_private_instance(&local_user_view, context.pool()).await?;
42
43     let community_id = data.community_id;
44     let mod_person_id = data.mod_person_id;
45     let page = data.page;
46     let limit = data.limit;
47     let removed_posts = blocking(context.pool(), move |conn| {
48       ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
49     })
50     .await??;
51
52     let locked_posts = blocking(context.pool(), move |conn| {
53       ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
54     })
55     .await??;
56
57     let stickied_posts = blocking(context.pool(), move |conn| {
58       ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
59     })
60     .await??;
61
62     let removed_comments = blocking(context.pool(), move |conn| {
63       ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
64     })
65     .await??;
66
67     let banned_from_community = blocking(context.pool(), move |conn| {
68       ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
69     })
70     .await??;
71
72     let added_to_community = blocking(context.pool(), move |conn| {
73       ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
74     })
75     .await??;
76
77     let transferred_to_community = blocking(context.pool(), move |conn| {
78       ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
79     })
80     .await??;
81
82     let hidden_communities = blocking(context.pool(), move |conn| {
83       ModHideCommunityView::list(conn, community_id, mod_person_id, page, limit)
84     })
85     .await??;
86
87     // These arrays are only for the full modlog, when a community isn't given
88     let (removed_communities, banned, added) = if data.community_id.is_none() {
89       blocking(context.pool(), move |conn| {
90         Ok((
91           ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
92           ModBanView::list(conn, mod_person_id, page, limit)?,
93           ModAddView::list(conn, mod_person_id, page, limit)?,
94         )) as Result<_, LemmyError>
95       })
96       .await??
97     } else {
98       (Vec::new(), Vec::new(), Vec::new())
99     };
100
101     // Return the jwt
102     Ok(GetModlogResponse {
103       removed_posts,
104       locked_posts,
105       stickied_posts,
106       removed_comments,
107       removed_communities,
108       banned_from_community,
109       banned,
110       added_to_community,
111       added,
112       transferred_to_community,
113       hidden_communities,
114     })
115   }
116 }