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
extract `user to be notified` logic into scope
ulferts99c138770092
diff --git a/app/models/user.rb b/app/models/user.rbindex cb21d88fc36..fafd653866a 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -76,7 +76,8 @@ class User < Principalscopes :find_by_login,:newest,:notified_on_all,- :watcher_recipients+ :watcher_recipients,+ :having_reminder_mail_to_send_nowdef self.create_blocked_scope(scope, blocked)scope.where(blocked_condition(blocked))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.rbnew file mode 100644index 00000000000..26b8c66b52e--- /dev/null+++ b/app/models/users/scopes/having_reminder_mail_to_send_now.rb@@ -0,0 +1,108 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Users::Scopes+ module HavingReminderMailToSendNow+ extend ActiveSupport::Concern++ class_methods do+ # Returns all users for which a reminder mails should be sent now. A user will be included if:+ # * 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.+ 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)++ subscriber_ids = Notification+ .unsent_reminders_before(recipient: recipient_candidates, time: Time.current)+ .group(:recipient_id)+ .select(:recipient_id)++ User.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}))+ )+ SQL+ end++ def case_statement_zone_name_to_offset+ return @case_statement_zone_name_to_offset if @case_statement_zone_name_to_offset++ current_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+ end+ end+ end+enddiff --git a/app/workers/cron/schedule_reminder_mails_job.rb b/app/workers/cron/schedule_reminder_mails_job.rbindex 5a3a45ccda3..cd3cc77e926 100644--- a/app/workers/cron/schedule_reminder_mails_job.rb+++ b/app/workers/cron/schedule_reminder_mails_job.rb@@ -34,80 +34,9 @@ module Cronself.cron_expression = '/30 * * * *'def perform- subscribers_of_current_slot_having_notifications do |subscriber_ids|- subscriber_ids.each do |recipient_id|- Mails::ReminderJob.perform_later(recipient_id)- end+ User.having_reminder_mail_to_send_now.pluck(:id).each do |subscriber_ids|+ Mails::ReminderJob.perform_later(subscriber_ids)endend-- private-- def subscribers_of_current_slot_having_notifications- # 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)-- subscriber_ids = Notification- .unsent_reminders_before(recipient: recipient_candidates, time: Time.current)- .group(:recipient_id)- .pluck(:recipient_id)-- yield(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}))- )- SQL- end-- def case_statement_zone_name_to_offset- return @case_statement_zone_name_to_offset if @case_statement_zone_name_to_offset-- current_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- endendenddiff --git a/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rbnew file mode 100644index 00000000000..c61100ca9eb--- /dev/null+++ b/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb@@ -0,0 +1,161 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe User, '.with_reminder_mail_to_send_now', type: :job do+ subject(:scope) do+ described_class.having_reminder_mail_to_send_now+ end++ # As it is hard to mock Postgres's "now()" method, in the specs here we need to adopt the slot time+ # relative to the local time of the user that we want to hit.+ let(:current_utc_time) { Time.current.getutc }+ let(:slot_time) { hitting_reminder_slot_for(hitting_user, current_utc_time) } # ie. "08:00", "08:30"++ let(:hitting_user) { paris_user }+ let(:paris_user) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) } # time_zone in winter is +01:00+ let(:moscow_user) { FactoryBot.create(:user, preferences: { time_zone: "Moscow" }) } # time_zone all year is +03:00+ let(:greenland_user) { FactoryBot.create(:user, preferences: { time_zone: "Greenland" }) } # time_zone in winter is -03:00+ let(:no_zone_user) { FactoryBot.create(:user) } # time_zone is nil+ let(:notifications) { FactoryBot.create(:notification, recipient: hitting_user) }++ before do+ allow(Setting).to receive(:notification_email_digest_time).and_return(slot_time)+ allow(Time).to receive(:current).and_return(current_utc_time)+ notifications+ end++ context 'for a user whose local time is matching the configured time' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago)+ end++ it 'contains the user' do+ expect(scope)+ .to match_array([paris_user])+ end+ end++ context 'for a user whose local time is matching the configured time (in a non CET time zone)' do+ let(:slot_time) { hitting_reminder_slot_for(moscow_user, current_utc_time) }+ let(:notifications) do+ FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ end++ it 'contains the user' do+ expect(scope)+ .to match_array([moscow_user])+ end+ end++ context 'for a user whose local time is matching the configured time but without a notification' do+ let(:notifications) do+ # There is a notification for a different user+ FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is matching the configured time but with an already read notification (IAN)' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago, read_ian: true)+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is matching the configured time but with an already read notification (reminder)' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago, read_mail_digest: true)+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is matching the configured time but with the user being inactive' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago)+ end++ before do+ paris_user.locked!+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is before the configured time' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ end++ it 'contains the user' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is after the configured time' do+ let(:notifications) do+ FactoryBot.create(:notification, recipient: greenland_user, created_at: 5.minutes.ago)+ end++ it 'contains the user' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user without a time zone' do+ let(:slot_time) { hitting_reminder_slot_for(no_zone_user, current_utc_time) }+ let(:notifications) do+ FactoryBot.create(:notification, recipient: no_zone_user, created_at: 5.minutes.ago)+ end++ it 'is including the user as UTC is assumed' do+ expect(scope)+ .to match_array([no_zone_user])+ end+ end+enddiff --git a/spec/workers/cron/schedule_reminder_mails_job_spec.rb b/spec/workers/cron/schedule_reminder_mails_job_spec.rbindex 4da5dd5059c..23a61cd8c19 100644--- a/spec/workers/cron/schedule_reminder_mails_job_spec.rb+++ b/spec/workers/cron/schedule_reminder_mails_job_spec.rb@@ -31,137 +31,37 @@require 'spec_helper'describe Cron::ScheduleReminderMailsJob, type: :job do- # As it is hard to mock Postgres's "now()" method, in the specs here we need to adopt the slot time- # relative to the local time of the user that we want to hit.- let(:current_utc_time) { Time.current.getutc }- let(:slot_time) { hitting_reminder_slot_for(hitting_user, current_utc_time) } # ie. "08:00", "08:30"+ subject(:job) { described_class.perform_now }- let(:hitting_user) { paris_user }- let(:paris_user) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) } # time_zone in winter is +01:00- let(:no_zone_user) { FactoryBot.create(:user) } # time_zone is nil- let(:notifications) { FactoryBot.create(:notification, recipient: hitting_user) }-- subject(:perform_job) do- described_class.new.perform- end+ let(:ids) { [23, 42] }before do- allow(Setting).to receive(:notification_email_digest_time).and_return(slot_time)- notifications- end-- context 'with paris_user as hitting_user' do- let(:moscow_user) { FactoryBot.create(:user, preferences: { time_zone: "Moscow" }) } # time_zone all year is +03:00- let(:greenland_user) { FactoryBot.create(:user, preferences: { time_zone: "Greenland" }) } # time_zone in winter is -03:00- let(:notifications) do- FactoryBot.create(:notification, recipient: hitting_user, created_at: 5.minutes.ago)- FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)- FactoryBot.create(:notification, recipient: greenland_user, created_at: 5.minutes.ago)- FactoryBot.create(:notification, recipient: no_zone_user, created_at: 5.minutes.ago)- end-- before do- allow(Time).to receive(:current).and_return(current_utc_time)- end-- it 'schedules ReminderMailJobs for all users that subscribed for that slot in their local time' do- # `hitting_user` is `paris_user`.- # `slot_time` (expressed string in local time) is set to be at the beginning of the first or second half hour- # block of the started hour.- expect { perform_job }- .to enqueue_job(Mails::ReminderJob)- .with(hitting_user.id)-- # `moscow_user` is in a different time zone (higher offset than Paris) so should not hit for the same `slot_time`- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(moscow_user.id)-- # `greenland_user` is in a different time (lower offset than Paris) so should not hit for the same `slot_time`- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(greenland_user.id)-- # `no_zone_user` should fall back to UTC time zone and thus have lower offset as `paris_user` and not hit- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(no_zone_user.id)- end- end-- context 'when slot_time in UTC' do- let(:hitting_user) { no_zone_user }- let(:notifications) do- FactoryBot.create(:notification, recipient: hitting_user, created_at: 5.minutes.ago)- end-- it 'schedules a job for users without timezone set' do- expect { perform_job }- .to enqueue_job(Mails::ReminderJob)- .with(hitting_user.id)- end- end-- context 'when hitting user is not active' do- let(:hitting_user) do- paris_user.locked!- paris_user- end- let(:notifications) do- FactoryBot.create(:notification, recipient: hitting_user)- end-- it 'does not schedule for users that are not active' do- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(hitting_user.id)- end- end-- context 'with a user without notifications' do- # Create another user just as `hitting_user` but without notifications- let(:paris_user_without_notifications) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) }-- it 'does not schedule reminder mail job' do- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(paris_user_without_notifications.id)- end+ scope = instance_double(ActiveRecord::Relation)+ allow(User)+ .to receive(:having_reminder_mail_to_send_now)+ .and_return(scope)++ allow(scope)+ .to receive(:pluck)+ .with(:id)+ .and_return(ids)end- context 'with a user with read IAN notifications' do- # Create another user just as `hitting_user` but without notifications- let(:paris_user_with_read_ian_notifications) do- FactoryBot.create(:user, preferences: { time_zone: "Paris" })- end- let(:notifications) do- FactoryBot.create(:notification,- recipient: paris_user_with_read_ian_notifications,- read_ian: true)- end-- it 'does not schedule reminder mail job' do- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(paris_user_with_read_ian_notifications.id)- end- end-- context 'with a user with who already received a reminder for a notification' do- # Create another user just as `hitting_user` but without notifications- let(:paris_user_with_read_mail_digest_notifications) do- FactoryBot.create(:user, preferences: { time_zone: "Paris" })- end- let(:notifications) do- FactoryBot.create(:notification,- recipient: paris_user_with_read_mail_digest_notifications,- read_mail_digest: true)- end-- it 'does not schedule reminder mail job' do- expect { perform_job }- .not_to enqueue_job(Mails::ReminderJob)- .with(paris_user_with_read_mail_digest_notifications.id)+ describe '#perform' do+ it 'schedules reminder jobs for every user with a reminder mails to be sent' do+ expect { subject }+ .to change(enqueued_jobs, :count)+ .by(2)++ expect(Mails::ReminderJob)+ .to have_been_enqueued+ .exactly(:once)+ .with(23)++ expect(Mails::ReminderJob)+ .to have_been_enqueued+ .exactly(:once)+ .with(42)endendend