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(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
66 let comment_report_view = blocking(context.pool(), move |conn| {
67 CommentReportView::read(conn, report.id, person_id)
71 let res = CommentReportResponse {
75 context.chat_server().do_send(SendModRoomMessage {
76 op: UserOperation::CreateCommentReport,
77 response: res.clone(),
78 community_id: comment_view.community.id,
83 ObjectId::new(comment_view.comment.ap_id),
84 &local_user_view.person.into(),
85 ObjectId::new(comment_view.community.actor_id),
95 /// Resolves or unresolves a comment report and notifies the moderators of the community
96 #[async_trait::async_trait(?Send)]
97 impl Perform for ResolveCommentReport {
98 type Response = CommentReportResponse;
100 #[tracing::instrument(skip(context, websocket_id))]
103 context: &Data<LemmyContext>,
104 websocket_id: Option<ConnectionId>,
105 ) -> Result<CommentReportResponse, LemmyError> {
106 let data: &ResolveCommentReport = self;
107 let local_user_view =
108 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
110 let report_id = data.report_id;
111 let person_id = local_user_view.person.id;
112 let report = blocking(context.pool(), move |conn| {
113 CommentReportView::read(conn, report_id, person_id)
117 let person_id = local_user_view.person.id;
118 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
120 let resolved = data.resolved;
121 let resolve_fun = move |conn: &'_ _| {
123 CommentReport::resolve(conn, report_id, person_id)
125 CommentReport::unresolve(conn, report_id, person_id)
129 blocking(context.pool(), resolve_fun)
131 .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?;
133 let report_id = data.report_id;
134 let comment_report_view = blocking(context.pool(), move |conn| {
135 CommentReportView::read(conn, report_id, person_id)
139 let res = CommentReportResponse {
143 context.chat_server().do_send(SendModRoomMessage {
144 op: UserOperation::ResolveCommentReport,
145 response: res.clone(),
146 community_id: report.community.id,
154 /// Lists comment reports for a community if an id is supplied
155 /// or returns all comment reports for communities a user moderates
156 #[async_trait::async_trait(?Send)]
157 impl Perform for ListCommentReports {
158 type Response = ListCommentReportsResponse;
160 #[tracing::instrument(skip(context, _websocket_id))]
163 context: &Data<LemmyContext>,
164 _websocket_id: Option<ConnectionId>,
165 ) -> Result<ListCommentReportsResponse, LemmyError> {
166 let data: &ListCommentReports = self;
167 let local_user_view =
168 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
170 let person_id = local_user_view.person.id;
171 let admin = local_user_view.person.admin;
172 let community_id = data.community_id;
173 let unresolved_only = data.unresolved_only;
175 let page = data.page;
176 let limit = data.limit;
177 let comment_reports = blocking(context.pool(), move |conn| {
178 CommentReportQueryBuilder::create(conn, person_id, admin)
179 .community_id(community_id)
180 .unresolved_only(unresolved_only)
187 let res = ListCommentReportsResponse { comment_reports };