feat(ntfy): add NtfyDispatchable concern for fanning out to admins

This commit is contained in:
Min Zeya Phyo
2026-07-28 02:13:51 +08:00
parent 88e9ff5ff7
commit e75c5b3bdc
2 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
module NtfyDispatchable
extend ActiveSupport::Concern
class_methods do
def dispatch_ntfy(event_type, title:, message:, priority: 3, tags: [], click: nil)
AdminUser.where(ntfy_enabled: true).find_each do |admin|
next unless admin.ntfy_configured?
SendNtfyNotificationJob.perform_later(
admin.id,
event_type,
title: title,
message: message,
priority: priority,
tags: tags,
click: click
)
end
end
end
end

View File

@@ -0,0 +1,49 @@
require "test_helper"
class NtfyDispatchableTest < ActiveJob::TestCase
setup do
ActiveJob::Base.queue_adapter = :test
@admin = AdminUser.create!(
name: "Dispatch Admin", email: "dispatch@example.com",
password: "password123",
ntfy_topic: "dispatch-topic", ntfy_token: "tk_dispatch",
ntfy_enabled: true,
ntfy_server_url: "https://ntfy.example.com"
)
stub_request(:post, "https://ntfy.example.com/dispatch-topic")
.to_return(status: 200, body: "", headers: {})
end
test "dispatch_ntfy enqueues a SendNtfyNotificationJob per configured admin" do
assert_enqueued_jobs 1 do
TestModel.dispatch_ntfy("sms_failed",
title: "SMS failed",
message: "msg_abc failed",
priority: 5,
tags: ["x"],
click: "https://app/admin/logs"
)
end
end
test "dispatch_ntfy skips admins without ntfy configured" do
AdminUser.create!(name: "No Ntfy", email: "none@example.com", password: "password123")
assert_enqueued_jobs 1 do
TestModel.dispatch_ntfy("sms_failed", title: "T", message: "M", priority: 3, tags: [])
end
end
test "dispatch_ntfy enqueues zero jobs if no admins configured" do
@admin.update!(ntfy_enabled: false)
assert_enqueued_jobs 0 do
TestModel.dispatch_ntfy("sms_failed", title: "T", message: "M", priority: 3, tags: [])
end
end
end
class TestModel
include NtfyDispatchable
end