Making universal tinyurl function
This commit is contained in:
parent
492eda5fdc
commit
2b3a2afe83
|
@ -9,7 +9,7 @@ from lnbits.db import COCKROACH, POSTGRES, Connection
|
|||
from lnbits.settings import AdminSettings, EditableSettings, SuperSettings, settings
|
||||
|
||||
from . import db
|
||||
from .models import BalanceCheck, Payment, User, Wallet
|
||||
from .models import BalanceCheck, Payment, TinyURL, User, Wallet
|
||||
|
||||
# accounts
|
||||
# --------
|
||||
|
@ -620,3 +620,30 @@ async def create_admin_settings(super_user: str, new_settings: dict):
|
|||
sql = f"INSERT INTO settings (super_user, editable_settings) VALUES (?, ?)"
|
||||
await db.execute(sql, (super_user, json.dumps(new_settings)))
|
||||
return await get_super_settings()
|
||||
|
||||
|
||||
# tinyurl
|
||||
# -------
|
||||
|
||||
|
||||
async def create_tinyurl(tiny_url: str):
|
||||
tinyurl_id = uuid4().hex[:8]
|
||||
await (conn or db).execute(
|
||||
"""
|
||||
INSERT INTO tiny_url (id, url) VALUES (?, ?)
|
||||
""",
|
||||
(tinyurl_id, domain),
|
||||
)
|
||||
return await get_tinyurl(tinyurl_id)
|
||||
|
||||
|
||||
async def get_tinyurl(tinyurl_id: str) -> Optional[BalanceCheck]:
|
||||
row = await (conn or db).fetchone(
|
||||
"""
|
||||
SELECT *
|
||||
FROM tiny_url
|
||||
WHERE id = ?
|
||||
""",
|
||||
(tinyurl_id),
|
||||
)
|
||||
return TinyURL.from_row(row) if row else None
|
||||
|
|
|
@ -269,3 +269,12 @@ async def m008_create_admin_settings_table(db):
|
|||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS tinyurl (
|
||||
id TEXT PRIMARY KEY,
|
||||
url TEXT,
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
|
|
@ -213,3 +213,12 @@ class BalanceCheck(BaseModel):
|
|||
@classmethod
|
||||
def from_row(cls, row: Row):
|
||||
return cls(wallet=row["wallet"], service=row["service"], url=row["url"])
|
||||
|
||||
|
||||
class TinyURL(BaseModel):
|
||||
id: str
|
||||
url: str
|
||||
|
||||
@classmethod
|
||||
def from_row(cls, row: Row) -> "TinyURL":
|
||||
return cls(**dict(row))
|
||||
|
|
|
@ -706,3 +706,26 @@ async def websocket_update_get(item_id: str, data: str):
|
|||
return {"sent": True, "data": data}
|
||||
except:
|
||||
return {"sent": False, "data": data}
|
||||
|
||||
|
||||
############################TINYURL##################################
|
||||
|
||||
|
||||
@core_app.post("/api/v1/tinyurl")
|
||||
async def api_create_tinyurl(url: str):
|
||||
return await create_tinyurl(url)
|
||||
|
||||
|
||||
@core_app.get("/api/v1/tinyurl/{tinyurl_id}")
|
||||
async def api_get_tinyurl(tinyurl_id: str):
|
||||
return await get_tinyurl(tinyurl_id)
|
||||
|
||||
|
||||
@core_app.get("/{tinyurl_id}")
|
||||
async def api_tinyurl(tinyurl_id: str):
|
||||
tinyurl = await get_tinyurl(tinyurl_id)
|
||||
if tinyurl:
|
||||
response = RedirectResponse(url=tinyurl.url)
|
||||
return response
|
||||
else:
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue
Block a user