Skip to content
Snippets Groups Projects
Verified Commit 72251fdf authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Add base notification sending code

parent 013027fe
No related branches found
No related tags found
No related merge requests found
# frozen_string_literal: true
module ForemanNotificationSend
module NotificationExtensions
def self.prepended(base)
......@@ -9,38 +8,60 @@ module ForemanNotificationSend
end
def send_notification
puts to_markdown
puts to_html
return unless Setting[:notification_send_enable]
sender = SenderBase.create_sender
sender.send_notification(self)
end
def level_to_symbol
case notification_blueprint.level
when 'success'
'✅'
when 'info'
'🛈'
when 'warning'
'⚠'
else
'❓'
end
end
def to_html
if actions
unless actions.empty?
action_str = ''
(actions[:links] || []).each do |action|
next unless action.key?(:href) && action.key?(:title)
url = action[:href]
url = Setting[:foreman_url] + url unless action[:external]
action_str += ' | ' unless action_str.empty?
action_str += "<a href=\"#{action[:href]}\">#{action[:title]}</a>"
action_str += "<a href=\"#{url}\">#{action[:title]}</a>"
end
action_str = "<br/>[#{action_str}]"
action_str = "<br/>[ #{action_str} ]"
end
"<b>#{notification_blueprint.group}</b>: #{message}#{action_str}"
"<b>#{notification_blueprint.group}</b>:<br/>#{level_to_symbol} #{message}#{action_str}"
end
def to_markdown
if actions
unless actions.empty?
action_str = ''
(actions[:links] || []).each do |action|
next unless action.key?(:href) && action.key?(:title)
url = action[:href]
url = Setting[:foreman_url] + url unless action[:external]
action_str += ' | ' unless action_str.empty?
action_str += "[#{action[:title]}](#{action[:href]})"
action_str += "[#{action[:title]}](#{url})"
end
action_str = " \n[#{action_str}]"
action_str = " \n\\[ #{action_str}\\ ]"
end
"**#{notification_blueprint.group}**: #{message}#{action_str}"
"**#{notification_blueprint.group}**:\n#{level_to_symbol} #{message}#{action_str}"
end
end
end
# frozen_string_literal: true
class Setting
class NotificationSend < ::Setting
def self.default_settings
[
set('notification_send_enable', _('Enable'), false, N_('Enable')),
set('notification_send_target_url', _('Target URI'), 'https://matrix.org', N_('Target URI')),
set('notification_send_target_room', _('Target Room'), '#test:matrix.org', N_('Target Room')),
set('notification_send_token', _('Token'), 'abcdefg', N_('Token'))
]
end
def self.load_defaults
# Check the table exists
return unless super
transaction do
default_settings.each do |s|
create! s.update(category: 'Setting::NotificationSend')
end
end
true
end
def self.humanized_category
N_('Notification Send')
end
end
end
# frozen_string_literal: true
module ForemanNotificationSend
class SenderBase
def self.create_sender
SenderMatrix.new
end
def send_notification(_notification)
raise NotImplementedException
end
end
end
# frozen_string_literal: true
require 'matrix_sdk/api'
module ForemanNotificationSend
class SenderMatrix < SenderBase
def send_notification(notification)
client.send_message_event(room, 'm.room.message',
msgtype: 'm.notice',
body: notification.to_markdown,
formatted_body: notification.to_html,
format: 'org.matrix.custom.html')
end
private
def client
MatrixSdk::Api.new Setting[:notification_send_target_url], access_token: Setting[:notification_send_token]
end
def room
@room ||= begin
room = Setting[:notification_send_target_room]
room = client.join_room(room).room_id if room.start_with? '#'
room
end
end
end
end
......@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
s.require_paths = ['lib']
s.add_dependency 'matrix_sdk', '~> 0.0.4'
s.add_development_dependency 'bundler', '~> 1.16'
s.add_development_dependency 'minitest', '~> 5.0'
s.add_development_dependency 'rake', '~> 10.0'
......
......@@ -5,6 +5,7 @@ module ForemanNotificationSend
engine_name 'foreman_notification_send'
config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
config.autoload_paths += Dir["#{config.root}/app/services"]
initializer 'foreman_ipxe.load_default_settings', before: :load_config_initializers do
require_dependency File.expand_path('../../app/models/setting/notification_send.rb', __dir__) if \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment