diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 74f880a..9db08d9 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -12,22 +12,28 @@ class WebhooksController < ApplicationController end user = User.find_by!(ln_account: payload[:user_login]) - - # TODO make configurable - notify_xmpp(user.address, payload[:amount], payload[:memo]) + notify = user.preferences.dig("lightning", "notify_sats_received") + case notify + 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 end private + # TODO refactor into mailer-like generic class/service def notify_xmpp(address, amt_sats, memo) payload = { type: "normal", from: "kosmos.org", # TODO domain config to: address, 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) end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb new file mode 100644 index 0000000..84f7dd5 --- /dev/null +++ b/app/mailers/notification_mailer.rb @@ -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 diff --git a/app/views/notification_mailer/lightning_sats_received.text.erb b/app/views/notification_mailer/lightning_sats_received.text.erb new file mode 100644 index 0000000..1e122d4 --- /dev/null +++ b/app/views/notification_mailer/lightning_sats_received.text.erb @@ -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 %> diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 06c834e..d38cf7c 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -55,22 +55,51 @@ RSpec.describe "Webhooks", type: :request do before do user.save! #FIXME this should not be necessary - post "/webhooks/lndhub", params: payload.to_json end it "returns a 200 status" do + post "/webhooks/lndhub", params: payload.to_json expect(response).to have_http_status(:ok) end - it "sends an XMPP message to the account owner's JID" do - expect(enqueued_jobs.size).to eq(1) + it "does not send notifications by default" do + expect(enqueued_jobs.size).to eq(0) + end - 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(/^12300 sats received/) + context "notification preference set to 'xmpp'" do + before do + user.update! preferences: { "lightning" => { "notify_sats_received" => "xmpp" }} + post "/webhooks/lndhub", params: payload.to_json + end + + 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