diff --git a/api_tests/package.json b/api_tests/package.json index 4d74cbb29..2901a3750 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 c026d166d..9badc31a0 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 7396d3f8c..a297f22ae 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 9267ee7fa..5ca2d6a62 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 30d819f30..2c96942cf 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 689488feb..88908bccd 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 8cc852bb2..271a319fa 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 4dea567c6..60152d6f8 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 40744a534..976516c5f 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 21808b45e..2b95e1fa2 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 000000000..fa69bbaf4 --- /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 000000000..7de8a2842 --- /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;