2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 collect_moderated_communities,
8 get_local_user_view_from_jwt,
11 use lemmy_db_queries::Reportable;
12 use lemmy_db_schema::source::comment_report::*;
14 comment_report_view::{CommentReportQueryBuilder, CommentReportView},
15 comment_view::CommentView,
17 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
18 use lemmy_websocket::{
19 messages::{SendModRoomMessage, SendUserRoomMessage},
24 /// Creates a comment report and notifies the moderators of the community
25 #[async_trait::async_trait(?Send)]
26 impl Perform for CreateCommentReport {
27 type Response = CreateCommentReportResponse;
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CreateCommentReportResponse, LemmyError> {
34 let data: &CreateCommentReport = &self;
35 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
37 // check size of report and check for whitespace
38 let reason = data.reason.trim();
39 if reason.is_empty() {
40 return Err(ApiError::err("report_reason_required").into());
42 if reason.chars().count() > 1000 {
43 return Err(ApiError::err("report_too_long").into());
46 let person_id = local_user_view.person.id;
47 let comment_id = data.comment_id;
48 let comment_view = blocking(context.pool(), move |conn| {
49 CommentView::read(&conn, comment_id, None)
53 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
55 let report_form = CommentReportForm {
56 creator_id: person_id,
58 original_comment_text: comment_view.comment.content,
59 reason: data.reason.to_owned(),
62 let report = blocking(context.pool(), move |conn| {
63 CommentReport::report(conn, &report_form)
66 .map_err(|_| ApiError::err("couldnt_create_report"))?;
68 let res = CreateCommentReportResponse { success: true };
70 context.chat_server().do_send(SendUserRoomMessage {
71 op: UserOperation::CreateCommentReport,
72 response: res.clone(),
73 local_recipient_id: local_user_view.local_user.id,
77 context.chat_server().do_send(SendModRoomMessage {
78 op: UserOperation::CreateCommentReport,
80 community_id: comment_view.community.id,
88 /// Resolves or unresolves a comment report and notifies the moderators of the community
89 #[async_trait::async_trait(?Send)]
90 impl Perform for ResolveCommentReport {
91 type Response = ResolveCommentReportResponse;
95 context: &Data<LemmyContext>,
96 websocket_id: Option<ConnectionId>,
97 ) -> Result<ResolveCommentReportResponse, LemmyError> {
98 let data: &ResolveCommentReport = &self;
99 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
101 let report_id = data.report_id;
102 let report = blocking(context.pool(), move |conn| {
103 CommentReportView::read(&conn, report_id)
107 let person_id = local_user_view.person.id;
108 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
110 let resolved = data.resolved;
111 let resolve_fun = move |conn: &'_ _| {
113 CommentReport::resolve(conn, report_id, person_id)
115 CommentReport::unresolve(conn, report_id, person_id)
119 if blocking(context.pool(), resolve_fun).await?.is_err() {
120 return Err(ApiError::err("couldnt_resolve_report").into());
123 let report_id = data.report_id;
124 let res = ResolveCommentReportResponse {
129 context.chat_server().do_send(SendModRoomMessage {
130 op: UserOperation::ResolveCommentReport,
131 response: res.clone(),
132 community_id: report.community.id,
140 /// Lists comment reports for a community if an id is supplied
141 /// or returns all comment reports for communities a user moderates
142 #[async_trait::async_trait(?Send)]
143 impl Perform for ListCommentReports {
144 type Response = ListCommentReportsResponse;
148 context: &Data<LemmyContext>,
149 websocket_id: Option<ConnectionId>,
150 ) -> Result<ListCommentReportsResponse, LemmyError> {
151 let data: &ListCommentReports = &self;
152 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
154 let person_id = local_user_view.person.id;
155 let community_id = data.community;
157 collect_moderated_communities(person_id, community_id, context.pool()).await?;
159 let page = data.page;
160 let limit = data.limit;
161 let comments = blocking(context.pool(), move |conn| {
162 CommentReportQueryBuilder::create(conn)
163 .community_ids(community_ids)
170 let res = ListCommentReportsResponse { comments };
172 context.chat_server().do_send(SendUserRoomMessage {
173 op: UserOperation::ListCommentReports,
174 response: res.clone(),
175 local_recipient_id: local_user_view.local_user.id,