REFACTOR: DRY update code that uses duplicated logic.

This commit is contained in:
Guo Xiang Tan 2017-10-06 14:33:38 +08:00
parent 4ba5e678d8
commit 4552840e54

View File

@ -92,21 +92,27 @@ class PostCreator
return false return false
end end
# Make sure max_allowed_message_recipients setting is respected
if @opts[:target_usernames].present? && !skip_validations? && !@user.staff? if @opts[:target_usernames].present? && !skip_validations? && !@user.staff?
errors[:base] << I18n.t(:max_pm_recepients, recipients_limit: SiteSetting.max_allowed_message_recipients) if @opts[:target_usernames].split(',').length > SiteSetting.max_allowed_message_recipients names = @opts[:target_usernames].split(',')
return false if errors[:base].present?
end
# Make sure none of the users have muted the creator # Make sure max_allowed_message_recipients setting is respected
names = @opts[:target_usernames] max_allowed_message_recipients = SiteSetting.max_allowed_message_recipients
if names.present? && !skip_validations? && !@user.staff?
users = User.where(username: names.split(',').flatten).pluck(:id, :username).to_h if names.length > max_allowed_message_recipients
errors[:base] << I18n.t(:max_pm_recepients,
recipients_limit: max_allowed_message_recipients
)
return false
end
# Make sure none of the users have muted the creator
users = User.where(username: names).pluck(:id, :username).to_h
MutedUser.where(user_id: users.keys, muted_user_id: @user.id).pluck(:user_id).each do |m| MutedUser.where(user_id: users.keys, muted_user_id: @user.id).pluck(:user_id).each do |m|
errors[:base] << I18n.t(:not_accepting_pms, username: users[m]) errors[:base] << I18n.t(:not_accepting_pms, username: users[m])
return false
end end
return false if errors[:base].present?
end end
if new_topic? if new_topic?