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
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
......
# 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 @@
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
......@@ -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
......
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