Removals stay removed across rooms (audit A3): Continue watching and Taste resets
What's taken off Continue watching (by hand, or watched to the end) is recorded in its own gone-continue file for three days and passed on by Rooms sync, so the other TV's copy can't bring it back; watching it again later does. A Taste reset in either room wins over older events. Older Orbits ignore the new file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -58,10 +58,27 @@ internal fun continueEntries(channels: List<SampleChannel>, marks: List<Continue
|
||||
if (!catchUpAvailable(channel, show, nowMs)) null else ContinueEntry(catchUpEntry(channel, show), m.positionMs)
|
||||
}
|
||||
|
||||
/**
|
||||
* What's been taken off Continue watching, by "channel:start", with when (audit A3, 9 November 2026): removed by hand or
|
||||
* watched to the end. Rooms sync passes it on, so the other TV's copy can't bring a programme back. Kept three days
|
||||
* (Catch Up holds two). Its own file, so an older Orbit simply ignores it.
|
||||
*/
|
||||
internal object ContinueGone {
|
||||
const val KEEP_MS = 3 * 24 * 60 * 60_000L
|
||||
fun fileFor(scheduleDir: File, profileFile: String) = File(scheduleDir, "gone-$profileFile")
|
||||
fun read(f: File): Map<String, Long> = runCatching { JSONObject(f.readText()).let { o -> o.keys().asSequence().associateWith { o.getLong(it) } } }.getOrDefault(emptyMap())
|
||||
fun merged(a: Map<String, Long>, b: Map<String, Long>, nowMs: Long): Map<String, Long> =
|
||||
(a.keys + b.keys).associateWith { k -> maxOf(a[k] ?: 0L, b[k] ?: 0L) }.filterValues { nowMs - it <= KEEP_MS }
|
||||
fun key(number: Int, startMs: Long) = "$number:$startMs"
|
||||
/** The marks still wanted: none taken off at or after its own time. */
|
||||
fun filter(marks: List<ContinueMark>, gone: Map<String, Long>) = marks.filter { m -> (gone[key(m.number, m.startMs)] ?: -1L) < m.atMs }
|
||||
}
|
||||
|
||||
internal object ContinueWatching {
|
||||
var marks by mutableStateOf<List<ContinueMark>>(emptyList())
|
||||
private set
|
||||
private var file: File? = null
|
||||
private var goneFile: File? = null
|
||||
|
||||
fun load(scheduleDir: File, profile: String?) {
|
||||
// Once: the lists made while live viewing counted too are set aside, now that only Catch Up adds to them.
|
||||
@@ -71,12 +88,20 @@ internal object ContinueWatching {
|
||||
flag.writeText("")
|
||||
}
|
||||
file = File(scheduleDir, "continue-${(profile ?: "all").filter { it.isLetterOrDigit() }.take(40)}.json")
|
||||
goneFile = ContinueGone.fileFor(scheduleDir, file!!.name)
|
||||
marks = runCatching { JSONArray(file!!.readText()).let { a -> (0 until minOf(a.length(), CONTINUE_MAX)).mapNotNull { runCatching { ContinueMark.fromJson(a.getJSONObject(it)) }.getOrNull() } } }.getOrDefault(emptyList())
|
||||
.let { ContinueGone.filter(it, goneFile?.let(ContinueGone::read).orEmpty()) }
|
||||
}
|
||||
fun note(number: Int, show: SampleBroadcast, positionMs: Long, nowMs: Long) {
|
||||
val next = notedMarks(marks, number, show, positionMs, nowMs)
|
||||
if (next != marks) { marks = next; save() }
|
||||
if (next != marks) {
|
||||
// Watched to the end: gone here, and remembered as gone for the other rooms.
|
||||
if (marks.any { it.number == number && it.startMs == show.startMs } && next.none { it.number == number && it.startMs == show.startMs }) gone(number, show.startMs, nowMs)
|
||||
marks = next; save()
|
||||
}
|
||||
}
|
||||
fun remove(number: Int, startMs: Long) { marks = marks.filterNot { it.number == number && it.startMs == startMs }; save() }
|
||||
fun remove(number: Int, startMs: Long, nowMs: Long = System.currentTimeMillis()) { gone(number, startMs, nowMs); marks = marks.filterNot { it.number == number && it.startMs == startMs }; save() }
|
||||
private fun gone(number: Int, startMs: Long, nowMs: Long) { runCatching { goneFile?.let { f ->
|
||||
writeSafely(f, JSONObject(ContinueGone.merged(ContinueGone.read(f), mapOf(ContinueGone.key(number, startMs) to nowMs), nowMs)).toString()) } } }
|
||||
private fun save() { runCatching { file?.let { writeSafely(it, JSONArray(marks.map { m -> m.toJson() }).toString()) } } }
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ internal object RoomSync {
|
||||
/** What this room sends: the family as it is, and Continue watching and Taste changed since [sinceMs]. */
|
||||
fun state(context: Context, scheduleDir: File, sinceMs: Long, nowMs: Long): JSONObject {
|
||||
val files = JSONObject()
|
||||
scheduleDir.listFiles { f -> Regex("continue-[A-Za-z0-9]{1,40}\\.json").matches(f.name) }?.forEach { files.put(it.name, it.readText()) }
|
||||
scheduleDir.listFiles { f -> Regex("continue-[A-Za-z0-9]{1,40}\\.json").matches(f.name) || GoneFiles.matches(f.name) }?.forEach { files.put(it.name, it.readText()) }
|
||||
File(scheduleDir, "taste").listFiles()?.forEach { f ->
|
||||
when {
|
||||
f.name.endsWith(".events.jsonl") -> f.readLines().filter { l -> (TasteEvent.of(l)?.atMs ?: runCatching { org.json.JSONObject(l).getLong("w") }.getOrNull())?.let { it >= sinceMs - ROOM_SYNC_OVERLAP_MS } == true }
|
||||
|
||||
@@ -79,6 +79,8 @@ private val ScheduleFiles = Regex("(schedule|progress|booked|tickets|failed)-\\d
|
||||
|
||||
/** Each person's Continue watching, and their Taste files, that travel with Family. */
|
||||
private val FamilyFiles = Regex("continue-[A-Za-z0-9]{1,40}\\.json")
|
||||
/** What's been taken off each person's Continue watching (ContinueGone); a name older Orbits don't send or take. */
|
||||
internal val GoneFiles = Regex("gone-continue-[A-Za-z0-9]{1,40}\\.json")
|
||||
private val TasteFiles = Regex("[A-Za-z0-9_-]{1,40}\\.(events\\.jsonl|state\\.json)")
|
||||
|
||||
/** Both rooms' profiles: the same id is the same person (the incoming details win); the result always has a grown-up. */
|
||||
@@ -93,6 +95,28 @@ internal fun mergedProfiles(here: List<Profile>, incoming: List<Profile>): List<
|
||||
* for each programme.
|
||||
*/
|
||||
internal fun mergeFamilyFiles(dir: File, files: JSONObject, nowMs: Long = System.currentTimeMillis()) {
|
||||
// Removals first (audit A3), so what's gone in either room stays gone when the lists are merged below.
|
||||
files.keys().asSequence().filter { GoneFiles.matches(it) }.toList().forEach { name ->
|
||||
val target = File(dir, name)
|
||||
val gone = ContinueGone.merged(ContinueGone.read(target), runCatching { JSONObject(files.getString(name)).let { o -> o.keys().asSequence().associateWith { o.getLong(it) } } }.getOrDefault(emptyMap()), nowMs)
|
||||
writeSafely(target, JSONObject(gone).toString())
|
||||
val list = File(dir, name.removePrefix("gone-"))
|
||||
if (list.exists()) runCatching {
|
||||
val a = JSONArray(list.readText()); val marks = (0 until a.length()).mapNotNull { runCatching { ContinueMark.fromJson(a.getJSONObject(it)) }.getOrNull() }
|
||||
writeSafely(list, JSONArray(ContinueGone.filter(marks, gone).map { it.toJson() }).toString())
|
||||
}
|
||||
}
|
||||
// A Taste reset in either room (audit A3): the latest reset wins, and nothing from before it comes back.
|
||||
files.keys().asSequence().filter { it.startsWith("taste/") && it.endsWith(".state.json") && TasteFiles.matches(it.removePrefix("taste/")) }.toList().forEach { name ->
|
||||
val target = File(dir, name)
|
||||
val theirs = runCatching { JSONObject(files.getString(name)).optLong("resetAt") }.getOrDefault(0L)
|
||||
val mine = runCatching { JSONObject(target.readText()) }.getOrNull()
|
||||
if (mine != null && theirs > mine.optLong("resetAt")) {
|
||||
writeSafely(target, mine.put("resetAt", theirs).put("suppress", JSONObject()).toString())
|
||||
val events = File(dir, name.removeSuffix(".state.json") + ".events.jsonl")
|
||||
if (events.exists()) writeSafely(events, compactTasteLines(events.readLines(), nowMs, theirs).joinToString("") { it + "\n" })
|
||||
}
|
||||
}
|
||||
files.keys().forEach { name ->
|
||||
val text = files.getString(name)
|
||||
when {
|
||||
@@ -100,7 +124,8 @@ internal fun mergeFamilyFiles(dir: File, files: JSONObject, nowMs: Long = System
|
||||
val target = File(dir, name).also { it.parentFile?.mkdirs() }
|
||||
// Lines a newer Orbit wrote are kept as they are, so this TV never loses them (compactTasteLines).
|
||||
val here = if (target.exists()) target.readLines() else emptyList()
|
||||
val merged = compactTasteLines(here + text.lines(), nowMs)
|
||||
val resetAt = runCatching { JSONObject(File(dir, name.removeSuffix(".events.jsonl") + ".state.json").readText()).optLong("resetAt") }.getOrDefault(0L)
|
||||
val merged = compactTasteLines(here + text.lines(), nowMs, resetAt)
|
||||
writeSafely(target, merged.joinToString("") { it + "\n" })
|
||||
}
|
||||
name.startsWith("taste/") && name.endsWith(".state.json") && TasteFiles.matches(name.removePrefix("taste/")) ->
|
||||
@@ -109,7 +134,8 @@ internal fun mergeFamilyFiles(dir: File, files: JSONObject, nowMs: Long = System
|
||||
val target = File(dir, name)
|
||||
fun marks(t: String) = runCatching { JSONArray(t).let { a -> (0 until a.length()).mapNotNull { runCatching { ContinueMark.fromJson(a.getJSONObject(it)) }.getOrNull() } } }.getOrDefault(emptyList())
|
||||
val all = (if (target.exists()) marks(target.readText()) else emptyList()) + marks(text)
|
||||
val latest = all.groupBy { it.number to it.startMs }.values.map { same -> same.maxBy { it.atMs } }.sortedByDescending { it.atMs }.take(20)
|
||||
val gone = ContinueGone.read(File(dir, "gone-$name"))
|
||||
val latest = ContinueGone.filter(all.groupBy { it.number to it.startMs }.values.map { same -> same.maxBy { it.atMs } }, gone).sortedByDescending { it.atMs }.take(20)
|
||||
writeSafely(target, JSONArray(latest.map { it.toJson() }).toString())
|
||||
}
|
||||
}
|
||||
@@ -160,7 +186,7 @@ internal object RoomBundle {
|
||||
.put("schedules", gzip(files(dir, dir.list().orEmpty().filter { ScheduleFiles.matches(it) }.sorted()).toString()))
|
||||
.put(RoomPart.Family.name, JSONObject().put("profiles", prefsJson(context, "orbit_profiles", null)).put("lock", prefsJson(context, "orbit_lock", listOf("after_dark_salt", "after_dark_pin")))
|
||||
// Each person's Taste and Continue watching (Rooms sync, 7 November 2026): merged on arrival, never overwriting.
|
||||
.put("people", gzip(files(dir, dir.list().orEmpty().filter { FamilyFiles.matches(it) }.sorted() +
|
||||
.put("people", gzip(files(dir, dir.list().orEmpty().filter { FamilyFiles.matches(it) || GoneFiles.matches(it) }.sorted() +
|
||||
File(dir, "taste").list().orEmpty().filter { TasteFiles.matches(it) }.sorted().map { "taste/$it" }).toString())))
|
||||
.put(RoomPart.Look.name, prefsJson(context, "orbit_settings", LookKeys))
|
||||
.put(RoomPart.Director.name, files(dir, DirectorFiles))
|
||||
|
||||
@@ -68,7 +68,7 @@ internal val TASTE_KNOWN_FIELDS = setOf("v", "id", "p", "k", "i", "s", "t", "c",
|
||||
* (a new kind of signal, or a newer schema) is kept exactly as written, as long as it has an id and a time, so tidying up
|
||||
* or a sync with an older TV never loses it. Damaged lines are still dropped.
|
||||
*/
|
||||
internal fun compactTasteLines(lines: List<String>, nowMs: Long): List<String> {
|
||||
internal fun compactTasteLines(lines: List<String>, nowMs: Long, resetAtMs: Long = 0L): List<String> {
|
||||
val byId = LinkedHashMap<String, Pair<Long, String>>()
|
||||
for (line in lines) {
|
||||
if (line.isBlank()) continue
|
||||
@@ -78,7 +78,7 @@ internal fun compactTasteLines(lines: List<String>, nowMs: Long): List<String> {
|
||||
val id = o.optString("id").takeIf { it.isNotBlank() && it.length <= 64 } ?: return@let null
|
||||
if (!o.has("w") || line.length > 4_000) null else Triple(id, o.optLong("w"), line.trim())
|
||||
} ?: continue
|
||||
if (nowMs - at > TASTE_MAX_AGE_MS || at > nowMs + 60_000L) continue
|
||||
if (nowMs - at > TASTE_MAX_AGE_MS || at > nowMs + 60_000L || at < resetAtMs) continue
|
||||
byId.putIfAbsent(id, at to text)
|
||||
}
|
||||
return byId.values.sortedBy { it.first }.takeLast(TASTE_MAX_EVENTS).map { it.second }
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
|
||||
/** Audit A3 (9 November 2026): what's removed in one room stays removed in the other. */
|
||||
class RoomRemovalsTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
private val now = 1_794_300_000_000L
|
||||
private fun mark(start: Long, at: Long) = ContinueMark(304, start, "4455505", 30 * 60_000L, 90 * 60_000L, at)
|
||||
private fun list(vararg m: ContinueMark) = JSONArray(m.map { it.toJson() }).toString()
|
||||
|
||||
@Test fun aRemovalInTheOtherRoomTakesItOffHere() {
|
||||
val dir = tmp.root
|
||||
File(dir, "continue-grown.json").writeText(list(mark(1_000L, now - 60_000)))
|
||||
// The other room still has it in its list, and also says it was taken off afterwards.
|
||||
mergeFamilyFiles(dir, JSONObject().put("continue-grown.json", list(mark(1_000L, now - 60_000)))
|
||||
.put("gone-continue-grown.json", JSONObject().put("304:1000", now - 30_000).toString()), now)
|
||||
assertEquals(0, JSONArray(File(dir, "continue-grown.json").readText()).length())
|
||||
}
|
||||
|
||||
@Test fun anOlderRoomCantBringItBack() {
|
||||
val dir = tmp.root
|
||||
File(dir, "gone-continue-grown.json").writeText(JSONObject().put("304:1000", now - 30_000).toString())
|
||||
mergeFamilyFiles(dir, JSONObject().put("continue-grown.json", list(mark(1_000L, now - 60_000))), now) // no gone file: an older Orbit
|
||||
assertEquals(0, JSONArray(File(dir, "continue-grown.json").readText()).length())
|
||||
}
|
||||
|
||||
@Test fun watchingItAgainAfterwardsBringsItBack() {
|
||||
assertEquals(1, ContinueGone.filter(listOf(mark(1_000L, now)), mapOf("304:1000" to now - 30_000)).size)
|
||||
}
|
||||
|
||||
@Test fun removalsAreKeptThreeDays() {
|
||||
val merged = ContinueGone.merged(mapOf("304:1" to now - 4 * 86_400_000L), mapOf("304:2" to now - 1_000), now)
|
||||
assertEquals(setOf("304:2"), merged.keys)
|
||||
}
|
||||
|
||||
@Test fun aTasteResetInTheOtherRoomClearsWhatCameBefore() {
|
||||
val dir = tmp.root; File(dir, "taste").mkdirs()
|
||||
val old = """{"v":1,"id":"a1","p":"grown","k":"stayed","i":"4455505","s":null,"t":"Film","c":304,"w":${now - 100_000},"ms":1500000,"src":"watching"}"""
|
||||
val newer = """{"v":1,"id":"b2","p":"grown","k":"stayed","i":"4455506","s":null,"t":"Film 2","c":304,"w":${now - 1_000},"ms":1500000,"src":"watching"}"""
|
||||
File(dir, "taste/grown.events.jsonl").writeText(old + "\n")
|
||||
File(dir, "taste/grown.state.json").writeText(JSONObject().put("v", 1).put("personalise", true).put("suppress", JSONObject()).put("resetAt", 0).toString())
|
||||
mergeFamilyFiles(dir, JSONObject().put("taste/grown.state.json", JSONObject().put("v", 1).put("personalise", true).put("resetAt", now - 50_000).toString())
|
||||
.put("taste/grown.events.jsonl", old + "\n" + newer + "\n"), now)
|
||||
assertEquals(listOf("b2"), File(dir, "taste/grown.events.jsonl").readLines().filter { it.isNotBlank() }.map { JSONObject(it).getString("id") })
|
||||
assertTrue(JSONObject(File(dir, "taste/grown.state.json").readText()).getBoolean("personalise")) // their own choice stays
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user