mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 05:49:30 +08:00
remove UserActionObserver, replace with after_save and service
interestingly there was some left over dead code from when stars existed in the topic_users table
This commit is contained in:
parent
96c70c74a1
commit
2f6a4cc6de
|
@ -52,6 +52,7 @@ class Post < ActiveRecord::Base
|
||||||
validates_with ::Validators::PostValidator
|
validates_with ::Validators::PostValidator
|
||||||
|
|
||||||
after_save :index_search
|
after_save :index_search
|
||||||
|
after_save :create_user_action
|
||||||
|
|
||||||
# We can pass several creating options to a post via attributes
|
# We can pass several creating options to a post via attributes
|
||||||
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check
|
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check
|
||||||
|
@ -642,6 +643,10 @@ class Post < ActiveRecord::Base
|
||||||
SearchIndexer.index(self)
|
SearchIndexer.index(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_user_action
|
||||||
|
UserActionCreator.log_post(self)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def parse_quote_into_arguments(quote)
|
def parse_quote_into_arguments(quote)
|
||||||
|
|
|
@ -22,6 +22,7 @@ class PostAction < ActiveRecord::Base
|
||||||
|
|
||||||
after_save :update_counters
|
after_save :update_counters
|
||||||
after_save :enforce_rules
|
after_save :enforce_rules
|
||||||
|
after_save :create_user_action
|
||||||
after_commit :notify_subscribers
|
after_commit :notify_subscribers
|
||||||
|
|
||||||
def disposed_by_id
|
def disposed_by_id
|
||||||
|
@ -453,6 +454,12 @@ SQL
|
||||||
SpamRulesEnforcer.enforce!(post.user)
|
SpamRulesEnforcer.enforce!(post.user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_user_action
|
||||||
|
if is_bookmark? || is_like?
|
||||||
|
UserActionCreator.log_post_action(self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def notify_subscribers
|
def notify_subscribers
|
||||||
if (is_like? || is_flag?) && post
|
if (is_like? || is_flag?) && post
|
||||||
post.publish_change_to_clients! :acted
|
post.publish_change_to_clients! :acted
|
||||||
|
|
|
@ -200,6 +200,7 @@ class Topic < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
SearchIndexer.index(self)
|
SearchIndexer.index(self)
|
||||||
|
UserActionCreator.log_topic(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_default_values
|
def initialize_default_values
|
||||||
|
|
|
@ -132,8 +132,6 @@ SQL
|
||||||
|
|
||||||
if rows == 0
|
if rows == 0
|
||||||
create_missing_record(user_id, topic_id, attrs)
|
create_missing_record(user_id, topic_id, attrs)
|
||||||
else
|
|
||||||
observe_after_save_callbacks_for topic_id, user_id
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -203,8 +201,6 @@ SQL
|
||||||
|
|
||||||
if rows == 0
|
if rows == 0
|
||||||
change(user_id, topic_id, last_visited_at: now, first_visited_at: now)
|
change(user_id, topic_id, last_visited_at: now, first_visited_at: now)
|
||||||
else
|
|
||||||
observe_after_save_callbacks_for(topic_id, user_id)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -323,11 +319,6 @@ SQL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def observe_after_save_callbacks_for(topic_id, user_id)
|
|
||||||
TopicUser.where(topic_id: topic_id, user_id: user_id).each do |topic_user|
|
|
||||||
UserActionObserver.instance.after_save topic_user
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.update_post_action_cache(opts={})
|
def self.update_post_action_cache(opts={})
|
||||||
|
|
|
@ -343,7 +343,7 @@ class PostAlerter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
UserActionObserver.log_notification(original_post, user, type, opts[:acting_user_id])
|
UserActionCreator.log_notification(original_post, user, type, opts[:acting_user_id])
|
||||||
|
|
||||||
topic_title = post.topic.title
|
topic_title = post.topic.title
|
||||||
# when sending a private message email, keep the original title
|
# when sending a private message email, keep the original title
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
class UserActionObserver < ActiveRecord::Observer
|
class UserActionCreator
|
||||||
observe :post_action, :topic, :post, :notification, :topic_user
|
def self.disable
|
||||||
|
@disabled = true
|
||||||
def after_save(model)
|
|
||||||
case
|
|
||||||
when (model.is_a?(PostAction) && (model.is_bookmark? || model.is_like?))
|
|
||||||
log_post_action(model)
|
|
||||||
when (model.is_a?(Topic))
|
|
||||||
log_topic(model)
|
|
||||||
when (model.is_a?(Post))
|
|
||||||
UserActionObserver.log_post(model)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.enable
|
||||||
|
@disabled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log_notification(post, user, notification_type, acting_user_id=nil)
|
def self.log_notification(post, user, notification_type, acting_user_id=nil)
|
||||||
|
return if @disabled
|
||||||
|
|
||||||
action =
|
action =
|
||||||
case notification_type
|
case notification_type
|
||||||
when Notification.types[:quoted]
|
when Notification.types[:quoted]
|
||||||
|
@ -44,6 +41,8 @@ class UserActionObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log_post(model)
|
def self.log_post(model)
|
||||||
|
return if @disabled
|
||||||
|
|
||||||
# first post gets nada
|
# first post gets nada
|
||||||
return if model.is_first_post?
|
return if model.is_first_post?
|
||||||
return if model.topic.blank?
|
return if model.topic.blank?
|
||||||
|
@ -78,7 +77,8 @@ class UserActionObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_topic(model)
|
def self.log_topic(model)
|
||||||
|
return if @disabled
|
||||||
|
|
||||||
# no action to log here, this can happen if a user is deleted
|
# no action to log here, this can happen if a user is deleted
|
||||||
# then topic has no user_id
|
# then topic has no user_id
|
||||||
|
@ -113,7 +113,9 @@ class UserActionObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_post_action(model)
|
def self.log_post_action(model)
|
||||||
|
return if @disabled
|
||||||
|
|
||||||
action = UserAction::BOOKMARK if model.is_bookmark?
|
action = UserAction::BOOKMARK if model.is_bookmark?
|
||||||
action = UserAction::LIKE if model.is_like?
|
action = UserAction::LIKE if model.is_like?
|
||||||
|
|
|
@ -83,7 +83,6 @@ module Discourse
|
||||||
# Activate observers that should always be running.
|
# Activate observers that should always be running.
|
||||||
config.active_record.observers = [
|
config.active_record.observers = [
|
||||||
:user_email_observer,
|
:user_email_observer,
|
||||||
:user_action_observer,
|
|
||||||
:post_alert_observer,
|
:post_alert_observer,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -187,7 +187,7 @@ class PostDestroyer
|
||||||
|
|
||||||
def recover_user_actions
|
def recover_user_actions
|
||||||
# TODO: Use a trash concept for `user_actions` to avoid churn and simplify this?
|
# TODO: Use a trash concept for `user_actions` to avoid churn and simplify this?
|
||||||
UserActionObserver.log_post(@post)
|
UserActionCreator.log_post(@post)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_associated_replies
|
def remove_associated_replies
|
||||||
|
|
|
@ -250,7 +250,7 @@ class PostRevisor
|
||||||
prev_owner = User.find(@post.user_id)
|
prev_owner = User.find(@post.user_id)
|
||||||
new_owner = User.find(@fields["user_id"])
|
new_owner = User.find(@fields["user_id"])
|
||||||
|
|
||||||
# UserActionObserver will create new UserAction records for the new owner
|
# UserActionCreator will create new UserAction records for the new owner
|
||||||
|
|
||||||
UserAction.where(target_post_id: @post.id)
|
UserAction.where(target_post_id: @post.id)
|
||||||
.where(user_id: prev_owner.id)
|
.where(user_id: prev_owner.id)
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
desc "rebuild the user_actions table"
|
desc "rebuild the user_actions table"
|
||||||
task "user_actions:rebuild" => :environment do
|
task "user_actions:rebuild" => :environment do
|
||||||
o = UserActionObserver.send :new
|
|
||||||
MessageBus.off
|
MessageBus.off
|
||||||
UserAction.delete_all
|
UserAction.delete_all
|
||||||
PostAction.all.each{|i| o.after_save(i)}
|
PostAction.all.each{|i| UserActionCreator.log_post_action(i)}
|
||||||
Topic.all.each {|i| o.after_save(i)}
|
Topic.all.each {|i| UserActionCreator.log_topic(i)}
|
||||||
Post.all.each {|i| o.after_save(i)}
|
Post.all.each {|i| UserActionCreator.log_post(i)}
|
||||||
Notification.all.each {|i| o.after_save(i)}
|
Notification.all.each do |notification|
|
||||||
# not really needed but who knows
|
UserActionCreator.log_notification(notification.post,
|
||||||
MessageBus.on
|
notification.user,
|
||||||
|
notification.notification_type,
|
||||||
|
notification.user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,8 @@ describe PostCreator do
|
||||||
|
|
||||||
it "generates the correct messages for a secure topic" do
|
it "generates the correct messages for a secure topic" do
|
||||||
|
|
||||||
|
UserActionCreator.enable
|
||||||
|
|
||||||
admin = Fabricate(:admin)
|
admin = Fabricate(:admin)
|
||||||
|
|
||||||
cat = Fabricate(:category)
|
cat = Fabricate(:category)
|
||||||
|
@ -140,6 +142,8 @@ describe PostCreator do
|
||||||
|
|
||||||
it 'generates the correct messages for a normal topic' do
|
it 'generates the correct messages for a normal topic' do
|
||||||
|
|
||||||
|
UserActionCreator.enable
|
||||||
|
|
||||||
p = nil
|
p = nil
|
||||||
messages = MessageBus.track_publish do
|
messages = MessageBus.track_publish do
|
||||||
p = creator.create
|
p = creator.create
|
||||||
|
|
|
@ -5,6 +5,7 @@ describe PostDestroyer do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:moderator) { Fabricate(:moderator) }
|
let(:moderator) { Fabricate(:moderator) }
|
||||||
|
|
|
@ -10,6 +10,7 @@ describe UserActionsController do
|
||||||
|
|
||||||
it 'renders list correctly' do
|
it 'renders list correctly' do
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
post = Fabricate(:post)
|
post = Fabricate(:post)
|
||||||
|
|
||||||
xhr :get, :index, username: post.user.username
|
xhr :get, :index, username: post.user.username
|
||||||
|
|
|
@ -21,6 +21,7 @@ describe DirectoryItem do
|
||||||
context 'refresh' do
|
context 'refresh' do
|
||||||
before do
|
before do
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
let!(:post) { create_post }
|
let!(:post) { create_post }
|
||||||
|
|
|
@ -33,6 +33,7 @@ describe PostMover do
|
||||||
p2.replies << p4
|
p2.replies << p4
|
||||||
# add a like to a post, enable observers so we get user actions
|
# add a like to a post, enable observers so we get user actions
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
@like = PostAction.act(another_user, p4, PostActionType.types[:like])
|
@like = PostAction.act(another_user, p4, PostActionType.types[:like])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -430,6 +430,7 @@ describe Topic do
|
||||||
|
|
||||||
it "should set up actions correctly" do
|
it "should set up actions correctly" do
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
|
|
||||||
expect(actions.map{|a| a.action_type}).not_to include(UserAction::NEW_TOPIC)
|
expect(actions.map{|a| a.action_type}).not_to include(UserAction::NEW_TOPIC)
|
||||||
expect(actions.map{|a| a.action_type}).to include(UserAction::NEW_PRIVATE_MESSAGE)
|
expect(actions.map{|a| a.action_type}).to include(UserAction::NEW_PRIVATE_MESSAGE)
|
||||||
|
|
|
@ -174,11 +174,6 @@ describe TopicUser do
|
||||||
expect(topic_user.last_visited_at.to_i).to eq(today.to_i)
|
expect(topic_user.last_visited_at.to_i).to eq(today.to_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'triggers the observer callbacks when updating' do
|
|
||||||
UserActionObserver.instance.expects(:after_save).twice
|
|
||||||
2.times { TopicUser.track_visit!(topic.id, user.id) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'read tracking' do
|
describe 'read tracking' do
|
||||||
|
|
|
@ -221,7 +221,7 @@ describe TrustLevel3Requirements do
|
||||||
|
|
||||||
describe "num_likes_given" do
|
describe "num_likes_given" do
|
||||||
it "counts likes given in the last 100 days" do
|
it "counts likes given in the last 100 days" do
|
||||||
ActiveRecord::Base.observers.enable :user_action_observer
|
UserActionCreator.enable
|
||||||
|
|
||||||
recent_post1 = create_post(created_at: 1.hour.ago)
|
recent_post1 = create_post(created_at: 1.hour.ago)
|
||||||
recent_post2 = create_post(created_at: 10.days.ago)
|
recent_post2 = create_post(created_at: 10.days.ago)
|
||||||
|
@ -237,7 +237,7 @@ describe TrustLevel3Requirements do
|
||||||
|
|
||||||
describe "num_likes_received" do
|
describe "num_likes_received" do
|
||||||
it "counts likes received in the last 100 days" do
|
it "counts likes received in the last 100 days" do
|
||||||
ActiveRecord::Base.observers.enable :user_action_observer
|
UserActionCreator.enable
|
||||||
|
|
||||||
t = Fabricate(:topic, user: user, created_at: 102.days.ago)
|
t = Fabricate(:topic, user: user, created_at: 102.days.ago)
|
||||||
old_post = create_post(topic: t, user: user, created_at: 102.days.ago)
|
old_post = create_post(topic: t, user: user, created_at: 102.days.ago)
|
||||||
|
|
|
@ -4,6 +4,7 @@ describe UserAction do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
ActiveRecord::Base.observers.enable :all
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
UserActionCreator.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
it { is_expected.to validate_presence_of :action_type }
|
it { is_expected.to validate_presence_of :action_type }
|
||||||
|
|
|
@ -922,6 +922,7 @@ describe User do
|
||||||
before do
|
before do
|
||||||
# To make testing easier, say 1 reply is too much
|
# To make testing easier, say 1 reply is too much
|
||||||
SiteSetting.stubs(:newuser_max_replies_per_topic).returns(1)
|
SiteSetting.stubs(:newuser_max_replies_per_topic).returns(1)
|
||||||
|
UserActionCreator.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
context "for a user who didn't create the topic" do
|
context "for a user who didn't create the topic" do
|
||||||
|
@ -938,7 +939,10 @@ describe User do
|
||||||
|
|
||||||
context "with a reply" do
|
context "with a reply" do
|
||||||
before do
|
before do
|
||||||
PostCreator.new(Fabricate(:user), raw: 'whatever this is a raw post', topic_id: topic.id, reply_to_post_number: post.post_number).create
|
PostCreator.new(Fabricate(:user),
|
||||||
|
raw: 'whatever this is a raw post',
|
||||||
|
topic_id: topic.id,
|
||||||
|
reply_to_post_number: post.post_number).create
|
||||||
end
|
end
|
||||||
|
|
||||||
it "resets the `posted_too_much` threshold" do
|
it "resets the `posted_too_much` threshold" do
|
||||||
|
|
|
@ -108,6 +108,7 @@ Spork.prefork do
|
||||||
#
|
#
|
||||||
ActiveRecord::Base.observers.disable :all
|
ActiveRecord::Base.observers.disable :all
|
||||||
SearchIndexer.disable
|
SearchIndexer.disable
|
||||||
|
UserActionCreator.disable
|
||||||
|
|
||||||
SiteSetting.provider.all.each do |setting|
|
SiteSetting.provider.all.each do |setting|
|
||||||
SiteSetting.remove_override!(setting.name)
|
SiteSetting.remove_override!(setting.name)
|
||||||
|
|
|
@ -74,7 +74,8 @@ describe PostOwnerChanger do
|
||||||
UserAction.create!( action_type: UserAction::REPLY, user_id: p2user.id, acting_user_id: p2user.id,
|
UserAction.create!( action_type: UserAction::REPLY, user_id: p2user.id, acting_user_id: p2user.id,
|
||||||
target_post_id: p2.id, target_topic_id: p2.topic_id, created_at: p2.created_at )
|
target_post_id: p2.id, target_topic_id: p2.topic_id, created_at: p2.created_at )
|
||||||
|
|
||||||
ActiveRecord::Base.observers.enable :user_action_observer
|
|
||||||
|
UserActionCreator.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
subject(:change_owners) { described_class.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! }
|
subject(:change_owners) { described_class.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user