FEATURE: webhook for flag events

This commit is contained in:
Arpit Jalan 2018-04-11 19:47:05 +05:30
parent 7ff78cc013
commit a16b616861
9 changed files with 116 additions and 0 deletions

View File

@ -68,6 +68,12 @@ module Jobs
args[:payload] = TagSerializer.new(tag, scope: guardian, root: false).as_json
end
def setup_flag(args)
flag = PostAction.find(args[:flag_id])
return if flag.blank?
args[:payload] = WebHookFlagSerializer.new(flag, scope: guardian, root: false).as_json
end
def ping_event?(event_type)
event_type.to_s == 'ping'.freeze
end

View File

@ -155,6 +155,7 @@ SQL
DiscourseEvent.trigger(:confirmed_spam_post, post) if trigger_spam
DiscourseEvent.trigger(:flag_reviewed, post)
DiscourseEvent.trigger(:flag_agreed, actions.first) if actions.first.present?
update_flagged_posts_count
end
@ -188,6 +189,7 @@ SQL
Post.with_deleted.where(id: post.id).update_all(cached)
DiscourseEvent.trigger(:flag_reviewed, post)
DiscourseEvent.trigger(:flag_disagreed, actions.first) if actions.first.present?
update_flagged_posts_count
end
@ -206,6 +208,7 @@ SQL
end
DiscourseEvent.trigger(:flag_reviewed, post)
DiscourseEvent.trigger(:flag_deferred, actions.first) if actions.first.present?
update_flagged_posts_count
end
@ -306,6 +309,10 @@ SQL
end
end
if post_action && PostActionType.notify_flag_type_ids.include?(post_action_type_id)
DiscourseEvent.trigger(:flag_created, post_action)
end
GivenDailyLike.increment_for(user.id) if post_action_type_id == PostActionType.types[:like]
# agree with other flags

View File

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

View File

@ -0,0 +1,43 @@
class WebHookFlagSerializer < ApplicationSerializer
attributes :id,
:post,
:flag_type,
:created_by,
:created_at,
:resolved_at,
:resolved_by
def post
BasicPostSerializer.new(object.post, scope: scope, root: false).as_json
end
def flag_type
object.post_action_type_key
end
def include_post?
object.post.present?
end
def created_by
object.user && object.user.username
end
def resolved_at
object.disposed_at
end
def include_resolved_at?
object.disposed_at.present?
end
def resolved_by
if object.disposed_by_id.present?
User.find(object.disposed_by_id).username
end
end
def include_resolved_by?
object.disposed_by_id.present?
end
end

View File

@ -72,3 +72,14 @@ end
WebHook.enqueue_hooks(:tag, tag_id: tag.id, event_name: event.to_s)
end
end
%i(
flag_created
flag_agreed
flag_disagreed
flag_deferred
).each do |event|
DiscourseEvent.on(event) do |flag|
WebHook.enqueue_hooks(:flag, flag_id: flag.id, event_name: event.to_s)
end
end

View File

@ -2957,6 +2957,9 @@ en:
tag_event:
name: "Tag Event"
details: "When a tag is created, updated or destroyed."
flag_event:
name: "Flag Event"
details: "When a flag is created, agreed, disagreed or deferred."
delivery_status:
title: "Delivery Status"
inactive: "Inactive"

View File

@ -27,3 +27,8 @@ WebHookEventType.seed do |b|
b.id = WebHookEventType::TAG
b.name = "tag"
end
WebHookEventType.seed do |b|
b.id = WebHookEventType::FLAG
b.name = "flag"
end

View File

@ -63,3 +63,11 @@ Fabricator(:tag_web_hook, from: :web_hook) do
web_hook.web_hook_event_types = [transients[:tag_hook]]
end
end
Fabricator(:flag_web_hook, from: :web_hook) do
transient flag_hook: WebHookEventType.find_by(name: 'flag')
after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:flag_hook]]
end
end

View File

@ -710,4 +710,36 @@ describe PostAction do
end
end
describe "triggers webhook events" do
let(:post) { Fabricate(:post) }
it 'flag created' do
event = DiscourseEvent.track_events { PostAction.act(eviltrout, post, PostActionType.types[:spam]) }.last
expect(event[:event_name]).to eq(:flag_created)
end
context "resolving flags" do
before do
@flag = PostAction.act(eviltrout, post, PostActionType.types[:spam])
end
it 'flag agreed' do
event = DiscourseEvent.track_events { PostAction.agree_flags!(post, moderator) }.last
expect(event[:event_name]).to eq(:flag_agreed)
expect(event[:params].first).to eq(@flag)
end
it 'flag disagreed' do
event = DiscourseEvent.track_events { PostAction.clear_flags!(post, moderator) }.last
expect(event[:event_name]).to eq(:flag_disagreed)
expect(event[:params].first).to eq(@flag)
end
it 'flag deferred' do
event = DiscourseEvent.track_events { PostAction.defer_flags!(post, moderator) }.last
expect(event[:event_name]).to eq(:flag_deferred)
expect(event[:params].first).to eq(@flag)
end
end
end
end