summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/hub_settings.py
blob: 280d1e2de708964659e8cd61a6fbe75f72a4adef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)