2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
10 use lemmy_apub::protocol::activities::community::report::Report;
11 use lemmy_apub_lib::object_id::ObjectId;
12 use lemmy_db_schema::{source::comment_report::*, traits::Reportable};
14 comment_report_view::{CommentReportQueryBuilder, CommentReportView},
15 comment_view::CommentView,
17 use lemmy_utils::{ConnectionId, LemmyError};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
20 /// Creates a comment report and notifies the moderators of the community
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreateCommentReport {
23 type Response = CommentReportResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommentReportResponse, LemmyError> {
31 let data: &CreateCommentReport = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 // check size of report and check for whitespace
36 let reason = data.reason.trim();
37 if reason.is_empty() {
38 return Err(LemmyError::from_message("report_reason_required"));
40 if reason.chars().count() > 1000 {
41 return Err(LemmyError::from_message("report_too_long"));
44 let person_id = local_user_view.person.id;
45 let comment_id = data.comment_id;
46 let comment_view = blocking(context.pool(), move |conn| {
47 CommentView::read(conn, comment_id, None)
51 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
53 let report_form = CommentReportForm {
54 creator_id: person_id,
56 original_comment_text: comment_view.comment.content,
57 reason: data.reason.to_owned(),
60 let report = blocking(context.pool(), move |conn| {
61 CommentReport::report(conn, &report_form)
64 .map_err(LemmyError::from)
65 .map_err(|e| e.with_message("couldnt_create_report"))?;
67 let comment_report_view = blocking(context.pool(), move |conn| {
68 CommentReportView::read(conn, report.id, person_id)
72 let res = CommentReportResponse {
76 context.chat_server().do_send(SendModRoomMessage {
77 op: UserOperation::CreateCommentReport,
78 response: res.clone(),
79 community_id: comment_view.community.id,
84 ObjectId::new(comment_view.comment.ap_id),
85 &local_user_view.person.into(),
86 ObjectId::new(comment_view.community.actor_id),
96 /// Resolves or unresolves a comment report and notifies the moderators of the community
97 #[async_trait::async_trait(?Send)]
98 impl Perform for ResolveCommentReport {
99 type Response = CommentReportResponse;
101 #[tracing::instrument(skip(context, websocket_id))]
104 context: &Data<LemmyContext>,
105 websocket_id: Option<ConnectionId>,
106 ) -> Result<CommentReportResponse, LemmyError> {
107 let data: &ResolveCommentReport = self;
108 let local_user_view =
109 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
111 let report_id = data.report_id;
112 let person_id = local_user_view.person.id;
113 let report = blocking(context.pool(), move |conn| {
114 CommentReportView::read(conn, report_id, person_id)
118 let person_id = local_user_view.person.id;
119 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
121 let resolved = data.resolved;
122 let resolve_fun = move |conn: &'_ _| {
124 CommentReport::resolve(conn, report_id, person_id)
126 CommentReport::unresolve(conn, report_id, person_id)
130 blocking(context.pool(), resolve_fun)
132 .map_err(LemmyError::from)
133 .map_err(|e| e.with_message("couldnt_resolve_report"))?;
135 let report_id = data.report_id;
136 let comment_report_view = blocking(context.pool(), move |conn| {
137 CommentReportView::read(conn, report_id, person_id)
141 let res = CommentReportResponse {
145 context.chat_server().do_send(SendModRoomMessage {
146 op: UserOperation::ResolveCommentReport,
147 response: res.clone(),
148 community_id: report.community.id,
156 /// Lists comment reports for a community if an id is supplied
157 /// or returns all comment reports for communities a user moderates
158 #[async_trait::async_trait(?Send)]
159 impl Perform for ListCommentReports {
160 type Response = ListCommentReportsResponse;
162 #[tracing::instrument(skip(context, _websocket_id))]
165 context: &Data<LemmyContext>,
166 _websocket_id: Option<ConnectionId>,
167 ) -> Result<ListCommentReportsResponse, LemmyError> {
168 let data: &ListCommentReports = self;
169 let local_user_view =
170 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
172 let person_id = local_user_view.person.id;
173 let admin = local_user_view.person.admin;
174 let community_id = data.community_id;
175 let unresolved_only = data.unresolved_only;
177 let page = data.page;
178 let limit = data.limit;
179 let comment_reports = blocking(context.pool(), move |conn| {
180 CommentReportQueryBuilder::create(conn, person_id, admin)
181 .community_id(community_id)
182 .unresolved_only(unresolved_only)
189 let res = ListCommentReportsResponse { comment_reports };