I can zap. Yee Haa! Next I need to mark events as having been zapped, and keep track of the zaps on an event.

This commit is contained in:
Robert C. Martin 2023-04-17 15:05:49 -05:00
parent f538ab26c0
commit 8e949d0d0b
3 changed files with 65 additions and 40 deletions

View File

@ -66,8 +66,8 @@
(str "https://" domain "/.well-known/lnurlp/" name)))
(defn make-zap-request [wallet-response event amount comment lnurl]
(let [{:strs [maxSendable minSendable metadata tag
commentAllowed allowsNostr nostrPubkey]} wallet-response
(let [{:strs [maxSendable minSendable
commentAllowed allowsNostr]} wallet-response
_ (when-not allowsNostr
(throw (Exception. (str "Recipient does not accept Nostr zaps."))))
_ (when (< amount minSendable)
@ -89,18 +89,36 @@
[_ request] (composers/body->event body)]
request))
(defn- ask-for-zap []
(let [zap-dialog
(dialog :content
(flow-panel
:items ["Amount in sats"
(text :id :amount
:text "21"
:columns 7)
"Comment:"
(text :id :comment
:text "Zap!"
:columns 20)])
:option-type :ok-cancel
:success-fn (fn [p]
[(* 1000 (Integer/parseInt (text (select (to-root p) [:#amount]))))
(text (select (to-root p) [:#comment]))])
:cancel-fn (fn [_p] nil))]
(show! (pack! zap-dialog))))
(defn zap-author [event _e]
(try
(let [zap-address (get-zap-address event)
lnurl (lud16->lnurl zap-address)
ln-response (client/get lnurl)
wallet-response (json/read-str (:body ln-response))
{:strs [callback metadata tag nostrPubkey
status reason]} wallet-response]
{:strs [callback status reason]} wallet-response]
(when (and (some? status) (not= status "OK"))
(throw (Exception. (str "Wallet error: " status reason))))
(let [amount 1000
comment "hi"
(let [[amount comment :as answer] (ask-for-zap)
_ (when (nil? answer) (throw (Exception. "cancel")))
zap-request (make-zap-request wallet-response event amount comment lnurl)
json-request (events/to-json zap-request)
encoded-request (URLEncoder/encode ^String json-request "UTF-8")
@ -118,7 +136,8 @@
(throw (Exception. (str "Invoice request error: " (get invoice-json "reason")))))
invoice (get invoice-json "pr")]
(update-mem :pending-zaps assoc invoice {:id (:id event)
:amount amount})
:amount amount
:comment comment})
(util/copy-to-clipboard invoice)
(alert (str "Invoice is copied to clipboard.\n"
"Paste it into your wallet and Zap!\n\n"
@ -127,13 +146,13 @@
)
(catch Exception e
(log-pr 1 'zap-author (.getMessage e))
(alert (str "Cannot zap. " (.getMessage e))))))
(when (not= "cancel" (.getMessage e))
(alert (str "Cannot zap. " (.getMessage e)))))))
(defn process-zap-receipt [event]
(prn 'zap-receipt event)
(let [[[receipt-invoice]] (events/get-tag event :bolt11)
transaction (get-mem [:pending-zaps receipt-invoice])]
(when (some? transaction)
(let [{:keys [id amount]} transaction]
(prn 'got-zap-receipt (util/hexify id) (/ amount 1000) 'sats)
(let [{:keys [id amount comment]} transaction]
(prn 'got-zap-receipt (util/hexify id) (/ amount 1000) 'sats comment)
(update-mem :pending-zaps dissoc receipt-invoice)))))

View File

@ -10,6 +10,7 @@
[more-speech.nostr.event-composers :as composers]
[more-speech.nostr.events :as events]
[more-speech.nostr.util :as util]
[more-speech.nostr.zaps :as zaps]
[more-speech.ui.formatter-util :as formatter-util]
[more-speech.ui.formatter-util :as f-util]
[more-speech.ui.formatters :as formatters]
@ -82,19 +83,26 @@
(when-let [profile (gateway/get-profile (get-db) id)]
(show-profile profile)))
(defn user-name-click [e]
(defn user-name-click [type frame e]
(let [x (.x (.getPoint e))
y (.y (.getPoint e))
node (.getComponent e)
pubkey (config node :user-data)
hex-id (util/hexify pubkey)
npub (bech32/encode "npub" pubkey)
p (popup :items [(action :name (str "Copy " (subs hex-id 0 10) "...")
:handler (partial copy-to-clipboard hex-id))
(action :name (str "Copy " (subs npub 0 10) "...")
:handler (partial copy-to-clipboard npub))
(action :name "Get info..."
:handler (fn [_e] (show-user-profile pubkey)))])]
event-id (config (select frame [:#id-label]) :user-data)
event (gateway/get-event (get-db) event-id)
popup-items [(action :name (str "Copy " (subs hex-id 0 10) "...")
:handler (partial copy-to-clipboard hex-id))
(action :name (str "Copy " (subs npub 0 10) "...")
:handler (partial copy-to-clipboard npub))
(action :name "Get info..."
:handler (fn [_e] (show-user-profile pubkey)))]
popup-items (if (= type :author)
(conj popup-items (action :name "Zap author..."
:handler (partial zaps/zap-author event)))
popup-items)
p (popup :items popup-items)]
(.show p (to-widget e) x y)))
(defn reaction-click [polarity]
@ -128,7 +136,23 @@
relays-popup (popup :enabled? false)
relays-label (label :id :relays-label :user-data relays-popup)
up-arrow (label :text " " :id :up-arrow)
dn-arrow (label :text " " :id :dn-arrow)]
dn-arrow (label :text " " :id :dn-arrow)
grid
(grid-panel
:columns 3
:preferred-size [-1 :by 70] ;icky.
:items [
(flow-panel :align :left :items [(bold-label "Author:") author-name-label])
(flow-panel :align :left :items [(bold-label "Subject:") subject-label])
(flow-panel :align :left :items [(bold-label "Reactions:") reactions-label up-arrow dn-arrow])
(flow-panel :align :left :items [(bold-label "Created at:") created-time-label])
(flow-panel :align :left :items [(bold-label "Reply to:") reply-to-label])
(flow-panel :align :left :items [(bold-label "Relays:") relays-label])
(flow-panel :align :left :items [(bold-label "id:") id-label])
(flow-panel :align :left :items [(bold-label "Citing:") citing-label])
(flow-panel :align :left :items [(bold-label "Root:") root-label])])]
(listen relays-label
:mouse-entered (fn [e]
(-> relays-popup
@ -146,25 +170,9 @@
(listen id-label :mouse-pressed copy-click)
(listen up-arrow :mouse-pressed up-click)
(listen dn-arrow :mouse-pressed dn-click)
(listen author-name-label :mouse-pressed user-name-click)
(listen reply-to-label :mouse-pressed user-name-click)
(let [grid
(grid-panel
:columns 3
:preferred-size [-1 :by 70] ;icky.
:items [
(flow-panel :align :left :items [(bold-label "Author:") author-name-label])
(flow-panel :align :left :items [(bold-label "Subject:") subject-label])
(flow-panel :align :left :items [(bold-label "Reactions:") reactions-label up-arrow dn-arrow])
(flow-panel :align :left :items [(bold-label "Created at:") created-time-label])
(flow-panel :align :left :items [(bold-label "Reply to:") reply-to-label])
(flow-panel :align :left :items [(bold-label "Relays:") relays-label])
(flow-panel :align :left :items [(bold-label "id:") id-label])
(flow-panel :align :left :items [(bold-label "Citing:") citing-label])
(flow-panel :align :left :items [(bold-label "Root:") root-label])])]
grid)))
(listen author-name-label :mouse-pressed (partial user-name-click :author grid))
(listen reply-to-label :mouse-pressed (partial user-name-click :reply-to grid))
grid))
(defn make-article-area []
(editor-pane

View File

@ -9,7 +9,6 @@
[more-speech.nostr.events :as events]
[more-speech.nostr.tab-searcher :as tab-searcher]
[more-speech.nostr.trust-updater :as trust-updater]
[more-speech.nostr.zaps :as zaps]
[more-speech.nostr.util :as util]
[more-speech.ui.formatters :as formatters]
[more-speech.ui.swing.article-panel :as article-panel]
@ -272,7 +271,6 @@
(menu :text "Add article to tab" :items add-article-actions)
(menu :text "Block article from tab" :items block-article-actions)
(action :name "DM author..." :handler (partial dm-author event))
(action :name "Zap author..." :handler (partial zaps/zap-author event))
])]
(.show p (to-widget e) (.x (.getPoint e)) (.y (.getPoint e))))))