mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
REFACTOR: Prefer accessing instance variables directly.
* No need to wrap instance variables with another method to read it. * Remove memoization that wasn't really memozing anything.
This commit is contained in:
parent
151da50e7c
commit
0210b0aabc
@ -7,10 +7,12 @@ module Jobs
|
|||||||
RETRY_BACKOFF = 5
|
RETRY_BACKOFF = 5
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
memoize_arguments(args)
|
@arguments = args
|
||||||
|
@retry_count = args[:retry_count] || 0
|
||||||
|
@web_hook = WebHook.find_by(id: @arguments[:web_hook_id])
|
||||||
validate_arguments!
|
validate_arguments!
|
||||||
|
|
||||||
unless ping_event?(arguments[:event_type])
|
unless ping_event?(@arguments[:event_type])
|
||||||
validate_argument!(:payload)
|
validate_argument!(:payload)
|
||||||
|
|
||||||
return if webhook_inactive?
|
return if webhook_inactive?
|
||||||
@ -27,21 +29,16 @@ module Jobs
|
|||||||
def validate_arguments!
|
def validate_arguments!
|
||||||
validate_argument!(:web_hook_id)
|
validate_argument!(:web_hook_id)
|
||||||
validate_argument!(:event_type)
|
validate_argument!(:event_type)
|
||||||
raise Discourse::InvalidParameters.new(:web_hook_id) if web_hook.blank?
|
raise Discourse::InvalidParameters.new(:web_hook_id) if @web_hook.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_argument!(key)
|
def validate_argument!(key)
|
||||||
raise Discourse::InvalidParameters.new(key) unless arguments[key].present?
|
raise Discourse::InvalidParameters.new(key) unless @arguments[key].present?
|
||||||
end
|
|
||||||
|
|
||||||
def memoize_arguments(args)
|
|
||||||
@arguments = args
|
|
||||||
@retry_count = @arguments[:retry_count] || 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_webhook!
|
def send_webhook!
|
||||||
uri = URI(web_hook.payload_url.strip)
|
uri = URI(@web_hook.payload_url.strip)
|
||||||
conn = Excon.new(uri.to_s, ssl_verify_peer: web_hook.verify_certificate, retry_limit: 0)
|
conn = Excon.new(uri.to_s, ssl_verify_peer: @web_hook.verify_certificate, retry_limit: 0)
|
||||||
|
|
||||||
web_hook_body = build_webhook_body
|
web_hook_body = build_webhook_body
|
||||||
web_hook_event = create_webhook_event(web_hook_body)
|
web_hook_event = create_webhook_event(web_hook_body)
|
||||||
@ -76,11 +73,13 @@ module Jobs
|
|||||||
|
|
||||||
case web_hook_response.status
|
case web_hook_response.status
|
||||||
when 200..299
|
when 200..299
|
||||||
return
|
|
||||||
when 404, 410
|
when 404, 410
|
||||||
if @retry_count >= MAX_RETRY_COUNT
|
if @retry_count >= MAX_RETRY_COUNT
|
||||||
web_hook.update_attribute(:active, false)
|
@web_hook.update!(active: false)
|
||||||
StaffActionLogger.new(Discourse.system_user).log_web_hook_deactivate(web_hook, web_hook_response.status)
|
|
||||||
|
StaffActionLogger
|
||||||
|
.new(Discourse.system_user)
|
||||||
|
.log_web_hook_deactivate(@web_hook, web_hook_response.status)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
retry_web_hook
|
retry_web_hook
|
||||||
@ -92,14 +91,14 @@ module Jobs
|
|||||||
@retry_count += 1
|
@retry_count += 1
|
||||||
return if @retry_count > MAX_RETRY_COUNT
|
return if @retry_count > MAX_RETRY_COUNT
|
||||||
delay = RETRY_BACKOFF**(@retry_count - 1)
|
delay = RETRY_BACKOFF**(@retry_count - 1)
|
||||||
Jobs.enqueue_in(delay.minutes, :emit_web_hook_event, arguments)
|
Jobs.enqueue_in(delay.minutes, :emit_web_hook_event, @arguments)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_webhook_event(web_hook_event)
|
def publish_webhook_event(web_hook_event)
|
||||||
MessageBus.publish("/web_hook_events/#{web_hook.id}", {
|
MessageBus.publish("/web_hook_events/#{@web_hook.id}", {
|
||||||
web_hook_event_id: web_hook_event.id,
|
web_hook_event_id: web_hook_event.id,
|
||||||
event_type: arguments[:event_type]
|
event_type: @arguments[:event_type]
|
||||||
}, user_ids: User.human_users.staff.pluck(:id))
|
}, user_ids: User.human_users.staff.pluck(:id))
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -108,39 +107,27 @@ module Jobs
|
|||||||
end
|
end
|
||||||
|
|
||||||
def webhook_inactive?
|
def webhook_inactive?
|
||||||
!web_hook.active?
|
!@web_hook.active?
|
||||||
end
|
end
|
||||||
|
|
||||||
def group_webhook_invalid?
|
def group_webhook_invalid?
|
||||||
web_hook.group_ids.present? && (arguments[:group_id].present? ||
|
@web_hook.group_ids.present? && (@arguments[:group_id].present? ||
|
||||||
!web_hook.group_ids.include?(arguments[:group_id]))
|
!@web_hook.group_ids.include?(@arguments[:group_id]))
|
||||||
end
|
end
|
||||||
|
|
||||||
def category_webhook_invalid?
|
def category_webhook_invalid?
|
||||||
web_hook.category_ids.present? && (!arguments[:category_id].present? ||
|
@web_hook.category_ids.present? && (!@arguments[:category_id].present? ||
|
||||||
!web_hook.category_ids.include?(arguments[:category_id]))
|
!@web_hook.category_ids.include?(@arguments[:category_id]))
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_webhook_invalid?
|
def tag_webhook_invalid?
|
||||||
web_hook.tag_ids.present? && (arguments[:tag_ids].blank? ||
|
@web_hook.tag_ids.present? && (@arguments[:tag_ids].blank? ||
|
||||||
(web_hook.tag_ids & arguments[:tag_ids]).blank?)
|
(@web_hook.tag_ids & @arguments[:tag_ids]).blank?)
|
||||||
end
|
|
||||||
|
|
||||||
def arguments
|
|
||||||
@arguments
|
|
||||||
end
|
|
||||||
|
|
||||||
def parsed_payload
|
|
||||||
@parsed_payload ||= JSON.parse(arguments[:payload])
|
|
||||||
end
|
|
||||||
|
|
||||||
def web_hook
|
|
||||||
@web_hook ||= WebHook.find_by(id: arguments[:web_hook_id])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_webhook_headers(uri, web_hook_body, web_hook_event)
|
def build_webhook_headers(uri, web_hook_body, web_hook_event)
|
||||||
content_type =
|
content_type =
|
||||||
case web_hook.content_type
|
case @web_hook.content_type
|
||||||
when WebHook.content_types['application/x-www-form-urlencoded']
|
when WebHook.content_types['application/x-www-form-urlencoded']
|
||||||
'application/x-www-form-urlencoded'
|
'application/x-www-form-urlencoded'
|
||||||
else
|
else
|
||||||
@ -156,13 +143,13 @@ module Jobs
|
|||||||
'User-Agent' => "Discourse/#{Discourse::VERSION::STRING}",
|
'User-Agent' => "Discourse/#{Discourse::VERSION::STRING}",
|
||||||
'X-Discourse-Instance' => Discourse.base_url,
|
'X-Discourse-Instance' => Discourse.base_url,
|
||||||
'X-Discourse-Event-Id' => web_hook_event.id,
|
'X-Discourse-Event-Id' => web_hook_event.id,
|
||||||
'X-Discourse-Event-Type' => arguments[:event_type]
|
'X-Discourse-Event-Type' => @arguments[:event_type]
|
||||||
}
|
}
|
||||||
|
|
||||||
headers['X-Discourse-Event'] = arguments[:event_name] if arguments[:event_name].present?
|
headers['X-Discourse-Event'] = @arguments[:event_name] if @arguments[:event_name].present?
|
||||||
|
|
||||||
if web_hook.secret.present?
|
if @web_hook.secret.present?
|
||||||
headers['X-Discourse-Event-Signature'] = "sha256=#{OpenSSL::HMAC.hexdigest("sha256", web_hook.secret, web_hook_body)}"
|
headers['X-Discourse-Event-Signature'] = "sha256=#{OpenSSL::HMAC.hexdigest("sha256", @web_hook.secret, web_hook_body)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
headers
|
headers
|
||||||
@ -171,10 +158,10 @@ module Jobs
|
|||||||
def build_webhook_body
|
def build_webhook_body
|
||||||
body = {}
|
body = {}
|
||||||
|
|
||||||
if ping_event?(arguments[:event_type])
|
if ping_event?(@arguments[:event_type])
|
||||||
body['ping'] = "OK"
|
body['ping'] = "OK"
|
||||||
else
|
else
|
||||||
body[arguments[:event_type]] = parsed_payload
|
body[@arguments[:event_type]] = JSON.parse(@arguments[:payload])
|
||||||
end
|
end
|
||||||
|
|
||||||
new_body = Plugin::Filter.apply(:after_build_web_hook_body, self, body)
|
new_body = Plugin::Filter.apply(:after_build_web_hook_body, self, body)
|
||||||
@ -182,7 +169,7 @@ module Jobs
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_webhook_event(web_hook_body)
|
def create_webhook_event(web_hook_body)
|
||||||
WebHookEvent.create!(web_hook_id: web_hook.id, payload: web_hook_body)
|
WebHookEvent.create!(web_hook: @web_hook, payload: web_hook_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user