Skip to content

Commit f5a44b7

Browse files
authored
Merge pull request #1 from lnbits/lnurlp
feat: Added lnurlp
2 parents be6dd31 + 70ebb05 commit f5a44b7

14 files changed

Lines changed: 1094 additions & 199 deletions

__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from .tasks import cleanup_empty_chats, wait_for_paid_invoices
99
from .views import chat_generic_router
1010
from .views_api import chat_api_router
11+
from .views_lnurl import chat_lnurl_router
1112

1213
chat_ext: APIRouter = APIRouter(prefix="/chat", tags=["Chat"])
13-
chat_ext.include_router(chat_generic_router)
14+
chat_ext.include_router(chat_lnurl_router)
1415
chat_ext.include_router(chat_api_router)
16+
chat_ext.include_router(chat_generic_router)
1517

1618

1719
chat_static_files = [

helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import re
22

3+
from fastapi import Request
4+
from lnurl import encode as lnurl_encode
5+
36

47
def is_valid_email_address(email: str) -> bool:
58
email_regex = r"[A-Za-z0-9\._%+-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,63}"
69
return re.fullmatch(email_regex, email) is not None
10+
11+
12+
def chat_lnurl_url(req: Request, chat_id: str) -> str:
13+
url = req.url_for("chat.api_lnurl_response", chat_id=chat_id)
14+
url = url.replace(path=url.path)
15+
url_str = str(url)
16+
if url.netloc.endswith(".onion"):
17+
url_str = url_str.replace("https://", "http://")
18+
return url_str
19+
20+
21+
def lnurl_encode_chat(req: Request, chat_id: str) -> str:
22+
return str(lnurl_encode(chat_lnurl_url(req, chat_id)).bech32)

migrations.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,49 @@ async def m007_chats_public_url(db):
127127
ALTER TABLE chat.chats ADD COLUMN public_url TEXT;
128128
"""
129129
)
130+
131+
132+
async def m008_chat_lnurlp_balance(db):
133+
"""
134+
Add lnurlp toggle to categories and balance to chats.
135+
"""
136+
137+
await db.execute(
138+
"""
139+
ALTER TABLE chat.categories ADD COLUMN lnurlp BOOLEAN DEFAULT 0;
140+
"""
141+
)
142+
await db.execute(
143+
f"""
144+
ALTER TABLE chat.chats ADD COLUMN balance {db.big_int} DEFAULT 0;
145+
"""
146+
)
147+
148+
149+
async def m009_chat_claims(db):
150+
"""
151+
Add chat claim fields.
152+
"""
153+
154+
await db.execute(
155+
"""
156+
ALTER TABLE chat.chats ADD COLUMN claimed_by_id TEXT;
157+
"""
158+
)
159+
await db.execute(
160+
"""
161+
ALTER TABLE chat.chats ADD COLUMN claimed_by_name TEXT;
162+
"""
163+
)
164+
165+
166+
async def m010_chat_claim_split(db):
167+
"""
168+
Add claim split percentage to categories.
169+
"""
170+
171+
await db.execute(
172+
"""
173+
ALTER TABLE chat.categories ADD COLUMN claim_split REAL DEFAULT 0;
174+
"""
175+
)

models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class CreateCategories(BaseModel):
88
name: str
99
wallet: str | None = None
1010
paid: bool | None = False
11+
lnurlp: bool | None = False
1112
tips: bool | None = False
1213
chars: int | None = None
1314
price_chars: float | None = None
1415
denomination: str | None = "sat"
16+
claim_split: float | None = 0
1517
notify_telegram: str | None = None
1618
notify_nostr: str | None = None
1719
notify_email: str | None = None
@@ -23,10 +25,12 @@ class Categories(BaseModel):
2325
name: str
2426
wallet: str | None = None
2527
paid: bool | None = False
28+
lnurlp: bool | None = False
2629
tips: bool | None = False
2730
chars: int | None = None
2831
price_chars: float | None = None
2932
denomination: str | None = "sat"
33+
claim_split: float | None = 0
3034
notify_telegram: str | None = None
3135
notify_nostr: str | None = None
3236
notify_email: str | None = None
@@ -39,29 +43,35 @@ class PublicCategories(BaseModel):
3943
id: str
4044
name: str
4145
paid: bool | None = False
46+
lnurlp: bool | None = False
4247
tips: bool | None = False
4348
chars: int | None = None
4449
price_chars: float | None = None
4550
denomination: str | None = "sat"
51+
claim_split: float | None = 0
4652

4753

4854
class CategoriesFilters(FilterModel):
4955
__search_fields__ = [
5056
"name",
5157
"paid",
58+
"lnurlp",
5259
"tips",
5360
"chars",
5461
"price_chars",
5562
"denomination",
63+
"claim_split",
5664
]
5765

5866
__sort_fields__ = [
5967
"name",
6068
"paid",
69+
"lnurlp",
6170
"tips",
6271
"chars",
6372
"price_chars",
6473
"denomination",
74+
"claim_split",
6575
"created_at",
6676
"updated_at",
6777
]
@@ -98,6 +108,9 @@ class ChatSession(BaseModel):
98108
resolved: bool = False
99109
unread: bool = True
100110
public_url: str | None = None
111+
balance: int = 0
112+
claimed_by_id: str | None = None
113+
claimed_by_name: str | None = None
101114
participants: list[dict] = Field(default_factory=list)
102115
messages: list[dict] = Field(default_factory=list)
103116
last_message_at: datetime | None = None

0 commit comments

Comments
 (0)