OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
use local time table to find reminder recipients
ulferts7207e0b6bdd5
diff --git a/app/models/users/scopes/having_reminder_mail_to_send_now.rb b/app/models/users/scopes/having_reminder_mail_to_send_now.rbindex 26b8c66b52e..0e5bc7e483b 100644--- a/app/models/users/scopes/having_reminder_mail_to_send_now.rb+++ b/app/models/users/scopes/having_reminder_mail_to_send_now.rb@@ -37,71 +37,42 @@ module Users::Scopes# * That user has an unread notification# * The user hasn't been informed about the unread notification before# * The user has configured reminder mails to be sent now.+ # This assumes that users only have full hours specified for the time they desire+ # to receive a reminder mail.def having_reminder_mail_to_send_now# Left outer join as not all user instances have preferences associated- # but we still want to select them- recipient_candidates = User.active- .left_joins(:preference)- .where(where_statement)+ # but we still want to select them.+ recipient_candidates = User+ .active+ .left_joins(:preference)+ .joins(local_time_join)subscriber_ids = Notification.unsent_reminders_before(recipient: recipient_candidates, time: Time.current).group(:recipient_id).select(:recipient_id)- User.where(id: subscriber_ids)+ where(id: subscriber_ids)end- def where_statement- current_timestamp_utc = Time.current.getutc-- age = age_statement(current_timestamp_utc)- <<-SQL.squish- #{age} < make_interval(mins=>30)- AND- #{age} >= make_interval(mins=>0)- SQL- end-- # Creates a SQL snippet for calculating the time between now and- # the reminder time slot with the each user's time zone- # @param [Time] current_timestamp_utc- def age_statement(current_timestamp_utc)- year = current_timestamp_utc.year- month = current_timestamp_utc.month- day = current_timestamp_utc.day-- case_statement = case_statement_zone_name_to_offset-- slot_time = Time.zone.parse(Setting.notification_email_digest_time)-- <<-SQL.squish- age(- now(),- make_timestamptz(#{year}, #{month}, #{day}, #{slot_time.hour}, #{slot_time.min}, 0, (#{case_statement}))- )+ def local_time_join+ <<~SQL.squish+ JOIN (#{local_time_table}) AS local_times+ ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone+ AND local_times.time = '#{Setting.notification_email_digest_time}:00+00:00'SQLend- def case_statement_zone_name_to_offset- return @case_statement_zone_name_to_offset if @case_statement_zone_name_to_offset-+ def local_time_tablecurrent_time = Time.current- statement = ActiveSupport::TimeZone.all.map do |zone|- offset = current_offset(zone, current_time)- "WHEN user_preferences.settings->>'time_zone' = '#{zone.name.gsub("'") { "''" }}' THEN '#{offset}'"- end- @case_statement_zone_name_to_offset = "CASE\n#{statement.join("\n")}\nELSE '+00:00'\nEND"- end- # The real offset of a time zone depends of the moment we ask. Winter and summer time- # have different offsets of UTC. For instance, time zone "Berlin" in winter has +01:00- # and in summer +02:00- # @param [Time] time- # @param [TimeZone] zone- # @return [String]- def current_offset(zone, time = Time.current)- time.in_time_zone(zone).formatted_offset+ times_with_zones = ActiveSupport::TimeZone+ .all+ .map do |z|+ [current_time.in_time_zone(z).strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]+ end++ "SELECT * FROM #{arel_table.grouping(Arel::Nodes::ValuesList.new(times_with_zones)).as('t(time, zone)').to_sql}"endendenddiff --git a/app/workers/notifications/schedule_reminder_mails_job.rb b/app/workers/notifications/schedule_reminder_mails_job.rbindex ca55ed20abb..21c0036cd73 100644--- a/app/workers/notifications/schedule_reminder_mails_job.rb+++ b/app/workers/notifications/schedule_reminder_mails_job.rb@@ -30,8 +30,8 @@module Notificationsclass ScheduleReminderMailsJob < Cron::CronJob- # runs every 30min step, so 00:00, 00:30, 01:00, 01:30...- self.cron_expression = '/30 * * * *'+ # runs every hour, so 00:00, 01:00...+ self.cron_expression = '0 * * * *'def performUser.having_reminder_mail_to_send_now.pluck(:id).each do |subscriber_ids|diff --git a/spec/features/notifications/reminder_mail_spec.rb b/spec/features/notifications/reminder_mail_spec.rbindex 50ac70664c0..0c136cbb5c3 100644--- a/spec/features/notifications/reminder_mail_spec.rb+++ b/spec/features/notifications/reminder_mail_spec.rb@@ -41,7 +41,7 @@ describe "Reminder email", type: :feature, js: true doendend- context 'configuring via the my page' do+ context 'when configuring via the my page' dolet(:reminders_settings_page) { Pages::My::Reminders.new(current_user) }current_user do@@ -51,7 +51,7 @@ describe "Reminder email", type: :feature, js: true doit_behaves_like 'reminder settings'end- context 'configuring via the user administration page' do+ context 'when configuring via the user administration page' dolet(:reminders_settings_page) { Pages::Reminders::Settings.new(other_user) }let(:other_user) { FactoryBot.create :user }@@ -63,7 +63,7 @@ describe "Reminder email", type: :feature, js: true doit_behaves_like 'reminder settings'end- context 'sending' do+ describe 'sending' dolet!(:project) { FactoryBot.create :project, members: { current_user => role } }let!(:mute_project) { FactoryBot.create :project, members: { current_user => role } }let(:role) { FactoryBot.create(:role, permissions: %i[view_work_packages]) }@@ -134,6 +134,5 @@ describe "Reminder email", type: :feature, js: true doexpect(ActionMailer::Base.deliveries.first.subject).to eql "OpenProject - 1 unread notification including a mention"end-endenddiff --git a/spec/support/schedule_reminder_mails.rb b/spec/support/schedule_reminder_mails.rbindex b94f904beed..ed4c0ee05fd 100644--- a/spec/support/schedule_reminder_mails.rb+++ b/spec/support/schedule_reminder_mails.rb@@ -29,6 +29,5 @@### Calculates a slot in the user's local time that hits for scheduling reminder mail jobsdef hitting_reminder_slot_for(user, current_utc_time = Time.current.getutc)- local_time = current_utc_time.in_time_zone(user.time_zone)- "#{'%02d' % local_time.hour}:#{local_time.min <= 30 ? '00' : '30'}"+ current_utc_time.in_time_zone(user.time_zone).strftime('%H:00')end