FIX: invited staged users would sometimes not get notified of replies

This commit is contained in:
Régis Hanol 2017-10-06 16:37:28 +02:00
parent 1477a0e910
commit 3bdd8f57c1
5 changed files with 25 additions and 30 deletions

View File

@ -38,26 +38,19 @@ class TopicUser < ActiveRecord::Base
end end
def auto_notification(user_id, topic_id, reason, notification_level) def auto_notification(user_id, topic_id, reason, notification_level)
if TopicUser.where("user_id = :user_id AND topic_id = :topic_id AND (notifications_reason_id IS NULL OR should_change = TopicUser
(notification_level < :notification_level AND notification_level > :normal_notification_level))", .where(user_id: user_id, topic_id: topic_id)
user_id: user_id, topic_id: topic_id, notification_level: notification_level, .where("notifications_reason_id IS NULL OR (notification_level < :min AND notification_level > :max)", min: notification_level, max: notification_levels[:regular])
normal_notification_level: notification_levels[:regular]).exists? .exists?
change(user_id, topic_id,
notification_level: notification_level, change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason) if should_change
notifications_reason_id: reason
)
end
end end
def auto_notification_for_staging(user_id, topic_id, reason) def auto_notification_for_staging(user_id, topic_id, reason, notification_level=notification_levels[:watching])
topic_user = TopicUser.find_or_initialize_by(user_id: user_id, topic_id: topic_id) change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason)
topic_user.notification_level = notification_levels[:watching]
topic_user.notifications_reason_id = reason
topic_user.save
end end
def unwatch_categories!(user, category_ids) def unwatch_categories!(user, category_ids)
track_threshold = user.user_option.auto_track_topics_after_msecs track_threshold = user.user_option.auto_track_topics_after_msecs
sql = <<SQL sql = <<SQL

View File

@ -93,7 +93,7 @@ class PostAlerter
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
users.each do |user| users.each do |user|
notification_level = TopicUser.get(post.topic, user).try(:notification_level) notification_level = TopicUser.get(post.topic, user).try(:notification_level)
if notified.include?(user) || notification_level == TopicUser.notification_levels[:watching] if notified.include?(user) || notification_level == TopicUser.notification_levels[:watching] || user.staged?
create_notification(user, Notification.types[:private_message], post) create_notification(user, Notification.types[:private_message], post)
end end
end end

View File

@ -692,7 +692,7 @@ module Email
if result.post if result.post
@incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id) @incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id)
if result.post.topic && result.post.topic.private_message? if result.post.topic && result.post.topic.private_message?
add_other_addresses(result.post.topic, user) add_other_addresses(result.post, user)
end end
end end
@ -707,7 +707,7 @@ module Email
html html
end end
def add_other_addresses(topic, sender) def add_other_addresses(post, sender)
%i(to cc bcc).each do |d| %i(to cc bcc).each do |d|
if @mail[d] && @mail[d].address_list && @mail[d].address_list.addresses if @mail[d] && @mail[d].address_list && @mail[d].address_list.addresses
@mail[d].address_list.addresses.each do |address_field| @mail[d].address_list.addresses.each do |address_field|
@ -718,13 +718,14 @@ module Email
next unless email["@"] next unless email["@"]
if should_invite?(email) if should_invite?(email)
user = find_or_create_user(email, display_name) user = find_or_create_user(email, display_name)
if user && can_invite?(topic, user) if user && can_invite?(post.topic, user)
topic.topic_allowed_users.create!(user_id: user.id) post.topic.topic_allowed_users.create!(user_id: user.id)
topic.add_small_action(sender, "invited_user", user.username) TopicUser.auto_notification_for_staging(user.id, post.topic_id, TopicUser.notification_reasons[:auto_watch])
post.topic.add_small_action(sender, "invited_user", user.username)
end end
# cap number of staged users created per email # cap number of staged users created per email
if @staged_users.count > SiteSetting.maximum_staged_users_per_email if @staged_users.count > SiteSetting.maximum_staged_users_per_email
topic.add_moderator_post(sender, I18n.t("emails.incoming.maximum_staged_user_per_email_reached")) post.topic.add_moderator_post(sender, I18n.t("emails.incoming.maximum_staged_user_per_email_reached"))
return return
end end
end end

View File

@ -508,12 +508,9 @@ class PostCreator
if @user.staged if @user.staged
TopicUser.auto_notification_for_staging(@user.id, @topic.id, TopicUser.notification_reasons[:auto_watch]) TopicUser.auto_notification_for_staging(@user.id, @topic.id, TopicUser.notification_reasons[:auto_watch])
elsif @user.user_option.notification_level_when_replying === NotificationLevels.topic_levels[:watching]
TopicUser.auto_notification(@user.id, @topic.id, TopicUser.notification_reasons[:created_post], NotificationLevels.topic_levels[:watching])
elsif @user.user_option.notification_level_when_replying === NotificationLevels.topic_levels[:regular]
TopicUser.auto_notification(@user.id, @topic.id, TopicUser.notification_reasons[:created_post], NotificationLevels.topic_levels[:regular])
else else
TopicUser.auto_notification(@user.id, @topic.id, TopicUser.notification_reasons[:created_post], NotificationLevels.topic_levels[:tracking]) notification_level = @user.user_option.notification_level_when_replying || NotificationLevels.topic_levels[:tracking]
TopicUser.auto_notification(@user.id, @topic.id, TopicUser.notification_reasons[:created_post], notification_level)
end end
end end

View File

@ -419,9 +419,13 @@ describe Email::Receiver do
it "invites everyone in the chain but emails configured as 'incoming' (via reply, group or category)" do it "invites everyone in the chain but emails configured as 'incoming' (via reply, group or category)" do
expect { process(:cc) }.to change(Topic, :count) expect { process(:cc) }.to change(Topic, :count)
emails = Topic.last.allowed_users.joins(:user_emails).pluck(:"user_emails.email")
expect(emails.size).to eq(3) topic = Topic.last
expect(emails).to include("someone@else.com", "discourse@bar.com", "wat@bar.com")
emails = topic.allowed_users.joins(:user_emails).pluck(:"user_emails.email")
expect(emails).to contain_exactly("someone@else.com", "discourse@bar.com", "wat@bar.com")
expect(topic.topic_users.count).to eq(3)
end end
it "cap the number of staged users created per email" do it "cap the number of staged users created per email" do