diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index 42bbfd5b1e5..289938329e6 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -40,10 +40,14 @@ class WebHook < ActiveRecord::Base end end - def self.enqueue_topic_hooks(event, topic, user) + def self.enqueue_topic_hooks(event, topic, user=nil) WebHook.enqueue_hooks(:topic, topic_id: topic.id, user_id: user&.id, category_id: topic&.category&.id, event_name: event.to_s) end + def self.enqueue_post_hooks(event, post, user=nil) + WebHook.enqueue_hooks(:post, post_id: post.id, topic_id: post&.topic&.id, user_id: user&.id, category_id: post&.topic&.category_id, event_name: event.to_s) + end + %i(topic_destroyed topic_recovered).each do |event| DiscourseEvent.on(event) do |topic, user| WebHook.enqueue_topic_hooks(event, topic, user) @@ -57,18 +61,16 @@ class WebHook < ActiveRecord::Base %i(post_created post_destroyed post_recovered).each do |event| - DiscourseEvent.on(event) do |post, _, user| - WebHook.enqueue_hooks(:post, - post_id: post.id, - topic_id: post&.topic&.id, - user_id: user&.id, - category_id: post.topic&.category&.id, - event_name: event.to_s - ) + WebHook.enqueue_post_hooks(event, post, user) end end + DiscourseEvent.on(:post_edited) do |post, topic_changed| + WebHook.enqueue_post_hooks(:post_edited, post) + WebHook.enqueue_topic_hooks(:topic_edited, post.topic) if post.is_first_post? && topic_changed + end + %i(user_created user_approved).each do |event| DiscourseEvent.on(event) do |user| WebHook.enqueue_hooks(:user, user_id: user.id, event_name: event.to_s) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index b9cccc9f9d9..f253d3712e3 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -446,6 +446,7 @@ class PostRevisor def post_process_post @post.invalidate_oneboxes = true @post.trigger_post_process + DiscourseEvent.trigger(:post_edited, @post, self.topic_changed?) end def update_topic_word_counts diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 88eaee6cb0d..0022c0e9d6c 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -120,14 +120,14 @@ describe WebHook do end it 'should enqueue the right hooks for post events' do - user # bypass a user_created event - WebHook.expects(:enqueue_hooks).once + WebHook.expects(:enqueue_post_hooks).once PostCreator.create(user, { raw: 'post', topic_id: topic.id, reply_to_post_number: 1, skip_validations: true }) - WebHook.expects(:enqueue_hooks).once + # post destroy or recover triggers a moderator post + WebHook.expects(:enqueue_post_hooks).twice PostDestroyer.new(user, post2).destroy - WebHook.expects(:enqueue_hooks).once + WebHook.expects(:enqueue_post_hooks).twice PostDestroyer.new(user, post2).recover end