feat(backend): policy rules show what they inherit, files of a chat or folder go to trash in one batch, trash list is paged
This commit is contained in:
1 parent
945adcf879
commit
f646f29ff9
9 files changed
+389
-15
No files matched your search
@@ -5,13 +5,15 @@ from dishka.integrations.fastapi import DishkaRoute, FromDishka
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from utils.policy import repository
|
||||
from utils.policy import overview, repository
|
||||
from utils.policy.models import (
|
||||
CaptureToggles,
|
||||
ChatKind,
|
||||
ChatMeta,
|
||||
PolicyCreate,
|
||||
PolicyRecord,
|
||||
PolicyRuleView,
|
||||
PolicyScopeView,
|
||||
)
|
||||
from utils.policy.resolver import resolve
|
||||
|
||||
@@ -50,6 +52,32 @@ async def effective_policy(
|
||||
return resolve(chat, folders, policies)
|
||||
|
||||
|
||||
@router.get("/overview")
|
||||
async def policy_overview(
|
||||
pool: FromDishka[asyncpg.Pool], account_id: Annotated[int, Query()]
|
||||
) -> list[PolicyRuleView]:
|
||||
return await overview.list_rules(pool, account_id)
|
||||
|
||||
|
||||
@router.get("/scope")
|
||||
async def policy_scope(
|
||||
pool: FromDishka[asyncpg.Pool],
|
||||
account_id: Annotated[int, Query()],
|
||||
chat_id: Annotated[int | None, Query()] = None,
|
||||
folder_id: Annotated[int | None, Query()] = None,
|
||||
) -> PolicyScopeView:
|
||||
if chat_id is not None:
|
||||
return await overview.chat_scope(pool, account_id, chat_id)
|
||||
scope = (
|
||||
await overview.folder_scope(pool, account_id, folder_id)
|
||||
if folder_id is not None
|
||||
else None
|
||||
)
|
||||
if scope is None:
|
||||
raise HTTPException(status_code=404, detail="scope not found")
|
||||
return scope
|
||||
|
||||
|
||||
@router.post("", status_code=201)
|
||||
async def create_policy(
|
||||
pool: FromDishka[asyncpg.Pool], body: PolicyCreate
|
||||
|
||||
@@ -14,6 +14,7 @@ from utils.read.models import (
|
||||
DEFAULT_LIMIT,
|
||||
Page,
|
||||
StorageFileView,
|
||||
StorageScopeView,
|
||||
StorageSummaryView,
|
||||
TrashItemView,
|
||||
TrashOutcome,
|
||||
@@ -30,7 +31,13 @@ class TrashRequest(BaseModel):
|
||||
|
||||
class RestoreRequest(BaseModel):
|
||||
account_id: int
|
||||
keys: list[str]
|
||||
keys: list[str] | None = None
|
||||
|
||||
|
||||
class ScopeRequest(BaseModel):
|
||||
account_id: int
|
||||
chat_id: int | None = None
|
||||
folder_id: int | None = None
|
||||
|
||||
|
||||
class PurgeRequest(BaseModel):
|
||||
@@ -57,11 +64,47 @@ async def files(
|
||||
return await storage_read.list_files(pool, account_id, group, page)
|
||||
|
||||
|
||||
async def _chat_ids(
|
||||
pool: asyncpg.Pool, account_id: int, chat_id: int | None, folder_id: int | None
|
||||
) -> list[int]:
|
||||
chat_ids = await storage_read.scope_chat_ids(pool, account_id, chat_id, folder_id)
|
||||
if chat_ids is None:
|
||||
raise HTTPException(status_code=404, detail="scope not found")
|
||||
return chat_ids
|
||||
|
||||
|
||||
@router.get("/scope")
|
||||
async def scope(
|
||||
pool: FromDishka[asyncpg.Pool],
|
||||
account_id: Annotated[int, Query()],
|
||||
chat_id: Annotated[int | None, Query()] = None,
|
||||
folder_id: Annotated[int | None, Query()] = None,
|
||||
) -> StorageScopeView:
|
||||
chat_ids = await _chat_ids(pool, account_id, chat_id, folder_id)
|
||||
return await storage_read.get_scope(pool, account_id, chat_ids)
|
||||
|
||||
|
||||
@router.get("/trash")
|
||||
async def trash_items(
|
||||
pool: FromDishka[asyncpg.Pool], account_id: Annotated[int, Query()]
|
||||
pool: FromDishka[asyncpg.Pool],
|
||||
account_id: Annotated[int, Query()],
|
||||
limit: Annotated[int, Query()] = DEFAULT_LIMIT,
|
||||
offset: Annotated[int, Query()] = 0,
|
||||
) -> list[TrashItemView]:
|
||||
return await storage_read.list_trash(pool, account_id)
|
||||
page = Page(limit=limit, offset=offset)
|
||||
return await storage_read.list_trash(pool, account_id, page)
|
||||
|
||||
|
||||
@router.post("/trash/scope")
|
||||
async def trash_scope(
|
||||
pool: FromDishka[asyncpg.Pool],
|
||||
storage: FromDishka[ContentAddressedStorage],
|
||||
body: ScopeRequest,
|
||||
) -> TrashOutcome:
|
||||
chat_ids = await _chat_ids(pool, body.account_id, body.chat_id, body.folder_id)
|
||||
blobs = await pool.fetch(storage_read.SCOPE_BLOBS, body.account_id, chat_ids)
|
||||
free = [blob for blob in blobs if blob["free"]]
|
||||
return await trash.trash_blobs(pool, storage, body.account_id, free)
|
||||
|
||||
|
||||
@router.post("/trash")
|
||||
|
||||
Reference in new issue
Block a user