A platform for community discussion. Free, open, simple.
FEAT: add webhooks for calendar events (#38315)
Bannon Tannerfb19e92f37a8
diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rbindex b52fb909a00..9ed93346240 100644--- a/app/models/web_hook_event_type.rb+++ b/app/models/web_hook_event_type.rb@@ -17,6 +17,7 @@ class WebHookEventType < ActiveRecord::BaseUSER_PROMOTED = 16TOPIC_VOTING = 17CHAT_MESSAGE = 18+ CALENDAR_EVENT = 19enum :group,{@@ -37,6 +38,7 @@ class WebHookEventType < ActiveRecord::Basevoting: 14,chat: 15,custom: 16,+ calendar: 17,},scopes: false@@ -88,6 +90,9 @@ class WebHookEventType < ActiveRecord::Basechat_message_edited: 1802,chat_message_trashed: 1803,chat_message_restored: 1804,+ calendar_event_created: 1901,+ calendar_event_updated: 1902,+ calendar_event_destroyed: 1903,}has_and_belongs_to_many :web_hooks@@ -119,6 +124,16 @@ class WebHookEventType < ActiveRecord::Base],)end+ unless defined?(SiteSetting.discourse_post_event_enabled) &&+ SiteSetting.discourse_post_event_enabled+ ids_to_exclude.concat(+ [+ TYPES[:calendar_event_created],+ TYPES[:calendar_event_updated],+ TYPES[:calendar_event_destroyed],+ ],+ )+ endself.where.not(id: ids_to_exclude)endenddiff --git a/db/fixtures/007_web_hook_event_types.rb b/db/fixtures/007_web_hook_event_types.rbindex d2959d55624..fd9d14b8c58 100644--- a/db/fixtures/007_web_hook_event_types.rb+++ b/db/fixtures/007_web_hook_event_types.rb@@ -235,3 +235,18 @@ WebHookEventType.seed do |b|b.name = "chat_message_restored"b.group = WebHookEventType.groups[:chat]end+WebHookEventType.seed do |b|+ b.id = WebHookEventType::TYPES[:calendar_event_created]+ b.name = "calendar_event_created"+ b.group = WebHookEventType.groups[:calendar]+end+WebHookEventType.seed do |b|+ b.id = WebHookEventType::TYPES[:calendar_event_updated]+ b.name = "calendar_event_updated"+ b.group = WebHookEventType.groups[:calendar]+end+WebHookEventType.seed do |b|+ b.id = WebHookEventType::TYPES[:calendar_event_destroyed]+ b.name = "calendar_event_destroyed"+ b.group = WebHookEventType.groups[:calendar]+enddiff --git a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rbindex 9902bf6cd45..e8cb3ddf27e 100644--- a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb+++ b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb@@ -79,7 +79,9 @@ module DiscoursePostEventevent = Event.find(params[:id])guardian.ensure_can_act_on_discourse_post_event!(event)event.publish_update!- event.destroy+ payload = WebHook.build_calendar_event_payload(event)+ event.destroy!+ WebHook.enqueue_calendar_event_hooks(:calendar_event_destroyed, event, payload)render json: success_jsonenddiff --git a/plugins/discourse-calendar/config/locales/client.en.yml b/plugins/discourse-calendar/config/locales/client.en.ymlindex 1f4981323e2..658825df773 100644--- a/plugins/discourse-calendar/config/locales/client.en.yml+++ b/plugins/discourse-calendar/config/locales/client.en.yml@@ -6,6 +6,12 @@ en:categories:discourse_post_event: "Discourse Event"discourse_calendar: "Calendar and Events"+ web_hooks:+ calendar_event:+ group_name: "Calendar Event"+ calendar_event_created: "Calendar event is created"+ calendar_event_updated: "Calendar event is updated"+ calendar_event_destroyed: "Calendar event is destroyed"js:notifications:titles:diff --git a/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rb b/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rbnew file mode 100644index 00000000000..349d26eb198--- /dev/null+++ b/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rb@@ -0,0 +1,62 @@+# frozen_string_literal: true++module DiscoursePostEvent+ module WebHookExtension+ extend ActiveSupport::Concern++ class_methods do+ def enqueue_calendar_event_hooks(event_name, calendar_event, payload = nil)+ if active_web_hooks(event_name).exists? && calendar_event.present?+ payload ||= WebHook.build_calendar_event_payload(calendar_event)++ WebHook.enqueue_hooks(+ :calendar_event,+ event_name,+ id: calendar_event.id,+ category_id: calendar_event.post&.topic&.category_id,+ tag_ids: calendar_event.post&.topic&.tags&.pluck(:id),+ payload: payload,+ )+ end+ end++ def build_calendar_event_payload(calendar_event)+ post = calendar_event.post+ topic = post&.topic++ {+ event: {+ id: calendar_event.id,+ name: calendar_event.name,+ description: calendar_event.description,+ location: calendar_event.location,+ url: calendar_event.url,+ status: DiscoursePostEvent::Event.statuses[calendar_event.status]&.to_s,+ starts_at: calendar_event.starts_at,+ ends_at: calendar_event.ends_at,+ recurrence: calendar_event.recurrence,+ recurrence_until: calendar_event.recurrence_until,+ timezone: calendar_event.timezone,+ custom_fields: calendar_event.custom_fields,+ closed: calendar_event.closed,+ max_attendees: calendar_event.max_attendees,+ chat_enabled: calendar_event.chat_enabled,+ allowed_groups: calendar_event.raw_invitees,+ },+ post: {+ id: post&.id,+ post_number: post&.post_number,+ url: post&.url,+ },+ topic: {+ id: topic&.id,+ title: topic&.title,+ url: topic&.url,+ category_id: topic&.category_id,+ tags: topic&.tags&.pluck(:name),+ },+ }.to_json+ end+ end+ end+enddiff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rbindex 2f9b3b7232d..cfa612d8bb5 100644--- a/plugins/discourse-calendar/plugin.rb+++ b/plugins/discourse-calendar/plugin.rb@@ -139,6 +139,7 @@ after_initialize dorequire_relative "lib/discourse_post_event/post_extension"require_relative "lib/discourse_post_event/rrule_generator"require_relative "lib/discourse_post_event/rrule_configurator"+ require_relative "lib/discourse_post_event/web_hook_extension"::ActionController::Base.prepend_view_path File.expand_path("../app/views", __FILE__)@@ -146,6 +147,7 @@ after_initialize doExportCsvController.prepend(DiscoursePostEvent::ExportCsvControllerExtension)Jobs::ExportCsvFile.prepend(DiscoursePostEvent::ExportPostEventCsvReportExtension)Post.prepend(DiscoursePostEvent::PostExtension)+ ::WebHook.prepend(DiscoursePostEvent::WebHookExtension)endadd_to_class(:user, :can_create_discourse_post_event?) do@@ -209,18 +211,45 @@ after_initialize doend,) { DiscoursePostEvent::EventSerializer.new(object.event, scope: scope, root: false) }- on(:post_created) { |post| DiscoursePostEvent::Event.update_from_raw(post) }+ on(:post_created) do |post|+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload+ if SiteSetting.discourse_post_event_enabled && post.event+ WebHook.enqueue_calendar_event_hooks(:calendar_event_created, post.event)+ end+ end- on(:post_edited) { |post| DiscoursePostEvent::Event.update_from_raw(post) }+ on(:post_edited) do |post|+ event_before = post.event+ had_event_before = event_before.present?+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload++ if SiteSetting.discourse_post_event_enabled+ if post.event && had_event_before+ WebHook.enqueue_calendar_event_hooks(:calendar_event_updated, post.event)+ elsif post.event && !had_event_before+ WebHook.enqueue_calendar_event_hooks(:calendar_event_created, post.event)+ elsif !post.event && had_event_before+ payload = WebHook.build_calendar_event_payload(event_before)+ WebHook.enqueue_calendar_event_hooks(:calendar_event_destroyed, event_before, payload)+ end+ end+ endon(:post_destroyed) do |post|if SiteSetting.discourse_post_event_enabled && post.event+ payload = WebHook.build_calendar_event_payload(post.event)post.event.update!(deleted_at: Time.now)+ WebHook.enqueue_calendar_event_hooks(:calendar_event_destroyed, post.event, payload)endendon(:post_recovered) do |post|- post.event.update!(deleted_at: nil) if SiteSetting.discourse_post_event_enabled && post.event+ if SiteSetting.discourse_post_event_enabled && post.event+ post.event.update!(deleted_at: nil)+ WebHook.enqueue_calendar_event_hooks(:calendar_event_created, post.event)+ endendadd_preloaded_topic_list_custom_field DiscoursePostEvent::TOPIC_POST_EVENT_STARTS_ATdiff --git a/plugins/discourse-calendar/spec/fabricators/outgoing_calendar_event_web_hook_fabricator.rb b/plugins/discourse-calendar/spec/fabricators/outgoing_calendar_event_web_hook_fabricator.rbnew file mode 100644index 00000000000..6d7f31b3a61--- /dev/null+++ b/plugins/discourse-calendar/spec/fabricators/outgoing_calendar_event_web_hook_fabricator.rb@@ -0,0 +1,10 @@+# frozen_string_literal: true++Fabricator(:outgoing_calendar_event_web_hook, from: :web_hook) do+ after_build do |web_hook|+ web_hook.web_hook_event_types =+ WebHookEventType.where(+ name: %w[calendar_event_created calendar_event_updated calendar_event_destroyed],+ )+ end+enddiff --git a/plugins/discourse-calendar/spec/integration/outgoing_web_hooks_spec.rb b/plugins/discourse-calendar/spec/integration/outgoing_web_hooks_spec.rbnew file mode 100644index 00000000000..db4aac2cf3f--- /dev/null+++ b/plugins/discourse-calendar/spec/integration/outgoing_web_hooks_spec.rb@@ -0,0 +1,137 @@+# frozen_string_literal: true++RSpec.describe "Outgoing calendar event webhooks" do+ fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }+ fab!(:web_hook, :outgoing_calendar_event_web_hook)++ before do+ SiteSetting.calendar_enabled = true+ SiteSetting.discourse_post_event_enabled = true+ end++ let(:job_args) do+ Jobs::EmitWebHookEvent+ .jobs+ .map { |job| job["args"].first }+ .find { |args| args["event_type"] == "calendar_event" }+ end+ let(:event_name) { job_args["event_name"] }+ let(:payload) { JSON.parse(job_args["payload"]) }++ describe "calendar_event_created" do+ it "fires when a post with event markup is created" do+ post = create_post_with_event(user)+ event = post.reload.event++ expect(job_args).to be_present+ expect(event_name).to eq("calendar_event_created")+ expect(job_args["category_id"]).to eq(post.topic.category_id)++ expect(payload["event"]["id"]).to eq(event.id)+ expect(payload["event"]["starts_at"]).to be_present+ expect(payload["post"]["id"]).to eq(post.id)+ expect(payload["post"]["url"]).to eq(post.url)+ expect(payload["topic"]["id"]).to eq(post.topic_id)+ expect(payload["topic"]["title"]).to eq(post.topic.title)+ expect(payload["topic"]["category_id"]).to eq(post.topic.category_id)+ end+ end++ describe "calendar_event_updated" do+ let(:event_post) { create_post_with_event(user) }++ it "fires when an existing event is edited" do+ event_post++ revisor = PostRevisor.new(event_post, event_post.topic)+ revisor.revise!(user, raw: "[event start=\"#{1.day.from_now.utc.iso8601}\"]\n[/event]")++ job = find_webhook_job(:calendar_event_updated)+ expect(job).to be_present+ expect(job["event_name"]).to eq("calendar_event_updated")+ end++ it "fires calendar_event_created when event markup is added to an existing post" do+ post = Fabricate(:post, user: user)+ revisor = PostRevisor.new(post, post.topic)+ revisor.revise!(user, raw: "[event start=\"#{1.day.from_now.utc.iso8601}\"]\n[/event]")++ job = find_webhook_job(:calendar_event_created)+ expect(job).to be_present+ expect(job["event_name"]).to eq("calendar_event_created")+ end+ end++ describe "calendar_event_destroyed" do+ let(:event_post) { create_post_with_event(user) }+ it "fires when event markup is removed from a post" do+ event_post++ revisor = PostRevisor.new(event_post, event_post.topic)+ revisor.revise!(user, raw: "No event here.")++ job = find_webhook_job(:calendar_event_destroyed)+ expect(job).to be_present+ expect(job["event_name"]).to eq("calendar_event_destroyed")+ end++ it "fires when a post with an event is destroyed" do+ event_post++ event = event_post.reload.event+ PostDestroyer.new(user, event_post).destroy++ job = find_webhook_job(:calendar_event_destroyed)+ expect(job).to be_present+ expect(job["event_name"]).to eq("calendar_event_destroyed")+ expect(payload["event"]["id"]).to eq(event.id)+ end++ it "fires when DELETE /discourse-post-event/events/:id is called" do+ event_post+ sign_in(user)++ event = event_post.reload.event+ delete "/discourse-post-event/events/#{event.id}.json"++ job = find_webhook_job(:calendar_event_destroyed)+ expect(job).to be_present+ expect(job["event_name"]).to eq("calendar_event_destroyed")+ expect(payload["event"]["id"]).to eq(event.id)+ end+ end++ describe "calendar_event_created on post recovery" do+ let(:event_post) { create_post_with_event(user) }++ it "fires when a destroyed post with an event is recovered" do+ event_post+ PostDestroyer.new(user, event_post).destroy++ expect { PostDestroyer.new(user, event_post).recover }.to change {+ Jobs::EmitWebHookEvent.jobs.count do |j|+ j["args"].first["event_name"] == "calendar_event_created"+ end+ }.by(1)+ end+ end++ describe "WebHookEventType.active" do+ it "excludes all 3 calendar event types when discourse_post_event_enabled is false" do+ SiteSetting.discourse_post_event_enabled = false++ active_calendar_event_types =+ WebHookEventType.active.where(group: WebHookEventType.groups[:calendar])+ expect(active_calendar_event_types.count).to eq(0)+ end+ end++ def find_webhook_job(event_name)+ Jobs::EmitWebHookEvent+ .jobs+ .map { |job| job["args"].first }+ .find do |args|+ args["event_type"] == "calendar_event" && args["event_name"] == event_name.to_s+ end+ end+enddiff --git a/spec/models/web_hook_event_type_spec.rb b/spec/models/web_hook_event_type_spec.rbindex 51f1499e27a..3afdd274ba0 100644--- a/spec/models/web_hook_event_type_spec.rb+++ b/spec/models/web_hook_event_type_spec.rb@@ -50,6 +50,7 @@ RSpec.describe WebHookEventType doSiteSetting.stubs(:assign_enabled).returns(true)SiteSetting.stubs(:topic_voting_enabled).returns(true)SiteSetting.stubs(:chat_enabled).returns(true)+ SiteSetting.stubs(:discourse_post_event_enabled).returns(true)plugins_event_types = WebHookEventType.active.map(&:name) - core_event_typesexpect(plugins_event_types).to match_array(%w[@@ -63,6 +64,9 @@ RSpec.describe WebHookEventType dochat_message_editedchat_message_trashedchat_message_restored+ calendar_event_created+ calendar_event_updated+ calendar_event_destroyed],)end