WIP Persist zaps, create and send zap receipts
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2024-05-09 14:31:37 +02:00
parent c0f4e7925e
commit c6c5d80fb4
17 changed files with 242 additions and 37 deletions

View File

@@ -52,7 +52,7 @@ class LnurlpayController < ApplicationController
return
end
if params[:nostr].present?
if params[:nostr].present?# TODO && Setting.nostr_enabled?
handle_zap_request amount, params[:nostr], params[:lnurl]
else
handle_pay_request address, amount, comment
@@ -131,6 +131,9 @@ class LnurlpayController < ApplicationController
return
end
# TODO might want to use the existing invoice and zap record if there are
# multiple calls with the same zap request
desc = "Zap for #{@user.address}"
desc = "#{desc}: \"#{event.content}\"" if event.content.present?
@@ -142,6 +145,10 @@ class LnurlpayController < ApplicationController
}
)
@user.zaps.create! request: event,
payment_request: invoice["payment_request"],
amount: amount
render json: { status: "OK", pr: invoice["payment_request"] }
end
end

View File

@@ -7,10 +7,14 @@ class WebhooksController < ApplicationController
def lndhub
@user = User.find_by!(ln_account: @payload[:user_login])
if @zap_request = fetch_nostr_event_from_description
NostrManager::PublishZapReceipt.call(
user: @user, zap_request: @zap_request
if zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
zap_receipt = NostrManager::CreateZapReceipt.call(
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)
end
send_notifications
@@ -28,21 +32,16 @@ class WebhooksController < ApplicationController
def process_payload
@payload = JSON.parse(request.body.read, symbolize_names: true)
head :no_content and return unless @payload[:type] == "incoming"
unless @payload[:type] == "incoming" &&
@payload[:state] == "settled"
head :no_content and return
end
rescue
head :unprocessable_entity and return
end
def fetch_nostr_event_from_description
memo_json = JSON.parse(@payload[:memo])
Nostr::Event.new(**memo_json.to_h.symbolize_keys)
rescue
nil
end
def send_notifications
notify = @user.preferences[:lightning_notify_sats_received]
case notify
case @user.preferences[:lightning_notify_sats_received]
when "xmpp"
notify_xmpp
when "email"