Class: BroadcastMessage

Inherits:
ApplicationRecord show all
Includes:
CacheMarkdownField, Sortable
Defined in:
app/models/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

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?, #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


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

def cache
  ::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
    Gitlab::JsonCache.new
  end
end

.cache_expires_inObject


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

def cache_expires_in
  2.weeks
end

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


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

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_and_future_messagesObject


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

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

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


54
55
56
57
58
# File 'app/models/broadcast_message.rb', line 54

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


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

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

109
110
111
# File 'app/models/broadcast_message.rb', line 109

def active?
  started? && !ended?
end

#ended?Boolean

Returns:

  • (Boolean)

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

def ended?
  ends_at < Time.current
end

#flush_redis_cacheObject


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

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)

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

def future?
  starts_at > Time.current
end

#matches_current_path(current_path) ⇒ Object


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

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

  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)

133
134
135
136
137
138
# File 'app/models/broadcast_message.rb', line 133

def matches_current_user_access_level?(user_access_level)
  return false if target_access_levels.present? && Feature.disabled?(:role_targeted_broadcast_messages)
  return true unless target_access_levels.present?

  target_access_levels.include? user_access_level
end

#now?Boolean

Returns:

  • (Boolean)

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

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

#now_or_future?Boolean

Returns:

  • (Boolean)

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

def now_or_future?
  now? || future?
end

#started?Boolean

Returns:

  • (Boolean)

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

def started?
  Time.current >= starts_at
end