diff --git a/app/models/topic.rb b/app/models/topic.rb index 0bde49f43d3..793cfef2307 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -134,7 +134,6 @@ class Topic < ActiveRecord::Base after_create do changed_to_category(category) - notifier.created_topic! user_id if archetype == Archetype.private_message DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE) else diff --git a/app/models/topic_notifier.rb b/app/models/topic_notifier.rb index 5ac1de2ed7f..f464f510640 100644 --- a/app/models/topic_notifier.rb +++ b/app/models/topic_notifier.rb @@ -14,7 +14,7 @@ class TopicNotifier end - def created_topic!(user_id) + def watch_topic!(user_id) change_level user_id, :watching, :created_topic end diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 185bede5350..69a2b88b33b 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -68,6 +68,7 @@ class TopicUser < ActiveRecord::Base # since there's more likely to be an existing record than not. If the update returns 0 rows affected # it then creates the row instead. def change(user_id, topic_id, attrs) + # Sometimes people pass objs instead of the ids. We can handle that. topic_id = topic_id.id if topic_id.is_a?(::Topic) user_id = user_id.id if user_id.is_a?(::User) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 18fffcc2613..3539774d114 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -20,6 +20,7 @@ class PostCreator # who is the post "author." For example when copying posts to a new # topic. # created_at - Post creation time (optional) + # auto_track - Automatically track this topic if needed (default true) # # When replying to a topic: # topic_id - topic we're replying to @@ -264,7 +265,9 @@ class PostCreator end def track_topic - TopicUser.auto_track(@user.id, @topic.id, TopicUser.notification_reasons[:created_post]) + unless @opts[:auto_track] == false + TopicUser.auto_track(@user.id, @topic.id, TopicUser.notification_reasons[:created_post]) + end end def enqueue_jobs diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 828db0d4351..f11e4a34156 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -2,6 +2,10 @@ class TopicCreator attr_accessor :errors + def self.create(user, guardian, opts) + self.new(user, guardian, opts).create + end + def initialize(user, guardian, opts) @user = user @guardian = guardian @@ -17,11 +21,19 @@ class TopicCreator process_private_message if @opts[:archetype] == Archetype.private_message save_topic + watch_topic + @topic end private + def watch_topic + unless @opts[:auto_track] == false + @topic.notifier.watch_topic!(@topic.user_id) + end + end + def setup topic_params = {title: @opts[:title], user_id: @user.id, last_post_user_id: @user.id} topic_params[:archetype] = @opts[:archetype] if @opts[:archetype].present? diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 98f29bb0180..ae059fd4c3d 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -21,6 +21,12 @@ describe PostCreator do let(:creator_with_meta_data) { PostCreator.new(user, basic_topic_params.merge(meta_data: {hello: "world"} )) } let(:creator_with_image_sizes) { PostCreator.new(user, basic_topic_params.merge(image_sizes: image_sizes)) } + it "can be created with auto tracking disabled" do + p = PostCreator.create(user, basic_topic_params.merge(auto_track: false)) + t = TopicUser.where(user_id: p.user_id, topic_id: p.topic_id).first + t.notification_level.should == TopicUser.notification_levels[:regular] + end + it "ensures the user can create the topic" do Guardian.any_instance.expects(:can_create?).with(Topic,nil).returns(false) lambda { creator.create }.should raise_error(Discourse::InvalidAccess) diff --git a/spec/components/unread_spec.rb b/spec/components/unread_spec.rb index e40b9d2a2bf..45c11cc7f80 100644 --- a/spec/components/unread_spec.rb +++ b/spec/components/unread_spec.rb @@ -6,13 +6,13 @@ describe Unread do before do @topic = Fabricate(:topic, posts_count: 13, highest_post_number: 13) + @topic.notifier.watch_topic!(@topic.user_id) @topic_user = TopicUser.get(@topic, @topic.user) @topic_user.stubs(:notification_level).returns(TopicUser.notification_levels[:tracking]) @topic_user.notification_level = TopicUser.notification_levels[:tracking] @unread = Unread.new(@topic, @topic_user) end - describe 'unread_posts' do it 'should have 0 unread posts if the user has seen all posts' do @topic_user.stubs(:last_read_post_number).returns(13) diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index aabfda32929..cbb9daa7e4b 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -20,6 +20,7 @@ describe TopicTrackingState do report.length.should == 0 new_post = post + post.topic.notifier.watch_topic!(post.topic.user_id) report = TopicTrackingState.report([user.id]) diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index 927d45b0593..d6a412b61ff 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -11,8 +11,12 @@ describe TopicUser do DateTime.expects(:now).at_least_once.returns(yesterday) end - let!(:topic) { Fabricate(:topic) } let!(:user) { Fabricate(:coding_horror) } + let!(:topic) { + user = Fabricate(:user) + guardian = Guardian.new(user) + TopicCreator.create(user, guardian, title: "this is my topic title") + } let(:topic_user) { TopicUser.get(topic,user) } let(:topic_creator_user) { TopicUser.get(topic, topic.user) }