Merge pull request #3445 from lukegb/bug/anonymous-emails
Don't send emails to anonymous users
This commit is contained in:
commit
d40d308e45
|
@ -16,6 +16,7 @@ module Jobs
|
||||||
# Find the user
|
# Find the user
|
||||||
@user = User.find_by(id: args[:user_id])
|
@user = User.find_by(id: args[:user_id])
|
||||||
return skip(I18n.t("email_log.no_user", user_id: args[:user_id])) unless @user
|
return skip(I18n.t("email_log.no_user", user_id: args[:user_id])) unless @user
|
||||||
|
return skip(I18n.t("email_log.anonymous_user")) if @user.anonymous?
|
||||||
return skip(I18n.t("email_log.suspended_not_pm")) if @user.suspended? && args[:type] != :user_private_message
|
return skip(I18n.t("email_log.suspended_not_pm")) if @user.suspended? && args[:type] != :user_private_message
|
||||||
|
|
||||||
seen_recently = (@user.last_seen_at.present? && @user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago)
|
seen_recently = (@user.last_seen_at.present? && @user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago)
|
||||||
|
|
|
@ -108,8 +108,15 @@ class User < ActiveRecord::Base
|
||||||
# set to true to optimize creation and save for imports
|
# set to true to optimize creation and save for imports
|
||||||
attr_accessor :import_mode
|
attr_accessor :import_mode
|
||||||
|
|
||||||
# excluding fake users like the system user
|
# excluding fake users like the system user or anonymous users
|
||||||
scope :real, -> { where('id > 0') }
|
scope :real, -> { where('id > 0').where('NOT EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM user_custom_fields ucf
|
||||||
|
WHERE
|
||||||
|
ucf.user_id = users.id AND
|
||||||
|
ucf.name = ? AND
|
||||||
|
ucf.value::int > 0
|
||||||
|
)', 'master_id') }
|
||||||
|
|
||||||
scope :staff, -> { where("admin OR moderator") }
|
scope :staff, -> { where("admin OR moderator") }
|
||||||
|
|
||||||
|
|
|
@ -2006,6 +2006,7 @@ en:
|
||||||
|
|
||||||
email_log:
|
email_log:
|
||||||
no_user: "Can't find user with id %{user_id}"
|
no_user: "Can't find user with id %{user_id}"
|
||||||
|
anonymous_user: "User is anonymous"
|
||||||
suspended_not_pm: "User is suspended, not a message"
|
suspended_not_pm: "User is suspended, not a message"
|
||||||
seen_recently: "User was seen recently"
|
seen_recently: "User was seen recently"
|
||||||
post_not_found: "Can't find a post with id %{post_id}"
|
post_not_found: "Can't find a post with id %{post_id}"
|
||||||
|
|
|
@ -86,3 +86,16 @@ Fabricator(:trust_level_4, from: :user) do
|
||||||
email { sequence(:email) { |i| "tl4#{i}@elderfun.com" } }
|
email { sequence(:email) { |i| "tl4#{i}@elderfun.com" } }
|
||||||
trust_level TrustLevel[4]
|
trust_level TrustLevel[4]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Fabricator(:anonymous, from: :user) do
|
||||||
|
name ''
|
||||||
|
username { sequence(:username) { |i| "anonymous#{i}" } }
|
||||||
|
email { sequence(:email) { |i| "anonymous#{i}@anonymous.com" } }
|
||||||
|
trust_level TrustLevel[1]
|
||||||
|
trust_level_locked true
|
||||||
|
|
||||||
|
after_create do |user|
|
||||||
|
user.custom_fields["master_id"] = 1
|
||||||
|
user.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -47,6 +47,16 @@ describe Jobs::NotifyMailingListSubscribers do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "to an anonymous user with mailing list on" do
|
||||||
|
let(:user) { Fabricate(:anonymous, mailing_list_mode: true) }
|
||||||
|
let!(:post) { Fabricate(:post, user: user) }
|
||||||
|
|
||||||
|
it "doesn't send the email to the user" do
|
||||||
|
UserNotifications.expects(:mailing_list_notify).with(user, post).never
|
||||||
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "with mailing list off" do
|
context "with mailing list off" do
|
||||||
let(:user) { Fabricate(:user, mailing_list_mode: false) }
|
let(:user) { Fabricate(:user, mailing_list_mode: false) }
|
||||||
let!(:post) { Fabricate(:post, user: user) }
|
let!(:post) { Fabricate(:post, user: user) }
|
||||||
|
|
|
@ -9,6 +9,7 @@ describe Jobs::UserEmail do
|
||||||
|
|
||||||
let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) }
|
let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) }
|
||||||
let(:suspended) { Fabricate(:user, last_seen_at: 10.minutes.ago, suspended_at: 5.minutes.ago, suspended_till: 7.days.from_now ) }
|
let(:suspended) { Fabricate(:user, last_seen_at: 10.minutes.ago, suspended_at: 5.minutes.ago, suspended_till: 7.days.from_now ) }
|
||||||
|
let(:anonymous) { Fabricate(:anonymous, last_seen_at: 11.minutes.ago ) }
|
||||||
let(:mailer) { Mail::Message.new(to: user.email) }
|
let(:mailer) { Mail::Message.new(to: user.email) }
|
||||||
|
|
||||||
it "raises an error when there is no user" do
|
it "raises an error when there is no user" do
|
||||||
|
@ -96,6 +97,22 @@ describe Jobs::UserEmail do
|
||||||
Jobs::UserEmail.new.execute(type: :private_message, user_id: suspended.id, post_id: pm_from_staff.id)
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: suspended.id, post_id: pm_from_staff.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'user is anonymous' do
|
||||||
|
before { SiteSetting.stubs(:allow_anonymous_posting).returns(true) }
|
||||||
|
|
||||||
|
it "doesn't send email for a pm from a regular user" do
|
||||||
|
Email::Sender.any_instance.expects(:send).never
|
||||||
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: anonymous.id, post_id: post.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't send email for a pm from a staff user" do
|
||||||
|
pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
||||||
|
pm_from_staff.topic.topic_allowed_users.create!(user_id: anonymous.id)
|
||||||
|
Email::Sender.any_instance.expects(:send).never
|
||||||
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: anonymous.id, post_id: pm_from_staff.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -169,6 +186,28 @@ describe Jobs::UserEmail do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'user is anonymous' do
|
||||||
|
before { SiteSetting.stubs(:allow_anonymous_posting).returns(true) }
|
||||||
|
|
||||||
|
it "doesn't send email for a pm from a regular user" do
|
||||||
|
Email::Sender.any_instance.expects(:send).never
|
||||||
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: notification.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't send email for a pm from staff" do
|
||||||
|
pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
||||||
|
pm_from_staff.topic.topic_allowed_users.create!(user_id: anonymous.id)
|
||||||
|
pm_notification = Fabricate(:notification,
|
||||||
|
user: anonymous,
|
||||||
|
topic: pm_from_staff.topic,
|
||||||
|
post_number: pm_from_staff.post_number,
|
||||||
|
data: { original_post_id: pm_from_staff.id }.to_json
|
||||||
|
)
|
||||||
|
Email::Sender.any_instance.expects(:send).never
|
||||||
|
Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: pm_notification.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue