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