kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
4.5 KiB
114 lines
4.5 KiB
3 years ago
|
#-- 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
|
||
3 years ago
|
module HavingReminderMailToSend
|
||
3 years ago
|
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.
|
||
3 years ago
|
# This assumes that users only have full hours specified for the times they desire
|
||
|
# to receive a reminder mail at.
|
||
3 years ago
|
# @param [DateTime] earliest_time The earliest time to consider as a matching slot. All full hours from that time
|
||
|
# to now are included.
|
||
|
# Only the time part is used and that is floored to the hour (e.g. 2021-05-03 10:34:12+02:00 -> 08:00:00).
|
||
|
# Needs to be before the current time.
|
||
|
def having_reminder_mail_to_send(earliest_time)
|
||
3 years ago
|
# Left outer join as not all user instances have preferences associated
|
||
3 years ago
|
# but we still want to select them.
|
||
|
recipient_candidates = User
|
||
|
.active
|
||
|
.left_joins(:preference)
|
||
3 years ago
|
.joins(local_time_join(earliest_time))
|
||
3 years ago
|
|
||
|
subscriber_ids = Notification
|
||
|
.unsent_reminders_before(recipient: recipient_candidates, time: Time.current)
|
||
|
.group(:recipient_id)
|
||
|
.select(:recipient_id)
|
||
|
|
||
3 years ago
|
where(id: subscriber_ids)
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def local_time_join(earliest_time)
|
||
3 years ago
|
# Joins the times local to the user preferences and then checks whether:
|
||
|
# * reminders are enabled
|
||
|
# * any of the configured reminder time is the local time
|
||
|
# If no time zone is present, utc is assumed.
|
||
|
# If no reminder settings are present, sending a reminder at 08:00 local time is assumed.
|
||
3 years ago
|
<<~SQL.squish
|
||
3 years ago
|
JOIN (#{local_time_table(earliest_time)}) AS local_times
|
||
3 years ago
|
ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
|
||
3 years ago
|
AND (
|
||
|
(
|
||
|
user_preferences.settings->'daily_reminders'->'times' IS NULL
|
||
|
AND local_times.time = '08:00:00+00:00'
|
||
|
)
|
||
|
OR
|
||
|
(
|
||
|
(user_preferences.settings->'daily_reminders'->'enabled')::boolean
|
||
|
AND user_preferences.settings->'daily_reminders'->'times' ? local_times.time
|
||
|
)
|
||
|
)
|
||
3 years ago
|
SQL
|
||
|
end
|
||
|
|
||
3 years ago
|
def local_time_table(earliest_time)
|
||
|
times = hours_between_earliest_and_now(earliest_time)
|
||
3 years ago
|
|
||
3 years ago
|
values_list = Arel::Nodes::ValuesList.new(times_for_zones(times))
|
||
3 years ago
|
|
||
3 years ago
|
"SELECT * FROM #{arel_table.grouping(values_list).as('t(time, zone)').to_sql}"
|
||
|
end
|
||
|
|
||
|
def times_for_zones(times)
|
||
|
ActiveSupport::TimeZone
|
||
|
.all
|
||
|
.map do |z|
|
||
|
times.map do |time|
|
||
|
[time.in_time_zone(z).strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
|
||
|
end
|
||
|
end
|
||
|
.flatten(1)
|
||
|
end
|
||
|
|
||
|
def hours_between_earliest_and_now(earliest_time)
|
||
|
raise ArgumentError if Time.current - earliest_time < 0
|
||
|
|
||
|
(0..(((Time.current - earliest_time) / 1.hour).round)).map do |i|
|
||
|
(earliest_time + i.hours).utc
|
||
|
end
|
||
3 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|