diff --git a/app/models/concerns/foreman_notification_send/notification_extensions.rb b/app/models/concerns/foreman_notification_send/notification_extensions.rb index 9135fcd7fc7868e19b00ec720059a5377f884c42..0faa833b67345a48c6005acae2a7aa7669f8c7fe 100644 --- a/app/models/concerns/foreman_notification_send/notification_extensions.rb +++ b/app/models/concerns/foreman_notification_send/notification_extensions.rb @@ -8,10 +8,18 @@ module ForemanNotificationSend end def send_notification - return unless Setting[:notification_send_enable] + if Setting[:notification_send_enable] + sender = SenderBase.create_sender( + backend: :matrix, + hs_url: Setting[:notification_send_target_url], + access_token: Setting[:notification_send_token], + room: Setting[:notification_send_target_room] + ) + sender.send_notification(self) + end - sender = SenderBase.create_sender - sender.send_notification(self) + NotificationTarget.select { |target| target.should_send?(self) } + .each { |target| target.send(self) } end def level_to_symbol diff --git a/app/models/foreman_notification_send/notification_target.rb b/app/models/foreman_notification_send/notification_target.rb new file mode 100644 index 0000000000000000000000000000000000000000..d50ffcae35349085a2d7f190c7dd8fed57cd6629 --- /dev/null +++ b/app/models/foreman_notification_send/notification_target.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module ForemanNotificationSend + class NotificationTarget < ApplicationRecord + before_validation do + backend = slugify_backend if backend + end + validate :validate_backend + + def should_send?(notification) + # TODO: Filter + end + + def send(notification) + sender = SenderBase.create_sender(configuration.deep_symbolize_keys.merge(backend: slugify_backend)) + sender.send_notification(notification) + end + + def slugify_backend + backend.to_s.downcase.to_sym + end + + def validate_backend + errors.add(:backend, 'is not a valid backend') if backend != :matrix + end + end +end diff --git a/app/services/foreman_notification_send/sender_base.rb b/app/services/foreman_notification_send/sender_base.rb index 7bce11c5ad6f8033700ecd8863a0e5f61e8ac1fd..fba35c5556d0cff1497b97e2ac30e78add9179a5 100644 --- a/app/services/foreman_notification_send/sender_base.rb +++ b/app/services/foreman_notification_send/sender_base.rb @@ -2,12 +2,12 @@ module ForemanNotificationSend class SenderBase - def self.create_sender - SenderMatrix.new - end + def self.create_sender(backend:, **args) + return SenderMatrix.new(args) if backend == :matrix - def send_notification(_notification) - raise NotImplementedException + raise NotImplementedError, 'Only Matrix backend implemented at the moment' end + + def send_notification(_notification); end end end diff --git a/app/services/foreman_notification_send/sender_matrix.rb b/app/services/foreman_notification_send/sender_matrix.rb index 423dcf5a286d52c2725ab3028deb980c1e07e688..453f1f5caebc79d6fbe79e92e71c25a35b9bff58 100644 --- a/app/services/foreman_notification_send/sender_matrix.rb +++ b/app/services/foreman_notification_send/sender_matrix.rb @@ -3,9 +3,18 @@ require 'matrix_sdk/api' module ForemanNotificationSend class SenderMatrix < SenderBase + def initialize(hs_url:, access_token:, room:, msgtype: 'm.notice') + raise ArgumentError, 'access_token must be a Matrix room ID/Alias' unless access_token.is_a?(MXID) && access_token.room? + + @hs_url = hs_url + @access_token = access_token + @room = room + @msgtype = msgtype + end + def send_notification(notification) client.send_message_event(room, 'm.room.message', - msgtype: 'm.notice', + msgtype: @msgtype, body: notification.to_markdown, formatted_body: notification.to_html, format: 'org.matrix.custom.html') @@ -14,13 +23,15 @@ module ForemanNotificationSend private def client - MatrixSdk::Api.new Setting[:notification_send_target_url], access_token: Setting[:notification_send_token] + #MatrixSdk::Api.new Setting[:notification_send_target_url], access_token: Setting[:notification_send_token] + MatrixSdk::Api.new @hs_url, access_token: @access_token end def room @room ||= begin - room = Setting[:notification_send_target_room] - room = client.join_room(room).room_id if room.start_with? '#' + room = @room + room = MatrixSdk::MXID.new(room) unless room.is_a? MatrixSdk::MXID + room = client.join_room(room).room_id if room.room_alias? room end end diff --git a/db/migrate/20200414141644_add_notification_targets.rb b/db/migrate/20200414141644_add_notification_targets.rb new file mode 100644 index 0000000000000000000000000000000000000000..268bf015b70ee1c81820076142038f9c6ecb7dbf --- /dev/null +++ b/db/migrate/20200414141644_add_notification_targets.rb @@ -0,0 +1,14 @@ +class AddNotificationTargets < ActiveRecord::Migration[5.1] + def change + create_table :notification_targets do |t| + t.string :name, null: false, unique: true + + t.string :backend, null: false + t.json :filter, null: false + t.json :configuration, null: false + + t.timestamps null: false + end + end +end + diff --git a/foreman_notification_send.gemspec b/foreman_notification_send.gemspec index 8f71279b1f1fa241be48164c83ed48abcb8b2943..579f92e40259f76a29d5e53508d5d31a01b39ced 100644 --- a/foreman_notification_send.gemspec +++ b/foreman_notification_send.gemspec @@ -17,7 +17,7 @@ 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_dependency 'matrix_sdk', '~> 1' s.add_development_dependency 'bundler', '~> 1.16' s.add_development_dependency 'minitest', '~> 5.0'