Skip to content

Commit

Permalink
fix: settings
Browse files Browse the repository at this point in the history
  • Loading branch information
motorina0 committed Oct 29, 2024
1 parent b18b0f3 commit 7870f96
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
18 changes: 13 additions & 5 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
IdentifierRanking,
Nip5Settings,
PublicDomain,
UserSetting,
)

db = Database("ext_nostrnip5")
Expand Down Expand Up @@ -304,14 +305,21 @@ async def delete_inferior_ranking(name: str, rank: int):
)


async def create_settings(settings: Nip5Settings):
assert settings.owner_id, "Missing owner ID."
await db.insert("nostrnip5.settings", settings)
async def create_settings(settings: UserSetting) -> UserSetting:
user_settings = await get_settings(settings.owner_id)
if not user_settings:
await db.insert("nostrnip5.settings", settings)
else:
await db.update("nostrnip5.settings", settings, "WHERE owner_id = :owner_id")
return settings


async def get_settings(owner_id: str) -> Optional[Nip5Settings]:
return await db.fetchone(
user_settings = await db.fetchone(
"SELECT * FROM nostrnip5.settings WHERE owner_id = :owner_id",
{"owner_id": owner_id},
Nip5Settings,
UserSetting,
)
if user_settings:
return user_settings.settings
return None
3 changes: 0 additions & 3 deletions migrations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

from lnbits.db import Database


Expand Down Expand Up @@ -102,4 +100,3 @@ async def m007_add_cost_extra_column_to_addresses(db: Database):
"ALTER TABLE nostrnip5.addresses ADD COLUMN "
"reimburse_amount REAL NOT NULL DEFAULT 0"
)

6 changes: 5 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ class AddressFilters(FilterModel):


class Nip5Settings(BaseModel):
owner_id: Optional[str] = None
cloudflare_access_token: Optional[str] = None
lnaddress_api_admin_key: Optional[str] = ""
lnaddress_api_endpoint: Optional[str] = "https://nostr.com"


class UserSetting(BaseModel):
owner_id: str
settings: Nip5Settings
2 changes: 1 addition & 1 deletion static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mapDomain = function (obj) {
obj.time = Quasar.utils.date.formatDate(
obj.time = Quasar.date.formatDate(
new Date(obj.time * 1000),
'YYYY-MM-DD HH:mm'
)
Expand Down
10 changes: 5 additions & 5 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
Nip5Settings,
RotateAddressData,
UpdateAddressData,
UserSetting,
)
from .services import (
activate_address,
Expand Down Expand Up @@ -609,8 +610,8 @@ async def api_settings_create_or_update(
user: User = Depends(check_user_exists),
):
owner_id = owner_id_from_user_id("admin" if user.admin else user.id)
settings.owner_id = owner_id
await create_settings(settings)
user_settings = UserSetting(owner_id=owner_id, settings=settings)
await create_settings(user_settings)


@nostrnip5_api_router.get("/api/v1/settings")
Expand All @@ -619,6 +620,5 @@ async def api_get_settings(
) -> Nip5Settings:
owner_id = owner_id_from_user_id("admin" if user.admin else user.id)
nip5_settings = await get_settings(owner_id)
if not nip5_settings:
raise HTTPException(HTTPStatus.NOT_FOUND, "Settings for user not found.")
return nip5_settings

return nip5_settings or Nip5Settings()

0 comments on commit 7870f96

Please sign in to comment.