2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class BroadcastMessage < ApplicationRecord
|
2016-10-06 17:17:11 -04:00
|
|
|
include CacheMarkdownField
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
|
|
|
|
2019-03-20 16:01:27 -04:00
|
|
|
cache_markdown_field :message, pipeline: :broadcast_message, whitelisted: true
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :message, presence: true
|
2013-11-12 07:28:12 -05:00
|
|
|
validates :starts_at, presence: true
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :ends_at, presence: true
|
2019-12-10 10:07:52 -05:00
|
|
|
validates :broadcast_type, presence: true
|
2013-11-12 08:08:20 -05:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :color, allow_blank: true, color: true
|
|
|
|
validates :font, allow_blank: true, color: true
|
2013-12-19 13:11:01 -05:00
|
|
|
|
2015-12-31 17:07:11 -05:00
|
|
|
default_value_for :color, '#E75E40'
|
|
|
|
default_value_for :font, '#FFFFFF'
|
|
|
|
|
2019-08-31 15:57:00 -04:00
|
|
|
CACHE_KEY = 'broadcast_message_current_json'
|
2019-12-10 10:07:52 -05:00
|
|
|
BANNER_CACHE_KEY = 'broadcast_message_current_banner_json'
|
|
|
|
NOTIFICATION_CACHE_KEY = 'broadcast_message_current_notification_json'
|
2017-08-09 09:49:30 -04:00
|
|
|
|
|
|
|
after_commit :flush_redis_cache
|
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
enum broadcast_type: {
|
|
|
|
banner: 1,
|
|
|
|
notification: 2
|
|
|
|
}
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def current_banner_messages(current_path = nil)
|
|
|
|
fetch_messages BANNER_CACHE_KEY, current_path do
|
|
|
|
current_and_future_messages.banner
|
|
|
|
end
|
2018-12-07 14:16:45 -05:00
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def current_notification_messages(current_path = nil)
|
|
|
|
fetch_messages NOTIFICATION_CACHE_KEY, current_path do
|
|
|
|
current_and_future_messages.notification
|
|
|
|
end
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def current(current_path = nil)
|
|
|
|
fetch_messages CACHE_KEY, current_path do
|
|
|
|
current_and_future_messages
|
|
|
|
end
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def current_and_future_messages
|
|
|
|
where('ends_at > :now', now: Time.current).order_id_asc
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def cache
|
2020-03-05 13:08:19 -05:00
|
|
|
::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
|
|
|
|
Gitlab::JsonCache.new(cache_key_with_version: false)
|
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def cache_expires_in
|
|
|
|
2.weeks
|
|
|
|
end
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_messages(cache_key, current_path)
|
|
|
|
messages = cache.fetch(cache_key, as: BroadcastMessage, expires_in: cache_expires_in) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
now_or_future = messages.select(&:now_or_future?)
|
2018-12-19 11:33:16 -05:00
|
|
|
|
2020-03-05 13:08:19 -05:00
|
|
|
# If there are cached entries but they don't match the ones we are
|
|
|
|
# displaying we'll refresh the cache so we don't need to keep filtering.
|
|
|
|
cache.expire(cache_key) if now_or_future != messages
|
2019-12-10 10:07:52 -05:00
|
|
|
|
|
|
|
now_or_future.select(&:now?).select { |message| message.matches_current_path(current_path) }
|
|
|
|
end
|
2018-04-11 13:49:56 -04:00
|
|
|
end
|
|
|
|
|
2015-12-31 17:07:11 -05:00
|
|
|
def active?
|
|
|
|
started? && !ended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2019-12-10 10:07:52 -05:00
|
|
|
Time.current >= starts_at
|
2015-12-31 17:07:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ended?
|
2019-12-10 10:07:52 -05:00
|
|
|
ends_at < Time.current
|
2015-12-31 17:07:11 -05:00
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
|
2017-08-18 08:52:11 -04:00
|
|
|
def now?
|
2019-12-10 10:07:52 -05:00
|
|
|
(starts_at..ends_at).cover?(Time.current)
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def future?
|
2019-12-10 10:07:52 -05:00
|
|
|
starts_at > Time.current
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def now_or_future?
|
|
|
|
now? || future?
|
|
|
|
end
|
|
|
|
|
2019-12-02 10:06:36 -05:00
|
|
|
def matches_current_path(current_path)
|
|
|
|
return true if current_path.blank? || target_path.blank?
|
|
|
|
|
|
|
|
current_path.match(Regexp.escape(target_path).gsub('\\*', '.*'))
|
|
|
|
end
|
|
|
|
|
2017-08-09 09:49:30 -04:00
|
|
|
def flush_redis_cache
|
2019-12-10 10:07:52 -05:00
|
|
|
[CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key|
|
|
|
|
self.class.cache.expire(key)
|
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
end
|
2013-11-12 06:47:28 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
BroadcastMessage.prepend_if_ee('EE::BroadcastMessage')
|