mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
added an option to bypass auto tracking of topics on post creation
This commit is contained in:
@ -134,7 +134,6 @@ class Topic < ActiveRecord::Base
|
|||||||
|
|
||||||
after_create do
|
after_create do
|
||||||
changed_to_category(category)
|
changed_to_category(category)
|
||||||
notifier.created_topic! user_id
|
|
||||||
if archetype == Archetype.private_message
|
if archetype == Archetype.private_message
|
||||||
DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE)
|
DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE)
|
||||||
else
|
else
|
||||||
|
@ -14,7 +14,7 @@ class TopicNotifier
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def created_topic!(user_id)
|
def watch_topic!(user_id)
|
||||||
change_level user_id, :watching, :created_topic
|
change_level user_id, :watching, :created_topic
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -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
|
# 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.
|
# it then creates the row instead.
|
||||||
def change(user_id, topic_id, attrs)
|
def change(user_id, topic_id, attrs)
|
||||||
|
|
||||||
# Sometimes people pass objs instead of the ids. We can handle that.
|
# Sometimes people pass objs instead of the ids. We can handle that.
|
||||||
topic_id = topic_id.id if topic_id.is_a?(::Topic)
|
topic_id = topic_id.id if topic_id.is_a?(::Topic)
|
||||||
user_id = user_id.id if user_id.is_a?(::User)
|
user_id = user_id.id if user_id.is_a?(::User)
|
||||||
|
@ -20,6 +20,7 @@ class PostCreator
|
|||||||
# who is the post "author." For example when copying posts to a new
|
# who is the post "author." For example when copying posts to a new
|
||||||
# topic.
|
# topic.
|
||||||
# created_at - Post creation time (optional)
|
# created_at - Post creation time (optional)
|
||||||
|
# auto_track - Automatically track this topic if needed (default true)
|
||||||
#
|
#
|
||||||
# When replying to a topic:
|
# When replying to a topic:
|
||||||
# topic_id - topic we're replying to
|
# topic_id - topic we're replying to
|
||||||
@ -264,7 +265,9 @@ class PostCreator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def track_topic
|
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
|
end
|
||||||
|
|
||||||
def enqueue_jobs
|
def enqueue_jobs
|
||||||
|
@ -2,6 +2,10 @@ class TopicCreator
|
|||||||
|
|
||||||
attr_accessor :errors
|
attr_accessor :errors
|
||||||
|
|
||||||
|
def self.create(user, guardian, opts)
|
||||||
|
self.new(user, guardian, opts).create
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(user, guardian, opts)
|
def initialize(user, guardian, opts)
|
||||||
@user = user
|
@user = user
|
||||||
@guardian = guardian
|
@guardian = guardian
|
||||||
@ -17,11 +21,19 @@ class TopicCreator
|
|||||||
process_private_message if @opts[:archetype] == Archetype.private_message
|
process_private_message if @opts[:archetype] == Archetype.private_message
|
||||||
save_topic
|
save_topic
|
||||||
|
|
||||||
|
watch_topic
|
||||||
|
|
||||||
@topic
|
@topic
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def watch_topic
|
||||||
|
unless @opts[:auto_track] == false
|
||||||
|
@topic.notifier.watch_topic!(@topic.user_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
topic_params = {title: @opts[:title], user_id: @user.id, last_post_user_id: @user.id}
|
topic_params = {title: @opts[:title], user_id: @user.id, last_post_user_id: @user.id}
|
||||||
topic_params[:archetype] = @opts[:archetype] if @opts[:archetype].present?
|
topic_params[:archetype] = @opts[:archetype] if @opts[:archetype].present?
|
||||||
|
@ -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_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)) }
|
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
|
it "ensures the user can create the topic" do
|
||||||
Guardian.any_instance.expects(:can_create?).with(Topic,nil).returns(false)
|
Guardian.any_instance.expects(:can_create?).with(Topic,nil).returns(false)
|
||||||
lambda { creator.create }.should raise_error(Discourse::InvalidAccess)
|
lambda { creator.create }.should raise_error(Discourse::InvalidAccess)
|
||||||
|
@ -6,13 +6,13 @@ describe Unread do
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
@topic = Fabricate(:topic, posts_count: 13, highest_post_number: 13)
|
@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 = TopicUser.get(@topic, @topic.user)
|
||||||
@topic_user.stubs(:notification_level).returns(TopicUser.notification_levels[:tracking])
|
@topic_user.stubs(:notification_level).returns(TopicUser.notification_levels[:tracking])
|
||||||
@topic_user.notification_level = TopicUser.notification_levels[:tracking]
|
@topic_user.notification_level = TopicUser.notification_levels[:tracking]
|
||||||
@unread = Unread.new(@topic, @topic_user)
|
@unread = Unread.new(@topic, @topic_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
describe 'unread_posts' do
|
describe 'unread_posts' do
|
||||||
it 'should have 0 unread posts if the user has seen all 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)
|
@topic_user.stubs(:last_read_post_number).returns(13)
|
||||||
|
@ -20,6 +20,7 @@ describe TopicTrackingState do
|
|||||||
report.length.should == 0
|
report.length.should == 0
|
||||||
|
|
||||||
new_post = post
|
new_post = post
|
||||||
|
post.topic.notifier.watch_topic!(post.topic.user_id)
|
||||||
|
|
||||||
report = TopicTrackingState.report([user.id])
|
report = TopicTrackingState.report([user.id])
|
||||||
|
|
||||||
|
@ -11,8 +11,12 @@ describe TopicUser do
|
|||||||
DateTime.expects(:now).at_least_once.returns(yesterday)
|
DateTime.expects(:now).at_least_once.returns(yesterday)
|
||||||
end
|
end
|
||||||
|
|
||||||
let!(:topic) { Fabricate(:topic) }
|
|
||||||
let!(:user) { Fabricate(:coding_horror) }
|
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_user) { TopicUser.get(topic,user) }
|
||||||
let(:topic_creator_user) { TopicUser.get(topic, topic.user) }
|
let(:topic_creator_user) { TopicUser.get(topic, topic.user) }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user