2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
11 ListPostReportsResponse,
16 use lemmy_apub::protocol::activities::community::report::Report;
17 use lemmy_apub_lib::object_id::ObjectId;
18 use lemmy_db_schema::{
19 source::post_report::{PostReport, PostReportForm},
23 post_report_view::{PostReportQueryBuilder, PostReportView},
26 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
27 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
29 /// Creates a post report and notifies the moderators of the community
30 #[async_trait::async_trait(?Send)]
31 impl Perform for CreatePostReport {
32 type Response = PostReportResponse;
36 context: &Data<LemmyContext>,
37 websocket_id: Option<ConnectionId>,
38 ) -> Result<PostReportResponse, LemmyError> {
39 let data: &CreatePostReport = self;
41 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
43 // check size of report and check for whitespace
44 let reason = data.reason.trim();
45 if reason.is_empty() {
46 return Err(ApiError::err_plain("report_reason_required").into());
48 if reason.chars().count() > 1000 {
49 return Err(ApiError::err_plain("report_too_long").into());
52 let person_id = local_user_view.person.id;
53 let post_id = data.post_id;
54 let post_view = blocking(context.pool(), move |conn| {
55 PostView::read(conn, post_id, None)
59 check_community_ban(person_id, post_view.community.id, context.pool()).await?;
61 let report_form = PostReportForm {
62 creator_id: person_id,
64 original_post_name: post_view.post.name,
65 original_post_url: post_view.post.url,
66 original_post_body: post_view.post.body,
67 reason: data.reason.to_owned(),
70 let report = blocking(context.pool(), move |conn| {
71 PostReport::report(conn, &report_form)
74 .map_err(|e| ApiError::err("couldnt_create_report", e))?;
76 let post_report_view = blocking(context.pool(), move |conn| {
77 PostReportView::read(conn, report.id, person_id)
81 let res = PostReportResponse { post_report_view };
83 context.chat_server().do_send(SendModRoomMessage {
84 op: UserOperation::CreatePostReport,
85 response: res.clone(),
86 community_id: post_view.community.id,
91 ObjectId::new(post_view.post.ap_id),
92 &local_user_view.person.into(),
93 ObjectId::new(post_view.community.actor_id),
103 /// Resolves or unresolves a post report and notifies the moderators of the community
104 #[async_trait::async_trait(?Send)]
105 impl Perform for ResolvePostReport {
106 type Response = PostReportResponse;
110 context: &Data<LemmyContext>,
111 websocket_id: Option<ConnectionId>,
112 ) -> Result<PostReportResponse, LemmyError> {
113 let data: &ResolvePostReport = self;
114 let local_user_view =
115 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
117 let report_id = data.report_id;
118 let person_id = local_user_view.person.id;
119 let report = blocking(context.pool(), move |conn| {
120 PostReportView::read(conn, report_id, person_id)
124 let person_id = local_user_view.person.id;
125 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
127 let resolved = data.resolved;
128 let resolve_fun = move |conn: &'_ _| {
130 PostReport::resolve(conn, report_id, person_id)
132 PostReport::unresolve(conn, report_id, person_id)
136 blocking(context.pool(), resolve_fun)
138 .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
140 let post_report_view = blocking(context.pool(), move |conn| {
141 PostReportView::read(conn, report_id, person_id)
145 let res = PostReportResponse { post_report_view };
147 context.chat_server().do_send(SendModRoomMessage {
148 op: UserOperation::ResolvePostReport,
149 response: res.clone(),
150 community_id: report.community.id,
158 /// Lists post reports for a community if an id is supplied
159 /// or returns all post reports for communities a user moderates
160 #[async_trait::async_trait(?Send)]
161 impl Perform for ListPostReports {
162 type Response = ListPostReportsResponse;
166 context: &Data<LemmyContext>,
167 _websocket_id: Option<ConnectionId>,
168 ) -> Result<ListPostReportsResponse, LemmyError> {
169 let data: &ListPostReports = self;
170 let local_user_view =
171 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
173 let person_id = local_user_view.person.id;
174 let admin = local_user_view.person.admin;
175 let community_id = data.community_id;
176 let unresolved_only = data.unresolved_only;
178 let page = data.page;
179 let limit = data.limit;
180 let post_reports = blocking(context.pool(), move |conn| {
181 PostReportQueryBuilder::create(conn, person_id, admin)
182 .community_id(community_id)
183 .unresolved_only(unresolved_only)
190 let res = ListPostReportsResponse { post_reports };