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