]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/report_count.rs
774b2fbbdaf8f8de09dfa26658a4360e6eb0e1bf
[lemmy.git] / crates / api / src / local_user / report_count.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   person::{GetReportCount, GetReportCountResponse},
5   utils::{blocking, get_local_user_view_from_jwt},
6 };
7 use lemmy_db_views::structs::{CommentReportView, PostReportView};
8 use lemmy_utils::{error::LemmyError, ConnectionId};
9 use lemmy_websocket::LemmyContext;
10
11 #[async_trait::async_trait(?Send)]
12 impl Perform for GetReportCount {
13   type Response = GetReportCountResponse;
14
15   #[tracing::instrument(skip(context, _websocket_id))]
16   async fn perform(
17     &self,
18     context: &Data<LemmyContext>,
19     _websocket_id: Option<ConnectionId>,
20   ) -> Result<GetReportCountResponse, LemmyError> {
21     let data: &GetReportCount = self;
22     let local_user_view =
23       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
24
25     let person_id = local_user_view.person.id;
26     let admin = local_user_view.person.admin;
27     let community_id = data.community_id;
28
29     let comment_reports = blocking(context.pool(), move |conn| {
30       CommentReportView::get_report_count(conn, person_id, admin, community_id)
31     })
32     .await??;
33
34     let post_reports = blocking(context.pool(), move |conn| {
35       PostReportView::get_report_count(conn, person_id, admin, community_id)
36     })
37     .await??;
38
39     let res = GetReportCountResponse {
40       community_id,
41       comment_reports,
42       post_reports,
43     };
44
45     Ok(res)
46   }
47 }