Add notification mailer, make wallet notifications configurable
This commit is contained in:
parent
43a43e1a2c
commit
ca7475dca2
@ -12,22 +12,28 @@ class WebhooksController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
user = User.find_by!(ln_account: payload[:user_login])
|
user = User.find_by!(ln_account: payload[:user_login])
|
||||||
|
notify = user.preferences.dig("lightning", "notify_sats_received")
|
||||||
# TODO make configurable
|
case notify
|
||||||
notify_xmpp(user.address, payload[:amount], payload[:memo])
|
when "xmpp"
|
||||||
|
notify_xmpp(user.address, payload[:amount], payload[:memo])
|
||||||
|
when "email"
|
||||||
|
NotificationMailer.with(user: user, amount_sats: payload[:amount])
|
||||||
|
.lightning_sats_received.deliver_later
|
||||||
|
end
|
||||||
|
|
||||||
head :ok
|
head :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# TODO refactor into mailer-like generic class/service
|
||||||
def notify_xmpp(address, amt_sats, memo)
|
def notify_xmpp(address, amt_sats, memo)
|
||||||
payload = {
|
payload = {
|
||||||
type: "normal",
|
type: "normal",
|
||||||
from: "kosmos.org", # TODO domain config
|
from: "kosmos.org", # TODO domain config
|
||||||
to: address,
|
to: address,
|
||||||
subject: "Sats received!",
|
subject: "Sats received!",
|
||||||
body: "#{amt_sats} sats received in your Lightning wallet:\n> #{memo}"
|
body: "#{helpers.number_with_delimiter amt_sats} sats received in your Lightning wallet:\n> #{memo}"
|
||||||
}
|
}
|
||||||
XmppSendMessageJob.perform_later(payload)
|
XmppSendMessageJob.perform_later(payload)
|
||||||
end
|
end
|
||||||
|
8
app/mailers/notification_mailer.rb
Normal file
8
app/mailers/notification_mailer.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
class NotificationMailer < ApplicationMailer
|
||||||
|
def lightning_sats_received
|
||||||
|
@user = params[:user]
|
||||||
|
@amount_sats = params[:amount_sats]
|
||||||
|
@subject = "Sats received"
|
||||||
|
mail to: @user.email, subject: @subject
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
You just received <%= number_with_delimiter @amount_sats %> sats in your Lightning account (<%= @user.address %>). Check your wallet app, or open the account page for details:
|
||||||
|
|
||||||
|
<%= wallet_transactions_url %>
|
@ -55,22 +55,51 @@ RSpec.describe "Webhooks", type: :request do
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
user.save! #FIXME this should not be necessary
|
user.save! #FIXME this should not be necessary
|
||||||
post "/webhooks/lndhub", params: payload.to_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a 200 status" do
|
it "returns a 200 status" do
|
||||||
|
post "/webhooks/lndhub", params: payload.to_json
|
||||||
expect(response).to have_http_status(:ok)
|
expect(response).to have_http_status(:ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sends an XMPP message to the account owner's JID" do
|
it "does not send notifications by default" do
|
||||||
expect(enqueued_jobs.size).to eq(1)
|
expect(enqueued_jobs.size).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
msg = enqueued_jobs.first['arguments'].first
|
context "notification preference set to 'xmpp'" do
|
||||||
expect(msg["type"]).to eq('normal')
|
before do
|
||||||
expect(msg["from"]).to eq('kosmos.org')
|
user.update! preferences: { "lightning" => { "notify_sats_received" => "xmpp" }}
|
||||||
expect(msg["to"]).to eq(user.address)
|
post "/webhooks/lndhub", params: payload.to_json
|
||||||
expect(msg["subject"]).to eq('Sats received!')
|
end
|
||||||
expect(msg["body"]).to match(/^12300 sats received/)
|
|
||||||
|
it "sends an XMPP message to the account owner's JID" do
|
||||||
|
expect(enqueued_jobs.size).to eq(1)
|
||||||
|
expect(enqueued_jobs.first["job_class"]).to eq("XmppSendMessageJob")
|
||||||
|
|
||||||
|
msg = enqueued_jobs.first["arguments"].first
|
||||||
|
expect(msg["type"]).to eq("normal")
|
||||||
|
expect(msg["from"]).to eq("kosmos.org")
|
||||||
|
expect(msg["to"]).to eq(user.address)
|
||||||
|
expect(msg["subject"]).to eq("Sats received!")
|
||||||
|
expect(msg["body"]).to match(/^12,300 sats received/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "notification preference set to 'email'" do
|
||||||
|
before do
|
||||||
|
user.update! preferences: { "lightning" => { "notify_sats_received" => "email" }}
|
||||||
|
post "/webhooks/lndhub", params: payload.to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sends an email notification to the account owner" do
|
||||||
|
expect(enqueued_jobs.size).to eq(1)
|
||||||
|
expect(enqueued_jobs.first["job_class"]).to eq("ActionMailer::MailDeliveryJob")
|
||||||
|
args = enqueued_jobs.first['arguments']
|
||||||
|
expect(args[0]).to eq("NotificationMailer")
|
||||||
|
expect(args[1]).to eq("lightning_sats_received")
|
||||||
|
expect(args[3]["params"]["user"]["_aj_globalid"]).to eq("gid://akkounts/User/1")
|
||||||
|
expect(args[3]["params"]["amount_sats"]).to eq(12300)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user