diff --git a/app/models/concerns/foreman_notification_send/notification_extensions.rb b/app/models/concerns/foreman_notification_send/notification_extensions.rb new file mode 100644 index 0000000000000000000000000000000000000000..a2afcdc579c92db37c5ed1a5530d8f9c941cb832 --- /dev/null +++ b/app/models/concerns/foreman_notification_send/notification_extensions.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true +module ForemanNotificationSend + module NotificationExtensions + def self.prepended(base) + base.class_eval do + after_save :send_notification + end + end + + def send_notification + 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 + 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=\"#{url}\">#{action[:title]}</a>" + end + action_str = "<br/>[ #{action_str} ]" + end + + "<b>#{notification_blueprint.group}</b>:<br/>#{level_to_symbol} #{message}#{action_str}" + end + + def to_markdown + 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]}](#{url})" + end + action_str = " \n\\[ #{action_str}\\ ]" + end + "**#{notification_blueprint.group}**:\n#{level_to_symbol} #{message}#{action_str}" + end + end +end diff --git a/app/models/concerns/notification_extensions.rb b/app/models/concerns/notification_extensions.rb deleted file mode 100644 index b0071ba76d9e5f8881b0d1a58ba5d80602cd22ec..0000000000000000000000000000000000000000 --- a/app/models/concerns/notification_extensions.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -module ForemanNotificationSend - module NotificationExtensions - def self.prepended(base) - base.class_eval do - after_save :send_notification - end - end - - def send_notification - puts to_markdown - puts to_html - end - - def to_html - if actions - action_str = '' - - (actions[:links] || []).each do |action| - next unless action.key?(:href) && action.key?(:title) - - action_str += ' | ' unless action_str.empty? - action_str += "<a href=\"#{action[:href]}\">#{action[:title]}</a>" - end - action_str = "<br/>[#{action_str}]" - end - "<b>#{notification_blueprint.group}</b>: #{message}#{action_str}" - end - - def to_markdown - if actions - action_str = '' - - (actions[:links] || []).each do |action| - next unless action.key?(:href) && action.key?(:title) - - action_str += ' | ' unless action_str.empty? - action_str += "[#{action[:title]}](#{action[:href]})" - end - action_str = " \n[#{action_str}]" - end - "**#{notification_blueprint.group}**: #{message}#{action_str}" - end - end -end diff --git a/app/models/setting/notification_send.rb b/app/models/setting/notification_send.rb index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..110dcc9191757c02fddb206c1f9a2ce6896c55fd 100644 --- a/app/models/setting/notification_send.rb +++ b/app/models/setting/notification_send.rb @@ -0,0 +1,31 @@ +# 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 diff --git a/app/services/foreman_notification_send/sender_base.rb b/app/services/foreman_notification_send/sender_base.rb new file mode 100644 index 0000000000000000000000000000000000000000..7bce11c5ad6f8033700ecd8863a0e5f61e8ac1fd --- /dev/null +++ b/app/services/foreman_notification_send/sender_base.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ForemanNotificationSend + class SenderBase + def self.create_sender + SenderMatrix.new + end + + def send_notification(_notification) + raise NotImplementedException + end + end +end diff --git a/app/services/foreman_notification_send/sender_matrix.rb b/app/services/foreman_notification_send/sender_matrix.rb new file mode 100644 index 0000000000000000000000000000000000000000..423dcf5a286d52c2725ab3028deb980c1e07e688 --- /dev/null +++ b/app/services/foreman_notification_send/sender_matrix.rb @@ -0,0 +1,28 @@ +# 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 diff --git a/foreman_notification_send.gemspec b/foreman_notification_send.gemspec index b75321315f7b1f4f737095fcb884eb8d65af3c64..8f71279b1f1fa241be48164c83ed48abcb8b2943 100644 --- a/foreman_notification_send.gemspec +++ b/foreman_notification_send.gemspec @@ -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' diff --git a/lib/foreman_notification_send/engine.rb b/lib/foreman_notification_send/engine.rb index 92e4cbefd507cea6ddc1c16726acc7ce3b4cf731..3425a6ef95e6ef89b27844c739a8ccaa566fb88d 100644 --- a/lib/foreman_notification_send/engine.rb +++ b/lib/foreman_notification_send/engine.rb @@ -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 \