Titles approved outside the app join their channels' lists, only ever added (build 71)

approve-add reads install-restore/approve.json with confirm ADD while Orbit is closed.
Each title joins its channel's list as a Chair placement does; copies of a work already
there are skipped, so a second run changes nothing; the channel files touched are kept
in approved-<time>/; one bad entry refuses the lot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 09:58:00 +00:00
co-authored by Claude Opus 5.5
parent 40ec46e7b5
commit f2e9f31639
4 changed files with 119 additions and 2 deletions
@@ -0,0 +1,55 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
/**
* Titles you approved outside the app (Stage 3 replacements, 23 November 2026), added to channels' lists exactly as the
* Director's Chair places a title: each joins its channel's list at the next top-up, and nothing already booked moves.
*
* - it only adds: nothing on a list is removed or changed, and a title you approve comes off that channel's "rejected" list
* (your approval is the newer decision), as it does in the Chair;
* - a title already on the channel, as the same item or another copy of the same work (name and year), isn't added twice,
* so running it again changes nothing;
* - every channel file it touches is kept as it was in approved-<time>/ beside the schedules, to put back if wanted;
* - each entry is checked first, and any bad entry refuses the lot.
*
* An entry: {"c": channel, "i": item id, "t": title, "y": year or null, "k": "Movie" or "Series", "p": provider ref or "", "r": why}.
*/
internal object ApprovedTitles {
private val REF = Regex("(imdb|tmdb|tvdb)\\.[A-Za-z0-9_-]{1,40}")
fun add(dir: File, userId: String, entries: JSONArray, nowMs: Long): String {
val rows = (0 until entries.length()).map { entries.getJSONObject(it) }
if (rows.isEmpty()) return "refused: nothing to add"
val wanted = rows.map { o ->
val channel = o.optInt("c", -1).takeIf { it in 100..999 } ?: return "refused: a bad channel"
val id = o.optString("i").takeIf(EmbyClient::validId) ?: return "refused: a bad item id"
val type = o.optString("k").takeIf { it == "Movie" || it == "Series" } ?: return "refused: a bad type for $id"
val title = o.optString("t").trim().take(160).takeIf { it.isNotEmpty() } ?: return "refused: no title for $id"
val ref = o.optString("p").takeIf { it.isEmpty() || REF.matches(it) } ?: return "refused: a bad ref for $id"
channel to Suggestion(id, title, if (o.isNull("y")) null else o.optInt("y"), type, "", o.optString("r").take(200).ifBlank { "Approved by you" }, ref = ref)
}
val store = ScheduleStore(dir)
val keep = File(dir, "approved-$nowMs")
val report = StringBuilder()
var added = 0
for ((channel, list) in wanted.groupBy({ it.first }, { it.second }).toSortedMap()) {
val before = store.loadChoices(userId, channel)
fun work(s: Suggestion) = filmKey(s.title) + "|" + (s.year ?: "")
val have = before.accepted.map { it.id }.toMutableSet(); val works = before.accepted.map(::work).toMutableSet()
val fresh = list.filter { s -> s.id !in have && work(s) !in works && have.add(s.id) && works.add(work(s)) }
report.append(" $channel+${fresh.size}")
if (fresh.isEmpty()) continue
File(dir, "choices-$channel.json").takeIf { it.exists() }?.let { keep.mkdirs(); it.copyTo(File(keep, it.name), overwrite = true) }
store.saveChoices(userId, channel, ChannelChoices(before.accepted + fresh, before.rejected - fresh.map { it.id }.toSet()))
added += fresh.size
}
return "added $added:$report" + if (keep.exists()) " (kept ${keep.name})" else ""
}
/** Entries as [add] takes them, for tests and tools. */
fun entry(channel: Int, id: String, title: String, year: Int?, type: String, ref: String = "", why: String = "") =
JSONObject().put("c", channel).put("i", id).put("t", title).put("y", year ?: JSONObject.NULL).put("k", type).put("p", ref).put("r", why)
}