A platform for community discussion. Free, open, simple.
REFACTOR: Prefer accessing instance variables directly.
Guo Xiang Tan0210b0aabc4a
diff --git a/app/jobs/regular/emit_web_hook_event.rb b/app/jobs/regular/emit_web_hook_event.rbindex 9d91f121c45..3b067609809 100644--- a/app/jobs/regular/emit_web_hook_event.rb+++ b/app/jobs/regular/emit_web_hook_event.rb@@ -7,10 +7,12 @@ module JobsRETRY_BACKOFF = 5def 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!- unless ping_event?(arguments[:event_type])+ unless ping_event?(@arguments[:event_type])validate_argument!(:payload)return if webhook_inactive?@@ -27,21 +29,16 @@ module Jobsdef validate_arguments!validate_argument!(:web_hook_id)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?enddef validate_argument!(key)- raise Discourse::InvalidParameters.new(key) unless arguments[key].present?- end-- def memoize_arguments(args)- @arguments = args- @retry_count = @arguments[:retry_count] || 0+ raise Discourse::InvalidParameters.new(key) unless @arguments[key].present?enddef send_webhook!- uri = URI(web_hook.payload_url.strip)- conn = Excon.new(uri.to_s, ssl_verify_peer: web_hook.verify_certificate, retry_limit: 0)+ uri = URI(@web_hook.payload_url.strip)+ conn = Excon.new(uri.to_s, ssl_verify_peer: @web_hook.verify_certificate, retry_limit: 0)web_hook_body = build_webhook_bodyweb_hook_event = create_webhook_event(web_hook_body)@@ -76,11 +73,13 @@ module Jobscase web_hook_response.statuswhen 200..299- returnwhen 404, 410if @retry_count >= MAX_RETRY_COUNT- web_hook.update_attribute(:active, false)- StaffActionLogger.new(Discourse.system_user).log_web_hook_deactivate(web_hook, web_hook_response.status)+ @web_hook.update!(active: false)++ StaffActionLogger+ .new(Discourse.system_user)+ .log_web_hook_deactivate(@web_hook, web_hook_response.status)endelseretry_web_hook@@ -92,14 +91,14 @@ module Jobs@retry_count += 1return if @retry_count > MAX_RETRY_COUNTdelay = 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)endenddef 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,- event_type: arguments[:event_type]+ event_type: @arguments[:event_type]}, user_ids: User.human_users.staff.pluck(:id))end@@ -108,39 +107,27 @@ module Jobsenddef webhook_inactive?- !web_hook.active?+ !@web_hook.active?enddef group_webhook_invalid?- web_hook.group_ids.present? && (arguments[:group_id].present? ||- !web_hook.group_ids.include?(arguments[:group_id]))+ @web_hook.group_ids.present? && (@arguments[:group_id].present? ||+ !@web_hook.group_ids.include?(@arguments[:group_id]))enddef category_webhook_invalid?- web_hook.category_ids.present? && (!arguments[:category_id].present? ||- !web_hook.category_ids.include?(arguments[:category_id]))+ @web_hook.category_ids.present? && (!@arguments[:category_id].present? ||+ !@web_hook.category_ids.include?(@arguments[:category_id]))enddef tag_webhook_invalid?- web_hook.tag_ids.present? && (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])+ @web_hook.tag_ids.present? && (@arguments[:tag_ids].blank? ||+ (@web_hook.tag_ids & @arguments[:tag_ids]).blank?)enddef build_webhook_headers(uri, web_hook_body, web_hook_event)content_type =- case web_hook.content_type+ case @web_hook.content_typewhen WebHook.content_types['application/x-www-form-urlencoded']'application/x-www-form-urlencoded'else@@ -156,13 +143,13 @@ module Jobs'User-Agent' => "Discourse/#{Discourse::VERSION::STRING}",'X-Discourse-Instance' => Discourse.base_url,'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?- headers['X-Discourse-Event-Signature'] = "sha256=#{OpenSSL::HMAC.hexdigest("sha256", web_hook.secret, web_hook_body)}"+ if @web_hook.secret.present?+ headers['X-Discourse-Event-Signature'] = "sha256=#{OpenSSL::HMAC.hexdigest("sha256", @web_hook.secret, web_hook_body)}"endheaders@@ -171,10 +158,10 @@ module Jobsdef build_webhook_bodybody = {}- if ping_event?(arguments[:event_type])+ if ping_event?(@arguments[:event_type])body['ping'] = "OK"else- body[arguments[:event_type]] = parsed_payload+ body[@arguments[:event_type]] = JSON.parse(@arguments[:payload])endnew_body = Plugin::Filter.apply(:after_build_web_hook_body, self, body)@@ -182,7 +169,7 @@ module Jobsenddef 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)endend