Class: WebHook

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/web_hook.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_web_hooks(type) ⇒ Object



42
43
44
45
46
47
48
# File 'app/models/web_hook.rb', line 42

def self.active_web_hooks(type)
  WebHook
    .where(active: true)
    .joins(:web_hook_event_types)
    .where("web_hooks.wildcard_web_hook = ? OR web_hook_event_types.name = ?", true, type.to_s)
    .distinct
end

.content_typesObject



26
27
28
# File 'app/models/web_hook.rb', line 26

def self.content_types
  @content_types ||= Enum.new("application/json" => 1, "application/x-www-form-urlencoded" => 2)
end

.default_event_typesObject



34
35
36
# File 'app/models/web_hook.rb', line 34

def self.default_event_types
  [WebHookEventType.find(WebHookEventType::POST)]
end

.enqueue_hooks(type, event, opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/models/web_hook.rb', line 50

def self.enqueue_hooks(type, event, opts = {})
  active_web_hooks(type).each do |web_hook|
    Jobs.enqueue(
      :emit_web_hook_event,
      opts.merge(web_hook_id: web_hook.id, event_name: event.to_s, event_type: type.to_s),
    )
  end
end

.enqueue_object_hooks(type, object, event, serializer = nil, opts = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'app/models/web_hook.rb', line 59

def self.enqueue_object_hooks(type, object, event, serializer = nil, opts = {})
  if active_web_hooks(type).exists?
    payload = WebHook.generate_payload(type, object, serializer)

    WebHook.enqueue_hooks(type, event, opts.merge(id: object.id, payload: payload))
  end
end

.enqueue_post_hooks(event, post, payload = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/web_hook.rb', line 86

def self.enqueue_post_hooks(event, post, payload = nil)
  if active_web_hooks("post").exists? && post.present?
    payload ||= WebHook.generate_payload(:post, post)

    WebHook.enqueue_hooks(
      :post,
      event,
      id: post.id,
      category_id: post.topic&.category_id,
      tag_ids: post.topic&.tags&.pluck(:id),
      payload: payload,
    )
  end
end

.enqueue_topic_hooks(event, topic, payload = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/web_hook.rb', line 67

def self.enqueue_topic_hooks(event, topic, payload = nil)
  if active_web_hooks("topic").exists? && topic.present?
    payload ||=
      begin
        topic_view = TopicView.new(topic.id, Discourse.system_user, skip_staff_action: true)
        WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer)
      end

    WebHook.enqueue_hooks(
      :topic,
      event,
      id: topic.id,
      category_id: topic.category_id,
      tag_ids: topic.tags.pluck(:id),
      payload: payload,
    )
  end
end

.generate_payload(type, object, serializer = nil) ⇒ Object



101
102
103
104
105
106
# File 'app/models/web_hook.rb', line 101

def self.generate_payload(type, object, serializer = nil)
  serializer ||= TagSerializer if type == :tag
  serializer ||= "WebHook#{type.capitalize}Serializer".constantize

  serializer.new(object, scope: self.guardian, root: false).to_json
end

.last_delivery_statusesObject



30
31
32
# File 'app/models/web_hook.rb', line 30

def self.last_delivery_statuses
  @last_delivery_statuses ||= Enum.new(inactive: 1, failed: 2, successful: 3, disabled: 4)
end

Instance Method Details

#strip_urlObject



38
39
40
# File 'app/models/web_hook.rb', line 38

def strip_url
  self.payload_url = (payload_url || "").strip.presence
end

#tag_names=(tag_names_arg) ⇒ Object



22
23
24
# File 'app/models/web_hook.rb', line 22

def tag_names=(tag_names_arg)
  DiscourseTagging.add_or_create_tags_by_name(self, tag_names_arg, unlimited: true)
end