]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/list.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api / src / comment_report / list.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{ListCommentReports, ListCommentReportsResponse},
5   context::LemmyContext,
6   utils::local_user_view_from_jwt,
7 };
8 use lemmy_db_views::comment_report_view::CommentReportQuery;
9 use lemmy_utils::error::LemmyError;
10
11 /// Lists comment reports for a community if an id is supplied
12 /// or returns all comment reports for communities a user moderates
13 #[async_trait::async_trait(?Send)]
14 impl Perform for ListCommentReports {
15   type Response = ListCommentReportsResponse;
16
17   #[tracing::instrument(skip(context))]
18   async fn perform(
19     &self,
20     context: &Data<LemmyContext>,
21   ) -> Result<ListCommentReportsResponse, LemmyError> {
22     let data: &ListCommentReports = self;
23     let local_user_view = local_user_view_from_jwt(&data.auth, context).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     let unresolved_only = data.unresolved_only;
29
30     let page = data.page;
31     let limit = data.limit;
32     let comment_reports = CommentReportQuery::builder()
33       .pool(&mut context.pool())
34       .my_person_id(person_id)
35       .admin(admin)
36       .community_id(community_id)
37       .unresolved_only(unresolved_only)
38       .page(page)
39       .limit(limit)
40       .build()
41       .list()
42       .await?;
43
44     Ok(ListCommentReportsResponse { comment_reports })
45   }
46 }