From 26d613bdca9043194a5508bf8bb776490ebfbd82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 14 Feb 2024 10:47:27 +0100 Subject: [PATCH] Allow other controllers to access lndhub user balance --- app/controllers/application_controller.rb | 22 +++++++++++++ .../services/lightning_controller.rb | 31 ++++--------------- .../lndhub_manager/fetch_user_balance.rb | 12 +++++++ 3 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 app/services/lndhub_manager/fetch_user_balance.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ff94797..a0da71a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -41,4 +41,26 @@ class ApplicationController < ActionController::Base def after_sign_in_path_for(user) session[:user_return_to] || root_path end + + def lndhub_authenticate(options={}) + if session[:ln_auth_token].present? && !options[:force_reauth] + @ln_auth_token = session[:ln_auth_token] + else + lndhub = Lndhub.new + auth_token = lndhub.authenticate(current_user) + session[:ln_auth_token] = auth_token + @ln_auth_token = auth_token + end + rescue => e + Sentry.capture_exception(e) if Setting.sentry_enabled? + end + + def lndhub_fetch_balance + @balance = LndhubManager::FetchUserBalance.call(auth_token: @ln_auth_token) + rescue AuthError + lndhub_authenticate(force_reauth: true) + raise if @fetch_balance_retried + @fetch_balance_retried = true + lndhub_fetch_balance + end end diff --git a/app/controllers/services/lightning_controller.rb b/app/controllers/services/lightning_controller.rb index 1253ab0..c02b91f 100644 --- a/app/controllers/services/lightning_controller.rb +++ b/app/controllers/services/lightning_controller.rb @@ -2,10 +2,11 @@ require "rqrcode" require "lnurl" class Services::LightningController < ApplicationController - before_action :authenticate_user! - before_action :authenticate_with_lndhub before_action :set_current_section - before_action :fetch_balance + before_action :require_service_available + before_action :authenticate_user! + before_action :lndhub_authenticate + before_action :lndhub_fetch_balance def index @wallet_setup_url = "lndhub://#{current_user.ln_account}:#{current_user.ln_password}@#{ENV['LNDHUB_PUBLIC_URL']}" @@ -55,32 +56,12 @@ class Services::LightningController < ApplicationController private - def authenticate_with_lndhub(options={}) - if session[:ln_auth_token].present? && !options[:force_reauth] - @ln_auth_token = session[:ln_auth_token] - else - lndhub = Lndhub.new - auth_token = lndhub.authenticate(current_user) - session[:ln_auth_token] = auth_token - @ln_auth_token = auth_token - end - rescue => e - Sentry.capture_exception(e) if Setting.sentry_enabled? - end - def set_current_section @current_section = :services end - def fetch_balance - lndhub = Lndhub.new - data = lndhub.balance @ln_auth_token - @balance = data["BTC"]["AvailableBalance"] rescue nil - rescue AuthError - authenticate_with_lndhub(force_reauth: true) - raise if @fetch_balance_retried - @fetch_balance_retried = true - fetch_balance + def require_service_available + http_status :not_found unless Setting.lndhub_enabled? end def fetch_transactions diff --git a/app/services/lndhub_manager/fetch_user_balance.rb b/app/services/lndhub_manager/fetch_user_balance.rb new file mode 100644 index 0000000..1bc0353 --- /dev/null +++ b/app/services/lndhub_manager/fetch_user_balance.rb @@ -0,0 +1,12 @@ +module LndhubManager + class FetchUserBalance < Lndhub + def initialize(auth_token:) + @auth_token = auth_token + end + + def call + data = fetch_balance(auth_token) + data["BTC"]["AvailableBalance"] rescue nil + end + end +end