mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Allow List for PMs (#10270)
* FEATURE: Allow List for PMs This feature adds a new user setting that is disabled by default that allows them to specify a list of users that are allowed to send them private messages. This way they don't have to maintain a large list of users they don't want to here from and instead just list the people they know they do want. Staff will still always be able to send messages to the user. * Update PR based on feedback
This commit is contained in:
@@ -110,7 +110,7 @@ class PostCreator
|
||||
return false
|
||||
end
|
||||
|
||||
# Make sure none of the users have muted the creator
|
||||
# Make sure none of the users have muted or ignored the creator
|
||||
users = User.where(username_lower: names).pluck(:id, :username).to_h
|
||||
|
||||
User
|
||||
@@ -128,6 +128,23 @@ class PostCreator
|
||||
errors.add(:base, I18n.t(:not_accepting_pms, username: users[m]))
|
||||
end
|
||||
|
||||
# Is Allowed PM users list enabled for any recipients?
|
||||
users_with_allowed_pms = allowed_pms_enabled(users).pluck(:id).uniq
|
||||
|
||||
# If any of the users has allowed_pm_users enabled check to see if the creator
|
||||
# is in their list
|
||||
if users_with_allowed_pms.any?
|
||||
users_sender_can_pm = allowed_pms_enabled(users)
|
||||
.where("allowed_pm_users.allowed_pm_user_id" => @user.id.to_i)
|
||||
.pluck(:id).uniq
|
||||
|
||||
# If not in the list add an error
|
||||
users_not_allowed = users_with_allowed_pms - users_sender_can_pm
|
||||
users_not_allowed.each do |id|
|
||||
errors.add(:base, I18n.t(:not_accepting_pms, username: users[id]))
|
||||
end
|
||||
end
|
||||
|
||||
return false if errors[:base].present?
|
||||
end
|
||||
|
||||
@@ -430,6 +447,17 @@ class PostCreator
|
||||
|
||||
private
|
||||
|
||||
def allowed_pms_enabled(users)
|
||||
User
|
||||
.joins("LEFT JOIN user_options ON user_options.user_id = users.id")
|
||||
.joins("LEFT JOIN allowed_pm_users ON allowed_pm_users.user_id = users.id")
|
||||
.where("
|
||||
user_options.user_id IS NOT NULL AND
|
||||
user_options.user_id IN (:user_ids) AND
|
||||
user_options.enable_allowed_pm_users
|
||||
", user_ids: users.keys)
|
||||
end
|
||||
|
||||
def create_topic
|
||||
return if @topic
|
||||
begin
|
||||
|
||||
Reference in New Issue
Block a user