"Fossies" - the Fresh Open Source Software Archive

Member "discourse-3.2.0/config/initializers/012-web_hook_events.rb" (30 Jan 2024, 3962 Bytes) of package /linux/www/discourse-3.2.0.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 latest Fossies "Diffs" side-by-side code changes report for "012-web_hook_events.rb": 3.1.5_vs_3.2.0.

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