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

Add preliminary work for many notification targets

parent 68346ec2
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,18 @@ module ForemanNotificationSend ...@@ -8,10 +8,18 @@ module ForemanNotificationSend
end end
def send_notification 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 NotificationTarget.select { |target| target.should_send?(self) }
sender.send_notification(self) .each { |target| target.send(self) }
end end
def level_to_symbol def level_to_symbol
......
# 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
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
module ForemanNotificationSend module ForemanNotificationSend
class SenderBase class SenderBase
def self.create_sender def self.create_sender(backend:, **args)
SenderMatrix.new return SenderMatrix.new(args) if backend == :matrix
end
def send_notification(_notification) raise NotImplementedError, 'Only Matrix backend implemented at the moment'
raise NotImplementedException
end end
def send_notification(_notification); end
end end
end end
...@@ -3,9 +3,18 @@ require 'matrix_sdk/api' ...@@ -3,9 +3,18 @@ require 'matrix_sdk/api'
module ForemanNotificationSend module ForemanNotificationSend
class SenderMatrix < SenderBase 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) def send_notification(notification)
client.send_message_event(room, 'm.room.message', client.send_message_event(room, 'm.room.message',
msgtype: 'm.notice', msgtype: @msgtype,
body: notification.to_markdown, body: notification.to_markdown,
formatted_body: notification.to_html, formatted_body: notification.to_html,
format: 'org.matrix.custom.html') format: 'org.matrix.custom.html')
...@@ -14,13 +23,15 @@ module ForemanNotificationSend ...@@ -14,13 +23,15 @@ module ForemanNotificationSend
private private
def client 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 end
def room def room
@room ||= begin @room ||= begin
room = Setting[:notification_send_target_room] room = @room
room = client.join_room(room).room_id if room.start_with? '#' room = MatrixSdk::MXID.new(room) unless room.is_a? MatrixSdk::MXID
room = client.join_room(room).room_id if room.room_alias?
room room
end end
end end
......
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
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