diff --git a/mvlog_worker.py b/mvlog_worker.py index 46e9f56..ad94c86 100755 --- a/mvlog_worker.py +++ b/mvlog_worker.py @@ -193,22 +193,52 @@ def send_push_notification(host: str, remote_root: str, output: str) -> bool: 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 - state = state if state is not None else read_state(host, job_dir) - output = state.get("output") if isinstance(state.get("output"), str) else "" - if not output or not remote_file_exists(host, f"{remote_root}/out-dir/{output}"): - return + cache_path = f"{remote_root}/{NOTIFIED_CACHE}" cache = read_remote_json(host, cache_path) - sent = cache.get("sent") if isinstance(cache.get("sent"), dict) else {} - if output in sent: + 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 - if send_push_notification(host, remote_root, output): - sent[output] = {"job": job, "sent_at": now()} - cache["sent"] = sent + + # 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"notification sent: {job} -> {output}") + print(f"show-notification sent: {job}") def publish_output(host: str, remote_root: str, local_mp4: Path, old_output: str | None) -> str: