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 = match blocking(context.pool(), move |conn| {
63 CommentReport::report(conn, &report_form)
68 Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
71 let res = CreateCommentReportResponse { success: true };
73 context.chat_server().do_send(SendUserRoomMessage {
74 op: UserOperation::CreateCommentReport,
75 response: res.clone(),
76 local_recipient_id: local_user_view.local_user.id,
80 context.chat_server().do_send(SendModRoomMessage {
81 op: UserOperation::CreateCommentReport,
83 community_id: comment_view.community.id,
91 /// Resolves or unresolves a comment report and notifies the moderators of the community
92 #[async_trait::async_trait(?Send)]
93 impl Perform for ResolveCommentReport {
94 type Response = ResolveCommentReportResponse;
98 context: &Data<LemmyContext>,
99 websocket_id: Option<ConnectionId>,
100 ) -> Result<ResolveCommentReportResponse, LemmyError> {
101 let data: &ResolveCommentReport = &self;
102 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
104 let report_id = data.report_id;
105 let report = blocking(context.pool(), move |conn| {
106 CommentReportView::read(&conn, report_id)
110 let person_id = local_user_view.person.id;
111 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
113 let resolved = data.resolved;
114 let resolve_fun = move |conn: &'_ _| {
116 CommentReport::resolve(conn, report_id, person_id)
118 CommentReport::unresolve(conn, report_id, person_id)
122 if blocking(context.pool(), resolve_fun).await?.is_err() {
123 return Err(ApiError::err("couldnt_resolve_report").into());
126 let report_id = data.report_id;
127 let res = ResolveCommentReportResponse {
132 context.chat_server().do_send(SendModRoomMessage {
133 op: UserOperation::ResolveCommentReport,
134 response: res.clone(),
135 community_id: report.community.id,
143 /// Lists comment reports for a community if an id is supplied
144 /// or returns all comment reports for communities a user moderates
145 #[async_trait::async_trait(?Send)]
146 impl Perform for ListCommentReports {
147 type Response = ListCommentReportsResponse;
151 context: &Data<LemmyContext>,
152 websocket_id: Option<ConnectionId>,
153 ) -> Result<ListCommentReportsResponse, LemmyError> {
154 let data: &ListCommentReports = &self;
155 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
157 let person_id = local_user_view.person.id;
158 let community_id = data.community;
160 collect_moderated_communities(person_id, community_id, context.pool()).await?;
162 let page = data.page;
163 let limit = data.limit;
164 let comments = blocking(context.pool(), move |conn| {
165 CommentReportQueryBuilder::create(conn)
166 .community_ids(community_ids)
173 let res = ListCommentReportsResponse { comments };
175 context.chat_server().do_send(SendUserRoomMessage {
176 op: UserOperation::ListCommentReports,
177 response: res.clone(),
178 local_recipient_id: local_user_view.local_user.id,