Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling private messages. Fixes #3640 #4094

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions crates/api/src/local_user/save_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down
2 changes: 2 additions & 0 deletions crates/api_common/src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub struct SaveUserSettings {
/// Whether user avatars or inline images in the UI that are gifs should be allowed to play or
/// should be paused
pub enable_animated_images: Option<bool>,
/// Whether a user can send / receive private messages
pub enable_private_messages: Option<bool>,
/// Whether to auto-collapse bot comments.
pub collapse_bot_comments: Option<bool>,
/// Some vote display mode settings
Expand Down
10 changes: 10 additions & 0 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Comment on lines +363 to +367
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be expressed in a different way that you may or may not prefer:

  local_user_view
    .local_user
    .enable_private_messages
    .then_some(())
    .ok_or_else(|| LemmyErrorType::CouldntCreatePrivateMessage.into())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of that one at least in this case, because it unnecessarily coerces it into an option, when it's already a bool.

}

#[tracing::instrument(skip_all)]
pub async fn build_federated_instances(
local_site: &LocalSite,
Expand Down
11 changes: 11 additions & 0 deletions crates/api_crud/src/private_message/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion crates/apub/src/objects/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -130,9 +136,18 @@ impl Object for ApubPrivateMessage {
let recipient = note.to[0].dereference(context).await?;
PersonBlock::read(&mut context.pool(), recipient.id, creator.id).await?;

// If its a local user, check that they can receive private messages
if recipient.local {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not really needed because the recipient of a federated private message is always a local user.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K fixed.

if let Ok(recipient_local_user) =
LocalUserView::read_person(&mut context.pool(), recipient.id).await
{
check_private_messages_enabled(&recipient_local_user)?;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for apub is done here, so it is working, its just not a publicized setting, so like blocks, senders don't know.

}
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(&note.content, &None, &note.source);
let content = process_markdown(&content, slur_regex, &url_blocklist, context).await?;
let content = markdown_rewrite_remote_links(content, context).await;
Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,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,
}
Expand Down
5 changes: 5 additions & 0 deletions crates/db_schema/src/source/local_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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,
Expand Down Expand Up @@ -116,6 +118,8 @@ pub struct LocalUserInsertForm {
#[new(default)]
pub enable_animated_images: Option<bool>,
#[new(default)]
pub enable_private_messages: Option<bool>,
#[new(default)]
pub collapse_bot_comments: Option<bool>,
#[new(default)]
pub default_comment_sort_type: Option<CommentSortType>,
Expand Down Expand Up @@ -147,6 +151,7 @@ pub struct LocalUserUpdateForm {
pub totp_2fa_enabled: Option<bool>,
pub enable_keyboard_navigation: Option<bool>,
pub enable_animated_images: Option<bool>,
pub enable_private_messages: Option<bool>,
pub collapse_bot_comments: Option<bool>,
pub default_comment_sort_type: Option<CommentSortType>,
}
1 change: 1 addition & 0 deletions crates/db_views/src/registration_application_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE local_user
DROP COLUMN enable_private_messages;

3 changes: 3 additions & 0 deletions migrations/2023-10-24-140438_enable_private_messages/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE local_user
ADD COLUMN enable_private_messages boolean DEFAULT TRUE NOT NULL;