fix(activity): Clear Completed leaves out an item that is no longer finished instead of failing the batch (#1508)

Clear Completed can fail outright with "Only terminal requests can be
dismissed". `dismiss-many` answers 409 and dismisses nothing when any
one request or download in the batch is not terminal. The list a user
clears from can be older than the server, so a request that was reopened
or retried since, or a download that started again, takes the whole
batch down with it. The earlier fix for pending requests (#1438) filters
on the client, which only helps when the list is current.

`dismiss-many` now skips such an item and carries on with the rest. The
response adds `skipped_item_keys` when something was left out, and
`count` is what was actually dismissed. A single `dismiss` of a
non-terminal item still answers 409.

I hit it with a request whose download had completed while the request
itself was still pending, but any reopen between the list and the click
does it.

This touches the same handler as #1507, so whichever lands second needs
a small rebase.

Tests: a batch with a finished and a reopened request dismisses the
finished one and reports the other, and a batch with a finished and a
running download does the same. Ruff, basedpyright and the backend suite
pass (3933 passed, e2e excluded).

Co-authored-by: CaliBrain <calibrain@l4n.xyz>
This commit is contained in:
splitsec2andCaliBrain authored and GitHub committed 2026-10-21 01:39:14 -04:00
1 parent 61c7ad3c62
commit 520b6b16d0
2 files changed
+104 -2

No files matched your search

+11 -2
View File
@@ -897,6 +897,7 @@ def register_activity_routes(
dismissal_items: list[dict[str, str]] = []
missing_item_keys: list[str] = []
skipped_item_keys: list[str] = []
live_queue_index: dict[str, tuple[str, dict[str, Any]]] | None = None
request_linked_task_ids: list[str] | None = None
@@ -953,7 +954,10 @@ def register_activity_routes(
item_count=len(items),
**_download_row_log_context(existing),
)
missing_item_keys.append(f"download:{viewer_task_id}")
if _check_terminal_download(effective_existing) is not None:
# The list this came from can be older than the download: one that started
# again since is left where it is, not allowed to fail the whole batch.
skipped_item_keys.append(f"download:{viewer_task_id}")
continue
if live_queue_index is None:
live_queue_index = _build_queue_index(queue_status(user_id=actor.owner_scope))
@@ -1006,6 +1010,8 @@ def register_activity_routes(
item_count=len(items),
**_request_row_log_context(request_row),
)
if _check_terminal_request(request_row) is not None:
# Same for a request that was reopened or retried since the list was drawn.
missing_item_keys.append(f"request:{request_id}")
continue
terminal_error = _check_terminal_request(request_row)
@@ -1064,7 +1070,10 @@ def register_activity_routes(
},
)
return jsonify({"status": "dismissed", "count": dismissed_count})
response: dict[str, Any] = {"status": "dismissed", "count": dismissed_count}
if skipped_item_keys:
response["skipped_item_keys"] = skipped_item_keys
return jsonify(response)
@app.route("/api/activity/history", methods=["GET"])
def api_activity_history() -> Response | tuple[Response, int]:
+93
View File
@@ -574,6 +574,99 @@ class TestActivityRoutes:
assert response.status_code == 409
assert response.json["error"] == "Only terminal requests can be dismissed"
def test_dismiss_many_skips_a_request_that_is_no_longer_terminal(self, main_module, client):
user = _create_user(main_module, prefix="reader")
_set_session(client, user_id=user["username"], db_user_id=user["id"], is_admin=False)
def _make(provider_id, status):
return main_module.user_db.create_request(
user_id=user["id"],
content_type="ebook",
request_level="book",
policy_mode="request_book",
book_data={
"title": f"Request {provider_id}",
"author": "Author",
"provider": "openlibrary",
"provider_id": provider_id,
},
status=status,
)
done = _make("skip-1", "rejected")
reopened = _make("skip-2", "pending") # the list was drawn before it was reopened
with patch.object(main_module, "get_auth_mode", return_value="builtin"):
response = client.post(
"/api/activity/dismiss-many",
json={
"items": [
{"item_type": "request", "item_key": f"request:{reopened['id']}"},
{"item_type": "request", "item_key": f"request:{done['id']}"},
]
},
)
assert response.status_code == 200
assert response.json["count"] == 1
assert response.json["skipped_item_keys"] == [f"request:{reopened['id']}"]
def test_dismiss_many_skips_a_download_that_is_running_again(self, main_module, client):
user = _create_user(main_module, prefix="reader")
_set_session(client, user_id=user["username"], db_user_id=user["id"], is_admin=False)
_record_terminal_download(
main_module,
task_id="dismiss-many-skip-done",
user_id=user["id"],
username=user["username"],
)
running = "dismiss-many-skip-running"
main_module.download_history_service.record_download(
task_id=running,
user_id=user["id"],
username=user["username"],
request_id=None,
source="direct_download",
source_display_name="Direct Download",
title="Running Again",
author="Author",
file_format="epub",
size="1 MB",
preview=None,
content_type="ebook",
downloads=None,
origin="direct",
retry_payload=None,
)
live_status = _sample_status_payload()
live_status["downloading"] = {
running: {
"id": running,
"title": "Running Again",
"author": "Author",
"source": "direct_download",
"status_message": "Downloading",
}
}
with (
patch.object(main_module, "get_auth_mode", return_value="builtin"),
patch.object(main_module.backend, "queue_status", return_value=live_status),
):
response = client.post(
"/api/activity/dismiss-many",
json={
"items": [
{"item_type": "download", "item_key": f"download:{running}"},
{"item_type": "download", "item_key": "download:dismiss-many-skip-done"},
]
},
)
assert response.status_code == 200
assert response.json["count"] == 1
assert response.json["skipped_item_keys"] == [f"download:{running}"]
def test_dismiss_emits_activity_update_to_user_room(self, main_module, client):
user = _create_user(main_module, prefix="reader")
_set_session(client, user_id=user["username"], db_user_id=user["id"], is_admin=False)