feat(api,userbot,frontend): add accounts from the web ui and isolate per-account settings

This commit is contained in:
hh
2026-08-05 23:49:52 +02:00
parent 92fd20137e
commit 9c265af3d3
19 changed files with 917 additions and 268 deletions
+34 -3
View File
@@ -110,10 +110,24 @@ async def get_policy(pool: asyncpg.Pool, policy_id: int) -> PolicyRecord | None:
return PolicyRecord(**dict(row)) if row else None
async def find_policy(
pool: asyncpg.Pool, account_id: int, scope_type: ScopeType, scope_id: int | None
) -> PolicyRecord | None:
row = await pool.fetchrow(
"SELECT * FROM capture_policy WHERE account_id = $1 AND scope_type = $2 "
"AND scope_id IS NOT DISTINCT FROM $3",
account_id,
scope_type.value,
scope_id,
)
return PolicyRecord(**dict(row)) if row else None
async def list_policies(pool: asyncpg.Pool, account_id: int) -> list[PolicyRecord]:
rows = await pool.fetch(
"SELECT * FROM capture_policy WHERE account_id = $1 OR account_id IS NULL "
"ORDER BY scope_type, scope_id",
"SELECT DISTINCT ON (scope_type, scope_id) * FROM capture_policy "
"WHERE account_id = $1 OR account_id IS NULL "
"ORDER BY scope_type, scope_id, account_id NULLS LAST",
account_id,
)
return [PolicyRecord(**dict(row)) for row in rows]
@@ -131,6 +145,22 @@ async def update_policy(
return PolicyRecord(**dict(row)) if row else None
async def override_policy(
pool: asyncpg.Pool, policy_id: int, account_id: int, toggles: CaptureToggles
) -> PolicyRecord | None:
record = await get_policy(pool, policy_id)
if record is None:
return None
if record.account_id == account_id:
return await update_policy(pool, policy_id, toggles)
existing = await find_policy(pool, account_id, record.scope_type, record.scope_id)
if existing is not None:
return await update_policy(pool, existing.id, toggles)
return await create_policy(
pool, account_id, record.scope_type, record.scope_id, toggles
)
async def delete_policy(pool: asyncpg.Pool, policy_id: int) -> bool:
result = await pool.execute("DELETE FROM capture_policy WHERE id = $1", policy_id)
return result.endswith("1")
@@ -138,7 +168,8 @@ async def delete_policy(pool: asyncpg.Pool, policy_id: int) -> bool:
async def load_policy_set(pool: asyncpg.Pool, account_id: int) -> PolicySet:
rows = await pool.fetch(
"SELECT * FROM capture_policy WHERE account_id = $1 OR account_id IS NULL",
"SELECT * FROM capture_policy WHERE account_id = $1 OR account_id IS NULL "
"ORDER BY account_id NULLS FIRST",
account_id,
)
policies = PolicySet()