"Fossies" - the Fresh Open Source Software Archive

Member "discourse-2.8.3/config/initializers/012-web_hook_events.rb" (14 Apr 2022, 3324 Bytes) of package /linux/www/discourse-2.8.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "012-web_hook_events.rb": 2.7.13_vs_2.8.0.

    1 # frozen_string_literal: true
    2 
    3 %i(
    4   topic_recovered
    5 ).each do |event|
    6   DiscourseEvent.on(event) do |topic, _|
    7     WebHook.enqueue_topic_hooks(event, topic)
    8   end
    9 end
   10 
   11 DiscourseEvent.on(:topic_status_updated) do |topic, status|
   12   WebHook.enqueue_topic_hooks("topic_#{status}_status_updated", topic)
   13 end
   14 
   15 DiscourseEvent.on(:topic_created) do |topic, _, _|
   16   WebHook.enqueue_topic_hooks(:topic_created, topic)
   17 end
   18 
   19 %i(
   20   post_created
   21   post_recovered
   22 ).each do |event|
   23   DiscourseEvent.on(event) do |post, _, _|
   24     WebHook.enqueue_post_hooks(event, post)
   25   end
   26 end
   27 
   28 DiscourseEvent.on(:post_edited) do |post, topic_changed|
   29   unless post.topic&.trashed?
   30 
   31     # if we are editing the OP and the topic is changed, do not send
   32     # the post_edited event -- this event is sent separately because
   33     # when we update the OP in the UI we send two API calls in this order:
   34     #
   35     # PUT /t/topic-name
   36     # PUT /post/243552
   37     if post.is_first_post? && topic_changed
   38       WebHook.enqueue_topic_hooks(:topic_edited, post.topic)
   39     else
   40       WebHook.enqueue_post_hooks(:post_edited, post)
   41     end
   42   end
   43 end
   44 
   45 %i(
   46   user_logged_out
   47   user_created
   48   user_logged_in
   49   user_approved
   50   user_updated
   51   user_confirmed_email
   52 ).each do |event|
   53   DiscourseEvent.on(event) do |user|
   54     WebHook.enqueue_object_hooks(:user, user, event)
   55   end
   56 end
   57 
   58 %i(
   59   group_created
   60   group_updated
   61 ).each do |event|
   62   DiscourseEvent.on(event) do |group|
   63     WebHook.enqueue_object_hooks(:group, group, event)
   64   end
   65 end
   66 
   67 %i(
   68   category_created
   69   category_updated
   70 ).each do |event|
   71   DiscourseEvent.on(event) do |category|
   72     WebHook.enqueue_object_hooks(:category, category, event)
   73   end
   74 end
   75 
   76 %i(
   77   tag_created
   78   tag_updated
   79 ).each do |event|
   80   DiscourseEvent.on(event) do |tag|
   81     WebHook.enqueue_object_hooks(:tag, tag, event, TagSerializer)
   82   end
   83 end
   84 
   85 %i(
   86   user_badge_granted
   87 ).each do |event|
   88   # user_badge_revoked
   89   DiscourseEvent.on(event) do |badge, user_id|
   90     ub = UserBadge.find_by(badge: badge, user_id: user_id)
   91     WebHook.enqueue_object_hooks(:user_badge, ub, event, UserBadgeSerializer)
   92   end
   93 end
   94 
   95 %i(
   96   reviewable_created
   97   reviewable_score_updated
   98 ).each do |event|
   99   DiscourseEvent.on(event) do |reviewable|
  100     WebHook.enqueue_object_hooks(:reviewable, reviewable, event, reviewable.serializer)
  101   end
  102 end
  103 
  104 DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable|
  105   WebHook.enqueue_object_hooks(:reviewable, reviewable, :reviewable_transitioned_to, reviewable.serializer)
  106 end
  107 
  108 DiscourseEvent.on(:notification_created) do |notification|
  109   WebHook.enqueue_object_hooks(:notification, notification, :notification_created, NotificationSerializer)
  110 end
  111 
  112 DiscourseEvent.on(:user_added_to_group) do |user, group, options|
  113   group_user = GroupUser.find_by(user: user, group: group)
  114   WebHook.enqueue_object_hooks(:group_user, group_user, :user_added_to_group, WebHookGroupUserSerializer)
  115 end
  116 
  117 DiscourseEvent.on(:like_created) do |post_action|
  118   user = post_action.user
  119   group_ids = user.groups.map(&:id)
  120   topic = Topic.includes(:tags).joins(:posts).find_by(posts: { id: post_action.post_id })
  121   category_id = topic&.category_id
  122   tag_ids = topic&.tag_ids
  123 
  124   WebHook.enqueue_object_hooks(:like,
  125     post_action, :post_liked, WebHookLikeSerializer,
  126     group_ids: group_ids, category_id: category_id, tag_ids: tag_ids
  127   )
  128 end