DEV: Add a plugin modifier for user_action_stream_builder (#25691)

Reactions needs this to be able to filter out likes received
actions, where there is also an associated reaction, since
now most reactions also count as a like.
This commit is contained in:
Martin Brennan 2024-02-16 10:24:39 +10:00 committed by GitHub
parent cc9480b24a
commit ae24e04a5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View File

@ -227,6 +227,7 @@ class UserAction < ActiveRecord::Base
LEFT JOIN categories c on c.id = t.category_id
LEFT JOIN post_custom_fields pc ON pc.post_id = a.target_post_id AND pc.name = 'action_code_who'
LEFT JOIN post_custom_fields pc2 ON pc2.post_id = a.target_post_id AND pc2.name = 'action_code_path'
/*left_join*/
/*where*/
/*order_by*/
/*offset*/
@ -257,6 +258,8 @@ class UserAction < ActiveRecord::Base
builder.order_by("a.created_at desc").offset(offset.to_i).limit(limit.to_i)
end
DiscoursePluginRegistry.apply_modifier(:user_action_stream_builder, builder)
builder.query
end

View File

@ -121,7 +121,7 @@ RSpec.describe UserAction do
end
describe "assignments" do
let(:stream) { UserAction.stream(user_id: user.id, guardian: Guardian.new(user)) }
let(:stream) { UserAction.stream(user_id: user.id, guardian: user.guardian) }
before do
log_test_action(action_type: UserAction::ASSIGNED)
@ -146,7 +146,7 @@ RSpec.describe UserAction do
describe "mentions" do
before { log_test_action(action_type: UserAction::MENTION) }
let(:stream) { UserAction.stream(user_id: user.id, guardian: Guardian.new(user)) }
let(:stream) { UserAction.stream(user_id: user.id, guardian: user.guardian) }
it "is returned by the stream" do
expect(stream.count).to eq(1)
@ -158,6 +158,33 @@ RSpec.describe UserAction do
expect(stream).to be_blank
end
end
describe "when a plugin registers the :user_action_stream_builder modifier" do
before do
log_test_action(action_type: UserAction::LIKE)
log_test_action(action_type: UserAction::WAS_LIKED)
end
after { DiscoursePluginRegistry.clear_modifiers! }
it "allows the plugin to modify the builder query" do
Plugin::Instance
.new
.register_modifier(:user_action_stream_builder) do |builder|
expect(builder).to be_a(MiniSqlMultisiteConnection::CustomBuilder)
builder.limit(1)
end
stream = UserAction.stream(user_id: user.id, guardian: user.guardian)
expect(stream.count).to eq(1)
DiscoursePluginRegistry.clear_modifiers!
stream = UserAction.stream(user_id: user.id, guardian: user.guardian)
expect(stream.count).to eq(2)
end
end
end
describe "when user likes" do