debian-mirror-gitlab/app/helpers/broadcast_messages_helper.rb

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

121 lines
3.7 KiB
Ruby
Raw Normal View History

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 = {})
return unless message.present?
2022-06-21 17:19:12 +05:30
render "shared/broadcast_message", { message: message, **opts }
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
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
2023-04-23 21:23:45 +05:30
def admin_broadcast_messages_data(broadcast_messages)
broadcast_messages.map do |message|
{
id: message.id,
status: broadcast_message_status(message),
2023-07-09 08:55:56 +05:30
message: message.message,
theme: message.theme,
broadcast_type: message.broadcast_type,
dismissable: message.dismissable,
2023-04-23 21:23:45 +05:30
starts_at: message.starts_at.iso8601,
ends_at: message.ends_at.iso8601,
target_roles: target_access_levels_display(message.target_access_levels),
target_path: message.target_path,
type: message.broadcast_type.capitalize,
edit_path: edit_admin_broadcast_message_path(message),
delete_path: "#{admin_broadcast_message_path(message)}.js"
}
end.to_json
end
2023-05-27 22:25:52 +05:30
def broadcast_message_data(broadcast_message)
{
id: broadcast_message.id,
message: broadcast_message.message,
broadcast_type: broadcast_message.broadcast_type,
theme: broadcast_message.theme,
dismissable: broadcast_message.dismissable.to_s,
target_access_levels: broadcast_message.target_access_levels,
messages_path: admin_broadcast_messages_path,
preview_path: preview_admin_broadcast_messages_path,
target_path: broadcast_message.target_path,
starts_at: broadcast_message.starts_at.iso8601,
ends_at: broadcast_message.ends_at.iso8601,
target_access_level_options: target_access_level_options.to_json
}
end
2022-05-07 20:08:51 +05:30
private
def current_user_access_level_for_project_or_group
return unless current_user.present?
strong_memoize(:current_user_access_level_for_project_or_group) do
2023-01-13 00:05:48 +05:30
case controller
when Projects::ApplicationController
2022-05-07 20:08:51 +05:30
next unless @project
@project.team.max_member_access(current_user.id)
2023-01-13 00:05:48 +05:30
when Groups::ApplicationController
2022-05-07 20:08:51 +05:30
next unless @group
@group.max_member_access_for_user(current_user)
end
end
end
2014-09-02 18:07:02 +05:30
end