Class: System::BroadcastMessage

Inherits:
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::PLANNER,
  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 HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::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, #mentionable_attributes_changed?, #mentioned_filtered_user_ids_for, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #store_mentions_after_commit?, #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, nullable_column?, 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 ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.cacheObject



83
84
85
86
87
# File 'app/models/system/broadcast_message.rb', line 83

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

.cache_expires_inObject



89
90
91
# File 'app/models/system/broadcast_message.rb', line 89

def cache_expires_in
  2.weeks
end

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



77
78
79
80
81
# File 'app/models/system/broadcast_message.rb', line 77

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



61
62
63
64
65
# File 'app/models/system/broadcast_message.rb', line 61

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



71
72
73
74
75
# File 'app/models/system/broadcast_message.rb', line 71

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



67
68
69
# File 'app/models/system/broadcast_message.rb', line 67

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  started? && !ended?
end

#ended?Boolean

Returns:

  • (Boolean)


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

def ended?
  ends_at.past?
end

#flush_redis_cacheObject



160
161
162
163
164
# File 'app/models/system/broadcast_message.rb', line 160

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)


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

def future?
  starts_at.future?
end

#matches_current_path(current_path) ⇒ Object



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

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)


138
139
140
141
142
# File 'app/models/system/broadcast_message.rb', line 138

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)


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

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

#now_or_future?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/system/broadcast_message.rb', line 134

def now_or_future?
  now? || future?
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  Time.current >= starts_at
end