From 48041630ca8f7cba505131829c84772daeb4bb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 10 May 2024 13:57:25 +0200 Subject: [PATCH] Limit number of relays to publish zap receipts to --- app/models/setting.rb | 3 ++ .../nostr_manager/publish_zap_receipt.rb | 3 +- .../admin/settings/services/_nostr.html.erb | 12 +++++++- .../nostr_manager/publish_zap_receipt_spec.rb | 30 +++++++++++++++++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index c0aa001..15c8755 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -169,6 +169,9 @@ class Setting < RailsSettings::Base field :nostr_public_key, type: :string, default: ENV["NOSTR_PUBLIC_KEY"].presence + field :nostr_zaps_relay_limit, type: :integer, + default: 12 + # # OpenCollective # diff --git a/app/services/nostr_manager/publish_zap_receipt.rb b/app/services/nostr_manager/publish_zap_receipt.rb index a88e8e4..3d3c4d0 100644 --- a/app/services/nostr_manager/publish_zap_receipt.rb +++ b/app/services/nostr_manager/publish_zap_receipt.rb @@ -7,8 +7,7 @@ module NostrManager def call tags = parse_tags(@zap.request_event.tags) - # TODO limit to 15 or so relays - tags[:relays].each do |relay_url| + tags[:relays].take(Setting.nostr_zaps_relay_limit).each do |relay_url| if @delayed NostrPublishEventJob.perform_later(event: @zap.receipt, relay: relay_url) else diff --git a/app/views/admin/settings/services/_nostr.html.erb b/app/views/admin/settings/services/_nostr.html.erb index c4f2182..302c52a 100644 --- a/app/views/admin/settings/services/_nostr.html.erb +++ b/app/views/admin/settings/services/_nostr.html.erb @@ -19,5 +19,15 @@ title: "Public key", description: "The corresponding public key of the accounts service" ) %> -<% end %> + +
+

Zaps

+ +<% end %> diff --git a/spec/services/nostr_manager/publish_zap_receipt_spec.rb b/spec/services/nostr_manager/publish_zap_receipt_spec.rb index e3a31fa..452e34e 100644 --- a/spec/services/nostr_manager/publish_zap_receipt_spec.rb +++ b/spec/services/nostr_manager/publish_zap_receipt_spec.rb @@ -6,11 +6,35 @@ RSpec.describe NostrManager::PublishZapReceipt, type: :model do describe "Default/delayed execution" do it "publishes zap receipts to all requested relays" do - 2.times do - expect(NostrPublishEventJob).to receive(:perform_later).and_return(true) - end + expect(NostrPublishEventJob).to receive(:perform_later) + .exactly(2).times.and_return(true) described_class.call(zap: zap) end + + context "with a long relay list" do + before do + relays = zap.request["tags"].find { |t| t.first == "relays" } + [ + "wss://aegonstargaryen.example.com", "wss://visenya.example.com", + "wss://rhaenys.example.com", "wss://housevelaryon.example.com", + "wss://aemond.example.com", "wss://jaehaerys.example.com", + "wss://daenerys.example.com", "wss://corlys.example.com", + "wss://laenor.example.com", "wss://alysanne.example.com", + "wss://balerion.example.com", "wss://meraxes.example.com", + "wss://vhaegar.example.com", "wss://vermax.example.com", + "wss://caraxes.example.com" + ].each do |url| + relays << url + end + end + + it "limits publishing attempts to the first 12 relays" do + expect(NostrPublishEventJob).to receive(:perform_later) + .exactly(12).times.and_return(true) + + described_class.call(zap: zap) + end + end end end