Add instance setting to disable email notifications (fixes #3011) (#5603)

* Add instance setting to disable email notifications (fixes #3011)

* Dont read site_view again. (#5606)

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Nutomic 2025-04-09 14:04:41 +00:00 committed by GitHub
parent 1f04aceb94
commit 80a5406c7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 36 additions and 5 deletions

View file

@ -88,6 +88,12 @@ pub async fn save_user_settings(
is_valid_matrix_id(matrix_user_id)?;
}
if let Some(send_notifications_to_email) = data.send_notifications_to_email {
if site_view.local_site.disable_email_notifications && send_notifications_to_email {
return Err(LemmyErrorType::EmailNotificationsDisabled.into());
}
}
let local_user_id = local_user_view.local_user.id;
let person_id = local_user_view.person.id;
let default_listing_type = data.default_listing_type;

View file

@ -262,6 +262,8 @@ pub struct CreateSite {
pub comment_downvotes: Option<FederationMode>,
#[cfg_attr(feature = "full", ts(optional))]
pub disallow_nsfw_content: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub disable_email_notifications: Option<bool>,
}
#[skip_serializing_none]
@ -395,6 +397,9 @@ pub struct EditSite {
/// Block NSFW content being created
#[cfg_attr(feature = "full", ts(optional))]
pub disallow_nsfw_content: Option<bool>,
/// Dont send email notifications to users for new replies, mentions etc
#[cfg_attr(feature = "full", ts(optional))]
pub disable_email_notifications: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -27,7 +27,7 @@ use lemmy_db_schema::{
},
traits::{Crud, Likeable},
};
use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_db_views::structs::{LocalUserView, PostView, SiteView};
use lemmy_utils::{
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
utils::{mention::scrape_text_for_mentions, validation::is_valid_body_field},
@ -42,6 +42,7 @@ pub async fn create_comment(
let slur_regex = slur_regex(&context).await?;
let url_blocklist = get_url_blocklist(&context).await?;
let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?;
let local_site = SiteView::read_local(&mut context.pool()).await?.local_site;
is_valid_body_field(&content, false)?;
// Check for a community ban
@ -115,11 +116,12 @@ pub async fn create_comment(
// Scan the comment for user mentions, add those rows
let mentions = scrape_text_for_mentions(&content);
let do_send_email = !local_site.disable_email_notifications;
let recipient_ids = send_local_notifs(
mentions,
PostOrCommentId::Comment(inserted_comment_id),
&local_user_view.person,
true,
do_send_email,
&context,
Some(&local_user_view),
local_instance_id,

View file

@ -171,11 +171,12 @@ pub async fn create_post(
// Scan the post body for user mentions, add those rows
let mentions = scrape_text_for_mentions(&inserted_post.body.clone().unwrap_or_default());
let do_send_email = !local_site.disable_email_notifications;
send_local_notifs(
mentions,
PostOrCommentId::Post(inserted_post.id),
&local_user_view.person,
true,
do_send_email,
&context,
Some(&local_user_view),
local_instance_id,

View file

@ -104,6 +104,7 @@ pub async fn create_site(
comment_upvotes: data.comment_upvotes,
comment_downvotes: data.comment_downvotes,
disallow_nsfw_content: data.disallow_nsfw_content,
disable_email_notifications: data.disable_email_notifications,
..Default::default()
};

View file

@ -113,6 +113,7 @@ pub async fn update_site(
comment_upvotes: data.comment_upvotes,
comment_downvotes: data.comment_downvotes,
disallow_nsfw_content: data.disallow_nsfw_content,
disable_email_notifications: data.disable_email_notifications,
..Default::default()
};

View file

@ -167,7 +167,8 @@ impl ActivityHandler for CreateOrUpdateNote {
// Calculate initial hot_rank
Comment::update_hot_rank(&mut context.pool(), comment.id).await?;
let do_send_email = self.kind == CreateOrUpdateType::Create;
let do_send_email =
self.kind == CreateOrUpdateType::Create && !site_view.local_site.disable_email_notifications;
let actor = self.actor.dereference(context).await?;
// Note:

View file

@ -126,7 +126,8 @@ impl ActivityHandler for CreateOrUpdatePage {
// Calculate initial hot_rank for post
Post::update_ranks(&mut context.pool(), post.id).await?;
let do_send_email = self.kind == CreateOrUpdateType::Create;
let do_send_email =
self.kind == CreateOrUpdateType::Create && !site_view.local_site.disable_email_notifications;
let actor = self.actor.dereference(context).await?;
// Send the post body mentions

View file

@ -100,6 +100,8 @@ pub struct LocalSite {
pub users_active_month: i64,
/// The number of users with any activity in the last half year.
pub users_active_half_year: i64,
/// Dont send email notifications to users for new replies, mentions etc
pub disable_email_notifications: bool,
}
#[derive(Clone, derive_new::new)]
@ -163,6 +165,8 @@ pub struct LocalSiteInsertForm {
pub default_post_time_range_seconds: Option<Option<i32>>,
#[new(default)]
pub disallow_nsfw_content: bool,
#[new(default)]
pub disable_email_notifications: bool,
}
#[derive(Clone, Default)]
@ -198,4 +202,5 @@ pub struct LocalSiteUpdateForm {
pub comment_downvotes: Option<FederationMode>,
pub default_post_time_range_seconds: Option<Option<i32>>,
pub disallow_nsfw_content: Option<bool>,
pub disable_email_notifications: Option<bool>,
}

View file

@ -446,6 +446,7 @@ diesel::table! {
users_active_week -> Int8,
users_active_month -> Int8,
users_active_half_year -> Int8,
disable_email_notifications -> Bool
}
}

View file

@ -162,6 +162,7 @@ pub enum LemmyErrorType {
},
CouldntParsePaginationToken,
PluginError(String),
EmailNotificationsDisabled,
}
/// Federation related errors, these dont need to be translated.

View file

@ -0,0 +1,3 @@
ALTER TABLE local_site
DROP COLUMN disable_email_notifications;

View file

@ -0,0 +1,3 @@
ALTER TABLE local_site
ADD COLUMN disable_email_notifications bool NOT NULL DEFAULT FALSE;