]> Untitled Git - lemmy.git/commitdiff
Adding a site option to email admins for new reports. (#2730)
authorDessalines <dessalines@users.noreply.github.com>
Tue, 14 Feb 2023 15:57:08 +0000 (10:57 -0500)
committerGitHub <noreply@github.com>
Tue, 14 Feb 2023 15:57:08 +0000 (00:57 +0900)
- Fixes #2551

12 files changed:
api_tests/package.json
crates/api/src/comment_report/create.rs
crates/api/src/post_report/create.rs
crates/api/src/private_message_report/create.rs
crates/api_common/src/site.rs
crates/api_common/src/utils.rs
crates/api_crud/src/site/update.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/local_site.rs
crates/utils/translations
migrations/2023-02-13-172528_add_report_email_admins/down.sql [new file with mode: 0644]
migrations/2023-02-13-172528_add_report_email_admins/up.sql [new file with mode: 0644]

index 4d74cbb2955ce4ea7f039dc60a0511fc2e64e8bb..2901a375085b837105ab9dff5061ee890f3ae9a4 100644 (file)
@@ -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",
index c026d166da61e317efbf5485122316bdc0b4b133..9badc31a07694a2b45cdf678c17a40253e155970 100644 (file)
@@ -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,
     };
index 7396d3f8c3512b1f8c5e04c997e41618013879d4..a297f22ae94e0803436f6f889b9c9033d60deb61 100644 (file)
@@ -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
index 9267ee7fa3225cb4fcb64bf2120f533528866bcd..5ca2d6a628a0fdb8e09dd6d52dccdd118ae7d797 100644 (file)
@@ -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,
     };
index 30d819f30f6cc3ac537930725d27aa622cb7c3ef..2c96942cfdee635bb3dc73c0358847bf1d263116 100644 (file)
@@ -195,6 +195,7 @@ pub struct EditSite {
   pub blocked_instances: Option<Vec<String>>,
   pub taglines: Option<Vec<String>>,
   pub registration_mode: Option<RegistrationMode>,
+  pub reports_email_admins: Option<bool>,
   pub auth: Sensitive<String>,
 }
 
index 689488feba61abd64f9db0fe424c9aa6a411dbdc..88908bccdf3112e4b220ff52598fbfe842548430 100644 (file)
@@ -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,
index 8cc852bb2fbbd41d9e29c497f4ab93f7ca691093..271a319fa7d943ce943d7f6154c3c3f53da92d06 100644 (file)
@@ -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)
index 4dea567c6ee4b9776a7b8cd831d9572b258c526c..60152d6f831b3dbd53f691cdb179fa2660d740d2 100644 (file)
@@ -698,6 +698,7 @@ table! {
     captcha_enabled -> Bool,
     captcha_difficulty -> Text,
     registration_mode -> RegistrationModeType,
+    reports_email_admins -> Bool,
     published -> Timestamp,
     updated -> Nullable<Timestamp>,
   }
index 40744a534a55b70602f7f5725de6c962daf44f45..976516c5ff91ff03f88e922f7c90f3c5f2eb9eb6 100644 (file)
@@ -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<chrono::NaiveDateTime>,
 }
@@ -62,6 +63,7 @@ pub struct LocalSiteInsertForm {
   pub captcha_enabled: Option<bool>,
   pub captcha_difficulty: Option<String>,
   pub registration_mode: Option<RegistrationMode>,
+  pub reports_email_admins: Option<bool>,
 }
 
 #[derive(Clone, TypedBuilder)]
@@ -89,6 +91,7 @@ pub struct LocalSiteUpdateForm {
   pub captcha_enabled: Option<bool>,
   pub captcha_difficulty: Option<String>,
   pub registration_mode: Option<RegistrationMode>,
+  pub reports_email_admins: Option<bool>,
   pub updated: Option<Option<chrono::NaiveDateTime>>,
 }
 
index 21808b45ea3ef7fa91654d4f6738b5144da6bfe7..2b95e1fa2f6ecb50a5b0575b1362b9f81a60e9b7 160000 (submodule)
@@ -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 (file)
index 0000000..fa69bba
--- /dev/null
@@ -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 (file)
index 0000000..7de8a28
--- /dev/null
@@ -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;