2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
10 use lemmy_db_queries::Reportable;
11 use lemmy_db_schema::source::comment_report::*;
13 comment_report_view::{CommentReportQueryBuilder, CommentReportView},
14 comment_view::CommentView,
16 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
19 /// Creates a comment report and notifies the moderators of the community
20 #[async_trait::async_trait(?Send)]
21 impl Perform for CreateCommentReport {
22 type Response = CommentReportResponse;
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<CommentReportResponse, LemmyError> {
29 let data: &CreateCommentReport = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 // check size of report and check for whitespace
34 let reason = data.reason.trim();
35 if reason.is_empty() {
36 return Err(ApiError::err_plain("report_reason_required").into());
38 if reason.chars().count() > 1000 {
39 return Err(ApiError::err_plain("report_too_long").into());
42 let person_id = local_user_view.person.id;
43 let comment_id = data.comment_id;
44 let comment_view = blocking(context.pool(), move |conn| {
45 CommentView::read(conn, comment_id, None)
49 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
51 let report_form = CommentReportForm {
52 creator_id: person_id,
54 original_comment_text: comment_view.comment.content,
55 reason: data.reason.to_owned(),
58 let report = blocking(context.pool(), move |conn| {
59 CommentReport::report(conn, &report_form)
62 .map_err(|e| ApiError::err("couldnt_create_report", e))?;
64 let comment_report_view = blocking(context.pool(), move |conn| {
65 CommentReportView::read(conn, report.id, person_id)
69 let res = CommentReportResponse {
73 context.chat_server().do_send(SendModRoomMessage {
74 op: UserOperation::CreateCommentReport,
75 response: res.clone(),
76 community_id: comment_view.community.id,
84 /// Resolves or unresolves a comment report and notifies the moderators of the community
85 #[async_trait::async_trait(?Send)]
86 impl Perform for ResolveCommentReport {
87 type Response = CommentReportResponse;
91 context: &Data<LemmyContext>,
92 websocket_id: Option<ConnectionId>,
93 ) -> Result<CommentReportResponse, LemmyError> {
94 let data: &ResolveCommentReport = self;
96 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
98 let report_id = data.report_id;
99 let person_id = local_user_view.person.id;
100 let report = blocking(context.pool(), move |conn| {
101 CommentReportView::read(conn, report_id, person_id)
105 let person_id = local_user_view.person.id;
106 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
108 let resolved = data.resolved;
109 let resolve_fun = move |conn: &'_ _| {
111 CommentReport::resolve(conn, report_id, person_id)
113 CommentReport::unresolve(conn, report_id, person_id)
117 blocking(context.pool(), resolve_fun)
119 .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
121 let report_id = data.report_id;
122 let comment_report_view = blocking(context.pool(), move |conn| {
123 CommentReportView::read(conn, report_id, person_id)
127 let res = CommentReportResponse {
131 context.chat_server().do_send(SendModRoomMessage {
132 op: UserOperation::ResolveCommentReport,
133 response: res.clone(),
134 community_id: report.community.id,
142 /// Lists comment reports for a community if an id is supplied
143 /// or returns all comment reports for communities a user moderates
144 #[async_trait::async_trait(?Send)]
145 impl Perform for ListCommentReports {
146 type Response = ListCommentReportsResponse;
150 context: &Data<LemmyContext>,
151 _websocket_id: Option<ConnectionId>,
152 ) -> Result<ListCommentReportsResponse, LemmyError> {
153 let data: &ListCommentReports = self;
154 let local_user_view =
155 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
157 let person_id = local_user_view.person.id;
158 let admin = local_user_view.person.admin;
159 let community_id = data.community_id;
160 let unresolved_only = data.unresolved_only;
162 let page = data.page;
163 let limit = data.limit;
164 let comment_reports = blocking(context.pool(), move |conn| {
165 CommentReportQueryBuilder::create(conn, person_id, admin)
166 .community_id(community_id)
167 .unresolved_only(unresolved_only)
174 let res = ListCommentReportsResponse { comment_reports };