debian-mirror-gitlab/app/models/broadcast_message.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
4.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2023-06-20 00:43:36 +05:30
class BroadcastMessage < MainClusterwide::ApplicationRecord
2016-11-03 12:29:30 +05:30
include CacheMarkdownField
2015-04-26 12:48:37 +05:30
include Sortable
2023-06-20 00:43:36 +05:30
include IgnorableColumns
2015-04-26 12:48:37 +05:30
2022-05-07 20:08:51 +05:30
ALLOWED_TARGET_ACCESS_LEVELS = [
Gitlab::Access::GUEST,
Gitlab::Access::REPORTER,
Gitlab::Access::DEVELOPER,
Gitlab::Access::MAINTAINER,
Gitlab::Access::OWNER
].freeze
2023-06-20 00:43:36 +05:30
ignore_column :namespace_id, remove_with: '16.0', remove_after: '2022-06-22'
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
2022-05-07 20:08:51 +05:30
validates :target_access_levels, inclusion: { in: ALLOWED_TARGET_ACCESS_LEVELS }
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
2023-01-13 00:05:48 +05:30
attribute :color, default: '#E75E40'
attribute :font, default: '#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
2022-06-21 17:19:12 +05:30
enum theme: {
indigo: 0,
'light-indigo': 1,
blue: 2,
'light-blue': 3,
green: 4,
'light-green': 5,
red: 6,
'light-red': 7,
dark: 8,
light: 9
}, _default: 0, _prefix: true
2020-01-01 13:55:28 +05:30
enum broadcast_type: {
banner: 1,
notification: 2
}
class << self
2022-05-07 20:08:51 +05:30
def current_banner_messages(current_path: nil, user_access_level: nil)
fetch_messages BANNER_CACHE_KEY, current_path, user_access_level do
2020-01-01 13:55:28 +05:30
current_and_future_messages.banner
end
2019-02-15 15:39:39 +05:30
end
2017-09-10 17:25:29 +05:30
2022-05-07 20:08:51 +05:30
def current_notification_messages(current_path: nil, user_access_level: nil)
fetch_messages NOTIFICATION_CACHE_KEY, current_path, user_access_level do
2020-01-01 13:55:28 +05:30
current_and_future_messages.notification
end
end
2017-09-10 17:25:29 +05:30
2022-05-07 20:08:51 +05:30
def current(current_path: nil, user_access_level: nil)
fetch_messages CACHE_KEY, current_path, user_access_level do
2020-01-01 13:55:28 +05:30
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
2022-05-07 20:08:51 +05:30
Gitlab::JsonCache.new
2020-03-13 15:44:24 +05:30
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
2020-01-01 13:55:28 +05:30
private
2019-02-15 15:39:39 +05:30
2022-05-07 20:08:51 +05:30
def fetch_messages(cache_key, current_path, user_access_level)
2020-01-01 13:55:28 +05:30
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
2022-05-07 20:08:51 +05:30
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
2020-01-01 13:55:28 +05:30
end
2018-10-15 14:42:47 +05:30
end
def active?
started? && !ended?
end
def started?
2020-01-01 13:55:28 +05:30
Time.current >= starts_at
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
2022-05-07 20:08:51 +05:30
def matches_current_user_access_level?(user_access_level)
2022-07-16 23:28:13 +05:30
return false if target_access_levels.present? && Feature.disabled?(:role_targeted_broadcast_messages)
2022-05-07 20:08:51 +05:30
return true unless target_access_levels.present?
target_access_levels.include? user_access_level
end
2020-01-01 13:55:28 +05:30
def matches_current_path(current_path)
2021-01-29 00:20:46 +05:30
return false if current_path.blank? && target_path.present?
2020-01-01 13:55:28 +05:30
return true if current_path.blank? || target_path.blank?
2021-06-08 01:23:25 +05:30
# 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-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
2021-06-08 01:23:25 +05:30
BroadcastMessage.prepend_mod_with('BroadcastMessage')