Compare commits
38 commits
2740d73678
...
ef5d2dcfcf
| Author | SHA1 | Date | |
|---|---|---|---|
| ef5d2dcfcf | |||
| e937883564 | |||
| 1bddb99132 | |||
| 4d91426e82 | |||
| b4d7653988 | |||
| 29045163a3 | |||
| d69ec7dda2 | |||
| 2db0102857 | |||
| e8fcecac40 | |||
| 5013709be7 | |||
| f76e21e960 | |||
| f965cf07c9 | |||
| 1ad99aa3d6 | |||
| 920125aaee | |||
| ba97205592 | |||
| c1e66fbf7f | |||
| 7843da21d8 | |||
| b467826622 | |||
| d740cb1f97 | |||
| 3425097a5c | |||
| cdfcee39ae | |||
| bdd49f8612 | |||
| 702ab70559 | |||
| 32ea79a137 | |||
| 41e64adfde | |||
| eb474b1390 | |||
| a41348df94 | |||
| 0c782e6239 | |||
| 1dcff37df5 | |||
| 68e6e3d02e | |||
| a77145e08e | |||
| c49abdb53f | |||
| 4fb6d90fcd | |||
|
|
a9ac6dcfc1 | ||
|
|
44f2cb5a62 | ||
| 33977c53d6 | |||
| 7cc622fc44 | |||
| c669da5822 |
20 changed files with 2501 additions and 1312 deletions
56
API_DOCUMENTATION.md
Normal file
56
API_DOCUMENTATION.md
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# Events API Documentation
|
||||
|
||||
## Public Events Endpoint
|
||||
|
||||
### GET `/api/v1/events/public`
|
||||
|
||||
Retrieve all events in the database with read-only access. No authentication required.
|
||||
|
||||
**Authentication:** None required (public endpoint)
|
||||
|
||||
**Headers:**
|
||||
```
|
||||
None required
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
- None
|
||||
|
||||
**Response:**
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "event_id",
|
||||
"wallet": "wallet_id",
|
||||
"name": "Event Name",
|
||||
"info": "Event description",
|
||||
"closing_date": "2024-12-31",
|
||||
"event_start_date": "2024-12-01",
|
||||
"event_end_date": "2024-12-02",
|
||||
"currency": "sat",
|
||||
"amount_tickets": 100,
|
||||
"price_per_ticket": 1000.0,
|
||||
"time": "2024-01-01T00:00:00Z",
|
||||
"sold": 0,
|
||||
"banner": null
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
```bash
|
||||
curl http://your-lnbits-instance/events/api/v1/events/public
|
||||
```
|
||||
|
||||
**Notes:**
|
||||
- This endpoint allows read-only access to all events in the database
|
||||
- No authentication required (truly public endpoint)
|
||||
- Returns events ordered by creation time (newest first)
|
||||
- Suitable for public event listings or read-only integrations
|
||||
|
||||
## Comparison with Existing Endpoints
|
||||
|
||||
| Endpoint | Authentication | Scope | Use Case |
|
||||
|----------|---------------|-------|----------|
|
||||
| `/api/v1/events` | Invoice Key | User's wallets only | Private event management |
|
||||
| `/api/v1/events/public` | None | All events | Public event browsing |
|
||||
19
README.md
19
README.md
|
|
@ -1,20 +1,10 @@
|
|||
<a href="https://lnbits.com" target="_blank" rel="noopener noreferrer">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/QE6SIrs.png">
|
||||
<img src="https://i.imgur.com/fyKPgVT.png" alt="LNbits" style="width:280px">
|
||||
</picture>
|
||||
</a>
|
||||
|
||||
[](./LICENSE)
|
||||
[](https://github.com/lnbits/lnbits)
|
||||
|
||||
# Events - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>
|
||||
|
||||
<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>
|
||||
|
||||
## Sell tickets for events and use the built-in scanner for registering attendees
|
||||
|
||||
Events allows you to create tickets for an event. Each ticket is in the form of a unique QR code. After registering and paying, the user gets a QR code to present at registration/entrance.
|
||||
Events alows you to make tickets for an event. Each ticket is in the form of a unique QR code. After registering, and paying for ticket, the user gets a QR code to present at registration/entrance.
|
||||
|
||||
Events includes a shareable ticket scanner, which can be used to register attendees.
|
||||
|
||||
|
|
@ -43,10 +33,3 @@ Events includes a shareable ticket scanner, which can be used to register attend
|
|||
|
||||
4. Use the built-in ticket scanner to validate registered, and paid, attendees\
|
||||

|
||||
|
||||
## Powered by LNbits
|
||||
|
||||
[LNbits](https://lnbits.com) is a free and open-source lightning accounts system.
|
||||
|
||||
[](https://shop.lnbits.com/)
|
||||
[](https://my.lnbits.com/login)
|
||||
|
|
|
|||
45
__init__.py
45
__init__.py
|
|
@ -21,6 +21,9 @@ events_static_files = [
|
|||
|
||||
scheduled_tasks: list[asyncio.Task] = []
|
||||
|
||||
# Module-level NostrClient — None when nostrclient is unavailable
|
||||
nostr_client = None
|
||||
|
||||
|
||||
def events_stop():
|
||||
for task in scheduled_tasks:
|
||||
|
|
@ -29,12 +32,50 @@ def events_stop():
|
|||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
|
||||
global nostr_client
|
||||
if nostr_client:
|
||||
asyncio.get_event_loop().create_task(nostr_client.stop())
|
||||
|
||||
|
||||
def events_start():
|
||||
from lnbits.tasks import create_permanent_unique_task
|
||||
|
||||
task = create_permanent_unique_task("ext_events", wait_for_paid_invoices)
|
||||
scheduled_tasks.append(task)
|
||||
task1 = create_permanent_unique_task("ext_events", wait_for_paid_invoices)
|
||||
scheduled_tasks.append(task1)
|
||||
|
||||
async def _start_nostr_client():
|
||||
global nostr_client
|
||||
await asyncio.sleep(10) # Wait for nostrclient to be ready
|
||||
try:
|
||||
from .nostr.nostr_client import NostrClient
|
||||
|
||||
nostr_client = NostrClient()
|
||||
logger.info("[EVENTS] Starting NostrClient for NIP-52 sync")
|
||||
await nostr_client.run_forever()
|
||||
except Exception as e:
|
||||
logger.warning(f"[EVENTS] NostrClient failed to start: {e}")
|
||||
logger.info("[EVENTS] Events will work without Nostr sync")
|
||||
|
||||
task2 = create_permanent_unique_task("ext_events_nostr", _start_nostr_client)
|
||||
scheduled_tasks.append(task2)
|
||||
|
||||
async def _sync_nostr_events():
|
||||
global nostr_client
|
||||
await asyncio.sleep(15) # Wait for NostrClient to connect
|
||||
if not nostr_client:
|
||||
logger.info("[EVENTS] No NostrClient, skipping Nostr sync")
|
||||
return
|
||||
try:
|
||||
from .nostr_sync import wait_for_nostr_events
|
||||
|
||||
await wait_for_nostr_events(nostr_client)
|
||||
except Exception as e:
|
||||
logger.error(f"[EVENTS] Nostr sync task failed: {e}")
|
||||
|
||||
task3 = create_permanent_unique_task(
|
||||
"ext_events_nostr_sync", _sync_nostr_events
|
||||
)
|
||||
scheduled_tasks.append(task3)
|
||||
|
||||
|
||||
__all__ = ["db", "events_ext", "events_start", "events_static_files", "events_stop"]
|
||||
|
|
|
|||
11
config.json
11
config.json
|
|
@ -1,11 +1,8 @@
|
|||
{
|
||||
"id": "events",
|
||||
"version": "1.2.1",
|
||||
"name": "Events",
|
||||
"repo": "https://github.com/lnbits/events",
|
||||
"short_description": "Sell and register event tickets",
|
||||
"description": "",
|
||||
"tile": "/events/static/image/events.png",
|
||||
"lnbits": "1.1.0",
|
||||
"min_lnbits_version": "1.3.0",
|
||||
"contributors": [
|
||||
{
|
||||
|
|
@ -54,9 +51,5 @@
|
|||
],
|
||||
"description_md": "https://raw.githubusercontent.com/lnbits/events/main/description.md",
|
||||
"terms_and_conditions_md": "https://raw.githubusercontent.com/lnbits/events/main/toc.md",
|
||||
"license": "MIT",
|
||||
"paid_features": "",
|
||||
"tags": ["Fun & Social", "Ticketing"],
|
||||
"donate": "",
|
||||
"hidden": false
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
|||
163
crud.py
163
crud.py
|
|
@ -1,10 +1,35 @@
|
|||
import json
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
|
||||
from lnbits.db import Database
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
|
||||
from .models import CreateEvent, Event, Ticket, TicketExtra
|
||||
from .models import CreateEvent, Event, EventsSettings, Ticket, TicketExtra
|
||||
|
||||
|
||||
def _parse_ticket_row(row) -> dict:
|
||||
"""
|
||||
Parse a database row into a dict suitable for Ticket model creation.
|
||||
Handles:
|
||||
- Empty string to None conversion for name/email
|
||||
- JSON string to dict conversion for extra field
|
||||
"""
|
||||
ticket_data = dict(row)
|
||||
|
||||
# Convert empty strings back to None for the model
|
||||
if ticket_data.get("name") == "":
|
||||
ticket_data["name"] = None
|
||||
if ticket_data.get("email") == "":
|
||||
ticket_data["email"] = None
|
||||
|
||||
# Parse extra field from JSON string if needed
|
||||
# (db.insert() serializes to JSON, but manual fetchone/fetchall returns string)
|
||||
extra = ticket_data.get("extra")
|
||||
if isinstance(extra, str):
|
||||
ticket_data["extra"] = json.loads(extra)
|
||||
|
||||
return ticket_data
|
||||
|
||||
db = Database("ext_events")
|
||||
|
||||
|
|
@ -13,13 +38,48 @@ async def create_ticket(
|
|||
payment_hash: str,
|
||||
wallet: str,
|
||||
event: str,
|
||||
name: str = "",
|
||||
email: str = "",
|
||||
name: Optional[str] = None,
|
||||
email: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
extra: Optional[dict] = None,
|
||||
) -> Ticket:
|
||||
now = datetime.now(timezone.utc)
|
||||
ticket = Ticket(
|
||||
|
||||
# TODO: Check if this empty string workaround is still needed.
|
||||
# This converts None to empty strings for database storage because:
|
||||
# 1. Database may have NOT NULL constraints on name/email columns
|
||||
# 2. When user_id is provided, name/email are not used (mutually exclusive)
|
||||
# 3. The get_ticket() functions convert empty strings back to None when reading
|
||||
# Consider using nullable columns instead of this empty string pattern.
|
||||
if user_id:
|
||||
db_name = ""
|
||||
db_email = ""
|
||||
else:
|
||||
db_name = name or ""
|
||||
db_email = email or ""
|
||||
|
||||
# Create ticket with database-compatible values for insertion
|
||||
# Using db.insert() ensures proper serialization of the extra field (TicketExtra)
|
||||
# across all database backends (SQLite, PostgreSQL, CockroachDB)
|
||||
db_ticket = Ticket(
|
||||
id=payment_hash,
|
||||
wallet=wallet,
|
||||
event=event,
|
||||
name=db_name,
|
||||
email=db_email,
|
||||
user_id=user_id,
|
||||
registered=False,
|
||||
paid=False,
|
||||
reg_timestamp=now,
|
||||
time=now,
|
||||
extra=TicketExtra(**extra) if extra else TicketExtra(),
|
||||
)
|
||||
|
||||
await db.insert("events.ticket", db_ticket)
|
||||
|
||||
# Return ticket with original name/email values (not empty strings)
|
||||
# This maintains consistency with how get_ticket() converts empty strings back to None
|
||||
return Ticket(
|
||||
id=payment_hash,
|
||||
wallet=wallet,
|
||||
event=event,
|
||||
|
|
@ -32,32 +92,54 @@ async def create_ticket(
|
|||
time=now,
|
||||
extra=TicketExtra(**extra) if extra else TicketExtra(),
|
||||
)
|
||||
await db.insert("events.ticket", ticket)
|
||||
return ticket
|
||||
|
||||
|
||||
async def update_ticket(ticket: Ticket) -> Ticket:
|
||||
await db.update("events.ticket", ticket)
|
||||
# Create a new Ticket object with corrected values for database constraints
|
||||
ticket_dict = ticket.dict()
|
||||
|
||||
# Convert None values to empty strings for database constraints
|
||||
if ticket_dict.get("name") is None:
|
||||
ticket_dict["name"] = ""
|
||||
if ticket_dict.get("email") is None:
|
||||
ticket_dict["email"] = ""
|
||||
|
||||
# Create a new Ticket object with the corrected values
|
||||
corrected_ticket = Ticket(**ticket_dict)
|
||||
|
||||
await db.update("events.ticket", corrected_ticket)
|
||||
return ticket
|
||||
|
||||
|
||||
async def get_ticket(payment_hash: str) -> Ticket | None:
|
||||
return await db.fetchone(
|
||||
async def get_ticket(payment_hash: str) -> Optional[Ticket]:
|
||||
row = await db.fetchone(
|
||||
"SELECT * FROM events.ticket WHERE id = :id",
|
||||
{"id": payment_hash},
|
||||
Ticket,
|
||||
)
|
||||
if not row:
|
||||
return None
|
||||
|
||||
return Ticket(**_parse_ticket_row(row))
|
||||
|
||||
|
||||
async def get_tickets(wallet_ids: str | list[str]) -> list[Ticket]:
|
||||
if isinstance(wallet_ids, str):
|
||||
wallet_ids = [wallet_ids]
|
||||
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
|
||||
return await db.fetchall(
|
||||
f"SELECT * FROM events.ticket WHERE wallet IN ({q})",
|
||||
model=Ticket,
|
||||
rows = await db.fetchall(f"SELECT * FROM events.ticket WHERE wallet IN ({q})")
|
||||
|
||||
return [Ticket(**_parse_ticket_row(row)) for row in rows]
|
||||
|
||||
|
||||
async def get_tickets_by_user_id(user_id: str) -> list[Ticket]:
|
||||
"""Get all tickets for a specific user by their user_id"""
|
||||
rows = await db.fetchall(
|
||||
"SELECT * FROM events.ticket WHERE user_id = :user_id ORDER BY time DESC",
|
||||
{"user_id": user_id}
|
||||
)
|
||||
|
||||
return [Ticket(**_parse_ticket_row(row)) for row in rows]
|
||||
|
||||
|
||||
async def delete_ticket(payment_hash: str) -> None:
|
||||
await db.execute("DELETE FROM events.ticket WHERE id = :id", {"id": payment_hash})
|
||||
|
|
@ -82,6 +164,12 @@ async def purge_unpaid_tickets(event_id: str) -> None:
|
|||
|
||||
async def create_event(data: CreateEvent) -> Event:
|
||||
event_id = urlsafe_short_hash()
|
||||
# Default end date to start date if not provided
|
||||
if not data.event_end_date:
|
||||
data.event_end_date = data.event_start_date
|
||||
# Default closing date to end date if not provided
|
||||
if not data.closing_date:
|
||||
data.closing_date = data.event_end_date
|
||||
event = Event(id=event_id, time=datetime.now(timezone.utc), **data.dict())
|
||||
await db.insert("events.events", event)
|
||||
return event
|
||||
|
|
@ -111,29 +199,62 @@ async def get_events(wallet_ids: str | list[str]) -> list[Event]:
|
|||
|
||||
|
||||
async def get_all_events() -> list[Event]:
|
||||
"""Get all events without wallet filtering (public endpoint)."""
|
||||
"""Get all events from the database without wallet filtering."""
|
||||
return await db.fetchall(
|
||||
"SELECT * FROM events.events ORDER BY time DESC",
|
||||
model=Event,
|
||||
)
|
||||
|
||||
|
||||
async def get_tickets_by_user_id(user_id: str) -> list[Ticket]:
|
||||
"""Get all tickets for a specific user by their user_id."""
|
||||
async def get_public_events() -> list[Event]:
|
||||
"""Get approved, non-canceled events for public display."""
|
||||
return await db.fetchall(
|
||||
"SELECT * FROM events.ticket WHERE user_id = :user_id ORDER BY time DESC",
|
||||
{"user_id": user_id},
|
||||
model=Ticket,
|
||||
"""
|
||||
SELECT * FROM events.events
|
||||
WHERE status = 'approved' AND canceled = FALSE
|
||||
ORDER BY event_start_date ASC
|
||||
""",
|
||||
model=Event,
|
||||
)
|
||||
|
||||
|
||||
async def get_pending_events() -> list[Event]:
|
||||
"""Get proposed events awaiting admin approval."""
|
||||
return await db.fetchall(
|
||||
"SELECT * FROM events.events WHERE status = 'proposed' ORDER BY time DESC",
|
||||
model=Event,
|
||||
)
|
||||
|
||||
|
||||
async def get_settings() -> EventsSettings:
|
||||
"""Get extension settings (single row, always exists after migration)."""
|
||||
row = await db.fetchone("SELECT * FROM events.settings WHERE id = 1")
|
||||
if row:
|
||||
return EventsSettings(**dict(row))
|
||||
return EventsSettings()
|
||||
|
||||
|
||||
async def update_settings(settings: EventsSettings) -> EventsSettings:
|
||||
"""Update extension settings."""
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE events.settings
|
||||
SET auto_approve = :auto_approve
|
||||
WHERE id = 1
|
||||
""",
|
||||
{"auto_approve": settings.auto_approve},
|
||||
)
|
||||
return settings
|
||||
|
||||
|
||||
async def delete_event(event_id: str) -> None:
|
||||
await db.execute("DELETE FROM events.events WHERE id = :id", {"id": event_id})
|
||||
|
||||
|
||||
async def get_event_tickets(event_id: str) -> list[Ticket]:
|
||||
return await db.fetchall(
|
||||
rows = await db.fetchall(
|
||||
"SELECT * FROM events.ticket WHERE event = :event",
|
||||
{"event": event_id},
|
||||
Ticket,
|
||||
)
|
||||
|
||||
return [Ticket(**_parse_ticket_row(row)) for row in rows]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
Sell tickets for events and manage attendee registration with a built-in QR scanner.
|
||||
Sell tickets for events and use the built-in scanner for registering attendants
|
||||
|
||||
Its features include:
|
||||
Events alows you to make tickets for an event. Each ticket is in the form of a uniqque QR code. After registering, and paying for ticket, the user gets a QR code to present at registration/entrance.
|
||||
|
||||
- Creating events with ticket pricing
|
||||
- Generating unique QR code tickets after payment
|
||||
- Providing a shareable ticket scanner for check-in
|
||||
- Tracking registered and checked-in attendees
|
||||
|
||||
A complete ticketing solution for event organizers, meetup hosts, and conference planners who want to sell tickets and manage attendance with Bitcoin.
|
||||
Events includes a shareable ticket scanner, which can be used to register attendees.
|
||||
|
|
|
|||
|
|
@ -162,24 +162,84 @@ async def m005_add_image_banner(db):
|
|||
await db.execute("ALTER TABLE events.events ADD COLUMN banner TEXT;")
|
||||
|
||||
|
||||
async def m006_add_extra_fields(db):
|
||||
async def m006_add_user_id_support(db):
|
||||
"""
|
||||
Add user_id column to tickets table to support LNbits user-id as identifier
|
||||
Make name and email optional when user_id is provided
|
||||
"""
|
||||
await db.execute("ALTER TABLE events.ticket ADD COLUMN user_id TEXT;")
|
||||
|
||||
# Since SQLite doesn't support changing column constraints directly,
|
||||
# we'll work around this by allowing the application logic to handle
|
||||
# the validation that either (name AND email) OR user_id is provided
|
||||
# The database will continue to expect name and email as NOT NULL
|
||||
# but we'll insert empty strings for user_id tickets
|
||||
|
||||
async def m007_add_extra_fields(db):
|
||||
"""
|
||||
Add a canceled and 'extra' column to events and ticket tables
|
||||
to support promo codes and ticket metadata.
|
||||
"""
|
||||
# Add canceled and 'extra' columns to events table
|
||||
# SQLite requires separate ALTER TABLE statements for each column
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN canceled BOOLEAN NOT NULL DEFAULT FALSE;"
|
||||
)
|
||||
await db.execute("ALTER TABLE events.events ADD COLUMN extra TEXT;")
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN extra TEXT;"
|
||||
)
|
||||
|
||||
# Add 'extra' column to ticket table
|
||||
await db.execute("ALTER TABLE events.ticket ADD COLUMN extra TEXT;")
|
||||
|
||||
|
||||
async def m007_add_user_id(db):
|
||||
async def m008_add_event_status(db):
|
||||
"""
|
||||
Add user_id column to tickets table.
|
||||
Allows ticket purchase via LNbits user-id without name/email.
|
||||
Add status column to events table for proposal/approval workflow.
|
||||
Values: 'proposed', 'approved', 'rejected'.
|
||||
Default 'approved' for backward compatibility with existing events.
|
||||
"""
|
||||
await db.execute("ALTER TABLE events.ticket ADD COLUMN user_id TEXT;")
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN status TEXT NOT NULL DEFAULT 'approved';"
|
||||
)
|
||||
|
||||
|
||||
async def m009_add_nostr_columns(db):
|
||||
"""
|
||||
Add columns to track published NIP-52 Nostr calendar events.
|
||||
"""
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN nostr_event_id TEXT;"
|
||||
)
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN nostr_event_created_at INTEGER;"
|
||||
)
|
||||
|
||||
|
||||
async def m010_add_events_settings(db):
|
||||
"""
|
||||
Create extension settings table for admin-configurable options.
|
||||
"""
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS events.settings (
|
||||
id INTEGER PRIMARY KEY DEFAULT 1,
|
||||
auto_approve BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
"""
|
||||
)
|
||||
await db.execute(
|
||||
"INSERT OR IGNORE INTO events.settings (id, auto_approve) VALUES (1, FALSE);"
|
||||
)
|
||||
|
||||
|
||||
async def m011_add_location_and_categories(db):
|
||||
"""
|
||||
Add location and categories columns for NIP-52 calendar event support.
|
||||
"""
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN location TEXT;"
|
||||
)
|
||||
await db.execute(
|
||||
"ALTER TABLE events.events ADD COLUMN categories TEXT;"
|
||||
)
|
||||
|
|
|
|||
93
models.py
93
models.py
|
|
@ -1,3 +1,4 @@
|
|||
import json
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
|
|
@ -28,35 +29,76 @@ class EventExtra(BaseModel):
|
|||
|
||||
|
||||
class CreateEvent(BaseModel):
|
||||
wallet: str
|
||||
name: str
|
||||
info: str
|
||||
closing_date: str
|
||||
event_start_date: str
|
||||
event_end_date: str
|
||||
wallet: Optional[str] = None
|
||||
name: str # title (required)
|
||||
info: str = "" # description (optional, visible by default)
|
||||
closing_date: Optional[str] = None # defaults to event_end_date or event_start_date
|
||||
event_start_date: str # required
|
||||
event_end_date: Optional[str] = None # defaults to event_start_date
|
||||
currency: str = "sat"
|
||||
amount_tickets: int = Query(..., ge=0)
|
||||
price_per_ticket: float = Query(..., ge=0)
|
||||
banner: str | None = None
|
||||
amount_tickets: int = 0 # 0 = unlimited / not ticketed
|
||||
price_per_ticket: float = 0 # 0 = free
|
||||
banner: Optional[str] = None # image URL (optional, visible by default)
|
||||
location: Optional[str] = None # venue/address (optional, visible by default)
|
||||
categories: list[str] = Field(default_factory=list) # NIP-52 't' tags
|
||||
extra: EventExtra = Field(default_factory=EventExtra)
|
||||
status: str = "approved" # proposed, approved, rejected
|
||||
|
||||
|
||||
class CreateTicket(BaseModel):
|
||||
name: Optional[str] = None
|
||||
email: Optional[EmailStr] = None
|
||||
user_id: Optional[str] = None
|
||||
promo_code: Optional[str] = None
|
||||
refund_address: Optional[str] = None
|
||||
|
||||
@root_validator
|
||||
def validate_identifiers(cls, values):
|
||||
# Ensure either (name AND email) OR user_id is provided
|
||||
name = values.get('name')
|
||||
email = values.get('email')
|
||||
user_id = values.get('user_id')
|
||||
|
||||
if not user_id and not (name and email):
|
||||
raise ValueError("Either user_id or both name and email must be provided")
|
||||
if user_id and (name or email):
|
||||
raise ValueError("Cannot provide both user_id and name/email")
|
||||
return values
|
||||
|
||||
|
||||
class Event(BaseModel):
|
||||
id: str
|
||||
wallet: str
|
||||
name: str
|
||||
info: str
|
||||
closing_date: str
|
||||
info: str = ""
|
||||
closing_date: str | None = None
|
||||
canceled: bool = False
|
||||
event_start_date: str
|
||||
event_end_date: str
|
||||
currency: str
|
||||
amount_tickets: int
|
||||
price_per_ticket: float
|
||||
event_end_date: str | None = None
|
||||
currency: str = "sat"
|
||||
amount_tickets: int = 0
|
||||
price_per_ticket: float = 0
|
||||
time: datetime
|
||||
sold: int = 0
|
||||
banner: str | None = None
|
||||
location: str | None = None
|
||||
categories: list[str] = Field(default_factory=list)
|
||||
extra: EventExtra = Field(default_factory=EventExtra)
|
||||
status: str = "approved" # proposed, approved, rejected
|
||||
nostr_event_id: str | None = None
|
||||
nostr_event_created_at: int | None = None
|
||||
|
||||
@validator("categories", pre=True)
|
||||
def parse_categories(cls, v):
|
||||
if isinstance(v, str):
|
||||
return json.loads(v) if v else []
|
||||
return v or []
|
||||
|
||||
|
||||
class EventsSettings(BaseModel):
|
||||
"""Extension-level settings for the events extension."""
|
||||
|
||||
auto_approve: bool = False # Skip approval for all users
|
||||
|
||||
|
||||
class TicketExtra(BaseModel):
|
||||
|
|
@ -66,29 +108,12 @@ class TicketExtra(BaseModel):
|
|||
refunded: bool = False
|
||||
|
||||
|
||||
class CreateTicket(BaseModel):
|
||||
name: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
user_id: Optional[str] = None
|
||||
promo_code: str | None = None
|
||||
refund_address: str | None = None
|
||||
|
||||
@root_validator
|
||||
def validate_identifiers(cls, values):
|
||||
user_id = values.get("user_id")
|
||||
name = values.get("name")
|
||||
email = values.get("email")
|
||||
if not user_id and not (name and email):
|
||||
raise ValueError("Either user_id or both name and email must be provided")
|
||||
return values
|
||||
|
||||
|
||||
class Ticket(BaseModel):
|
||||
id: str
|
||||
wallet: str
|
||||
event: str
|
||||
name: str = ""
|
||||
email: str = ""
|
||||
name: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
user_id: Optional[str] = None
|
||||
registered: bool
|
||||
paid: bool
|
||||
|
|
|
|||
0
nostr/__init__.py
Normal file
0
nostr/__init__.py
Normal file
27
nostr/event.py
Normal file
27
nostr/event.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import hashlib
|
||||
import json
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class NostrEvent(BaseModel):
|
||||
id: str = ""
|
||||
pubkey: str
|
||||
created_at: int
|
||||
kind: int
|
||||
tags: List[List[str]] = []
|
||||
content: str = ""
|
||||
sig: Optional[str] = None
|
||||
|
||||
def serialize(self) -> List:
|
||||
return [0, self.pubkey, self.created_at, self.kind, self.tags, self.content]
|
||||
|
||||
def serialize_json(self) -> str:
|
||||
e = self.serialize()
|
||||
return json.dumps(e, separators=(",", ":"), ensure_ascii=False)
|
||||
|
||||
@property
|
||||
def event_id(self) -> str:
|
||||
data = self.serialize_json()
|
||||
return hashlib.sha256(data.encode()).hexdigest()
|
||||
144
nostr/nostr_client.py
Normal file
144
nostr/nostr_client.py
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
"""
|
||||
Bidirectional Nostr client for the events extension.
|
||||
|
||||
Connects to the nostrclient extension's internal WebSocket to publish
|
||||
and subscribe to NIP-52 calendar events. Based on nostrmarket's
|
||||
NostrClient pattern.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from asyncio import Queue
|
||||
from collections import OrderedDict
|
||||
from typing import Optional
|
||||
|
||||
from loguru import logger
|
||||
from websocket import WebSocketApp
|
||||
|
||||
from lnbits.helpers import encrypt_internal_message, urlsafe_short_hash
|
||||
from lnbits.settings import settings
|
||||
|
||||
from .event import NostrEvent
|
||||
|
||||
MAX_SEEN_EVENTS = 500
|
||||
|
||||
|
||||
class NostrClient:
|
||||
def __init__(self):
|
||||
self.receive_event_queue: Queue = Queue()
|
||||
self.send_req_queue: Queue = Queue()
|
||||
self.ws: Optional[WebSocketApp] = None
|
||||
self.subscription_id = "events-" + urlsafe_short_hash()[:32]
|
||||
self.running = False
|
||||
self._seen_events: OrderedDict[str, None] = OrderedDict()
|
||||
|
||||
@property
|
||||
def is_websocket_connected(self):
|
||||
if not self.ws:
|
||||
return False
|
||||
return self.ws.keep_running
|
||||
|
||||
async def connect(self) -> WebSocketApp:
|
||||
relay_endpoint = encrypt_internal_message("relay", urlsafe=True)
|
||||
ws_url = (
|
||||
f"ws://localhost:{settings.port}"
|
||||
f"/nostrclient/api/v1/{relay_endpoint}"
|
||||
)
|
||||
|
||||
logger.info("[EVENTS] Connecting to nostrclient WebSocket...")
|
||||
|
||||
def on_open(_):
|
||||
logger.info("[EVENTS] Connected to nostrclient WebSocket")
|
||||
|
||||
def on_message(_, message):
|
||||
try:
|
||||
self.receive_event_queue.put_nowait(message)
|
||||
except Exception as e:
|
||||
logger.error(f"[EVENTS] Failed to queue message: {e}")
|
||||
|
||||
def on_error(_, error):
|
||||
logger.warning(f"[EVENTS] WebSocket error: {error}")
|
||||
|
||||
def on_close(_, status_code, message):
|
||||
logger.warning(
|
||||
f"[EVENTS] WebSocket closed: {status_code} {message}"
|
||||
)
|
||||
self.receive_event_queue.put_nowait(
|
||||
ValueError("WebSocket closed")
|
||||
)
|
||||
|
||||
ws = WebSocketApp(
|
||||
ws_url,
|
||||
on_message=on_message,
|
||||
on_open=on_open,
|
||||
on_close=on_close,
|
||||
on_error=on_error,
|
||||
)
|
||||
|
||||
from threading import Thread
|
||||
|
||||
wst = Thread(target=ws.run_forever)
|
||||
wst.daemon = True
|
||||
wst.start()
|
||||
|
||||
return ws
|
||||
|
||||
async def run_forever(self):
|
||||
self.running = True
|
||||
while self.running:
|
||||
try:
|
||||
if not self.is_websocket_connected:
|
||||
self.ws = await self.connect()
|
||||
await asyncio.sleep(5)
|
||||
|
||||
req = await self.send_req_queue.get()
|
||||
assert self.ws
|
||||
self.ws.send(json.dumps(req))
|
||||
except Exception as ex:
|
||||
logger.warning(f"[EVENTS] NostrClient error: {ex}")
|
||||
await asyncio.sleep(60)
|
||||
|
||||
def is_duplicate_event(self, event_id: str) -> bool:
|
||||
"""Check if an event has been seen recently."""
|
||||
if event_id in self._seen_events:
|
||||
return True
|
||||
self._seen_events[event_id] = None
|
||||
if len(self._seen_events) > MAX_SEEN_EVENTS:
|
||||
self._seen_events.popitem(last=False)
|
||||
return False
|
||||
|
||||
async def get_event(self):
|
||||
"""Get next event from the receive queue."""
|
||||
value = await self.receive_event_queue.get()
|
||||
if isinstance(value, ValueError):
|
||||
raise value
|
||||
return value
|
||||
|
||||
async def publish_nostr_event(self, e: NostrEvent):
|
||||
await self.send_req_queue.put(["EVENT", e.dict()])
|
||||
|
||||
async def subscribe(self, filters: list[dict]):
|
||||
"""Subscribe to events matching the given filters."""
|
||||
self.subscription_id = "events-" + urlsafe_short_hash()[:32]
|
||||
await self.send_req_queue.put(
|
||||
["REQ", self.subscription_id] + filters
|
||||
)
|
||||
logger.info(
|
||||
f"[EVENTS] Subscribed to NIP-52 events "
|
||||
f"(sub: {self.subscription_id[:20]}...)"
|
||||
)
|
||||
|
||||
async def unsubscribe(self):
|
||||
"""Unsubscribe from current subscription."""
|
||||
await self.send_req_queue.put(["CLOSE", self.subscription_id])
|
||||
|
||||
async def stop(self):
|
||||
await self.unsubscribe()
|
||||
self.running = False
|
||||
await asyncio.sleep(2)
|
||||
if self.ws:
|
||||
try:
|
||||
self.ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.ws = None
|
||||
119
nostr_publisher.py
Normal file
119
nostr_publisher.py
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
"""
|
||||
NIP-52 calendar event publishing for the events extension.
|
||||
|
||||
Builds kind 31922 (date-based) calendar events from the Event model,
|
||||
signs them with the event creator's Account keypair, and publishes
|
||||
via the NostrClient to nostrclient relays.
|
||||
|
||||
Reference: https://github.com/nostr-protocol/nips/blob/master/52.md
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import coincurve
|
||||
from loguru import logger
|
||||
|
||||
from .models import Event
|
||||
from .nostr.event import NostrEvent
|
||||
|
||||
|
||||
def build_nip52_event(event: Event, pubkey: str) -> NostrEvent:
|
||||
"""
|
||||
Convert an Event model to a NIP-52 kind 31922 (date-based) calendar event.
|
||||
|
||||
Tags:
|
||||
d - event.id (addressable identifier)
|
||||
title - event.name
|
||||
start - event.event_start_date (ISO date string)
|
||||
end - event.event_end_date (optional)
|
||||
image - event.banner (optional)
|
||||
Content: event.info (description)
|
||||
"""
|
||||
tags = [
|
||||
["d", event.id],
|
||||
["title", event.name],
|
||||
["start", event.event_start_date],
|
||||
]
|
||||
|
||||
if event.event_end_date:
|
||||
tags.append(["end", event.event_end_date])
|
||||
if event.banner:
|
||||
tags.append(["image", event.banner])
|
||||
if event.location:
|
||||
tags.append(["location", event.location])
|
||||
for cat in (event.categories or []):
|
||||
tags.append(["t", cat])
|
||||
|
||||
nostr_event = NostrEvent(
|
||||
pubkey=pubkey,
|
||||
created_at=int(time.time()),
|
||||
kind=31922,
|
||||
tags=tags,
|
||||
content=event.info or "",
|
||||
)
|
||||
nostr_event.id = nostr_event.event_id
|
||||
return nostr_event
|
||||
|
||||
|
||||
def build_nip52_delete_event(event: Event, pubkey: str) -> NostrEvent:
|
||||
"""
|
||||
Build a kind 5 delete event for a published NIP-52 calendar event.
|
||||
|
||||
Uses an 'a' tag to reference the parameterized replaceable event
|
||||
(kind 31922) per NIP-09.
|
||||
"""
|
||||
nostr_event = NostrEvent(
|
||||
pubkey=pubkey,
|
||||
created_at=int(time.time()),
|
||||
kind=5,
|
||||
tags=[
|
||||
["a", f"31922:{pubkey}:{event.id}"],
|
||||
],
|
||||
content="Event canceled",
|
||||
)
|
||||
nostr_event.id = nostr_event.event_id
|
||||
return nostr_event
|
||||
|
||||
|
||||
def sign_nostr_event(nostr_event: NostrEvent, private_key_hex: str) -> None:
|
||||
"""Sign a NostrEvent in-place using Schnorr signature."""
|
||||
privkey = coincurve.PrivateKey(bytes.fromhex(private_key_hex))
|
||||
sig = privkey.sign_schnorr(bytes.fromhex(nostr_event.id))
|
||||
nostr_event.sig = sig.hex()
|
||||
|
||||
|
||||
async def publish_event_to_nostr(
|
||||
nostr_client,
|
||||
event: Event,
|
||||
account_pubkey: str,
|
||||
account_prvkey: str,
|
||||
delete: bool = False,
|
||||
) -> Optional[NostrEvent]:
|
||||
"""
|
||||
Build, sign, and publish a NIP-52 calendar event (or delete event).
|
||||
|
||||
Returns the published NostrEvent for metadata storage, or None on failure.
|
||||
"""
|
||||
if not nostr_client:
|
||||
logger.debug("[EVENTS] No NostrClient available, skipping publish")
|
||||
return None
|
||||
|
||||
try:
|
||||
if delete:
|
||||
nostr_event = build_nip52_delete_event(event, account_pubkey)
|
||||
else:
|
||||
nostr_event = build_nip52_event(event, account_pubkey)
|
||||
|
||||
sign_nostr_event(nostr_event, account_prvkey)
|
||||
await nostr_client.publish_nostr_event(nostr_event)
|
||||
|
||||
logger.info(
|
||||
f"[EVENTS] Published NIP-52 {'delete' if delete else 'calendar'} "
|
||||
f"event: {nostr_event.id[:16]}... (kind {nostr_event.kind})"
|
||||
)
|
||||
return nostr_event
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"[EVENTS] Failed to publish to Nostr: {e}")
|
||||
return None
|
||||
170
nostr_sync.py
Normal file
170
nostr_sync.py
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
"""
|
||||
Bidirectional Nostr sync for the events extension.
|
||||
|
||||
Subscribes to NIP-52 calendar events (kind 31922/31923) from relays
|
||||
and upserts them into the local database. Enables federated event
|
||||
discovery — events published by other LNbits instances or Nostr
|
||||
clients appear in the local events listing.
|
||||
"""
|
||||
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from .crud import create_event, db, get_event, update_event
|
||||
from .models import CreateEvent, Event
|
||||
from .nostr.nostr_client import NostrClient
|
||||
|
||||
|
||||
async def process_nostr_message(nostr_client: NostrClient, message: str):
|
||||
"""Process an incoming Nostr relay message."""
|
||||
try:
|
||||
data = json.loads(message)
|
||||
except json.JSONDecodeError:
|
||||
return
|
||||
|
||||
if not isinstance(data, list) or len(data) < 2:
|
||||
return
|
||||
|
||||
msg_type = data[0]
|
||||
|
||||
if msg_type == "EVENT" and len(data) >= 3:
|
||||
event_data = data[2]
|
||||
await _handle_calendar_event(nostr_client, event_data)
|
||||
elif msg_type == "EOSE":
|
||||
logger.debug("[EVENTS] End of stored events from relay")
|
||||
elif msg_type == "NOTICE":
|
||||
logger.info(f"[EVENTS] Relay notice: {data[1]}")
|
||||
|
||||
|
||||
async def _handle_calendar_event(nostr_client: NostrClient, event_data: dict):
|
||||
"""Handle an incoming NIP-52 calendar event (kind 31922 or 31923)."""
|
||||
kind = event_data.get("kind")
|
||||
if kind not in (31922, 31923):
|
||||
return
|
||||
|
||||
event_id = event_data.get("id", "")
|
||||
if nostr_client.is_duplicate_event(event_id):
|
||||
return
|
||||
|
||||
tags = {t[0]: t[1] for t in event_data.get("tags", []) if len(t) >= 2}
|
||||
tag_lists = {}
|
||||
for t in event_data.get("tags", []):
|
||||
if len(t) >= 2:
|
||||
tag_lists.setdefault(t[0], []).append(t[1])
|
||||
|
||||
d_tag = tags.get("d")
|
||||
if not d_tag:
|
||||
return
|
||||
|
||||
title = tags.get("title", "Untitled Event")
|
||||
start = tags.get("start")
|
||||
if not start:
|
||||
return
|
||||
|
||||
end = tags.get("end")
|
||||
description = event_data.get("content", "")
|
||||
image = tags.get("image")
|
||||
location = tags.get("location")
|
||||
categories = tag_lists.get("t", [])
|
||||
|
||||
# Check if we already have this event (by d-tag as our event ID
|
||||
# or by nostr_event_id)
|
||||
existing = await get_event(d_tag)
|
||||
if not existing:
|
||||
# Check by nostr_event_id
|
||||
existing = await db.fetchone(
|
||||
"SELECT * FROM events.events WHERE nostr_event_id = :nid",
|
||||
{"nid": event_id},
|
||||
Event,
|
||||
)
|
||||
|
||||
if existing:
|
||||
# Update if the incoming event is newer
|
||||
incoming_created_at = event_data.get("created_at", 0)
|
||||
if (
|
||||
existing.nostr_event_created_at
|
||||
and incoming_created_at <= existing.nostr_event_created_at
|
||||
):
|
||||
return # We already have a newer version
|
||||
|
||||
existing.name = title
|
||||
existing.info = description
|
||||
existing.event_start_date = start
|
||||
existing.event_end_date = end
|
||||
existing.banner = image
|
||||
existing.location = location
|
||||
existing.categories = categories
|
||||
existing.nostr_event_id = event_id
|
||||
existing.nostr_event_created_at = incoming_created_at
|
||||
await update_event(existing)
|
||||
logger.info(f"[EVENTS] Updated event from Nostr: {title}")
|
||||
else:
|
||||
# Create new event from Nostr
|
||||
# Events discovered from Nostr are auto-approved (they're already public)
|
||||
event = CreateEvent(
|
||||
wallet="", # No wallet — discovered from Nostr, not ticketed locally
|
||||
name=title,
|
||||
info=description,
|
||||
event_start_date=start,
|
||||
event_end_date=end,
|
||||
banner=image,
|
||||
location=location,
|
||||
categories=categories,
|
||||
status="approved",
|
||||
)
|
||||
# Use the d-tag as the event ID for correlation
|
||||
from lnbits.db import Database
|
||||
|
||||
new_event = Event(
|
||||
id=d_tag,
|
||||
wallet="",
|
||||
name=title,
|
||||
info=description,
|
||||
event_start_date=start,
|
||||
event_end_date=end,
|
||||
banner=image,
|
||||
location=location,
|
||||
categories=categories,
|
||||
status="approved",
|
||||
time=datetime.now(timezone.utc),
|
||||
nostr_event_id=event_id,
|
||||
nostr_event_created_at=event_data.get("created_at", 0),
|
||||
)
|
||||
try:
|
||||
await db.insert("events.events", new_event)
|
||||
logger.info(f"[EVENTS] Discovered event from Nostr: {title}")
|
||||
except Exception as e:
|
||||
# Likely duplicate key — skip
|
||||
logger.debug(f"[EVENTS] Skipped duplicate event: {e}")
|
||||
|
||||
|
||||
async def wait_for_nostr_events(nostr_client: NostrClient):
|
||||
"""
|
||||
Background task: subscribe to NIP-52 events and process them.
|
||||
"""
|
||||
logger.info("[EVENTS] Starting Nostr event sync...")
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Subscribe to NIP-52 calendar events
|
||||
await nostr_client.subscribe([
|
||||
{"kinds": [31922, 31923]},
|
||||
])
|
||||
|
||||
# Process incoming events
|
||||
while True:
|
||||
message = await nostr_client.get_event()
|
||||
await process_nostr_message(nostr_client, message)
|
||||
|
||||
except ValueError:
|
||||
# WebSocket closed — will reconnect
|
||||
logger.warning("[EVENTS] Nostr connection lost, resubscribing...")
|
||||
await asyncio.sleep(10)
|
||||
except Exception as e:
|
||||
logger.error(f"[EVENTS] Nostr sync error: {e}")
|
||||
await asyncio.sleep(30)
|
||||
|
||||
|
||||
import asyncio # noqa: E402
|
||||
|
|
@ -7,8 +7,11 @@ authors = [{ name = "Alan Bits", email = "alan@lnbits.com" }]
|
|||
urls = { Homepage = "https://lnbits.com", Repository = "https://github.com/lnbits/events" }
|
||||
dependencies = [ "lnbits>1" ]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
[tool.poetry]
|
||||
package-mode = false
|
||||
|
||||
[tool.uv]
|
||||
dev-dependencies = [
|
||||
"black",
|
||||
"pytest-asyncio",
|
||||
"pytest",
|
||||
|
|
@ -17,9 +20,6 @@ dev = [
|
|||
"ruff",
|
||||
]
|
||||
|
||||
[tool.poetry]
|
||||
package-mode = false
|
||||
|
||||
[tool.mypy]
|
||||
exclude = "(nostr/*)"
|
||||
plugins = ["pydantic.mypy"]
|
||||
|
|
|
|||
|
|
@ -11,6 +11,12 @@ window.app = Vue.createApp({
|
|||
data() {
|
||||
return {
|
||||
events: [],
|
||||
allUserEvents: [],
|
||||
pendingEvents: [],
|
||||
isAdmin: false,
|
||||
settings: {
|
||||
auto_approve: false
|
||||
},
|
||||
tickets: [],
|
||||
currencies: [],
|
||||
eventsTable: {
|
||||
|
|
@ -73,7 +79,8 @@ window.app = Vue.createApp({
|
|||
field: 'sold'
|
||||
},
|
||||
{name: 'info', align: 'left', label: 'Info', field: 'info'},
|
||||
{name: 'banner', align: 'left', label: 'Banner', field: 'banner'}
|
||||
{name: 'banner', align: 'left', label: 'Banner', field: 'banner'},
|
||||
{name: 'status', align: 'left', label: 'Status', field: 'status'}
|
||||
],
|
||||
pagination: {
|
||||
rowsPerPage: 10
|
||||
|
|
@ -113,6 +120,73 @@ window.app = Vue.createApp({
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
getSettings() {
|
||||
LNbits.api
|
||||
.request('GET', '/events/api/v1/settings')
|
||||
.then(response => {
|
||||
this.settings = response.data
|
||||
})
|
||||
.catch(() => {
|
||||
// Not admin or settings not available
|
||||
})
|
||||
},
|
||||
saveSettings() {
|
||||
LNbits.api
|
||||
.request('PUT', '/events/api/v1/settings', null, this.settings)
|
||||
.then(() => {
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: 'Settings saved'
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
LNbits.utils.notifyApiError(err)
|
||||
})
|
||||
},
|
||||
approveEvent(eventId) {
|
||||
LNbits.utils
|
||||
.confirmDialog('Approve this event?')
|
||||
.onOk(() => {
|
||||
LNbits.api
|
||||
.request(
|
||||
'PUT',
|
||||
'/events/api/v1/events/' + eventId + '/approve'
|
||||
)
|
||||
.then(() => {
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: 'Event approved'
|
||||
})
|
||||
this.getEvents()
|
||||
this.getPendingEvents()
|
||||
})
|
||||
.catch(err => {
|
||||
LNbits.utils.notifyApiError(err)
|
||||
})
|
||||
})
|
||||
},
|
||||
rejectEvent(eventId) {
|
||||
LNbits.utils
|
||||
.confirmDialog('Reject this event?')
|
||||
.onOk(() => {
|
||||
LNbits.api
|
||||
.request(
|
||||
'PUT',
|
||||
'/events/api/v1/events/' + eventId + '/reject'
|
||||
)
|
||||
.then(() => {
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: 'Event rejected'
|
||||
})
|
||||
this.getEvents()
|
||||
this.getPendingEvents()
|
||||
})
|
||||
.catch(err => {
|
||||
LNbits.utils.notifyApiError(err)
|
||||
})
|
||||
})
|
||||
},
|
||||
getTickets() {
|
||||
LNbits.api
|
||||
.request(
|
||||
|
|
@ -154,6 +228,7 @@ window.app = Vue.createApp({
|
|||
LNbits.utils.exportCSV(this.ticketsTable.columns, this.tickets)
|
||||
},
|
||||
getEvents() {
|
||||
// Always fetch own events
|
||||
LNbits.api
|
||||
.request(
|
||||
'GET',
|
||||
|
|
@ -166,6 +241,41 @@ window.app = Vue.createApp({
|
|||
})
|
||||
this.checkCanceledEvents()
|
||||
})
|
||||
|
||||
// Admin: also fetch all users' events
|
||||
LNbits.api
|
||||
.request(
|
||||
'GET',
|
||||
'/events/api/v1/events/all'
|
||||
)
|
||||
.then(response => {
|
||||
this.isAdmin = true
|
||||
// Exclude own events (already in this.events)
|
||||
const ownWalletIds = this.g.user.wallets.map(w => w.id)
|
||||
this.allUserEvents = response.data
|
||||
.filter(obj => !ownWalletIds.includes(obj.wallet))
|
||||
.map(obj => mapEvents(obj))
|
||||
})
|
||||
.catch(() => {
|
||||
this.isAdmin = false
|
||||
this.allUserEvents = []
|
||||
})
|
||||
},
|
||||
getPendingEvents() {
|
||||
LNbits.api
|
||||
.request(
|
||||
'GET',
|
||||
'/events/api/v1/events/pending'
|
||||
)
|
||||
.then(response => {
|
||||
this.pendingEvents = response.data.map(obj => {
|
||||
return mapEvents(obj)
|
||||
})
|
||||
})
|
||||
.catch(() => {
|
||||
// Not an admin or no pending events
|
||||
this.pendingEvents = []
|
||||
})
|
||||
},
|
||||
sendEventData() {
|
||||
const wallet = _.findWhere(this.g.user.wallets, {
|
||||
|
|
@ -290,6 +400,8 @@ window.app = Vue.createApp({
|
|||
if (this.g.user.wallets.length) {
|
||||
this.getTickets()
|
||||
this.getEvents()
|
||||
this.getPendingEvents()
|
||||
this.getSettings()
|
||||
this.currencies = await LNbits.api.getCurrencies()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
8
tasks.py
8
tasks.py
|
|
@ -21,8 +21,12 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||
if not payment.extra or "events" != payment.extra.get("tag"):
|
||||
return
|
||||
|
||||
if not payment.extra.get("name") or not payment.extra.get("email"):
|
||||
logger.warning(f"Ticket {payment.payment_hash} missing name or email.")
|
||||
# Check if ticket has either name/email or user_id
|
||||
has_name_email = payment.extra.get("name") and payment.extra.get("email")
|
||||
has_user_id = payment.extra.get("user_id")
|
||||
|
||||
if not has_name_email and not has_user_id:
|
||||
logger.warning(f"Ticket {payment.payment_hash} missing name/email or user_id.")
|
||||
return
|
||||
|
||||
ticket = await get_ticket(payment.payment_hash)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,24 @@
|
|||
%} {% block page %}
|
||||
<div class="row q-col-gutter-md">
|
||||
<div class="col-12 col-md-8 col-lg-7 q-gutter-y-md">
|
||||
<!-- Settings (admin only) -->
|
||||
<q-card v-if="isAdmin">
|
||||
<q-card-section>
|
||||
<div class="row items-center justify-between">
|
||||
<div class="col">
|
||||
<span class="text-subtitle1">Settings</span>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<q-toggle
|
||||
v-model="settings.auto_approve"
|
||||
label="Auto-approve events"
|
||||
@update:model-value="saveSettings"
|
||||
></q-toggle>
|
||||
</div>
|
||||
</div>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<q-btn unelevated color="primary" @click="openEventDialog"
|
||||
|
|
@ -10,6 +28,58 @@
|
|||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<!-- Pending Event Approvals -->
|
||||
<q-card v-if="pendingEvents.length > 0">
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
<div class="col">
|
||||
<h5 class="text-subtitle1 q-my-none">
|
||||
<q-icon name="pending" color="orange" class="q-mr-sm"></q-icon>
|
||||
Pending Approvals
|
||||
<q-badge color="orange" :label="pendingEvents.length" class="q-ml-sm"></q-badge>
|
||||
</h5>
|
||||
</div>
|
||||
</div>
|
||||
<q-list separator>
|
||||
<q-item v-for="event in pendingEvents" :key="event.id">
|
||||
<q-item-section>
|
||||
<q-item-label v-text="event.name"></q-item-label>
|
||||
<q-item-label caption>
|
||||
<span v-text="event.event_start_date"></span>
|
||||
—
|
||||
<span v-text="event.info.substring(0, 80)"></span><span v-if="event.info.length > 80">...</span>
|
||||
</q-item-label>
|
||||
<q-item-label caption>
|
||||
<span v-text="event.amount_tickets"></span> tickets •
|
||||
<span v-text="event.price_per_ticket"></span> <span v-text="event.currency"></span>
|
||||
</q-item-label>
|
||||
</q-item-section>
|
||||
<q-item-section side>
|
||||
<div class="row q-gutter-sm">
|
||||
<q-btn
|
||||
dense
|
||||
color="green"
|
||||
icon="check_circle"
|
||||
label="Approve"
|
||||
size="sm"
|
||||
@click="approveEvent(event.id)"
|
||||
></q-btn>
|
||||
<q-btn
|
||||
dense
|
||||
outline
|
||||
color="red"
|
||||
icon="block"
|
||||
label="Reject"
|
||||
size="sm"
|
||||
@click="rejectEvent(event.id)"
|
||||
></q-btn>
|
||||
</div>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
</q-list>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
|
|
@ -76,9 +146,36 @@
|
|||
></q-btn>
|
||||
</q-td>
|
||||
<q-td v-for="col in props.cols" :key="col.name" :props="props">
|
||||
<span v-text="col.value"></span>
|
||||
<q-badge
|
||||
v-if="col.name === 'status'"
|
||||
:color="col.value === 'approved' ? 'green' : col.value === 'proposed' ? 'orange' : 'red'"
|
||||
:label="col.value"
|
||||
></q-badge>
|
||||
<span v-else v-text="col.value"></span>
|
||||
</q-td>
|
||||
<q-td auto-width>
|
||||
<q-btn
|
||||
v-if="isAdmin && props.row.status === 'proposed'"
|
||||
flat
|
||||
dense
|
||||
size="xs"
|
||||
@click="approveEvent(props.row.id)"
|
||||
icon="check_circle"
|
||||
color="green"
|
||||
>
|
||||
<q-tooltip>Approve</q-tooltip>
|
||||
</q-btn>
|
||||
<q-btn
|
||||
v-if="isAdmin && props.row.status === 'proposed'"
|
||||
flat
|
||||
dense
|
||||
size="xs"
|
||||
@click="rejectEvent(props.row.id)"
|
||||
icon="block"
|
||||
color="red"
|
||||
>
|
||||
<q-tooltip>Reject</q-tooltip>
|
||||
</q-btn>
|
||||
<q-btn
|
||||
flat
|
||||
dense
|
||||
|
|
@ -150,6 +247,48 @@
|
|||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<!-- All Users' Events (admin only) -->
|
||||
<q-card v-if="isAdmin && allUserEvents.length > 0">
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
<div class="col">
|
||||
<h5 class="text-subtitle1 q-my-none">
|
||||
All Users' Events
|
||||
<q-badge color="blue" :label="allUserEvents.length" class="q-ml-sm"></q-badge>
|
||||
</h5>
|
||||
</div>
|
||||
</div>
|
||||
<q-table
|
||||
dense
|
||||
flat
|
||||
:rows="allUserEvents"
|
||||
row-key="id"
|
||||
:columns="eventsTable.columns"
|
||||
:pagination="{rowsPerPage: 10}"
|
||||
>
|
||||
<template v-slot:header="props">
|
||||
<q-tr :props="props">
|
||||
<q-th v-for="col in props.cols" :key="col.name" :props="props">
|
||||
<span v-text="col.label"></span>
|
||||
</q-th>
|
||||
</q-tr>
|
||||
</template>
|
||||
<template v-slot:body="props">
|
||||
<q-tr :props="props">
|
||||
<q-td v-for="col in props.cols" :key="col.name" :props="props">
|
||||
<q-badge
|
||||
v-if="col.name === 'status'"
|
||||
:color="col.value === 'approved' ? 'green' : col.value === 'proposed' ? 'orange' : 'red'"
|
||||
:label="col.value"
|
||||
></q-badge>
|
||||
<span v-else v-text="col.value"></span>
|
||||
</q-td>
|
||||
</q-tr>
|
||||
</template>
|
||||
</q-table>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
|
|
|
|||
108
tests/test_api.py
Normal file
108
tests/test_api.py
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from ..views_api import events_api_router
|
||||
from ..models import Event
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_events_public():
|
||||
"""Test the new public events API endpoint"""
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(events_api_router)
|
||||
|
||||
# Mock the database
|
||||
with patch('events.crud.get_all_events') as mock_get_all_events:
|
||||
# Create mock events
|
||||
mock_events = [
|
||||
Event(
|
||||
id="test_event_1",
|
||||
wallet="test_wallet_1",
|
||||
name="Test Event 1",
|
||||
info="Test event description",
|
||||
closing_date="2024-12-31",
|
||||
event_start_date="2024-12-01",
|
||||
event_end_date="2024-12-02",
|
||||
currency="sat",
|
||||
amount_tickets=100,
|
||||
price_per_ticket=1000.0,
|
||||
time=datetime.now(timezone.utc),
|
||||
sold=0,
|
||||
banner=None
|
||||
),
|
||||
Event(
|
||||
id="test_event_2",
|
||||
wallet="test_wallet_2",
|
||||
name="Test Event 2",
|
||||
info="Another test event",
|
||||
closing_date="2024-12-31",
|
||||
event_start_date="2024-12-03",
|
||||
event_end_date="2024-12-04",
|
||||
currency="sat",
|
||||
amount_tickets=50,
|
||||
price_per_ticket=500.0,
|
||||
time=datetime.now(timezone.utc),
|
||||
sold=0,
|
||||
banner=None
|
||||
)
|
||||
]
|
||||
|
||||
mock_get_all_events.return_value = mock_events
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
# Test the endpoint without any authentication
|
||||
response = client.get("/api/v1/events/public")
|
||||
|
||||
# Verify the response
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert len(data) == 2
|
||||
assert data[0]["id"] == "test_event_1"
|
||||
assert data[1]["id"] == "test_event_2"
|
||||
assert data[0]["name"] == "Test Event 1"
|
||||
assert data[1]["name"] == "Test Event 2"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_all_events_crud():
|
||||
"""Test the get_all_events CRUD function"""
|
||||
from events.crud import get_all_events
|
||||
|
||||
with patch('events.crud.db.fetchall') as mock_fetchall:
|
||||
# Mock database response
|
||||
mock_events = [
|
||||
{
|
||||
"id": "test_event_1",
|
||||
"wallet": "test_wallet_1",
|
||||
"name": "Test Event 1",
|
||||
"info": "Test event description",
|
||||
"closing_date": "2024-12-31",
|
||||
"event_start_date": "2024-12-01",
|
||||
"event_end_date": "2024-12-02",
|
||||
"currency": "sat",
|
||||
"amount_tickets": 100,
|
||||
"price_per_ticket": 1000.0,
|
||||
"time": datetime.now(timezone.utc),
|
||||
"sold": 0,
|
||||
"banner": None
|
||||
}
|
||||
]
|
||||
|
||||
mock_fetchall.return_value = mock_events
|
||||
|
||||
events = await get_all_events()
|
||||
|
||||
# Verify the function was called with correct parameters
|
||||
mock_fetchall.assert_called_once_with(
|
||||
"SELECT * FROM events.events ORDER BY time DESC",
|
||||
model=Event,
|
||||
)
|
||||
|
||||
# Verify the result
|
||||
assert len(events) == 1
|
||||
assert events[0]["id"] == "test_event_1"
|
||||
276
views_api.py
276
views_api.py
|
|
@ -3,9 +3,10 @@ from http import HTTPStatus
|
|||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from lnbits.core.crud import get_standalone_payment, get_user
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.core.models import Account, WalletTypeInfo
|
||||
from lnbits.core.services import create_invoice
|
||||
from lnbits.decorators import (
|
||||
check_admin,
|
||||
require_admin_key,
|
||||
require_invoice_key,
|
||||
)
|
||||
|
|
@ -21,22 +22,53 @@ from .crud import (
|
|||
delete_event,
|
||||
delete_event_tickets,
|
||||
delete_ticket,
|
||||
get_all_events,
|
||||
get_event,
|
||||
get_event_tickets,
|
||||
get_events,
|
||||
get_pending_events,
|
||||
get_public_events,
|
||||
get_settings,
|
||||
get_ticket,
|
||||
get_tickets,
|
||||
get_tickets_by_user_id,
|
||||
# TODO: consider exposing purge_unpaid_tickets via an admin endpoint
|
||||
update_event,
|
||||
update_settings,
|
||||
update_ticket,
|
||||
)
|
||||
from .models import CreateEvent, CreateTicket, Ticket
|
||||
from .models import CreateEvent, CreateTicket, EventsSettings, Ticket
|
||||
from .nostr_publisher import publish_event_to_nostr
|
||||
from .services import refund_tickets, set_ticket_paid
|
||||
|
||||
events_api_router = APIRouter()
|
||||
|
||||
|
||||
async def _publish_or_delete_nostr_event(event, delete=False):
|
||||
"""Publish (or delete) a NIP-52 calendar event using the creator's keypair."""
|
||||
try:
|
||||
from lnbits.core.crud.wallets import get_wallet
|
||||
from lnbits.core.crud.users import get_account
|
||||
|
||||
from . import nostr_client
|
||||
|
||||
wallet_obj = await get_wallet(event.wallet)
|
||||
if not wallet_obj:
|
||||
return
|
||||
account = await get_account(wallet_obj.user)
|
||||
if not account or not account.pubkey or not account.prvkey:
|
||||
return
|
||||
|
||||
nostr_event = await publish_event_to_nostr(
|
||||
nostr_client, event, account.pubkey, account.prvkey, delete=delete
|
||||
)
|
||||
if nostr_event and not delete:
|
||||
event.nostr_event_id = nostr_event.id
|
||||
event.nostr_event_created_at = nostr_event.created_at
|
||||
await update_event(event)
|
||||
except Exception as e:
|
||||
logger.warning(f"[EVENTS] Nostr publish failed: {e}")
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/events")
|
||||
async def api_events(
|
||||
all_wallets: bool = Query(False),
|
||||
|
|
@ -53,18 +85,65 @@ async def api_events(
|
|||
|
||||
@events_api_router.get("/api/v1/events/public")
|
||||
async def api_events_public():
|
||||
"""Retrieve all events (read-only, no auth required)."""
|
||||
return [event.dict() for event in await get_all_events()]
|
||||
"""
|
||||
Retrieve approved, non-canceled events for public display.
|
||||
No authentication required.
|
||||
"""
|
||||
events = await get_public_events()
|
||||
return [event.dict() for event in events]
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/events/all")
|
||||
async def api_events_all(
|
||||
admin: Account = Depends(check_admin),
|
||||
):
|
||||
"""Get all events across all wallets. LNbits admin only."""
|
||||
from .crud import get_all_events
|
||||
|
||||
events = await get_all_events()
|
||||
return [event.dict() for event in events]
|
||||
|
||||
|
||||
@events_api_router.post("/api/v1/events")
|
||||
@events_api_router.put("/api/v1/events/{event_id}")
|
||||
async def api_event_create(
|
||||
data: CreateEvent,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
event_id: str | None = None,
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
):
|
||||
if event_id:
|
||||
"""
|
||||
Create a new event. Any authenticated user can create events.
|
||||
Admin-created events are auto-approved. Non-admin events require
|
||||
approval unless auto_approve is enabled in extension settings.
|
||||
"""
|
||||
if not data.wallet:
|
||||
data.wallet = wallet.wallet.id
|
||||
|
||||
from lnbits.settings import settings
|
||||
|
||||
ext_settings = await get_settings()
|
||||
user_id = wallet.wallet.user
|
||||
is_admin = (
|
||||
user_id == settings.super_user
|
||||
or user_id in settings.lnbits_admin_users
|
||||
)
|
||||
if not is_admin and not ext_settings.auto_approve:
|
||||
data.status = "proposed"
|
||||
|
||||
event = await create_event(data)
|
||||
|
||||
# Publish to Nostr if approved
|
||||
if event.status == "approved":
|
||||
await _publish_or_delete_nostr_event(event)
|
||||
|
||||
return event.dict()
|
||||
|
||||
|
||||
@events_api_router.put("/api/v1/events/{event_id}")
|
||||
async def api_event_update(
|
||||
event_id: str,
|
||||
data: CreateEvent,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
"""Update an existing event. Requires admin key (event owner)."""
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
|
|
@ -78,8 +157,10 @@ async def api_event_create(
|
|||
for k, v in data.dict().items():
|
||||
setattr(event, k, v)
|
||||
event = await update_event(event)
|
||||
else:
|
||||
event = await create_event(data)
|
||||
|
||||
# Republish to Nostr if event is approved (kind 31922 is replaceable)
|
||||
if event.status == "approved" and event.nostr_event_id:
|
||||
await _publish_or_delete_nostr_event(event)
|
||||
|
||||
return event.dict()
|
||||
|
||||
|
|
@ -101,6 +182,10 @@ async def api_event_cancel(
|
|||
event = await update_event(event)
|
||||
await refund_tickets(event.id)
|
||||
|
||||
# Delete NIP-52 event from Nostr if it was published
|
||||
if event.nostr_event_id:
|
||||
await _publish_or_delete_nostr_event(event, delete=True)
|
||||
|
||||
return event.dict()
|
||||
|
||||
|
||||
|
|
@ -117,11 +202,93 @@ async def api_form_delete(
|
|||
if event.wallet != wallet.wallet.id:
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your event.")
|
||||
|
||||
# Delete NIP-52 event from Nostr if it was published
|
||||
if event.nostr_event_id:
|
||||
await _publish_or_delete_nostr_event(event, delete=True)
|
||||
|
||||
await delete_event(event_id)
|
||||
await delete_event_tickets(event_id)
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
|
||||
#########Event Approval##########
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/events/pending")
|
||||
async def api_events_pending(
|
||||
admin: Account = Depends(check_admin),
|
||||
):
|
||||
"""Get all proposed events awaiting approval. LNbits admin only."""
|
||||
events = await get_pending_events()
|
||||
return [event.dict() for event in events]
|
||||
|
||||
|
||||
@events_api_router.put("/api/v1/events/{event_id}/approve")
|
||||
async def api_event_approve(
|
||||
event_id: str,
|
||||
admin: Account = Depends(check_admin),
|
||||
):
|
||||
"""Approve a proposed event. LNbits admin only."""
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Event does not exist."
|
||||
)
|
||||
if event.status != "proposed":
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=f"Event is already {event.status}.",
|
||||
)
|
||||
event.status = "approved"
|
||||
event = await update_event(event)
|
||||
|
||||
# Publish NIP-52 calendar event to Nostr
|
||||
await _publish_or_delete_nostr_event(event)
|
||||
|
||||
return event.dict()
|
||||
|
||||
|
||||
@events_api_router.put("/api/v1/events/{event_id}/reject")
|
||||
async def api_event_reject(
|
||||
event_id: str,
|
||||
admin: Account = Depends(check_admin),
|
||||
):
|
||||
"""Reject a proposed event. LNbits admin only."""
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Event does not exist."
|
||||
)
|
||||
if event.status != "proposed":
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=f"Event is already {event.status}.",
|
||||
)
|
||||
event.status = "rejected"
|
||||
event = await update_event(event)
|
||||
return event.dict()
|
||||
|
||||
|
||||
#########Settings##########
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/settings")
|
||||
async def api_get_settings(
|
||||
admin: Account = Depends(check_admin),
|
||||
) -> EventsSettings:
|
||||
"""Get extension settings. LNbits admin only."""
|
||||
return await get_settings()
|
||||
|
||||
|
||||
@events_api_router.put("/api/v1/settings")
|
||||
async def api_update_settings(
|
||||
data: EventsSettings,
|
||||
admin: Account = Depends(check_admin),
|
||||
) -> EventsSettings:
|
||||
"""Update extension settings. LNbits admin only."""
|
||||
return await update_settings(data)
|
||||
|
||||
|
||||
#########Tickets##########
|
||||
|
||||
|
||||
|
|
@ -141,7 +308,7 @@ async def api_tickets(
|
|||
|
||||
@events_api_router.get("/api/v1/tickets/user/{user_id}")
|
||||
async def api_tickets_by_user_id(user_id: str) -> list[Ticket]:
|
||||
"""Get all tickets for a specific user by their user_id."""
|
||||
"""Get all tickets for a specific user by their user_id"""
|
||||
return await get_tickets_by_user_id(user_id)
|
||||
|
||||
|
||||
|
|
@ -149,6 +316,7 @@ async def api_tickets_by_user_id(user_id: str) -> list[Ticket]:
|
|||
async def api_ticket_create(event_id: str, data: CreateTicket):
|
||||
if data.user_id:
|
||||
return await api_ticket_make_ticket_with_user_id(event_id, data.user_id)
|
||||
else:
|
||||
promo_code = data.promo_code.upper() if data.promo_code else None
|
||||
refund_address = data.refund_address
|
||||
return await api_ticket_make_ticket(
|
||||
|
|
@ -156,8 +324,53 @@ async def api_ticket_create(event_id: str, data: CreateTicket):
|
|||
)
|
||||
|
||||
|
||||
async def api_ticket_make_ticket_with_user_id(event_id: str, user_id: str):
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Event does not exist."
|
||||
)
|
||||
|
||||
price = event.price_per_ticket
|
||||
extra = {"tag": "events", "user_id": user_id}
|
||||
|
||||
if event.currency != "sats":
|
||||
price = await fiat_amount_as_satoshis(event.price_per_ticket, event.currency)
|
||||
|
||||
extra["fiat"] = True
|
||||
extra["currency"] = event.currency
|
||||
extra["fiatAmount"] = event.price_per_ticket
|
||||
extra["rate"] = await get_fiat_rate_satoshis(event.currency)
|
||||
|
||||
try:
|
||||
payment = await create_invoice(
|
||||
wallet_id=event.wallet,
|
||||
amount=price,
|
||||
memo=f"{event_id}",
|
||||
extra=extra,
|
||||
)
|
||||
await create_ticket(
|
||||
payment_hash=payment.payment_hash,
|
||||
wallet=event.wallet,
|
||||
event=event.id,
|
||||
user_id=user_id,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
|
||||
) from exc
|
||||
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/tickets/{event_id}/user/{user_id}")
|
||||
async def api_ticket_make_ticket_user_id(event_id: str, user_id: str):
|
||||
return await api_ticket_make_ticket_with_user_id(event_id, user_id)
|
||||
|
||||
|
||||
@events_api_router.get("/api/v1/tickets/{event_id}/{name}/{email}")
|
||||
async def api_ticket_make_ticket(event_id, name, email, promo_code, refund_address):
|
||||
async def api_ticket_make_ticket(
|
||||
event_id, name, email, promo_code=None, refund_address=None
|
||||
):
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
|
|
@ -212,43 +425,6 @@ async def api_ticket_make_ticket(event_id, name, email, promo_code, refund_addre
|
|||
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}
|
||||
|
||||
|
||||
async def api_ticket_make_ticket_with_user_id(event_id: str, user_id: str):
|
||||
event = await get_event(event_id)
|
||||
if not event:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Event does not exist."
|
||||
)
|
||||
|
||||
price = event.price_per_ticket
|
||||
extra = {"tag": "events", "user_id": user_id}
|
||||
|
||||
if event.currency != "sats":
|
||||
extra["fiat"] = True
|
||||
extra["currency"] = event.currency
|
||||
extra["fiatAmount"] = event.price_per_ticket
|
||||
extra["rate"] = await get_fiat_rate_satoshis(event.currency)
|
||||
price = await fiat_amount_as_satoshis(event.price_per_ticket, event.currency)
|
||||
|
||||
try:
|
||||
payment = await create_invoice(
|
||||
wallet_id=event.wallet,
|
||||
amount=price,
|
||||
memo=f"{event_id}",
|
||||
extra=extra,
|
||||
)
|
||||
await create_ticket(
|
||||
payment_hash=payment.payment_hash,
|
||||
wallet=event.wallet,
|
||||
event=event.id,
|
||||
user_id=user_id,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
|
||||
) from exc
|
||||
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}
|
||||
|
||||
|
||||
@events_api_router.post("/api/v1/tickets/{event_id}/{payment_hash}")
|
||||
async def api_ticket_send_ticket(event_id, payment_hash):
|
||||
event = await get_event(event_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue