2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class BroadcastMessage < ApplicationRecord
|
2016-10-06 17:17:11 -04:00
|
|
|
include CacheMarkdownField
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
ALLOWED_TARGET_ACCESS_LEVELS = [
|
|
|
|
Gitlab::Access::GUEST,
|
|
|
|
Gitlab::Access::REPORTER,
|
|
|
|
Gitlab::Access::DEVELOPER,
|
|
|
|
Gitlab::Access::MAINTAINER,
|
|
|
|
Gitlab::Access::OWNER
|
|
|
|
].freeze
|
|
|
|
|
2019-03-20 16:01:27 -04:00
|
|
|
cache_markdown_field :message, pipeline: :broadcast_message, whitelisted: true
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :message, presence: true
|
2013-11-12 07:28:12 -05:00
|
|
|
validates :starts_at, presence: true
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :ends_at, presence: true
|
2019-12-10 10:07:52 -05:00
|
|
|
validates :broadcast_type, presence: true
|
2022-03-07 07:16:11 -05:00
|
|
|
validates :target_access_levels, inclusion: { in: ALLOWED_TARGET_ACCESS_LEVELS }
|
2013-11-12 08:08:20 -05:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :color, allow_blank: true, color: true
|
|
|
|
validates :font, allow_blank: true, color: true
|
2013-12-19 13:11:01 -05:00
|
|
|
|
2015-12-31 17:07:11 -05:00
|
|
|
default_value_for :color, '#E75E40'
|
|
|
|
default_value_for :font, '#FFFFFF'
|
|
|
|
|
2019-08-31 15:57:00 -04:00
|
|
|
CACHE_KEY = 'broadcast_message_current_json'
|
2019-12-10 10:07:52 -05:00
|
|
|
BANNER_CACHE_KEY = 'broadcast_message_current_banner_json'
|
|
|
|
NOTIFICATION_CACHE_KEY = 'broadcast_message_current_notification_json'
|
2017-08-09 09:49:30 -04:00
|
|
|
|
|
|
|
after_commit :flush_redis_cache
|
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
enum broadcast_type: {
|
|
|
|
banner: 1,
|
|
|
|
notification: 2
|
|
|
|
}
|
|
|
|
|
|
|
|
class << self
|
2022-03-07 07:16:11 -05:00
|
|
|
def current_banner_messages(current_path: nil, user_access_level: nil)
|
|
|
|
fetch_messages BANNER_CACHE_KEY, current_path, user_access_level do
|
2019-12-10 10:07:52 -05:00
|
|
|
current_and_future_messages.banner
|
|
|
|
end
|
2018-12-07 14:16:45 -05:00
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
def current_notification_messages(current_path: nil, user_access_level: nil)
|
|
|
|
fetch_messages NOTIFICATION_CACHE_KEY, current_path, user_access_level do
|
2019-12-10 10:07:52 -05:00
|
|
|
current_and_future_messages.notification
|
|
|
|
end
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
def current(current_path: nil, user_access_level: nil)
|
|
|
|
fetch_messages CACHE_KEY, current_path, user_access_level do
|
2019-12-10 10:07:52 -05:00
|
|
|
current_and_future_messages
|
|
|
|
end
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def current_and_future_messages
|
|
|
|
where('ends_at > :now', now: Time.current).order_id_asc
|
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def cache
|
2020-03-05 13:08:19 -05:00
|
|
|
::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
|
2022-02-28 13:14:03 -05:00
|
|
|
Gitlab::JsonCache.new
|
2020-03-05 13:08:19 -05:00
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
def cache_expires_in
|
|
|
|
2.weeks
|
|
|
|
end
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
private
|
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
def fetch_messages(cache_key, current_path, user_access_level)
|
2019-12-10 10:07:52 -05:00
|
|
|
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 11:33:16 -05:00
|
|
|
|
2020-03-05 13:08:19 -05: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 10:07:52 -05:00
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
messages = now_or_future.select(&:now?)
|
|
|
|
messages = messages.select do |message|
|
|
|
|
message.matches_current_user_access_level?(user_access_level)
|
|
|
|
end
|
|
|
|
messages.select do |message|
|
|
|
|
message.matches_current_path(current_path)
|
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
2018-04-11 13:49:56 -04:00
|
|
|
end
|
|
|
|
|
2015-12-31 17:07:11 -05:00
|
|
|
def active?
|
|
|
|
started? && !ended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2019-12-10 10:07:52 -05:00
|
|
|
Time.current >= starts_at
|
2015-12-31 17:07:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ended?
|
2019-12-10 10:07:52 -05:00
|
|
|
ends_at < Time.current
|
2015-12-31 17:07:11 -05:00
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
|
2017-08-18 08:52:11 -04:00
|
|
|
def now?
|
2019-12-10 10:07:52 -05:00
|
|
|
(starts_at..ends_at).cover?(Time.current)
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def future?
|
2019-12-10 10:07:52 -05:00
|
|
|
starts_at > Time.current
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def now_or_future?
|
|
|
|
now? || future?
|
|
|
|
end
|
|
|
|
|
2022-03-07 07:16:11 -05:00
|
|
|
def matches_current_user_access_level?(user_access_level)
|
|
|
|
return false if target_access_levels.present? && Feature.disabled?(:role_targeted_broadcast_messages, default_enabled: :yaml)
|
|
|
|
return true unless target_access_levels.present?
|
|
|
|
|
|
|
|
target_access_levels.include? user_access_level
|
|
|
|
end
|
|
|
|
|
2019-12-02 10:06:36 -05:00
|
|
|
def matches_current_path(current_path)
|
2020-10-28 11:08:49 -04:00
|
|
|
return false if current_path.blank? && target_path.present?
|
2019-12-02 10:06:36 -05:00
|
|
|
return true if current_path.blank? || target_path.blank?
|
|
|
|
|
2021-04-22 05:09:45 -04:00
|
|
|
# Ensure paths are consistent across callers.
|
|
|
|
# This fixes a mismatch between requests in the GUI and CLI
|
|
|
|
#
|
|
|
|
# This has to be reassigned due to frozen strings being provided.
|
|
|
|
unless current_path.start_with?("/")
|
|
|
|
current_path = "/#{current_path}"
|
|
|
|
end
|
|
|
|
|
2020-04-24 05:09:44 -04:00
|
|
|
escaped = Regexp.escape(target_path).gsub('\\*', '.*')
|
|
|
|
regexp = Regexp.new "^#{escaped}$", Regexp::IGNORECASE
|
|
|
|
|
|
|
|
regexp.match(current_path)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
2017-08-09 09:49:30 -04:00
|
|
|
def flush_redis_cache
|
2019-12-10 10:07:52 -05:00
|
|
|
[CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key|
|
|
|
|
self.class.cache.expire(key)
|
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
end
|
2013-11-12 06:47:28 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
BroadcastMessage.prepend_mod_with('BroadcastMessage')
|