Refactors duplicate payment check in Fava

Improves payment recording logic by fetching recent entries and filtering using Python, replacing the BQL query.

This addresses issues with matching against set types in BQL, enhancing reliability.
This commit is contained in:
padreug 2025-11-10 10:25:05 +01:00
parent fbda8e2980
commit 8342318fde
2 changed files with 44 additions and 24 deletions

View file

@ -152,18 +152,28 @@ async def on_invoice_paid(payment: Payment) -> None:
fava = get_fava_client()
try:
# Query Fava for existing payment entry
query = f"SELECT * WHERE links ~ 'ln-{payment.payment_hash[:16]}'"
async with httpx.AsyncClient(timeout=5.0) as client:
response = await client.get(
f"{fava.base_url}/query",
params={"query_string": query}
)
result = response.json()
# Check if payment already recorded by fetching recent entries
# Note: We can't use BQL query with `links ~ 'pattern'` because links is a set type
# and BQL doesn't support regex matching on sets. Instead, fetch entries and filter in Python.
link_to_find = f"ln-{payment.payment_hash[:16]}"
if result.get('data', {}).get('rows'):
logger.info(f"Payment {payment.payment_hash} already recorded in Fava, skipping")
return
async with httpx.AsyncClient(timeout=5.0) as client:
# Get recent entries from Fava's journal endpoint
response = await client.get(
f"{fava.base_url}/api/journal",
params={"time": ""} # Get all entries
)
if response.status_code == 200:
data = response.json()
entries = data.get('entries', [])
# Check if any entry has our payment link
for entry in entries:
entry_links = entry.get('links', [])
if link_to_find in entry_links:
logger.info(f"Payment {payment.payment_hash} already recorded in Fava, skipping")
return
except Exception as e:
logger.warning(f"Could not check Fava for duplicate payment: {e}")