diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-10 15:07:52 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-10 15:07:52 +0000 |
commit | 27d91a629918e417a9e87825e838209b9ace79c1 (patch) | |
tree | e066c3fc84e3011641e662252810cb2c240edb90 /app/models/broadcast_message.rb | |
parent | 5e11c9b77cb1b2b77ee29359047b55807afe255d (diff) | |
download | gitlab-ce-27d91a629918e417a9e87825e838209b9ace79c1.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/broadcast_message.rb')
-rw-r--r-- | app/models/broadcast_message.rb | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/app/models/broadcast_message.rb b/app/models/broadcast_message.rb index 9c2ae92071d..b3d72ebdcf3 100644 --- a/app/models/broadcast_message.rb +++ b/app/models/broadcast_message.rb @@ -9,6 +9,7 @@ class BroadcastMessage < ApplicationRecord validates :message, presence: true validates :starts_at, presence: true validates :ends_at, presence: true + validates :broadcast_type, presence: true validates :color, allow_blank: true, color: true validates :font, allow_blank: true, color: true @@ -17,35 +18,62 @@ class BroadcastMessage < ApplicationRecord default_value_for :font, '#FFFFFF' CACHE_KEY = 'broadcast_message_current_json' + BANNER_CACHE_KEY = 'broadcast_message_current_banner_json' + NOTIFICATION_CACHE_KEY = 'broadcast_message_current_notification_json' after_commit :flush_redis_cache - def self.current(current_path = nil) - messages = cache.fetch(CACHE_KEY, as: BroadcastMessage, expires_in: cache_expires_in) do - current_and_future_messages + 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 end - return [] unless messages&.present? + def current_notification_messages(current_path = nil) + fetch_messages NOTIFICATION_CACHE_KEY, current_path do + current_and_future_messages.notification + end + end - now_or_future = messages.select(&:now_or_future?) + def current(current_path = nil) + fetch_messages CACHE_KEY, current_path do + current_and_future_messages + end + end - # 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. - cache.expire(CACHE_KEY) if now_or_future.empty? + def current_and_future_messages + where('ends_at > :now', now: Time.current).order_id_asc + end - now_or_future.select(&:now?).select { |message| message.matches_current_path(current_path) } - end + def cache + Gitlab::JsonCache.new(cache_key_with_version: false) + end - def self.current_and_future_messages - where('ends_at > :now', now: Time.zone.now).order_id_asc - end + def cache_expires_in + 2.weeks + end - def self.cache - Gitlab::JsonCache.new(cache_key_with_version: false) - end + 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?) - def self.cache_expires_in - 2.weeks + # 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. + cache.expire(cache_key) if now_or_future.empty? + + now_or_future.select(&:now?).select { |message| message.matches_current_path(current_path) } + end end def active? @@ -53,19 +81,19 @@ class BroadcastMessage < ApplicationRecord end def started? - Time.zone.now >= starts_at + Time.current >= starts_at end def ended? - ends_at < Time.zone.now + ends_at < Time.current end def now? - (starts_at..ends_at).cover?(Time.zone.now) + (starts_at..ends_at).cover?(Time.current) end def future? - starts_at > Time.zone.now + starts_at > Time.current end def now_or_future? @@ -79,7 +107,9 @@ class BroadcastMessage < ApplicationRecord end def flush_redis_cache - self.class.cache.expire(CACHE_KEY) + [CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key| + self.class.cache.expire(key) + end end end |