summaryrefslogtreecommitdiff
path: root/app/models/broadcast_message.rb
blob: 2d237383e60ebec0033a551490c31a6dd8291773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

class BroadcastMessage < ActiveRecord::Base
  include CacheMarkdownField
  include Sortable

  cache_markdown_field :message, pipeline: :broadcast_message

  validates :message,   presence: true
  validates :starts_at, presence: true
  validates :ends_at,   presence: true

  validates :color, allow_blank: true, color: true
  validates :font,  allow_blank: true, color: true

  default_value_for :color, '#E75E40'
  default_value_for :font,  '#FFFFFF'

  CACHE_KEY = 'broadcast_message_current_json'.freeze
  LEGACY_CACHE_KEY = 'broadcast_message_current'.freeze

  after_commit :flush_redis_cache

  def self.current
    messages = cache.fetch(CACHE_KEY, as: BroadcastMessage, expires_in: cache_expires_in) do
      remove_legacy_cache_key
      current_and_future_messages
    end

    return [] unless messages&.present?

    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.
    cache.expire(CACHE_KEY) if now_or_future.empty?

    now_or_future.select(&:now?)
  end

  def self.current_and_future_messages
    where('ends_at > :now', now: Time.zone.now).order_id_asc
  end

  def self.cache
    Gitlab::JsonCache.new(cache_key_with_version: false)
  end

  def self.cache_expires_in
    nil
  end

  # 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
    cache.expire(LEGACY_CACHE_KEY)
  end

  def active?
    started? && !ended?
  end

  def started?
    Time.zone.now >= starts_at
  end

  def ended?
    ends_at < Time.zone.now
  end

  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

  def flush_redis_cache
    self.class.cache.expire(CACHE_KEY)
    self.class.remove_legacy_cache_key
  end
end