Add Webhooks and XMPP notifications for incoming sats #79

Merged
raucao merged 10 commits from feature/webhooks into master 2023-01-13 04:33:02 +00:00
Showing only changes of commit 9e988e92d1 - Show all commits

View File

@@ -6,15 +6,33 @@ class WebhooksController < ApplicationController
def lndhub
begin
payload = JSON.parse(request.body.read, symbolize_names: true)
raucao marked this conversation as resolved
Review

doesn't rails automatically parse the JSON because the proper content type is set?
so params[:type] and params[:user_login] should be enough?

doesn't rails automatically parse the JSON because the proper content type is set? so `params[:type]` and `params[:user_login]` should be enough?
Review

I didn't know, so I tried (also with explicitly setting the content type in the spec). Doesn't do it.

I didn't know, so I tried (also with explicitly setting the content type in the spec). Doesn't do it.
return unless payload[:type] == "incoming"
rescue
head :unprocessable_entity and return
end
user = User.find_by(ln_account: payload[:user_login])
raucao marked this conversation as resolved Outdated
Outdated
Review

should not be the case, but this might be nil. maybe we should do a User.find_by!

should not be the case, but this might be nil. maybe we should do a `User.find_by!`

Good idea! Fails much cleaner and more expressively than when trying to access properties on nil later.

Good idea! Fails much cleaner and more expressively than when trying to access properties on `nil` later.
# TODO make configurable
notify_xmpp(user.address, payload[:amount], payload[:memo])
head :ok
end
private
def notify_xmpp(address, amt_sats, memo)
payload = {
type: "normal",
from: "kosmos.org", # TODO domain config
# to: address,
to: "raucao@kosmos.org",
subject: "Sats received!",
body: "#{amt_sats} sats received in your wallet. Memo: \"#{memo}\""
}
XmppSendMessageJob.perform_later(payload)
end
def authorize_request
if !ENV['WEBHOOKS_ALLOWED_IPS'].split(',').include?(request.remote_ip)
head :forbidden and return