mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Support for sending PMs to email addresses (#4988)
* Added support for sending PMs to email addresses. * Made changes after review. * Added settings validator. * Fixed tests.
This commit is contained in:
@@ -162,11 +162,16 @@ class TopicCreator
|
||||
return unless @opts[:archetype] == Archetype.private_message
|
||||
topic.subtype = TopicSubtype.user_to_user unless topic.subtype
|
||||
|
||||
unless @opts[:target_usernames].present? || @opts[:target_group_names].present?
|
||||
unless @opts[:target_usernames].present? || @opts[:target_emails].present? || @opts[:target_group_names].present?
|
||||
rollback_with!(topic, :no_user_selected)
|
||||
end
|
||||
|
||||
if @opts[:target_emails].present? && !@guardian.cand_send_private_messages_to_email? then
|
||||
rollback_with!(topic, :reply_by_email_disabled)
|
||||
end
|
||||
|
||||
add_users(topic, @opts[:target_usernames])
|
||||
add_emails(topic, @opts[:target_emails])
|
||||
add_groups(topic, @opts[:target_group_names])
|
||||
topic.topic_allowed_users.build(user_id: @user.id)
|
||||
end
|
||||
@@ -195,6 +200,23 @@ class TopicCreator
|
||||
rollback_with!(topic, :target_user_not_found) unless len == names.length
|
||||
end
|
||||
|
||||
def add_emails(topic, emails)
|
||||
return unless emails
|
||||
|
||||
emails = emails.split(',').flatten
|
||||
len = 0
|
||||
|
||||
emails.each do |email|
|
||||
display_name = email.split("@").first
|
||||
user = find_or_create_user(email, display_name)
|
||||
@added_users << user
|
||||
topic.topic_allowed_users.build(user_id: user.id)
|
||||
len += 1
|
||||
end
|
||||
|
||||
rollback_with!(topic, :target_user_not_found) unless len == emails.length
|
||||
end
|
||||
|
||||
def add_groups(topic, groups)
|
||||
return unless groups
|
||||
names = groups.split(',').flatten
|
||||
@@ -213,4 +235,23 @@ class TopicCreator
|
||||
def check_can_send_permission!(topic, obj)
|
||||
rollback_with!(topic, :cant_send_pm) unless @opts[:skip_validations] || @guardian.can_send_private_message?(obj)
|
||||
end
|
||||
|
||||
def find_or_create_user(email, display_name)
|
||||
user = User.find_by_email(email)
|
||||
|
||||
if user.nil? && SiteSetting.enable_staged_users
|
||||
username = UserNameSuggester.sanitize_username(display_name) if display_name.present?
|
||||
user = User.create!(
|
||||
email: email,
|
||||
username: UserNameSuggester.suggest(username.presence || email),
|
||||
name: display_name.presence || User.suggest_name(email),
|
||||
staged: true
|
||||
)
|
||||
end
|
||||
|
||||
user
|
||||
rescue
|
||||
rollback_with!(topic, :target_user_not_found)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user