From 39eeb2cbb3bd45a01501647db2da87068662945b Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 7 Nov 2024 22:16:28 -0500 Subject: [PATCH] Allow disabling private messages. Fixes #3640 (#4094) * Allow disabling private messages. Fixes #3640 * Fix typo. * Fixing local user check in apub code. * Removing pointless local check. --- crates/api/src/local_user/save_settings.rs | 1 + crates/api_common/src/person.rs | 3 +++ crates/api_common/src/utils.rs | 10 ++++++++++ crates/api_crud/src/private_message/create.rs | 11 +++++++++++ crates/apub/src/objects/private_message.rs | 15 ++++++++++++++- crates/db_schema/src/schema.rs | 1 + crates/db_schema/src/source/local_user.rs | 5 +++++ .../db_views/src/registration_application_view.rs | 1 + .../down.sql | 3 +++ .../up.sql | 3 +++ 10 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 migrations/2023-10-24-140438_enable_private_messages/down.sql create mode 100644 migrations/2023-10-24-140438_enable_private_messages/up.sql diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 08820cadd..ac2e321a1 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -141,6 +141,7 @@ pub async fn save_user_settings( post_listing_mode: data.post_listing_mode, enable_keyboard_navigation: data.enable_keyboard_navigation, enable_animated_images: data.enable_animated_images, + enable_private_messages: data.enable_private_messages, collapse_bot_comments: data.collapse_bot_comments, ..Default::default() }; diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 6b64d8467..742dc88db 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -163,6 +163,9 @@ pub struct SaveUserSettings { /// should be paused #[cfg_attr(feature = "full", ts(optional))] pub enable_animated_images: Option, + /// Whether a user can send / receive private messages + #[cfg_attr(feature = "full", ts(optional))] + pub enable_private_messages: Option, /// Whether to auto-collapse bot comments. #[cfg_attr(feature = "full", ts(optional))] pub collapse_bot_comments: Option, diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 6b79ce6ba..64b0df346 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -355,6 +355,16 @@ pub fn check_private_instance( } } +/// If private messages are disabled, dont allow them to be sent / received +#[tracing::instrument(skip_all)] +pub fn check_private_messages_enabled(local_user_view: &LocalUserView) -> Result<(), LemmyError> { + if !local_user_view.local_user.enable_private_messages { + Err(LemmyErrorType::CouldntCreatePrivateMessage)? + } else { + Ok(()) + } +} + #[tracing::instrument(skip_all)] pub async fn build_federated_instances( local_site: &LocalSite, diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 5f3c7b639..1a6a78d00 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -5,6 +5,7 @@ use lemmy_api_common::{ private_message::{CreatePrivateMessage, PrivateMessageResponse}, send_activity::{ActivityChannel, SendActivityData}, utils::{ + check_private_messages_enabled, get_interface_language, get_url_blocklist, local_site_to_slur_regex, @@ -46,6 +47,16 @@ pub async fn create_private_message( ) .await?; + check_private_messages_enabled(&local_user_view)?; + + // Don't allow local sends to people who have private messages disabled + let recipient_local_user_opt = LocalUserView::read_person(&mut context.pool(), data.recipient_id) + .await + .ok(); + if let Some(recipient_local_user) = recipient_local_user_opt { + check_private_messages_enabled(&recipient_local_user)?; + } + let private_message_form = PrivateMessageInsertForm::new( local_user_view.person.id, data.recipient_id, diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 3a61eb4a5..f3a9f140c 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -16,7 +16,12 @@ use activitypub_federation::{ use chrono::{DateTime, Utc}; use lemmy_api_common::{ context::LemmyContext, - utils::{get_url_blocklist, local_site_opt_to_slur_regex, process_markdown}, + utils::{ + check_private_messages_enabled, + get_url_blocklist, + local_site_opt_to_slur_regex, + process_markdown, + }, }; use lemmy_db_schema::{ source::{ @@ -28,6 +33,7 @@ use lemmy_db_schema::{ traits::Crud, utils::naive_now, }; +use lemmy_db_views::structs::LocalUserView; use lemmy_utils::{ error::{FederationError, LemmyError, LemmyErrorType, LemmyResult}, utils::markdown::markdown_to_html, @@ -130,9 +136,16 @@ impl Object for ApubPrivateMessage { let recipient = note.to[0].dereference(context).await?; PersonBlock::read(&mut context.pool(), recipient.id, creator.id).await?; + // Check that they can receive private messages + if let Ok(recipient_local_user) = + LocalUserView::read_person(&mut context.pool(), recipient.id).await + { + check_private_messages_enabled(&recipient_local_user)?; + } let local_site = LocalSite::read(&mut context.pool()).await.ok(); let slur_regex = &local_site_opt_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(context).await?; + let content = read_from_string_or_source(¬e.content, &None, ¬e.source); let content = process_markdown(&content, slur_regex, &url_blocklist, context).await?; let content = markdown_rewrite_remote_links(content, context).await; diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index a986f55d1..960abf44a 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -480,6 +480,7 @@ diesel::table! { totp_2fa_enabled -> Bool, enable_keyboard_navigation -> Bool, enable_animated_images -> Bool, + enable_private_messages -> Bool, collapse_bot_comments -> Bool, default_comment_sort_type -> CommentSortTypeEnum, } diff --git a/crates/db_schema/src/source/local_user.rs b/crates/db_schema/src/source/local_user.rs index d5bbfbe19..fd15253cc 100644 --- a/crates/db_schema/src/source/local_user.rs +++ b/crates/db_schema/src/source/local_user.rs @@ -63,6 +63,8 @@ pub struct LocalUser { /// Whether user avatars and inline images in the UI that are gifs should be allowed to play or /// should be paused pub enable_animated_images: bool, + /// Whether a user can send / receive private messages + pub enable_private_messages: bool, /// Whether to auto-collapse bot comments. pub collapse_bot_comments: bool, pub default_comment_sort_type: CommentSortType, @@ -117,6 +119,8 @@ pub struct LocalUserInsertForm { #[new(default)] pub enable_animated_images: Option, #[new(default)] + pub enable_private_messages: Option, + #[new(default)] pub collapse_bot_comments: Option, #[new(default)] pub default_comment_sort_type: Option, @@ -148,6 +152,7 @@ pub struct LocalUserUpdateForm { pub totp_2fa_enabled: Option, pub enable_keyboard_navigation: Option, pub enable_animated_images: Option, + pub enable_private_messages: Option, pub collapse_bot_comments: Option, pub default_comment_sort_type: Option, } diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index e0a1ea953..b5821ef26 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -240,6 +240,7 @@ mod tests { totp_2fa_enabled: inserted_sara_local_user.totp_2fa_enabled, enable_keyboard_navigation: inserted_sara_local_user.enable_keyboard_navigation, enable_animated_images: inserted_sara_local_user.enable_animated_images, + enable_private_messages: inserted_sara_local_user.enable_private_messages, collapse_bot_comments: inserted_sara_local_user.collapse_bot_comments, }, creator: Person { diff --git a/migrations/2023-10-24-140438_enable_private_messages/down.sql b/migrations/2023-10-24-140438_enable_private_messages/down.sql new file mode 100644 index 000000000..0b4846679 --- /dev/null +++ b/migrations/2023-10-24-140438_enable_private_messages/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_user + DROP COLUMN enable_private_messages; + diff --git a/migrations/2023-10-24-140438_enable_private_messages/up.sql b/migrations/2023-10-24-140438_enable_private_messages/up.sql new file mode 100644 index 000000000..a0c2919de --- /dev/null +++ b/migrations/2023-10-24-140438_enable_private_messages/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_user + ADD COLUMN enable_private_messages boolean DEFAULT TRUE NOT NULL; +