Class: System::BroadcastMessage

Inherits:
MainClusterwide::ApplicationRecord show all
Includes:
CacheMarkdownField, Sortable
Defined in:
app/models/system/broadcast_message.rb

Constant Summary collapse

ALLOWED_TARGET_ACCESS_LEVELS =
[
  Gitlab::Access::GUEST,
  Gitlab::Access::REPORTER,
  Gitlab::Access::DEVELOPER,
  Gitlab::Access::MAINTAINER,
  Gitlab::Access::OWNER
].freeze
CACHE_KEY =
'broadcast_message_current_json'
'broadcast_message_current_banner_json'
NOTIFICATION_CACHE_KEY =
'broadcast_message_current_notification_json'

Constants included from CacheMarkdownField

CacheMarkdownField::INVALIDATED_BY

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from CacheMarkdownField

#skip_markdown_cache_validation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CacheMarkdownField

#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #mentionable_attributes_changed?, #mentioned_filtered_user_ids_for, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #updated_cached_html_for

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.cacheObject



80
81
82
83
84
# File 'app/models/system/broadcast_message.rb', line 80

def cache
  ::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
    Gitlab::Cache::JsonCaches::JsonKeyed.new
  end
end

.cache_expires_inObject



86
87
88
# File 'app/models/system/broadcast_message.rb', line 86

def cache_expires_in
  2.weeks
end

.current(current_path: nil, user_access_level: nil) ⇒ Object



74
75
76
77
78
# File 'app/models/system/broadcast_message.rb', line 74

def current(current_path: nil, user_access_level: nil)
  fetch_messages CACHE_KEY, current_path, user_access_level do
    current_and_future_messages
  end
end

.current_banner_messages(current_path: nil, user_access_level: nil) ⇒ Object



58
59
60
61
62
# File 'app/models/system/broadcast_message.rb', line 58

def current_banner_messages(current_path: nil, user_access_level: nil)
  fetch_messages BANNER_CACHE_KEY, current_path, user_access_level do
    current_and_future_messages.banner
  end
end

.current_notification_messages(current_path: nil, user_access_level: nil) ⇒ Object



68
69
70
71
72
# File 'app/models/system/broadcast_message.rb', line 68

def current_notification_messages(current_path: nil, user_access_level: nil)
  fetch_messages NOTIFICATION_CACHE_KEY, current_path, user_access_level do
    current_and_future_messages.notification
  end
end

.current_show_in_cli_banner_messagesObject



64
65
66
# File 'app/models/system/broadcast_message.rb', line 64

def current_show_in_cli_banner_messages
  current_banner_messages.select(&:show_in_cli?)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/system/broadcast_message.rb', line 111

def active?
  started? && !ended?
end

#ended?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/system/broadcast_message.rb', line 119

def ended?
  ends_at < Time.current
end

#flush_redis_cacheObject



157
158
159
160
161
# File 'app/models/system/broadcast_message.rb', line 157

def flush_redis_cache
  [CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key|
    self.class.cache.expire(key)
  end
end

#future?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/system/broadcast_message.rb', line 127

def future?
  starts_at > Time.current
end

#matches_current_path(current_path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/system/broadcast_message.rb', line 141

def matches_current_path(current_path)
  return false if current_path.blank? && target_path.present?
  return true if current_path.blank? || target_path.blank?

  # 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.
  current_path = "/#{current_path}" unless current_path.start_with?("/")

  escaped = Regexp.escape(target_path).gsub('\\*', '.*')
  regexp = Regexp.new "^#{escaped}$", Regexp::IGNORECASE

  regexp.match(current_path)
end

#matches_current_user_access_level?(user_access_level) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
# File 'app/models/system/broadcast_message.rb', line 135

def matches_current_user_access_level?(user_access_level)
  return true unless target_access_levels.present?

  target_access_levels.include? user_access_level
end

#now?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/system/broadcast_message.rb', line 123

def now?
  (starts_at..ends_at).cover?(Time.current)
end

#now_or_future?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/system/broadcast_message.rb', line 131

def now_or_future?
  now? || future?
end

#started?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/system/broadcast_message.rb', line 115

def started?
  Time.current >= starts_at
end