mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-17 19:12:37 -05:00
FIX: change **default** notification state when a topic is recategorized within 5 days of creation
This commit is contained in:
parent
eafeec51a5
commit
9ae9aed010
3 changed files with 50 additions and 18 deletions
|
@ -15,18 +15,19 @@ class CategoryUser < ActiveRecord::Base
|
|||
TopicUser.notification_levels
|
||||
end
|
||||
|
||||
def self.auto_track_new_topic(topic)
|
||||
apply_default_to_topic(topic,
|
||||
TopicUser.notification_levels[:tracking],
|
||||
TopicUser.notification_reasons[:auto_track_category]
|
||||
)
|
||||
%w{watch track}.each do |s|
|
||||
define_singleton_method("auto_#{s}_new_topic") do |topic, new_category=nil|
|
||||
category_id = topic.category_id
|
||||
|
||||
if new_category && topic.created_at > 5.days.ago
|
||||
# we want to apply default of the new category
|
||||
category_id = new_category.id
|
||||
# remove defaults from previous category
|
||||
remove_default_from_topic(topic.id, TopicUser.notification_levels[:"#{s}ing"], TopicUser.notification_reasons[:"auto_#{s}_category"])
|
||||
end
|
||||
|
||||
def self.auto_watch_new_topic(topic)
|
||||
apply_default_to_topic(topic,
|
||||
TopicUser.notification_levels[:watching],
|
||||
TopicUser.notification_reasons[:auto_watch_category]
|
||||
)
|
||||
apply_default_to_topic(topic.id, category_id, TopicUser.notification_levels[:"#{s}ing"], TopicUser.notification_reasons[:"auto_#{s}_category"])
|
||||
end
|
||||
end
|
||||
|
||||
def self.batch_set(user, level, category_ids)
|
||||
|
@ -56,7 +57,7 @@ class CategoryUser < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def self.apply_default_to_topic(topic, level, reason)
|
||||
def self.apply_default_to_topic(topic_id, category_id, level, reason)
|
||||
# Can not afford to slow down creation of topics when a pile of users are watching new topics, reverting to SQL for max perf here
|
||||
sql = <<-SQL
|
||||
INSERT INTO topic_users(user_id, topic_id, notification_level, notifications_reason_id)
|
||||
|
@ -68,14 +69,30 @@ class CategoryUser < ActiveRecord::Base
|
|||
SQL
|
||||
|
||||
exec_sql(sql,
|
||||
topic_id: topic.id,
|
||||
category_id: topic.category_id,
|
||||
topic_id: topic_id,
|
||||
category_id: category_id,
|
||||
level: level,
|
||||
reason: reason
|
||||
)
|
||||
end
|
||||
|
||||
private_class_method :apply_default_to_topic
|
||||
def self.remove_default_from_topic(topic_id, level, reason)
|
||||
sql = <<-SQL
|
||||
DELETE FROM topic_users
|
||||
WHERE topic_id = :topic_id
|
||||
AND notifications_changed_at IS NULL
|
||||
AND notification_level = :level
|
||||
AND notifications_reason_id = :reason
|
||||
SQL
|
||||
|
||||
exec_sql(sql,
|
||||
topic_id: topic_id,
|
||||
level: level,
|
||||
reason: reason
|
||||
)
|
||||
end
|
||||
|
||||
private_class_method :apply_default_to_topic, :remove_default_from_topic
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -482,8 +482,8 @@ class Topic < ActiveRecord::Base
|
|||
Category.where(id: new_category.id).update_all("topic_count = topic_count + 1")
|
||||
CategoryFeaturedTopic.feature_topics_for(old_category) unless @import_mode
|
||||
CategoryFeaturedTopic.feature_topics_for(new_category) unless @import_mode || old_category.id == new_category.id
|
||||
CategoryUser.auto_watch_new_topic(self)
|
||||
CategoryUser.auto_track_new_topic(self)
|
||||
CategoryUser.auto_watch_new_topic(self, new_category)
|
||||
CategoryUser.auto_track_new_topic(self, new_category)
|
||||
end
|
||||
|
||||
true
|
||||
|
|
|
@ -52,8 +52,8 @@ describe CategoryUser do
|
|||
end
|
||||
|
||||
it "watches categories that have been changed" do
|
||||
watched_category = Fabricate(:category)
|
||||
user = Fabricate(:user)
|
||||
watched_category = Fabricate(:category)
|
||||
CategoryUser.create!(user: user, category: watched_category, notification_level: CategoryUser.notification_levels[:watching])
|
||||
|
||||
post = create_post
|
||||
|
@ -65,6 +65,21 @@ describe CategoryUser do
|
|||
expect(tu.notification_level).to eq TopicUser.notification_levels[:watching]
|
||||
end
|
||||
|
||||
it "unwatches categories that have been changed" do
|
||||
user = Fabricate(:user)
|
||||
watched_category = Fabricate(:category)
|
||||
CategoryUser.create!(user: user, category: watched_category, notification_level: CategoryUser.notification_levels[:watching])
|
||||
|
||||
post = create_post(category: watched_category)
|
||||
tu = TopicUser.get(post.topic, user)
|
||||
expect(tu.notification_level).to eq TopicUser.notification_levels[:watching]
|
||||
|
||||
# Now, change the topic's category
|
||||
unwatched_category = Fabricate(:category)
|
||||
post.topic.change_category_to_id(unwatched_category.id)
|
||||
expect(TopicUser.get(post.topic, user)).to be_blank
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue