added an option to bypass auto tracking of topics on post creation

This commit is contained in:
Sam 2013-07-22 11:40:39 +10:00
parent 9e4b0df7ff
commit 31bb08bcdd
9 changed files with 31 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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,8 +265,10 @@ class PostCreator
end
def track_topic
unless @opts[:auto_track] == false
TopicUser.auto_track(@user.id, @topic.id, TopicUser.notification_reasons[:created_post])
end
end
def enqueue_jobs
if @post && !@post.errors.present?

View File

@ -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?

View File

@ -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)

View File

@ -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)

View File

@ -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])

View File

@ -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) }