diff --git a/app/components/form_elements/fieldset_toggle_component.html.erb b/app/components/form_elements/fieldset_toggle_component.html.erb
index 9965490..ae56907 100644
--- a/app/components/form_elements/fieldset_toggle_component.html.erb
+++ b/app/components/form_elements/fieldset_toggle_component.html.erb
@@ -6,7 +6,7 @@
<% if @description.present? %>
-
<%= @descripton %>
+
<%= @description %>
<% end %>
diff --git a/app/components/form_elements/fieldset_toggle_component.rb b/app/components/form_elements/fieldset_toggle_component.rb
index 09fd36d..9be4a8a 100644
--- a/app/components/form_elements/fieldset_toggle_component.rb
+++ b/app/components/form_elements/fieldset_toggle_component.rb
@@ -12,7 +12,7 @@ module FormElements
@enabled = enabled
@input_enabled = input_enabled
@title = title
- @descripton = description
+ @description = description
@button_text = @enabled ? "Switch off" : "Switch on"
end
end
diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb
index cb7de77..a21c542 100644
--- a/app/controllers/settings_controller.rb
+++ b/app/controllers/settings_controller.rb
@@ -12,7 +12,11 @@ class SettingsController < ApplicationController
end
def show
- if @settings_section == "nostr"
+ case @settings_section
+ when "lightning"
+ @notifications_enabled = @user.preferences[:lightning_notify_sats_received] != "disabled" ||
+ @user.preferences[:lightning_notify_zap_received] != "disabled"
+ when "nostr"
session[:shared_secret] ||= SecureRandom.base64(12)
end
end
@@ -147,11 +151,9 @@ class SettingsController < ApplicationController
end
def user_params
- params.require(:user).permit(:display_name, :avatar, preferences: [
- :lightning_notify_sats_received,
- :remotestorage_notify_auth_created,
- :xmpp_exchange_contacts_with_invitees
- ])
+ params.require(:user).permit(
+ :display_name, :avatar, preferences: UserPreferences.pref_keys
+ )
end
def email_params
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb
index eb1c383..bc7d15f 100644
--- a/app/controllers/webhooks_controller.rb
+++ b/app/controllers/webhooks_controller.rb
@@ -7,14 +7,14 @@ class WebhooksController < ApplicationController
def lndhub
@user = User.find_by!(ln_account: @payload[:user_login])
- if zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
+ if @zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
zap_receipt = NostrManager::CreateZapReceipt.call(
- zap: zap,
+ zap: @zap,
paid_at: Time.parse(@payload[:settled_at]).to_i,
preimage: @payload[:preimage]
)
- zap.update! receipt: zap_receipt.to_h
- NostrManager::PublishZapReceipt.call(zap: zap)
+ @zap.update! receipt: @zap_receipt.to_h
+ NostrManager::PublishZapReceipt.call(zap: @zap)
end
send_notifications
@@ -41,7 +41,16 @@ class WebhooksController < ApplicationController
end
def send_notifications
- case @user.preferences[:lightning_notify_sats_received]
+ return if @payload[:amount] < @user.preferences[:lightning_notify_min_sats]
+
+ if @user.preferences[:lightning_notify_only_with_message]
+ return if @payload[:memo].blank?
+ end
+
+ target = @zap.present? ? @user.preferences[:lightning_notify_zap_received] :
+ @user.preferences[:lightning_notify_sats_received]
+
+ case target
when "xmpp"
notify_xmpp
when "email"
diff --git a/app/models/user_preferences.rb b/app/models/user_preferences.rb
index ffdf7f6..0eba822 100644
--- a/app/models/user_preferences.rb
+++ b/app/models/user_preferences.rb
@@ -26,4 +26,8 @@ class UserPreferences
end
hash.stringify_keys!.to_h
end
+
+ def self.pref_keys
+ DEFAULT_PREFS.keys.map(&:to_sym)
+ end
end
diff --git a/app/views/settings/_lightning.html.erb b/app/views/settings/_lightning.html.erb
index 6e75343..42623d5 100644
--- a/app/views/settings/_lightning.html.erb
+++ b/app/views/settings/_lightning.html.erb
@@ -5,7 +5,7 @@
<%= render FormElements::FieldsetComponent.new(
positioning: :horizontal,
title: "Sats received",
- description: "Notify me when sats are sent to my Lightning Address"
+ description: "Notify me when sats are sent to my Lightning account"
) do %>
<% f.fields_for :preferences do |p| %>
<%= p.select :lightning_notify_sats_received, options_for_select([
@@ -15,6 +15,38 @@
], selected: @user.preferences[:lightning_notify_sats_received]) %>
<% end %>
<% end %>
+ <% if @user.nostr_pubkey.present? %>
+ <%= render FormElements::FieldsetComponent.new(
+ positioning: :horizontal,
+ title: "Zap received",
+ description: "Notify me when someone zaps me on Nostr"
+ ) do %>
+ <% f.fields_for :preferences do |p| %>
+ <%= p.select :lightning_notify_zap_received, options_for_select([
+ ["off", "disabled"],
+ ["Chat (Jabber)", "xmpp"],
+ ["E-Mail", "email"]
+ ], selected: @user.preferences[:lightning_notify_zap_received]) %>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% if @notifications_enabled %>
+ <%= render FormElements::FieldsetToggleComponent.new(
+ field_name: "user[preferences][lightning_notify_only_with_message]",
+ enabled: @user.preferences[:lightning_notify_only_with_message],
+ title: "Ignore transactions without message",
+ description: "Only send notifications when there is a message attached to the payment"
+ ) %>
+ <%= render FormElements::FieldsetComponent.new(
+ title: "Minimum amount",
+ description: "Only send notifications when amount is higher than this"
+ ) do %>
+ <%= f.number_field :lightning_notify_min_sats,
+ name: "user[preferences][lightning_notify_min_sats]",
+ class: "w-full",
+ value: @user.preferences[:lightning_notify_min_sats].to_i %>
+ <% end %>
+ <% end %>
diff --git a/config/default_preferences.yml b/config/default_preferences.yml
index c65d658..8e31491 100644
--- a/config/default_preferences.yml
+++ b/config/default_preferences.yml
@@ -1,3 +1,6 @@
-lightning_notify_sats_received: disabled # or xmpp, email
-remotestorage_notify_auth_created: email # or xmpp, email
+lightning_notify_sats_received: email
+lightning_notify_zap_received: disabled
+lightning_notify_min_sats: 0
+lightning_notify_only_with_message: false
+remotestorage_notify_auth_created: email
xmpp_exchange_contacts_with_invitees: true
diff --git a/spec/models/user_preferences_spec.rb b/spec/models/user_preferences_spec.rb
index 4bb7b42..22e3e29 100644
--- a/spec/models/user_preferences_spec.rb
+++ b/spec/models/user_preferences_spec.rb
@@ -38,4 +38,15 @@ RSpec.describe UserPreferences, type: :model do
expect(res['lightning_notify_sats_received_threshold']).to eq(1000)
end
end
+
+ describe ".pref_keys" do
+ let(:default_prefs) { YAML.load_file("#{Rails.root}/config/default_preferences.yml") }
+
+ it "returns the keys of all default preferences as an array of symbols" do
+ expect(UserPreferences.pref_keys).to be_a(Array)
+ expect(UserPreferences.pref_keys).to include(:lightning_notify_sats_received)
+ expect(UserPreferences.pref_keys).to include(:xmpp_exchange_contacts_with_invitees)
+ expect(UserPreferences.pref_keys.length).to eq(default_prefs.keys.length)
+ end
+ end
end
diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb
index 65e0f5b..2851bb2 100644
--- a/spec/requests/webhooks_spec.rb
+++ b/spec/requests/webhooks_spec.rb
@@ -60,11 +60,6 @@ RSpec.describe "Webhooks", type: :request do
expect(response).to have_http_status(:ok)
end
- it "does not send notifications by default" do
- post "/webhooks/lndhub", params: payload.to_json
- expect(enqueued_jobs.size).to eq(0)
- end
-
it "does not send a zap receipt" do
expect(NostrManager::PublishZapReceipt).not_to receive(:call)
post "/webhooks/lndhub", params: payload.to_json
@@ -106,6 +101,34 @@ RSpec.describe "Webhooks", type: :request do
expect(args[3]["params"]["amount_sats"]).to eq(12300)
end
end
+
+ describe "minimum threshold amount not reached" do
+ before do
+ user.update! preferences: {
+ lightning_notify_sats_received: "xmpp",
+ lightning_notify_min_sats: 21000
+ }
+ end
+
+ it "does not send a notification" do
+ post "/webhooks/lndhub", params: payload.to_json
+ expect(enqueued_jobs.size).to eq(0)
+ end
+ end
+
+ describe "no memo/description/message" do
+ before do
+ user.update! preferences: {
+ lightning_notify_sats_received: "xmpp",
+ lightning_notify_only_with_message: true
+ }
+ end
+
+ it "does not send a notification" do
+ post "/webhooks/lndhub", params: payload.merge({ memo: "" }).to_json
+ expect(enqueued_jobs.size).to eq(0)
+ end
+ end
end
describe "Valid payload for zap transaction" do
@@ -154,6 +177,17 @@ RSpec.describe "Webhooks", type: :request do
expect(NostrManager::PublishZapReceipt).to receive(:call).with(zap: zap)
post "/webhooks/lndhub", params: payload.to_json
end
+
+ context "with notifications disabled for zaps" do
+ before do
+ user.update! preferences: { lightning_notify_zap_received: "disabled" }
+ end
+
+ it "does not send a notification" do
+ post "/webhooks/lndhub", params: payload.to_json
+ expect(enqueued_jobs.size).to eq(0)
+ end
+ end
end
end
end