From fd449fdbb11e9be087fc21809825041849da4a45 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson <ace@haxalot.com> Date: Tue, 14 Apr 2020 17:36:17 +0200 Subject: [PATCH] Add preliminary work for many notification targets --- .../notification_extensions.rb | 14 +++++++--- .../notification_target.rb | 27 +++++++++++++++++++ .../foreman_notification_send/sender_base.rb | 10 +++---- .../sender_matrix.rb | 19 ++++++++++--- ...20200414141644_add_notification_targets.rb | 14 ++++++++++ 5 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 app/models/foreman_notification_send/notification_target.rb create mode 100644 db/migrate/20200414141644_add_notification_targets.rb diff --git a/app/models/concerns/foreman_notification_send/notification_extensions.rb b/app/models/concerns/foreman_notification_send/notification_extensions.rb index b9faa1f..c1cdbe7 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 0000000..d50ffca --- /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 7bce11c..fba35c5 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 423dcf5..453f1f5 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 0000000..268bf01 --- /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 + -- GitLab