mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Emails to banned users will be sent for notifications of PMs from staff users
This commit is contained in:
parent
2305cf94ea
commit
c8d5db38d6
2 changed files with 44 additions and 3 deletions
|
@ -13,7 +13,8 @@ module Jobs
|
||||||
|
|
||||||
# Find the user
|
# Find the user
|
||||||
user = User.where(id: args[:user_id]).first
|
user = User.where(id: args[:user_id]).first
|
||||||
return if !user || user.is_banned?
|
return unless user
|
||||||
|
return if user.is_banned? && 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)
|
||||||
seen_recently = false if user.email_always
|
seen_recently = false if user.email_always
|
||||||
|
@ -36,9 +37,8 @@ module Jobs
|
||||||
notification = nil
|
notification = nil
|
||||||
notification = Notification.where(id: args[:notification_id]).first if args[:notification_id].present?
|
notification = Notification.where(id: args[:notification_id]).first if args[:notification_id].present?
|
||||||
if notification.present?
|
if notification.present?
|
||||||
|
|
||||||
# Don't email a user about a post when we've seen them recently.
|
# Don't email a user about a post when we've seen them recently.
|
||||||
return if seen_recently
|
return if seen_recently && !user.is_banned?
|
||||||
|
|
||||||
# Load the post if present
|
# Load the post if present
|
||||||
email_args[:post] ||= notification.post
|
email_args[:post] ||= notification.post
|
||||||
|
@ -69,6 +69,7 @@ module Jobs
|
||||||
post &&
|
post &&
|
||||||
(post.topic.blank? ||
|
(post.topic.blank? ||
|
||||||
post.user_deleted? ||
|
post.user_deleted? ||
|
||||||
|
(user.is_banned? && !post.user.try(:staff?)) ||
|
||||||
PostTiming.where(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id).present?)
|
PostTiming.where(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id).present?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ describe Jobs::UserEmail do
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) }
|
let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) }
|
||||||
|
let(:banned) { Fabricate(:user, last_seen_at: 10.minutes.ago, banned_at: 5.minutes.ago, banned_till: 7.days.from_now ) }
|
||||||
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
|
||||||
|
@ -82,6 +83,19 @@ describe Jobs::UserEmail do
|
||||||
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'user is banned' do
|
||||||
|
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: banned.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: banned.id)
|
||||||
|
Email::Sender.any_instance.expects(:send).never
|
||||||
|
Jobs::UserEmail.new.execute(type: :private_message, user_id: banned.id, post_id: pm_from_staff.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,6 +121,32 @@ describe Jobs::UserEmail do
|
||||||
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'user is banned' do
|
||||||
|
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: banned.id, notification_id: notification.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'pm from staff' do
|
||||||
|
before do
|
||||||
|
@pm_from_staff = Fabricate(:post, user: Fabricate(:moderator))
|
||||||
|
@pm_from_staff.topic.topic_allowed_users.create!(user_id: banned.id)
|
||||||
|
@pm_notification = Fabricate(:notification, user: banned, topic: @pm_from_staff.topic, post_number: @pm_from_staff.post_number)
|
||||||
|
UserNotifications.expects(:user_private_message).with(banned, notification: @pm_notification, post: @pm_from_staff).returns(mailer)
|
||||||
|
end
|
||||||
|
|
||||||
|
subject(:execute_user_email_job) { Jobs::UserEmail.new.execute(type: :user_private_message, user_id: banned.id, notification_id: @pm_notification.id) }
|
||||||
|
|
||||||
|
it "sends an email" do
|
||||||
|
execute_user_email_job
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sends an email even if user was last seen recently" do
|
||||||
|
banned.update_column(:last_seen_at, 1.minute.ago)
|
||||||
|
execute_user_email_job
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue