diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 3404fc90d47..a66b6c41afc 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -38,26 +38,19 @@ class TopicUser < ActiveRecord::Base end 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 - (notification_level < :notification_level AND notification_level > :normal_notification_level))", - user_id: user_id, topic_id: topic_id, notification_level: notification_level, - normal_notification_level: notification_levels[:regular]).exists? - change(user_id, topic_id, - notification_level: notification_level, - notifications_reason_id: reason - ) - end + should_change = TopicUser + .where(user_id: user_id, topic_id: topic_id) + .where("notifications_reason_id IS NULL OR (notification_level < :min AND notification_level > :max)", min: notification_level, max: notification_levels[:regular]) + .exists? + + change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason) if should_change end - def auto_notification_for_staging(user_id, topic_id, reason) - topic_user = TopicUser.find_or_initialize_by(user_id: user_id, topic_id: topic_id) - topic_user.notification_level = notification_levels[:watching] - topic_user.notifications_reason_id = reason - topic_user.save + def auto_notification_for_staging(user_id, topic_id, reason, notification_level=notification_levels[:watching]) + change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason) end def unwatch_categories!(user, category_ids) - track_threshold = user.user_option.auto_track_topics_after_msecs sql = < 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 end end diff --git a/lib/post_creator.rb b/lib/post_creator.rb index f4bf70f5609..dc28b66bce0 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -508,12 +508,9 @@ class PostCreator if @user.staged 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 - 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 diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 839f36192f3..c6eee47b72a 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -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 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) - expect(emails).to include("someone@else.com", "discourse@bar.com", "wat@bar.com") + + topic = Topic.last + + 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 it "cap the number of staged users created per email" do