2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
11 ListPostReportsResponse,
16 use lemmy_db_queries::Reportable;
17 use lemmy_db_schema::source::post_report::{PostReport, PostReportForm};
19 post_report_view::{PostReportQueryBuilder, PostReportView},
22 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
23 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
25 /// Creates a post report and notifies the moderators of the community
26 #[async_trait::async_trait(?Send)]
27 impl Perform for CreatePostReport {
28 type Response = PostReportResponse;
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<PostReportResponse, LemmyError> {
35 let data: &CreatePostReport = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 // check size of report and check for whitespace
40 let reason = data.reason.trim();
41 if reason.is_empty() {
42 return Err(ApiError::err_plain("report_reason_required").into());
44 if reason.chars().count() > 1000 {
45 return Err(ApiError::err_plain("report_too_long").into());
48 let person_id = local_user_view.person.id;
49 let post_id = data.post_id;
50 let post_view = blocking(context.pool(), move |conn| {
51 PostView::read(conn, post_id, None)
55 check_community_ban(person_id, post_view.community.id, context.pool()).await?;
57 let report_form = PostReportForm {
58 creator_id: person_id,
60 original_post_name: post_view.post.name,
61 original_post_url: post_view.post.url,
62 original_post_body: post_view.post.body,
63 reason: data.reason.to_owned(),
66 let report = blocking(context.pool(), move |conn| {
67 PostReport::report(conn, &report_form)
70 .map_err(|e| ApiError::err("couldnt_create_report", e))?;
72 let post_report_view = blocking(context.pool(), move |conn| {
73 PostReportView::read(conn, report.id, person_id)
77 let res = PostReportResponse { post_report_view };
79 context.chat_server().do_send(SendModRoomMessage {
80 op: UserOperation::CreatePostReport,
81 response: res.clone(),
82 community_id: post_view.community.id,
90 /// Resolves or unresolves a post report and notifies the moderators of the community
91 #[async_trait::async_trait(?Send)]
92 impl Perform for ResolvePostReport {
93 type Response = PostReportResponse;
97 context: &Data<LemmyContext>,
98 websocket_id: Option<ConnectionId>,
99 ) -> Result<PostReportResponse, LemmyError> {
100 let data: &ResolvePostReport = self;
101 let local_user_view =
102 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
104 let report_id = data.report_id;
105 let person_id = local_user_view.person.id;
106 let report = blocking(context.pool(), move |conn| {
107 PostReportView::read(conn, report_id, person_id)
111 let person_id = local_user_view.person.id;
112 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
114 let resolved = data.resolved;
115 let resolve_fun = move |conn: &'_ _| {
117 PostReport::resolve(conn, report_id, person_id)
119 PostReport::unresolve(conn, report_id, person_id)
123 blocking(context.pool(), resolve_fun)
125 .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
127 let post_report_view = blocking(context.pool(), move |conn| {
128 PostReportView::read(conn, report_id, person_id)
132 let res = PostReportResponse { post_report_view };
134 context.chat_server().do_send(SendModRoomMessage {
135 op: UserOperation::ResolvePostReport,
136 response: res.clone(),
137 community_id: report.community.id,
145 /// Lists post reports for a community if an id is supplied
146 /// or returns all post reports for communities a user moderates
147 #[async_trait::async_trait(?Send)]
148 impl Perform for ListPostReports {
149 type Response = ListPostReportsResponse;
153 context: &Data<LemmyContext>,
154 _websocket_id: Option<ConnectionId>,
155 ) -> Result<ListPostReportsResponse, LemmyError> {
156 let data: &ListPostReports = self;
157 let local_user_view =
158 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
160 let person_id = local_user_view.person.id;
161 let admin = local_user_view.person.admin;
162 let community_id = data.community_id;
163 let unresolved_only = data.unresolved_only;
165 let page = data.page;
166 let limit = data.limit;
167 let post_reports = blocking(context.pool(), move |conn| {
168 PostReportQueryBuilder::create(conn, person_id, admin)
169 .community_id(community_id)
170 .unresolved_only(unresolved_only)
177 let res = ListPostReportsResponse { post_reports };