From 3f43fe81016f2d10d21ec89fa93ed791202c7e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 23 May 2024 00:01:25 +0200 Subject: [PATCH 1/5] Fix missing description for FieldsetToggleComponent --- app/components/form_elements/fieldset_toggle_component.html.erb | 2 +- app/components/form_elements/fieldset_toggle_component.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 From b541e95bb74ea5b70128f14946fe79bd6c426d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 23 May 2024 00:22:38 +0200 Subject: [PATCH 2/5] Change default for lightning notifications --- config/default_preferences.yml | 2 +- spec/requests/webhooks_spec.rb | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/config/default_preferences.yml b/config/default_preferences.yml index c65d658..8b622c1 100644 --- a/config/default_preferences.yml +++ b/config/default_preferences.yml @@ -1,3 +1,3 @@ -lightning_notify_sats_received: disabled # or xmpp, email remotestorage_notify_auth_created: email # or xmpp, email +lightning_notify_sats_received: email # or xmpp xmpp_exchange_contacts_with_invitees: true diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 65e0f5b..922e811 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 From bad3b7a2be86f9cdbce2b8e3ebcb244c40f28951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 23 May 2024 00:23:42 +0200 Subject: [PATCH 3/5] Use dynamic list for allowed user preference params --- app/controllers/settings_controller.rb | 8 +++----- app/models/user_preferences.rb | 4 ++++ spec/models/user_preferences_spec.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index cb7de77..8e307ca 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -147,11 +147,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/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/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 From 5348a229a6ffe80ad14245d435bf94260b00d966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 29 May 2024 15:12:07 +0100 Subject: [PATCH 4/5] WIP Add new lightning notification settings --- app/controllers/settings_controller.rb | 6 ++++- app/views/settings/_lightning.html.erb | 34 +++++++++++++++++++++++++- config/default_preferences.yml | 7 ++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 8e307ca..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 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 8b622c1..8e31491 100644 --- a/config/default_preferences.yml +++ b/config/default_preferences.yml @@ -1,3 +1,6 @@ -remotestorage_notify_auth_created: email # or xmpp, email -lightning_notify_sats_received: email # or xmpp +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 From 1685d6ecf8f60331a3084c9230951cf604eea29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sat, 1 Jun 2024 17:24:53 +0200 Subject: [PATCH 5/5] Respect new Lightning notification settings --- app/controllers/webhooks_controller.rb | 19 +++++++++---- spec/requests/webhooks_spec.rb | 39 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) 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/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 922e811..2851bb2 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -101,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 @@ -149,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