diff options
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/hub_settings.py')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/hub_settings.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/hub_settings.py b/packages/meshbay-hub/src/meshbay_hub/hub_settings.py new file mode 100644 index 0000000..280d1e2 --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/hub_settings.py @@ -0,0 +1,44 @@ +""" +Instance-wide settings stored in the `hub_settings` table. + +One reader per concern, so callers never touch raw strings or key names. A +missing row means the built-in default — an upgrade never changes behaviour on +its own, and a downgrade that drops the table just returns to defaults. +""" + +from sqlalchemy.ext.asyncio import AsyncSession + +from meshbay_hub.db.models import HubSetting + +# Whether a member may create a group that is listed in the public directory and +# open for anyone to join. Off makes the hub private-groups-only. +ALLOW_PUBLIC_GROUPS = "allow_public_groups" + +_DEFAULTS: dict[str, str] = { + ALLOW_PUBLIC_GROUPS: "true", +} + + +async def get_raw(db: AsyncSession, key: str) -> str | None: + row = await db.get(HubSetting, key) + return row.value if row else None + + +async def set_raw(db: AsyncSession, key: str, value: str) -> None: + """Upsert. The caller owns the commit.""" + row = await db.get(HubSetting, key) + if row: + row.value = value + else: + db.add(HubSetting(key=key, value=value)) + + +async def get_bool(db: AsyncSession, key: str) -> bool: + raw = await get_raw(db, key) + if raw is None: + raw = _DEFAULTS.get(key, "false") + return raw == "true" + + +async def public_groups_allowed(db: AsyncSession) -> bool: + return await get_bool(db, ALLOW_PUBLIC_GROUPS) |