Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add friendly names to calendar subscription feeds (#39154)

Rafael dos Santos Silvab38c2d7e9b98
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 16507492465..d3726926183 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1934,6 +1934,9 @@ class UsersController < ApplicationController
end
end
format.ics do
+ @calendar_name =
+ I18n.t("calendar_subscriptions.bookmarks_feed_name", site_title: SiteSetting.title)
+
bookmark_query = Bookmark.with_reminders.where(user_id: user.id)
after_param = params[:after].presence || 3.months.ago.iso8601
diff --git a/app/views/users/bookmarks.ics.erb b/app/views/users/bookmarks.ics.erb
index f28cece62d1..53862fe5478 100644
--- a/app/views/users/bookmarks.ics.erb
+++ b/app/views/users/bookmarks.ics.erb
@@ -1,6 +1,7 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Discourse//<%= Discourse.current_hostname %>//<%= Discourse.full_version %>//EN
+X-WR-CALNAME:<%= IcalEncoder.encode(@calendar_name) %>
<% @bookmark_reminders.each do |bookmark| %>
BEGIN:VEVENT
UID:bookmark_reminder_#<%= bookmark.id %>@<%= Discourse.current_hostname %>
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f5fdbe79f97..59b0679b603 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1253,6 +1253,7 @@ en:
calendar_subscriptions:
application_name: "Calendar Subscriptions"
+ bookmarks_feed_name: "%{site_title} - Bookmark reminders"
user_api_key:
title: 'Authorize "%{application_name}"'
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.rb
index ec78d0d5f43..c108e121a8b 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
@@ -23,6 +23,8 @@ module DiscoursePostEvent
respond_to do |format|
format.ics do
+ @calendar_name = calendar_name_for_ics
+
after_time = filtered_events_params[:after]&.to_datetime || Time.current
before_time = filtered_events_params[:before]&.to_datetime || 1.year.from_now
@@ -212,5 +214,20 @@ module DiscoursePostEvent
time.in_time_zone(event.timezone).iso8601(3)
end
end
+
+ def calendar_name_for_ics
+ translation_key =
+ if filtered_events_params[:attending_user].present? &&
+ current_user&.username_lower == filtered_events_params[:attending_user].downcase
+ "my_events_feed_name"
+ else
+ "all_events_feed_name"
+ end
+
+ I18n.t(
+ "discourse_calendar.calendar_subscriptions.#{translation_key}",
+ site_title: SiteSetting.title,
+ )
+ end
end
end
diff --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.erb
index 495afce6a1a..9ff3896023c 100644
--- 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.erb
@@ -1,6 +1,7 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Discourse//<%= Discourse.current_hostname %>//<%= Discourse.full_version %>//EN
+X-WR-CALNAME:<%= IcalEncoder.encode(@calendar_name) %>
<%- @ics_events.each do |entry| -%>
<%- event = entry[:event] -%>
<%- starts_at = entry[:starts_at] -%>
diff --git a/plugins/discourse-calendar/config/locales/server.en.yml b/plugins/discourse-calendar/config/locales/server.en.yml
index 9fb92445942..c76524d8c45 100644
--- a/plugins/discourse-calendar/config/locales/server.en.yml
+++ b/plugins/discourse-calendar/config/locales/server.en.yml
@@ -65,6 +65,9 @@ en:
calendar_first_day_of_week: "Set the first day of the week for calendar display. Options are Saturday, Sunday, or Monday."
discourse_calendar:
+ calendar_subscriptions:
+ all_events_feed_name: "%{site_title} - All events"
+ my_events_feed_name: "%{site_title} - My events"
invite_user_notification: "%{username} invited you to: %{description}"
calendar_must_be_in_first_post: "Calendar tag can only be used in first post of a topic."
more_than_one_calendar: "You can’t have more than one calendar in a post."
diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
index 22b00c16256..93246f71b30 100644
--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
@@ -75,8 +75,14 @@ module DiscoursePostEvent
)
body = response.body
+ calendar_name =
+ I18n.t(
+ "discourse_calendar.calendar_subscriptions.all_events_feed_name",
+ site_title: SiteSetting.title,
+ )
expect(body).to include("BEGIN:VCALENDAR")
expect(body).to include("END:VCALENDAR")
+ expect(body).to include("X-WR-CALNAME:#{IcalEncoder.encode(calendar_name)}")
expect(body).to include("BEGIN:VEVENT")
expect(body).to include("END:VEVENT")
expect(body).to include("SUMMARY:Test Event 1")
@@ -267,6 +273,12 @@ module DiscoursePostEvent
params: events_params.merge(user_api_key: user_api_key.key)
expect(response.status).to eq(200)
+ calendar_name =
+ I18n.t(
+ "discourse_calendar.calendar_subscriptions.my_events_feed_name",
+ site_title: SiteSetting.title,
+ )
+ expect(response.body).to include("X-WR-CALNAME:#{IcalEncoder.encode(calendar_name)}")
expect(response.body).to include("SUMMARY:Going Event")
expect(response.body).to include("SUMMARY:Interested Event")
end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index d25dd21fe0b..b313eb4ad3a 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -7333,10 +7333,13 @@ RSpec.describe UsersController do
sign_in(user1)
get "/u/#{user1.username}/bookmarks.ics"
expect(response.status).to eq(200)
+ calendar_name =
+ I18n.t("calendar_subscriptions.bookmarks_feed_name", site_title: SiteSetting.title)
expect(response.body).to eq(<<~ICS)
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Discourse//#{Discourse.current_hostname}//#{Discourse.full_version}//EN
+ X-WR-CALNAME:#{IcalEncoder.encode(calendar_name)}
BEGIN:VEVENT
UID:bookmark_reminder_##{bookmark1.id}@#{Discourse.current_hostname}
DTSTAMP:#{bookmark1.updated_at.strftime(I18n.t("datetime_formats.formats.calendar_ics"))}