FEATURE: Webhook for tag events

This commit is contained in:
Vinoth Kannan 2018-04-04 17:49:20 +05:30
parent 689144b2bf
commit 434cbc649f
10 changed files with 92 additions and 0 deletions

View File

@ -110,6 +110,7 @@ class TagsController < ::ApplicationController
tag.name = new_tag_name
if tag.save
StaffActionLogger.new(current_user).log_custom('renamed_tag', previous_value: params[:tag_id], new_value: new_tag_name)
DiscourseEvent.trigger(:tag_updated, tag)
render json: { tag: { id: new_tag_name } }
else
render_json_error tag.errors.full_messages

View File

@ -62,6 +62,12 @@ module Jobs
args[:payload] = WebHookCategorySerializer.new(category, scope: guardian, root: false).as_json
end
def setup_tag(args)
tag = Tag.find(args[:tag_id])
return if tag.blank?
args[:payload] = TagSerializer.new(tag, scope: guardian, root: false).as_json
end
def ping_event?(event_type)
event_type.to_s == 'ping'.freeze
end

View File

@ -16,6 +16,9 @@ class Tag < ActiveRecord::Base
after_save :index_search
after_commit :trigger_tag_created_event, on: :create
after_commit :trigger_tag_destroyed_event, on: :destroy
def self.ensure_consistency!
update_topic_counts # topic_count counter cache can miscount
end
@ -101,6 +104,16 @@ class Tag < ActiveRecord::Base
def index_search
SearchIndexer.index(self)
end
def trigger_tag_created_event
DiscourseEvent.trigger(:tag_created, self)
true
end
def trigger_tag_destroyed_event
DiscourseEvent.trigger(:tag_destroyed, self)
true
end
end
# == Schema Information

View File

@ -4,6 +4,7 @@ class WebHookEventType < ActiveRecord::Base
USER = 3
GROUP = 4
CATEGORY = 5
TAG = 6
has_and_belongs_to_many :web_hooks

View File

@ -62,3 +62,13 @@ end
WebHook.enqueue_hooks(:category, category_id: category.id, event_name: event.to_s)
end
end
%i(
tag_created
tag_updated
tag_destroyed
).each do |event|
DiscourseEvent.on(event) do |tag|
WebHook.enqueue_hooks(:tag, tag_id: tag.id, event_name: event.to_s)
end
end

View File

@ -2933,6 +2933,9 @@ en:
category_event:
name: "Category Event"
details: "When a category is created, updated or destroyed."
tag_event:
name: "Tag Event"
details: "When a tag is created, updated or destroyed."
delivery_status:
title: "Delivery Status"
inactive: "Inactive"

View File

@ -22,3 +22,8 @@ WebHookEventType.seed do |b|
b.id = WebHookEventType::CATEGORY
b.name = "category"
end
WebHookEventType.seed do |b|
b.id = WebHookEventType::TAG
b.name = "tag"
end

View File

@ -55,3 +55,11 @@ Fabricator(:category_web_hook, from: :web_hook) do
web_hook.web_hook_event_types = [transients[:category_hook]]
end
end
Fabricator(:tag_web_hook, from: :web_hook) do
transient tag_hook: WebHookEventType.find_by(name: 'tag')
after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:tag_hook]]
end
end

View File

@ -18,6 +18,28 @@ describe Tag do
SiteSetting.min_trust_level_to_tag_topics = 0
end
describe 'new' do
subject { Fabricate.build(:tag) }
it 'triggers a extensibility event' do
event = DiscourseEvent.track_events { subject.save! }.last
expect(event[:event_name]).to eq(:tag_created)
expect(event[:params].first).to eq(subject)
end
end
describe 'destroy' do
subject { Fabricate(:tag) }
it 'triggers a extensibility event' do
event = DiscourseEvent.track_events { subject.destroy! }.last
expect(event[:event_name]).to eq(:tag_destroyed)
expect(event[:params].first).to eq(subject)
end
end
it "can delete tags on deleted topics" do
topic.trash!
expect { tag.destroy }.to change { Tag.count }.by(-1)

View File

@ -54,6 +54,29 @@ describe TagsController do
end
end
describe "#update" do
let(:tag) { Fabricate(:tag) }
let(:admin) { Fabricate(:admin) }
before do
tag
sign_in(admin)
end
it "triggers a extensibility event" do
event = DiscourseEvent.track_events {
put "/tags/#{tag.name}.json", params: {
tag: {
id: 'hello'
}
}
}.last
expect(event[:event_name]).to eq(:tag_updated)
expect(event[:params].first).to eq(tag)
end
end
describe '#personal_messages' do
let(:regular_user) { Fabricate(:trust_level_4) }
let(:moderator) { Fabricate(:moderator) }