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
|
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'
|
|
|
|
|
2018-12-07 19:16:45 +00:00
|
|
|
CACHE_KEY = 'broadcast_message_current_json'.freeze
|
|
|
|
LEGACY_CACHE_KEY = 'broadcast_message_current'.freeze
|
2017-08-09 13:49:30 +00:00
|
|
|
|
|
|
|
after_commit :flush_redis_cache
|
|
|
|
|
2013-11-12 13:08:20 +00:00
|
|
|
def self.current
|
2018-12-19 16:33:16 +00:00
|
|
|
messages = cache.fetch(CACHE_KEY, as: BroadcastMessage, expires_in: cache_expires_in) do
|
2018-12-07 19:16:45 +00:00
|
|
|
remove_legacy_cache_key
|
2018-12-19 16:33:16 +00:00
|
|
|
current_and_future_messages
|
2018-12-07 19:16:45 +00:00
|
|
|
end
|
2017-08-18 12:52:11 +00:00
|
|
|
|
2018-12-07 19:16:45 +00:00
|
|
|
return [] unless messages&.present?
|
2017-08-18 12:52:11 +00:00
|
|
|
|
|
|
|
now_or_future = messages.select(&:now_or_future?)
|
|
|
|
|
|
|
|
# If there are cached entries but none are to be displayed we'll purge the
|
|
|
|
# cache so we don't keep running this code all the time.
|
2018-12-19 16:33:16 +00:00
|
|
|
cache.expire(CACHE_KEY) if now_or_future.empty?
|
2017-08-18 12:52:11 +00:00
|
|
|
|
|
|
|
now_or_future.select(&:now?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current_and_future_messages
|
2017-08-15 10:27:37 +00:00
|
|
|
where('ends_at > :now', now: Time.zone.now).order_id_asc
|
2015-12-31 22:07:11 +00:00
|
|
|
end
|
|
|
|
|
2018-12-19 16:33:16 +00:00
|
|
|
def self.cache
|
|
|
|
Gitlab::JsonCache.new(cache_key_with_version: false)
|
|
|
|
end
|
|
|
|
|
2018-04-11 17:49:56 +00:00
|
|
|
def self.cache_expires_in
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-12-07 19:16:45 +00:00
|
|
|
# This can be removed in GitLab 12.0+
|
|
|
|
# The old cache key had an indefinite lifetime, and in an HA
|
|
|
|
# environment a one-shot migration would not work because the cache
|
|
|
|
# would be repopulated by a node that has not been upgraded.
|
|
|
|
def self.remove_legacy_cache_key
|
2018-12-19 16:33:16 +00:00
|
|
|
cache.expire(LEGACY_CACHE_KEY)
|
2018-12-07 19:16:45 +00:00
|
|
|
end
|
|
|
|
|
2015-12-31 22:07:11 +00:00
|
|
|
def active?
|
|
|
|
started? && !ended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
|
|
|
Time.zone.now >= starts_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def ended?
|
|
|
|
ends_at < Time.zone.now
|
|
|
|
end
|
2017-08-09 13:49:30 +00:00
|
|
|
|
2017-08-18 12:52:11 +00:00
|
|
|
def now?
|
|
|
|
(starts_at..ends_at).cover?(Time.zone.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def future?
|
|
|
|
starts_at > Time.zone.now
|
|
|
|
end
|
|
|
|
|
|
|
|
def now_or_future?
|
|
|
|
now? || future?
|
|
|
|
end
|
|
|
|
|
2017-08-09 13:49:30 +00:00
|
|
|
def flush_redis_cache
|
2018-12-19 16:33:16 +00:00
|
|
|
self.class.cache.expire(CACHE_KEY)
|
2018-12-07 19:16:45 +00:00
|
|
|
self.class.remove_legacy_cache_key
|
2017-08-09 13:49:30 +00:00
|
|
|
end
|
2013-11-12 11:47:28 +00:00
|
|
|
end
|