2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 collect_moderated_communities,
7 get_local_user_view_from_jwt,
11 CreatePostReportResponse,
13 ListPostReportsResponse,
15 ResolvePostReportResponse,
18 use lemmy_db_queries::Reportable;
19 use lemmy_db_schema::source::post_report::{PostReport, PostReportForm};
21 post_report_view::{PostReportQueryBuilder, PostReportView},
24 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
25 use lemmy_websocket::{
26 messages::{SendModRoomMessage, SendUserRoomMessage},
31 /// Creates a post report and notifies the moderators of the community
32 #[async_trait::async_trait(?Send)]
33 impl Perform for CreatePostReport {
34 type Response = CreatePostReportResponse;
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<CreatePostReportResponse, LemmyError> {
41 let data: &CreatePostReport = &self;
42 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
44 // check size of report and check for whitespace
45 let reason = data.reason.trim();
46 if reason.is_empty() {
47 return Err(ApiError::err("report_reason_required").into());
49 if reason.chars().count() > 1000 {
50 return Err(ApiError::err("report_too_long").into());
53 let person_id = local_user_view.person.id;
54 let post_id = data.post_id;
55 let post_view = blocking(context.pool(), move |conn| {
56 PostView::read(&conn, post_id, None)
60 check_community_ban(person_id, post_view.community.id, context.pool()).await?;
62 let report_form = PostReportForm {
63 creator_id: person_id,
65 original_post_name: post_view.post.name,
66 original_post_url: post_view.post.url,
67 original_post_body: post_view.post.body,
68 reason: data.reason.to_owned(),
71 let report = blocking(context.pool(), move |conn| {
72 PostReport::report(conn, &report_form)
75 .map_err(|_| ApiError::err("couldnt_create_report"))?;
77 let res = CreatePostReportResponse { success: true };
79 context.chat_server().do_send(SendUserRoomMessage {
80 op: UserOperation::CreatePostReport,
81 response: res.clone(),
82 local_recipient_id: local_user_view.local_user.id,
86 context.chat_server().do_send(SendModRoomMessage {
87 op: UserOperation::CreatePostReport,
89 community_id: post_view.community.id,
97 /// Resolves or unresolves a post report and notifies the moderators of the community
98 #[async_trait::async_trait(?Send)]
99 impl Perform for ResolvePostReport {
100 type Response = ResolvePostReportResponse;
104 context: &Data<LemmyContext>,
105 websocket_id: Option<ConnectionId>,
106 ) -> Result<ResolvePostReportResponse, LemmyError> {
107 let data: &ResolvePostReport = &self;
108 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
110 let report_id = data.report_id;
111 let report = blocking(context.pool(), move |conn| {
112 PostReportView::read(&conn, report_id)
116 let person_id = local_user_view.person.id;
117 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
119 let resolved = data.resolved;
120 let resolve_fun = move |conn: &'_ _| {
122 PostReport::resolve(conn, report_id, person_id)
124 PostReport::unresolve(conn, report_id, person_id)
128 let res = ResolvePostReportResponse {
133 if blocking(context.pool(), resolve_fun).await?.is_err() {
134 return Err(ApiError::err("couldnt_resolve_report").into());
137 context.chat_server().do_send(SendModRoomMessage {
138 op: UserOperation::ResolvePostReport,
139 response: res.clone(),
140 community_id: report.community.id,
148 /// Lists post reports for a community if an id is supplied
149 /// or returns all post reports for communities a user moderates
150 #[async_trait::async_trait(?Send)]
151 impl Perform for ListPostReports {
152 type Response = ListPostReportsResponse;
156 context: &Data<LemmyContext>,
157 websocket_id: Option<ConnectionId>,
158 ) -> Result<ListPostReportsResponse, LemmyError> {
159 let data: &ListPostReports = &self;
160 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
162 let person_id = local_user_view.person.id;
163 let community_id = data.community;
165 collect_moderated_communities(person_id, community_id, context.pool()).await?;
167 let page = data.page;
168 let limit = data.limit;
169 let posts = blocking(context.pool(), move |conn| {
170 PostReportQueryBuilder::create(conn)
171 .community_ids(community_ids)
178 let res = ListPostReportsResponse { posts };
180 context.chat_server().do_send(SendUserRoomMessage {
181 op: UserOperation::ListPostReports,
182 response: res.clone(),
183 local_recipient_id: local_user_view.local_user.id,