web: move web-push sending to webserver; remove push logic from worker; add enable_show_local.php

This commit is contained in:
hbrain 2026-05-28 18:52:54 +00:00
parent f29b0dee5e
commit e0359dacf7
2 changed files with 184 additions and 73 deletions

View file

@ -23,14 +23,11 @@ from shlex import quote
ROOT = Path(__file__).resolve().parent
MOVMAKER = ROOT / "movmaker.py"
INTRO_LOGO = ROOT / "img" / "moto_travel.png"
PUSH_SENDER = ROOT / "send_push.js"
PUSH_CONFIG = Path.home() / ".config" / "mvlog" / "push.json"
STATE_NAME = ".movmaker-state.json"
LOCK_NAME = ".movmaker-lock"
ENABLED_NAME = ".movmaker-enabled"
PREVIEW_NAME = ".movmaker-preview"
HIDDEN_NAME = ".mvlog-hidden"
NOTIFIED_CACHE = "cache/push_notifications.json"
INTRO_LOGO_ALPHA = "0.5"
MVLOG_WORKER_RECOMMENDED_INTERVAL_MINUTES = 5
@ -171,74 +168,6 @@ def copy_job_from_remote(host: str, job_dir: str, local_input: Path) -> None:
raise
def send_push_notification(host: str, remote_root: str, output: str) -> bool:
if not PUSH_SENDER.exists() or not PUSH_CONFIG.exists():
return False
res = ssh(host, f"cat {remote_q(remote_root + '/cache/push_subscriptions.json')} 2>/dev/null", check=False)
if res.returncode != 0 or not res.stdout.strip():
return False
title = output.rsplit('.', 1)[0].replace('_', ' ').title()
payload = json.dumps({
"title": "New MVLog video",
"body": title,
"url": "https://bubulescu.org/mvlog/",
"tag": f"mvlog-{output}",
}, ensure_ascii=False)
proc = subprocess.run(["node", str(PUSH_SENDER), str(PUSH_CONFIG), payload], input=res.stdout, text=True, capture_output=True, check=False)
if proc.stdout.strip():
print(proc.stdout.strip())
if proc.stderr.strip():
print(proc.stderr.strip(), file=sys.stderr)
return proc.returncode == 0
def maybe_send_first_show_notification(host: str, remote_root: str, job_dir: str, job: str, state: dict | None = None) -> None:
"""
Send a web-push notification the first time the 'Show' checkbox is set for a job.
We record notifications in cache/push_notifications.json under the 'shown' map keyed by job
to ensure each job triggers only once when Show is enabled.
"""
# Don't notify for hidden jobs
if remote_file_exists(host, f"{job_dir}/{HIDDEN_NAME}"):
return
cache_path = f"{remote_root}/{NOTIFIED_CACHE}"
cache = read_remote_json(host, cache_path)
shown = cache.get("shown") if isinstance(cache.get("shown"), dict) else {}
# If we've already recorded that Show was sent for this job, skip
if job in shown:
return
# Notify when the enabled marker exists (Show checked)
if not remote_file_exists(host, f"{job_dir}/{ENABLED_NAME}"):
return
# Determine a title/url for the notification. Prefer existing output filename if present.
state = state if state is not None else read_state(host, job_dir)
output = state.get("output") if isinstance(state.get("output"), str) else ""
title = output.rsplit('.', 1)[0].replace('_', ' ').title() if output else job.replace('_', ' ').title()
payload = json.dumps({
"title": "Show enabled",
"body": title,
"url": f"https://bubulescu.org/mvlog/?job={job}",
"tag": f"mvlog-show-{job}",
}, ensure_ascii=False)
# Gather subscriptions and send
res = ssh(host, f"cat {remote_q(remote_root + '/cache/push_subscriptions.json')} 2>/dev/null", check=False)
if res.returncode != 0 or not res.stdout.strip():
return
proc = subprocess.run(["node", str(PUSH_SENDER), str(PUSH_CONFIG), payload], input=res.stdout, text=True, capture_output=True, check=False)
if proc.stdout.strip():
print(proc.stdout.strip())
if proc.stderr.strip():
print(proc.stderr.strip(), file=sys.stderr)
if proc.returncode == 0:
shown[job] = {"sent_at": now()}
cache["shown"] = shown
write_remote_json(host, cache_path, cache)
print(f"show-notification sent: {job}")
def publish_output(host: str, remote_root: str, local_mp4: Path, old_output: str | None) -> str:
@ -292,7 +221,6 @@ def render_job(local_input: Path, local_out: Path, extra_args: list[str]) -> Pat
def process_job(host: str, remote_root: str, job: str, work_dir: Path, extra_args: list[str], force: bool) -> None:
job_dir = f"{remote_root}/in-dir/{job}"
state = read_state(host, job_dir)
maybe_send_first_show_notification(host, remote_root, job_dir, job, state)
full_requested = remote_file_exists(host, f"{job_dir}/{ENABLED_NAME}")
preview_requested = remote_file_exists(host, f"{job_dir}/{PREVIEW_NAME}")
if not full_requested and not preview_requested:
@ -386,7 +314,6 @@ def process_job(host: str, remote_root: str, job: str, work_dir: Path, extra_arg
"output": output,
"generated_at": now(),
})
maybe_send_first_show_notification(host, remote_root, job_dir, job, new_state)
write_state(host, job_dir, new_state)
print(f"published: {job} -> {output}")
return