2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class BroadcastMessage < ApplicationRecord
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
|
|
|
|
2019-03-20 20:01:27 +00:00
|
|
|
cache_markdown_field :message, pipeline: :broadcast_message, whitelisted: true
|
2016-10-06 21:17:11 +00:00
|
|
|
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :message, presence: true
|
2013-11-12 12:28:12 +00:00
|
|
|
validates :starts_at, presence: true
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :ends_at, presence: true
|
2019-12-10 15:07:52 +00:00
|
|
|
validates :broadcast_type, presence: true
|
2013-11-12 13:08:20 +00:00
|
|
|
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :color, allow_blank: true, color: true
|
|
|
|
validates :font, allow_blank: true, color: true
|
2013-12-19 18:11:01 +00:00
|
|
|
|
2015-12-31 22:07:11 +00:00
|
|
|
default_value_for :color, '#E75E40'
|
|
|
|
default_value_for :font, '#FFFFFF'
|
|
|
|
|
2019-08-31 19:57:00 +00:00
|
|
|
CACHE_KEY = 'broadcast_message_current_json'
|
2019-12-10 15:07:52 +00:00
|
|
|
BANNER_CACHE_KEY = 'broadcast_message_current_banner_json'
|
|
|
|
NOTIFICATION_CACHE_KEY = 'broadcast_message_current_notification_json'
|
2017-08-09 13:49:30 +00:00
|
|
|
|
|
|
|
after_commit :flush_redis_cache
|
|
|
|
|
2019-12-10 15:07:52 +00: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 19:16:45 +00:00
|
|
|
end
|
2017-08-18 12:52:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00: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 12:52:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00:00
|
|
|
def current(current_path = nil)
|
|
|
|
fetch_messages CACHE_KEY, current_path do
|
|
|
|
current_and_future_messages
|
|
|
|
end
|
|
|
|
end
|
2017-08-18 12:52:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00:00
|
|
|
def current_and_future_messages
|
|
|
|
where('ends_at > :now', now: Time.current).order_id_asc
|
|
|
|
end
|
2017-08-18 12:52:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00:00
|
|
|
def cache
|
2020-03-05 18:08:19 +00:00
|
|
|
::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
|
|
|
|
Gitlab::JsonCache.new(cache_key_with_version: false)
|
|
|
|
end
|
2019-12-10 15:07:52 +00:00
|
|
|
end
|
2017-08-18 12:52:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00:00
|
|
|
def cache_expires_in
|
|
|
|
2.weeks
|
|
|
|
end
|
2015-12-31 22:07:11 +00:00
|
|
|
|
2019-12-10 15:07:52 +00: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 16:33:16 +00:00
|
|
|
|
2020-03-05 18:08:19 +00: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 15:07:52 +00:00
|
|
|
|
|
|
|
now_or_future.select(&:now?).select { |message| message.matches_current_path(current_path) }
|
|
|
|
end
|
2018-04-11 17:49:56 +00:00
|
|
|
end
|
|
|
|
|
2015-12-31 22:07:11 +00:00
|
|
|
def active?
|
|
|
|
started? && !ended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2019-12-10 15:07:52 +00:00
|
|
|
Time.current >= starts_at
|
2015-12-31 22:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ended?
|
2019-12-10 15:07:52 +00:00
|
|
|
ends_at < Time.current
|
2015-12-31 22:07:11 +00:00
|
|
|
end
|
2017-08-09 13:49:30 +00:00
|
|
|
|
2017-08-18 12:52:11 +00:00
|
|
|
def now?
|
2019-12-10 15:07:52 +00:00
|
|
|
(starts_at..ends_at).cover?(Time.current)
|
2017-08-18 12:52:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def future?
|
2019-12-10 15:07:52 +00:00
|
|
|
starts_at > Time.current
|
2017-08-18 12:52:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def now_or_future?
|
|
|
|
now? || future?
|
|
|
|
end
|
|
|
|
|
2019-12-02 15:06:36 +00:00
|
|
|
def matches_current_path(current_path)
|
2020-10-28 15:08:49 +00:00
|
|
|
return false if current_path.blank? && target_path.present?
|
2019-12-02 15:06:36 +00:00
|
|
|
return true if current_path.blank? || target_path.blank?
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
escaped = Regexp.escape(target_path).gsub('\\*', '.*')
|
|
|
|
regexp = Regexp.new "^#{escaped}$", Regexp::IGNORECASE
|
|
|
|
|
|
|
|
regexp.match(current_path)
|
2019-12-02 15:06:36 +00:00
|
|
|
end
|
|
|
|
|
2017-08-09 13:49:30 +00:00
|
|
|
def flush_redis_cache
|
2019-12-10 15:07:52 +00:00
|
|
|
[CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key|
|
|
|
|
self.class.cache.expire(key)
|
|
|
|
end
|
2017-08-09 13:49:30 +00:00
|
|
|
end
|
2013-11-12 11:47:28 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
BroadcastMessage.prepend_if_ee('EE::BroadcastMessage')
|