From ddb62681648cec473a19dce29861d48882e71943 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 14 Feb 2023 10:57:08 -0500 Subject: [PATCH] Adding a site option to email admins for new reports. (#2730) - Fixes #2551 --- api_tests/package.json | 2 +- crates/api/src/comment_report/create.rs | 13 ++++++++++- crates/api/src/post_report/create.rs | 13 ++++++++++- .../api/src/private_message_report/create.rs | 13 ++++++++++- crates/api_common/src/site.rs | 1 + crates/api_common/src/utils.rs | 22 +++++++++++++++++++ crates/api_crud/src/site/update.rs | 1 + crates/db_schema/src/schema.rs | 1 + crates/db_schema/src/source/local_site.rs | 3 +++ crates/utils/translations | 2 +- .../down.sql | 1 + .../up.sql | 2 ++ 12 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 migrations/2023-02-13-172528_add_report_email_admins/down.sql create mode 100644 migrations/2023-02-13-172528_add_report_email_admins/up.sql diff --git a/api_tests/package.json b/api_tests/package.json index 4d74cbb2..2901a375 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -18,7 +18,7 @@ "eslint": "^8.25.0", "eslint-plugin-prettier": "^4.0.0", "jest": "^27.0.6", - "lemmy-js-client": "0.17.0-rc.61", + "lemmy-js-client": "0.17.2-rc.1", "node-fetch": "^2.6.1", "prettier": "^2.7.1", "ts-jest": "^27.0.3", diff --git a/crates/api/src/comment_report/create.rs b/crates/api/src/comment_report/create.rs index c026d166..9badc31a 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentReportResponse, CreateCommentReport}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt}, + utils::{check_community_ban, get_local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -54,6 +54,17 @@ impl Perform for CreateCommentReport { let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?; + // Email the admins + if local_site.reports_email_admins { + send_new_report_email_to_admins( + &comment_report_view.creator.name, + &comment_report_view.comment_creator.name, + context.pool(), + context.settings(), + ) + .await?; + } + let res = CommentReportResponse { comment_report_view, }; diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index 7396d3f8..a297f22a 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{CreatePostReport, PostReportResponse}, - utils::{check_community_ban, get_local_user_view_from_jwt}, + utils::{check_community_ban, get_local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -56,6 +56,17 @@ impl Perform for CreatePostReport { let post_report_view = PostReportView::read(context.pool(), report.id, person_id).await?; + // Email the admins + if local_site.reports_email_admins { + send_new_report_email_to_admins( + &post_report_view.creator.name, + &post_report_view.post_creator.name, + context.pool(), + context.settings(), + ) + .await?; + } + let res = PostReportResponse { post_report_view }; context diff --git a/crates/api/src/private_message_report/create.rs b/crates/api/src/private_message_report/create.rs index 9267ee7f..5ca2d6a6 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse}, - utils::get_local_user_view_from_jwt, + utils::{get_local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -53,6 +53,17 @@ impl Perform for CreatePrivateMessageReport { let private_message_report_view = PrivateMessageReportView::read(context.pool(), report.id).await?; + // Email the admins + if local_site.reports_email_admins { + send_new_report_email_to_admins( + &private_message_report_view.creator.name, + &private_message_report_view.private_message_creator.name, + context.pool(), + context.settings(), + ) + .await?; + } + let res = PrivateMessageReportResponse { private_message_report_view, }; diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 30d819f3..2c96942c 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -195,6 +195,7 @@ pub struct EditSite { pub blocked_instances: Option>, pub taglines: Option>, pub registration_mode: Option, + pub reports_email_admins: Option, pub auth: Sensitive, } diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 689488fe..88908bcc 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -483,6 +483,28 @@ pub async fn send_new_applicant_email_to_admins( Ok(()) } +/// Send a report to all admins +pub async fn send_new_report_email_to_admins( + reporter_username: &str, + reported_username: &str, + pool: &DbPool, + settings: &Settings, +) -> Result<(), LemmyError> { + // Collect the admins with emails + let admins = LocalUserSettingsView::list_admins_with_emails(pool).await?; + + let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),); + + for admin in &admins { + let email = &admin.local_user.email.clone().expect("email"); + let lang = get_interface_language_from_settings(admin); + let subject = lang.new_report_subject(&settings.hostname, reporter_username, reported_username); + let body = lang.new_report_body(reports_link); + send_email(&subject, email, &admin.person.name, &body, settings)?; + } + Ok(()) +} + pub async fn check_registration_application( local_user_view: &LocalUserView, local_site: &LocalSite, diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 8cc852bb..271a319f 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -119,6 +119,7 @@ impl PerformCrud for EditSite { .federation_worker_count(data.federation_worker_count) .captcha_enabled(data.captcha_enabled) .captcha_difficulty(data.captcha_difficulty.clone()) + .reports_email_admins(data.reports_email_admins) .build(); let update_local_site = LocalSite::update(context.pool(), &local_site_form) diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 4dea567c..60152d6f 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -698,6 +698,7 @@ table! { captcha_enabled -> Bool, captcha_difficulty -> Text, registration_mode -> RegistrationModeType, + reports_email_admins -> Bool, published -> Timestamp, updated -> Nullable, } diff --git a/crates/db_schema/src/source/local_site.rs b/crates/db_schema/src/source/local_site.rs index 40744a53..976516c5 100644 --- a/crates/db_schema/src/source/local_site.rs +++ b/crates/db_schema/src/source/local_site.rs @@ -31,6 +31,7 @@ pub struct LocalSite { pub captcha_enabled: bool, pub captcha_difficulty: String, pub registration_mode: RegistrationMode, + pub reports_email_admins: bool, pub published: chrono::NaiveDateTime, pub updated: Option, } @@ -62,6 +63,7 @@ pub struct LocalSiteInsertForm { pub captcha_enabled: Option, pub captcha_difficulty: Option, pub registration_mode: Option, + pub reports_email_admins: Option, } #[derive(Clone, TypedBuilder)] @@ -89,6 +91,7 @@ pub struct LocalSiteUpdateForm { pub captcha_enabled: Option, pub captcha_difficulty: Option, pub registration_mode: Option, + pub reports_email_admins: Option, pub updated: Option>, } diff --git a/crates/utils/translations b/crates/utils/translations index 21808b45..2b95e1fa 160000 --- a/crates/utils/translations +++ b/crates/utils/translations @@ -1 +1 @@ -Subproject commit 21808b45ea3ef7fa91654d4f6738b5144da6bfe7 +Subproject commit 2b95e1fa2f6ecb50a5b0575b1362b9f81a60e9b7 diff --git a/migrations/2023-02-13-172528_add_report_email_admins/down.sql b/migrations/2023-02-13-172528_add_report_email_admins/down.sql new file mode 100644 index 00000000..fa69bbaf --- /dev/null +++ b/migrations/2023-02-13-172528_add_report_email_admins/down.sql @@ -0,0 +1 @@ +alter table local_site drop column reports_email_admins; diff --git a/migrations/2023-02-13-172528_add_report_email_admins/up.sql b/migrations/2023-02-13-172528_add_report_email_admins/up.sql new file mode 100644 index 00000000..7de8a284 --- /dev/null +++ b/migrations/2023-02-13-172528_add_report_email_admins/up.sql @@ -0,0 +1,2 @@ +-- Adding a field to email admins for new reports +alter table local_site add column reports_email_admins boolean not null default false; -- 2.44.1