# frozen_string_literal: true
module ApplicationHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper
include InputSanitizeHelper
def module_page?
controller_name == 'my_modules' ||
controller_name == 'my_module_repositories'
end
def experiment_page?
controller_name == 'experiments'
end
def project_page?
controller_name == 'projects' &&
action_name.in?(%w(show experiment_archive))
end
def all_projects_page?
controller_name == 'projects' && action_name.in?(%w(index archive))
end
def display_tooltip(message, len = Constants::NAME_TRUNCATION_LENGTH)
return '' unless message
if message.strip.length > len
sanitize_input("
\
#{truncate(message.strip, length: len)} \
\
#{message.strip}
")
else
truncate(message.strip, length: len)
end
end
def module_repository_page?
controller_name == 'my_modules' && !@repository.nil?
end
def displayable_flash_type?(type)
%w(success warning alert error notice).include?(type)
end
def flash_alert_class(type)
case type
when 'success'
'alert-success'
when 'warning'
'alert-warning'
when 'error', 'alert'
'alert-danger'
else
'alert-info'
end
end
def flash_icon_class(type)
case type
when 'error', 'warning'
'fa-exclamation-triangle'
else
'fa-check-circle'
end
end
def smart_annotation_notification(options = {})
title = options.fetch(:title) { :title_must_be_present }
message = options.fetch(:message) { :message_must_be_present }
old_text = options[:old_text] || ''
new_text = options[:new_text]
subject = options[:subject]
return if new_text.blank?
sa_user = /\[\@(.*?)~([0-9a-zA-Z]+)\]/
# fetch user ids from the previous text
old_user_ids = []
old_text.gsub(sa_user) do |el|
match = el.match(sa_user)
old_user_ids << match[2].base62_decode
end
# fetch user ids from the new text
new_user_ids = []
new_text.gsub(sa_user) do |el|
match = el.match(sa_user)
new_user_ids << match[2].base62_decode
end
# check if the user has been already mentioned
annotated_users = []
new_user_ids.each do |el|
annotated_users << el unless old_user_ids.include?(el)
end
# restrict the list of ids and generate notification
annotated_users.uniq.each do |user_id|
target_user = User.find_by_id(user_id)
next unless target_user
generate_annotation_notification(target_user, title, subject)
end
end
def generate_annotation_notification(target_user, title, subject)
GeneralNotification.send_notifications(
{
type: :smart_annotation_added,
title: sanitize_input(title),
subject_id: subject.id,
subject_class: subject.class.name,
subject_name: subject.respond_to?(:name) && subject.name,
user: target_user
}
)
end
def custom_link_open_new_tab(text)
text.gsub(/\ e
Rails.logger.error e.message
'icon_small/missing.svg'
end
def sso_enabled?
ENV['SSO_ENABLED'] == 'true'
end
def sso_provider_enabled?
okta_enabled?.present? || azure_ad_enabled?.present? || saml_enabled?.present? || openid_connect_enabled?.present?
end
def okta_enabled?
ApplicationSettings.instance.values.dig('okta', 'enabled')
end
def azure_ad_enabled?
provider_conf = ApplicationSettings.instance.values['azure_ad_apps']
provider_conf.present? && provider_conf[0]['enabled']
end
def saml_enabled?
ApplicationSettings.instance.values.dig('saml', 'enabled')
end
def openid_connect_enabled?
ApplicationSettings.instance.values.dig('openid_connect', 'enabled')
end
def wopi_enabled?
ENV['WOPI_ENABLED'] == 'true'
end
# Check whether the wopi file can be edited and return appropriate response
def wopi_file_edit_button_status(asset)
file_ext = asset.file_name.split('.').last&.downcase
if Constants::WOPI_EDITABLE_FORMATS.include?(file_ext)
edit_supported = true
title = ''
else
edit_supported = false
title = if Constants::FILE_TEXT_FORMATS.include?(file_ext)
I18n.t('assets.wopi_supported_text_formats_title')
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
I18n.t('assets.wopi_supported_table_formats_title')
else
I18n.t('assets.wopi_supported_presentation_formats_title')
end
end
return edit_supported, title
end
def create_2fa_qr_code(user)
user.assign_2fa_token!
qr_code_url = ROTP::TOTP.new(user.otp_secret, issuer: 'SciNote').provisioning_uri(user.email)
RQRCode::QRCode.new(qr_code_url).as_svg(module_size: 4)
end
def login_disclaimer
# login_disclaimer: { title: "...", body: "...", action: "..." }
ApplicationSettings.instance.values['login_disclaimer']
end
def show_grey_background?
return false unless controller_name && action_name
Extends::COLORED_BACKGROUND_ACTIONS.include?("#{controller_name}/#{action_name}")
end
end