2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module BroadcastMessagesHelper
|
2022-05-07 20:08:51 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def current_broadcast_banner_messages
|
2022-05-07 20:08:51 +05:30
|
|
|
BroadcastMessage.current_banner_messages(
|
|
|
|
current_path: request.path,
|
|
|
|
user_access_level: current_user_access_level_for_project_or_group
|
|
|
|
).select do |message|
|
2020-04-08 14:13:33 +05:30
|
|
|
cookies["hide_broadcast_message_#{message.id}"].blank?
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def current_broadcast_notification_message
|
2022-05-07 20:08:51 +05:30
|
|
|
not_hidden_messages = BroadcastMessage.current_notification_messages(
|
|
|
|
current_path: request.path,
|
|
|
|
user_access_level: current_user_access_level_for_project_or_group
|
|
|
|
).select do |message|
|
2020-04-08 14:13:33 +05:30
|
|
|
cookies["hide_broadcast_message_#{message.id}"].blank?
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
not_hidden_messages.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_message(message, opts = {})
|
2016-01-19 16:12:03 +05:30
|
|
|
return unless message.present?
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
render "shared/broadcast_message", { message: message, opts: opts }
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_message_style(broadcast_message)
|
2020-03-13 15:44:24 +05:30
|
|
|
return '' if broadcast_message.notification?
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
style = []
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
if broadcast_message.color.present?
|
2016-01-19 16:12:03 +05:30
|
|
|
style << "background-color: #{broadcast_message.color}"
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
if broadcast_message.font.present?
|
2016-01-19 16:12:03 +05:30
|
|
|
style << "color: #{broadcast_message.font}"
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
style.join('; ')
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_message_status(broadcast_message)
|
|
|
|
if broadcast_message.active?
|
|
|
|
'Active'
|
|
|
|
elsif broadcast_message.ended?
|
|
|
|
'Expired'
|
|
|
|
else
|
|
|
|
'Pending'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def render_broadcast_message(broadcast_message)
|
2020-04-22 19:07:51 +05:30
|
|
|
if broadcast_message.notification?
|
|
|
|
Banzai.render_field_and_post_process(broadcast_message, :message, {
|
2020-04-08 14:13:33 +05:30
|
|
|
current_user: current_user,
|
|
|
|
skip_project_check: true,
|
|
|
|
broadcast_message_placeholders: true
|
|
|
|
}).html_safe
|
|
|
|
else
|
|
|
|
Banzai.render_field(broadcast_message, :message).html_safe
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def broadcast_type_options
|
|
|
|
BroadcastMessage.broadcast_types.keys.map { |w| [w.humanize, w] }
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
def target_access_level_options
|
|
|
|
BroadcastMessage::ALLOWED_TARGET_ACCESS_LEVELS.map do |access_level|
|
|
|
|
[Gitlab::Access.human_access(access_level), access_level]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_access_levels_display(access_levels)
|
|
|
|
access_levels.map do |access_level|
|
|
|
|
Gitlab::Access.human_access(access_level)
|
|
|
|
end.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_user_access_level_for_project_or_group
|
|
|
|
return if Feature.disabled?(:role_targeted_broadcast_messages, default_enabled: :yaml)
|
|
|
|
return unless current_user.present?
|
|
|
|
|
|
|
|
strong_memoize(:current_user_access_level_for_project_or_group) do
|
|
|
|
if controller.is_a? Projects::ApplicationController
|
|
|
|
next unless @project
|
|
|
|
|
|
|
|
@project.team.max_member_access(current_user.id)
|
|
|
|
elsif controller.is_a? Groups::ApplicationController
|
|
|
|
next unless @group
|
|
|
|
|
|
|
|
@group.max_member_access_for_user(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|