Behebe R-11: Übersicht und Suche lesen in Blöcken und prüfen Rechte vor der Begrenzung.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a3f328832e
commit
d0f09830bb
3 files changed
+92
-18
No files matched your search
+43
-17
@@ -1182,37 +1182,61 @@ def post_fulfill(
|
||||
return deadline_payload(conn, principal.account_id, deadline)
|
||||
|
||||
|
||||
VIEW_LIMIT = 200
|
||||
SEARCH_LIMIT = 50
|
||||
SCAN_BATCH = 200
|
||||
SCAN_CAP = 20000
|
||||
|
||||
|
||||
def _scan(conn, sql: str, params: tuple = ()):
|
||||
"""Rows in the order of the query, read in batches.
|
||||
|
||||
The rights are checked per row after reading. A fixed LIMIT in the query would count rows
|
||||
of other people too, so the own ones could fall off the end. The reader stops once it has
|
||||
enough. `SCAN_CAP` bounds the work on very large data sets.
|
||||
"""
|
||||
offset = 0
|
||||
while offset < SCAN_CAP:
|
||||
rows = conn.execute(f"{sql} LIMIT %s OFFSET %s", (*params, SCAN_BATCH, offset)).fetchall()
|
||||
yield from rows
|
||||
if len(rows) < SCAN_BATCH:
|
||||
return
|
||||
offset += SCAN_BATCH
|
||||
|
||||
|
||||
@router.get("/api/v1/views/open-tasks")
|
||||
def open_tasks(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
rows = conn.execute(
|
||||
items = []
|
||||
for task in _scan(
|
||||
conn,
|
||||
"""
|
||||
SELECT * FROM tasks
|
||||
WHERE deleted_at IS NULL AND task_status = 'open'
|
||||
ORDER BY due_on NULLS LAST, created_at, id
|
||||
LIMIT 200
|
||||
"""
|
||||
).fetchall()
|
||||
items = []
|
||||
for task in rows:
|
||||
""",
|
||||
):
|
||||
if _task_visible(conn, principal.account_id, task):
|
||||
items.append(task_payload(conn, principal.account_id, task))
|
||||
if len(items) >= VIEW_LIMIT:
|
||||
break
|
||||
return {"items": items, "next_cursor": None}
|
||||
|
||||
|
||||
@router.get("/api/v1/views/open-deadlines")
|
||||
def open_deadlines(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
rows = conn.execute(
|
||||
items = []
|
||||
for deadline in _scan(
|
||||
conn,
|
||||
"""
|
||||
SELECT * FROM deadlines
|
||||
WHERE deleted_at IS NULL AND deadline_status = 'open'
|
||||
ORDER BY due_on, created_at, id
|
||||
LIMIT 200
|
||||
"""
|
||||
).fetchall()
|
||||
items = []
|
||||
for deadline in rows:
|
||||
""",
|
||||
):
|
||||
if _deadline_visible(conn, principal.account_id, deadline):
|
||||
items.append(deadline_payload(conn, principal.account_id, deadline))
|
||||
if len(items) >= VIEW_LIMIT:
|
||||
break
|
||||
return {"items": items, "next_cursor": None}
|
||||
|
||||
|
||||
@@ -1266,19 +1290,21 @@ def search(q: str, principal: Principal = Depends(get_principal), conn: Connecti
|
||||
],
|
||||
}
|
||||
)
|
||||
elements = conn.execute(
|
||||
elements = _scan(
|
||||
conn,
|
||||
"""
|
||||
SELECT e.*
|
||||
FROM elements e
|
||||
LEFT JOIN note_bodies n ON n.element_id = e.id
|
||||
WHERE e.deleted_at IS NULL
|
||||
AND (e.title ILIKE %s ESCAPE '\\' OR COALESCE(n.document_json::text, '') ILIKE %s ESCAPE '\\')
|
||||
ORDER BY e.updated_at DESC
|
||||
LIMIT 100
|
||||
ORDER BY e.updated_at DESC, e.id
|
||||
""",
|
||||
(pattern, pattern),
|
||||
).fetchall()
|
||||
)
|
||||
for element in elements:
|
||||
if len(items) >= SEARCH_LIMIT:
|
||||
break
|
||||
from app.access.policy import visible_bindings_for_element
|
||||
|
||||
bindings = visible_bindings_for_element(conn, principal.account_id, element)
|
||||
@@ -1306,7 +1332,7 @@ def search(q: str, principal: Principal = Depends(get_principal), conn: Connecti
|
||||
],
|
||||
}
|
||||
)
|
||||
return {"items": items[:50], "next_cursor": None}
|
||||
return {"items": items[:SEARCH_LIMIT], "next_cursor": None}
|
||||
|
||||
|
||||
@router.patch("/api/v1/cases/{case_id}/content-order")
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
"""R-11: other people's rows must not push your own out of the overview and the search."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from tests.support import Actor, case_of, note_on, register
|
||||
|
||||
|
||||
def test_own_entries_are_found_behind_many_foreign_ones(client, db, monkeypatch):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.api import routes
|
||||
from app.main import app
|
||||
|
||||
# A tiny batch size makes the paging visible with few rows.
|
||||
monkeypatch.setattr(routes, "SCAN_BATCH", 2)
|
||||
register(db, "ada-scan@example.test", "Ada")
|
||||
register(db, "ben-scan@example.test", "Ben")
|
||||
ada = Actor(client, "ada-scan@example.test", "Ada")
|
||||
ben = Actor(TestClient(app), "ben-scan@example.test", "Ben")
|
||||
ada.login()
|
||||
ben.login()
|
||||
|
||||
ada_case = case_of(ada, "Adas Akte")
|
||||
for index in range(7):
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": f"Adas Vorgang {index}"}, status=201)
|
||||
ada.request("POST", "/api/v1/tasks", json={"title": f"Frühe Aufgabe {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201)
|
||||
ada.request("POST", "/api/v1/deadlines", json={"title": f"Frühe Frist {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201)
|
||||
note_on(ada, f"Gemeinsames Stichwort {index}", "Zebrastreifen", ada_case["id"])
|
||||
|
||||
ben_matter = ben.request("POST", "/api/v1/matters", json={"title": "Bens Vorgang"}, status=201)
|
||||
ben.request("POST", "/api/v1/tasks", json={"title": "Bens späte Aufgabe", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201)
|
||||
ben.request("POST", "/api/v1/deadlines", json={"title": "Bens späte Frist", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201)
|
||||
ben_case = case_of(ben, "Bens Akte")
|
||||
note_on(ben, "Bens Zebrastreifen-Notiz", "Zebrastreifen", ben_case["id"])
|
||||
|
||||
assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-tasks")["items"]] == ["Bens späte Aufgabe"]
|
||||
assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-deadlines")["items"]] == ["Bens späte Frist"]
|
||||
found = ben.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"]
|
||||
assert [item["title"] for item in found] == ["Bens Zebrastreifen-Notiz"]
|
||||
|
||||
# The own entries still come in order and the cap on the list holds.
|
||||
monkeypatch.setattr(routes, "VIEW_LIMIT", 3)
|
||||
first = ada.request("GET", "/api/v1/views/open-tasks")["items"]
|
||||
assert [item["title"] for item in first] == ["Frühe Aufgabe 0", "Frühe Aufgabe 1", "Frühe Aufgabe 2"]
|
||||
monkeypatch.setattr(routes, "SEARCH_LIMIT", 4)
|
||||
assert len(ada.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"]) == 4
|
||||
Reference in new issue
Block a user