feat(files): download from anywhere — a link box, a pasted link, the palette; the running downloads in a tray
- Files toolbar link box (paste button, Enter); a lone http(s) link pasted onto any page opens the one app-wide download dialog, which now names the link; the palette's "Download from a link…" focuses the box on its history entry. - The download tray, bottom right on every page: what waits, runs, is paused or failed (reason + Retry, the queue's right-click / "…" menu), and what finished in this tab as "Done · Open" until closed; folds to a pill (per-account pref); closed, it stays shut — across a reload — until the work changes or something finishes. Back to top steps up over it, the page end keeps clear. - A finish is announced once: "Downloaded: …", whether a phone plays it, Copy link + Open (toasts and notices may carry a second action). - The inbox's ready notice opens the file in its folder: download_done carries job_id + folder_id. - GET /api/downloads is the queue only (in flight, failed, finished in the last 10 minutes), with each row's source key.
This commit is contained in:
@@ -95,16 +95,24 @@ def _notify_download_done(db, user_id: int | None, title: str | None, job_id, *,
|
||||
notification failure must never mask the completed job, so it is isolated + rolled back (mirrors
|
||||
scheduler._notify_done). `kind` ("download"/"edit"/"upload") lets the inbox pick accurate copy; the
|
||||
English title here is only a fallback for an unknown type. `user_id`/`title` are captured by the
|
||||
caller BEFORE its commit, so reading them here needs no post-commit refresh of an expired row."""
|
||||
caller BEFORE its commit; only the folder is read here, once, for the notice's "Open"."""
|
||||
if user_id is None:
|
||||
return
|
||||
try:
|
||||
# The notice's "Open" lands on the file in its folder: both ride along (a folder the file
|
||||
# has left since is the page's to notice — it then looks among every file).
|
||||
job = db.get(DownloadJob, job_id)
|
||||
create_notification(
|
||||
db,
|
||||
user_id=user_id,
|
||||
type="download_done",
|
||||
title=f"Ready to watch: {title}" if title else "Ready to watch",
|
||||
data={"title": title, "kind": kind},
|
||||
data={
|
||||
"title": title,
|
||||
"kind": kind,
|
||||
"job_id": job_id,
|
||||
"folder_id": job.folder_id if job else None,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
|
||||
@@ -53,3 +53,7 @@ NOT_REFETCHABLE = frozenset({JobKind.EDIT, JobKind.UPLOAD, JobKind.CLIP})
|
||||
# States a route sets to STOP a running job. The worker's supervisor must step in (kill the child, free
|
||||
# the asset, leave the route's status intact) when it observes one of these on a job it is running.
|
||||
STOPPED_BY_ROUTE = frozenset({JobStatus.PAUSED, JobStatus.CANCELED})
|
||||
|
||||
|
||||
# A job that will not move again on its own: the queue lists it only briefly after it got here.
|
||||
FINISHED = frozenset({JobStatus.DONE, JobStatus.CANCELED})
|
||||
|
||||
@@ -30,7 +30,7 @@ from app.downloads import edit as editmod
|
||||
from app.downloads import links as linksmod
|
||||
from app.downloads import quota, service, storage
|
||||
from app.downloads import urlguard
|
||||
from app.downloads.status import NOT_REFETCHABLE, JobStatus
|
||||
from app.downloads.status import FINISHED, NOT_REFETCHABLE, JobStatus
|
||||
from app.models import (
|
||||
DownloadJob,
|
||||
DownloadLink,
|
||||
@@ -58,6 +58,7 @@ from app.routes.files import (
|
||||
quota_detail,
|
||||
serialize,
|
||||
serialize_job,
|
||||
source_keys,
|
||||
)
|
||||
from app.security import hash_password
|
||||
from app.userscope import is_messageable_user, messageable_clauses
|
||||
@@ -396,12 +397,22 @@ def enqueue_clip(
|
||||
return serialize_job(db, job)
|
||||
|
||||
|
||||
# How long a finished job stays in the queue: the tray polls it from every page and must see a job
|
||||
# turn done (its "Done · Open" row and the done notice) — the finished file itself is the library's.
|
||||
QUEUE_RECENT = timedelta(minutes=10)
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_downloads(user: User = Depends(current_user), db: Session = Depends(get_db)) -> list[dict]:
|
||||
"""The queue: what is waiting, running, paused or failed, and what finished a moment ago."""
|
||||
recent = datetime.now(timezone.utc) - QUEUE_RECENT
|
||||
jobs = (
|
||||
db.execute(
|
||||
select(DownloadJob)
|
||||
.where(DownloadJob.user_id == user.id)
|
||||
.where(
|
||||
DownloadJob.user_id == user.id,
|
||||
DownloadJob.status.not_in(FINISHED) | (DownloadJob.updated_at >= recent),
|
||||
)
|
||||
.order_by(DownloadJob.created_at.desc(), DownloadJob.id.desc())
|
||||
.limit(MAX_LIST)
|
||||
)
|
||||
@@ -410,10 +421,12 @@ def list_downloads(user: User = Depends(current_user), db: Session = Depends(get
|
||||
)
|
||||
assets = assets_for(db, jobs)
|
||||
positions = positions_for(db, user, jobs)
|
||||
sources = source_keys(db, jobs)
|
||||
return [
|
||||
{
|
||||
**serialize(j, assets.get(j.asset_id) if j.asset_id is not None else None),
|
||||
"position_seconds": positions.get(j.id, 0),
|
||||
"source_key": sources[j.id],
|
||||
}
|
||||
for j in jobs
|
||||
]
|
||||
|
||||
@@ -556,7 +556,7 @@ def list_files(
|
||||
assets = assets_for(db, jobs)
|
||||
positions = positions_for(db, user, jobs)
|
||||
shared_at = _shared_at(db, user, jobs) if shared else {}
|
||||
sources = _source_keys(db, jobs)
|
||||
sources = source_keys(db, jobs)
|
||||
return [
|
||||
{
|
||||
**serialize(j, assets.get(j.asset_id) if j.asset_id is not None else None),
|
||||
@@ -569,9 +569,9 @@ def list_files(
|
||||
]
|
||||
|
||||
|
||||
def _source_keys(db: Session, jobs: Sequence[DownloadJob]) -> dict[int, str]:
|
||||
"""Where each file came from (its source folder's key), for the list's Source column and the
|
||||
"back to its source folder" move: a file in a source folder has its folder's key; one moved into
|
||||
def source_keys(db: Session, jobs: Sequence[DownloadJob]) -> dict[int, str]:
|
||||
"""Where each file came from (its source folder's key), for the list's Source column, the
|
||||
"back to its source folder" move and the download tray's site: a file in a source folder has its folder's key; one moved into
|
||||
an own folder (or someone else's, shared) is traced back by `folders.source_key_for`."""
|
||||
ids = {j.folder_id for j in jobs if j.folder_id is not None}
|
||||
by_id = (
|
||||
|
||||
@@ -261,3 +261,22 @@ def test_the_longest_host_has_room_for_its_folder(db, make_user):
|
||||
uid = make_user().id
|
||||
job = _job(db, uid, _asset(db, "url", f"https://{host}/v"), kind="url")
|
||||
assert _folder(db, job).source_key == f"site:{host}"
|
||||
|
||||
|
||||
def test_the_ready_notice_names_the_file_and_its_folder(db, make_user):
|
||||
# The notice's "Open" goes to the file in its folder, so both ride along as data.
|
||||
from app.downloads import pipeline
|
||||
|
||||
user = make_user()
|
||||
job = _job(db, user.id, _asset(db))
|
||||
pipeline._notify_download_done(db, user.id, "A title", job.id, kind="download")
|
||||
db.commit()
|
||||
note = db.scalars(select(Notification).where(Notification.user_id == user.id)).one()
|
||||
assert note.type == "download_done"
|
||||
assert note.data == {
|
||||
"title": "A title",
|
||||
"kind": "download",
|
||||
"job_id": job.id,
|
||||
"folder_id": job.folder_id,
|
||||
}
|
||||
assert job.folder_id is not None
|
||||
|
||||
@@ -506,3 +506,35 @@ def test_a_new_file_at_an_assets_path_forgets_the_old_hash(db, make_user):
|
||||
asset.sha256 = "ab" * 32
|
||||
asset.rel_path = "converted.mp4"
|
||||
assert asset.sha256 is None
|
||||
|
||||
|
||||
# --- the queue (the tray's poll) ------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_queue_is_what_is_in_flight_and_what_just_finished_not_the_library(
|
||||
db, make_user, as_user
|
||||
):
|
||||
# The tray polls this on every page: a finished file is the library's, so it is listed only
|
||||
# while the tray may still be showing it done.
|
||||
from sqlalchemy import update
|
||||
|
||||
user = make_user()
|
||||
old = _file(db, user.id, title="Old")
|
||||
fresh = _file(db, user.id, title="Fresh")
|
||||
waiting = _file(db, user.id, title="Waiting", status=JobStatus.QUEUED)
|
||||
stuck = _file(db, user.id, title="Stuck", status=JobStatus.ERROR)
|
||||
paused = _file(db, user.id, title="Paused", status=JobStatus.PAUSED)
|
||||
stopped = _file(db, user.id, title="Stopped", status=JobStatus.ERROR)
|
||||
dropped = _file(db, user.id, title="Dropped", status=JobStatus.CANCELED)
|
||||
for job in (old, stopped, dropped):
|
||||
db.execute(
|
||||
update(DownloadJob)
|
||||
.where(DownloadJob.id == job.id)
|
||||
.values(updated_at=NOW - timedelta(hours=1))
|
||||
)
|
||||
db.commit()
|
||||
rows = as_user(user).get("/api/downloads").json()
|
||||
# An old failure still waits for the user; an old finished or cancelled job does not.
|
||||
assert {row["id"] for row in rows} == {fresh.id, waiting.id, stuck.id, paused.id, stopped.id}
|
||||
# The tray names where each came from, as the library does.
|
||||
assert {row["source_key"] for row in rows} == {"youtube"}
|
||||
|
||||
Reference in New Issue
Block a user