A platform for community discussion. Free, open, simple.
DEV: add ical format response for discourse-post-events index route (#35143)
Kelvac554f1f60b9
diff --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 323a1074843..5ec5dc45aac 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@@ -2,6 +2,8 @@module DiscoursePostEventclass EventsController < DiscoursePostEventController+ skip_before_action :check_xhr, only: [:index], if: :ics_request?+def index@events =DiscoursePostEvent::EventFinder.search(current_user, filtered_events_params).includes(@@ -14,12 +16,21 @@ module DiscoursePostEvent# The detailed serializer is currently not used anywhere in the frontend, but available via APIserializer = params[:include_details] == "true" ? EventSerializer : BasicEventSerializer- render json:- ActiveModel::ArraySerializer.new(- @events,- each_serializer: serializer,- scope: guardian,- ).as_json+ respond_to do |format|+ format.ics do+ filename = "events-#{Digest::SHA1.hexdigest(@events.map(&:id).sort.join("-"))}.ics"+ response.headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""+ end++ format.json do+ render json:+ ActiveModel::ArraySerializer.new(+ @events,+ each_serializer: serializer,+ scope: guardian,+ ).as_json+ end+ endenddef invite@@ -118,16 +129,20 @@ module DiscoursePostEventprivate+ def ics_request?+ request.format.symbol == :ics+ end+def filtered_events_paramsparams.permit(:post_id,:category_id,:include_subcategories,:limit,- :before,:attending_user,:before,:after,+ :order,)endenddiff --git a/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erbnew file mode 100644index 00000000000..5c014b1a0ff--- /dev/null+++ b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb@@ -0,0 +1,15 @@+BEGIN:VCALENDAR+VERSION:2.0+PRODID:-//Discourse//<%= Discourse.current_hostname %>//<%= Discourse.full_version %>//EN+<% @events.each do |event| %>+ BEGIN:VEVENT+ UID:calendar_event_#<%= event.id %>@<%= Discourse.current_hostname %>+ DTSTAMP:<%= Time.now.utc.strftime("%Y%m%dT%H%M%SZ") %>+ DTSTART:<%= event.starts_at.strftime("%Y%m%dT%H%M%SZ") %>+ DTEND:<%= event.ends_at.presence ? event.ends_at.strftime("%Y%m%dT%H%M%SZ") : (event.starts_at + 1.hour).strftime("%Y%m%dT%H%M%SZ") %>+ SUMMARY:<%= event.name.presence || event.post.topic.title %>+ DESCRIPTION:<%= PrettyText.format_for_email(event.post.excerpt, event.post).html_safe.gsub(/\r?\n/, '\n') %>+ URL:<%= event.post.topic.url(event.post.post_number) %>+ END:VEVENT+<% end %>+END:VCALENDARdiff --git a/plugins/discourse-calendar/config/routes.rb b/plugins/discourse-calendar/config/routes.rbindex 5af4a238b54..42b0625a80f 100644--- a/plugins/discourse-calendar/config/routes.rb+++ b/plugins/discourse-calendar/config/routes.rb@@ -1,7 +1,13 @@# frozen_string_literal: trueDiscoursePostEvent::Engine.routes.draw do- get "/discourse-post-event/events" => "events#index", :format => :json+ get "/discourse-post-event/events" => "events#index",+ :constraints => {+ format: /(json|ics)/,+ },+ :defaults => {+ format: :json,+ }get "/discourse-post-event/events/:id" => "events#show"delete "/discourse-post-event/events/:id" => "events#destroy"post "/discourse-post-event/events" => "events#create"diff --git a/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb b/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rbindex a6df79114fb..0964a4982be 100644--- a/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb+++ b/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb@@ -10,7 +10,7 @@ module DiscoursePostEvent.then { |query| filter_by_attending_user(query, params, guardian, user) }.then { |query| filter_by_dates(query, params) }.then { |query| filter_by_category(query, params) }- .then { |query| apply_ordering(query) }+ .then { |query| apply_ordering(query, params) }.then { |query| apply_limit(query, params) }end@@ -27,11 +27,15 @@ module DiscoursePostEvent.merge(Post.secured(guardian)).merge(topics.or(pms)).joins(latest_event_date_join)- .select("discourse_post_event_events.*, latest_event_dates.starts_at")+ .select(+ "discourse_post_event_events.*, latest_event_dates.starts_at, latest_event_dates.finished_at",+ ).where("(discourse_post_event_events.recurrence IS NOT NULL) OR (latest_event_dates.starts_at IS NOT NULL) OR (discourse_post_event_events.original_starts_at IS NOT NULL)",)- .distinct+ .group(+ "discourse_post_event_events.id, latest_event_dates.starts_at, latest_event_dates.finished_at",+ )enddef self.latest_event_date_join@@ -143,8 +147,11 @@ module DiscoursePostEventevents.where(topics: { category_id: category_ids })end- def self.apply_ordering(events)- events.order("latest_event_dates.starts_at ASC, discourse_post_event_events.id ASC")+ def self.apply_ordering(events, params)+ order_direction = params[:order] == "desc" ? "DESC" : "ASC"+ events.order(+ "latest_event_dates.starts_at #{order_direction}, discourse_post_event_events.id #{order_direction}",+ )enddef self.apply_limit(events, params)diff --git a/plugins/discourse-calendar/spec/lib/discourse_post_event/event_finder_spec.rb b/plugins/discourse-calendar/spec/lib/discourse_post_event/event_finder_spec.rbindex 9844cf8ba1e..c419becc406 100644--- a/plugins/discourse-calendar/spec/lib/discourse_post_event/event_finder_spec.rb+++ b/plugins/discourse-calendar/spec/lib/discourse_post_event/event_finder_spec.rb@@ -237,5 +237,31 @@ describe DiscoursePostEvent::EventFinder doexpect(results).to include(current_event)endend++ describe "with an order parameter provided" do+ let!(:event1) { Fabricate(:event, original_starts_at: 3.days.from_now) }+ let!(:event2) { Fabricate(:event, original_starts_at: 1.day.from_now) }+ let!(:event3) { Fabricate(:event, original_starts_at: 2.days.from_now) }++ it "returns events in ascending order by default" do+ results = finder.search(current_user)+ expect(results.pluck(:id)).to eq([event2.id, event3.id, event1.id])+ end++ it "returns events in ascending order when order=asc" do+ results = finder.search(current_user, { order: "asc" })+ expect(results.pluck(:id)).to eq([event2.id, event3.id, event1.id])+ end++ it "returns events in descending order when order=desc" do+ results = finder.search(current_user, { order: "desc" })+ expect(results.pluck(:id)).to eq([event1.id, event3.id, event2.id])+ end++ it "defaults to ascending order for invalid order values" do+ results = finder.search(current_user, { order: "invalid" })+ expect(results.pluck(:id)).to eq([event2.id, event3.id, event1.id])+ end+ endendenddiff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rbindex f1a2bc0b91f..50b599880d8 100644--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb@@ -58,6 +58,30 @@ module DiscoursePostEventexpect(event_ids).to match_array([active_event1.id, active_event2.id])end++ it "should return events in ics format" do+ event1 = Fabricate(:event, original_starts_at: 1.day.from_now, name: "Test Event 1")+ event2 = Fabricate(:event, original_starts_at: 2.days.from_now, name: "Test Event 2")++ get "/discourse-post-event/events.ics"++ expect(response.status).to eq(200)+ expect(response.content_type).to include("text/calendar")++ event_ids_sorted = [event1.id, event2.id].sort.join("-")+ expected_filename = "events-#{Digest::SHA1.hexdigest(event_ids_sorted)}.ics"+ expect(response.headers["Content-Disposition"]).to eq(+ "attachment; filename=\"#{expected_filename}\"",+ )++ body = response.body+ expect(body).to include("BEGIN:VCALENDAR")+ expect(body).to include("END:VCALENDAR")+ expect(body).to include("BEGIN:VEVENT")+ expect(body).to include("END:VEVENT")+ expect(body).to include("SUMMARY:Test Event 1")+ expect(body).to include("SUMMARY:Test Event 2")+ endendcontext "with an existing post" do