Profiles keep fields a newer Orbit adds; rooms sync accepts newer message versions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -58,7 +58,9 @@ internal data class Profile(val id: String, val name: String, val picture: Strin
|
||||
/** Kids only: Arcade for Kids, or null for a profile saved before it existed (then [arcade] decides). */
|
||||
val arcadeMode: KidsArcade? = null,
|
||||
/** Kids only, with [KidsArcade.Chosen]: the games chosen for it, by Arcade game id. */
|
||||
val arcadeGames: Set<String> = emptySet()) {
|
||||
val arcadeGames: Set<String> = emptySet(),
|
||||
/** Fields a newer Orbit added that this one doesn't know, kept as they came so syncing through this TV never loses them (12 November 2026). */
|
||||
val extra: String = "{}") {
|
||||
/** Arcade for Kids as it applies: older profiles had only on (the kids-suitable games) or off. */
|
||||
val kidsArcade: KidsArcade get() = arcadeMode ?: if (arcade) KidsArcade.KidsSuitable else KidsArcade.Off
|
||||
/** Arcade for Kids changed; [arcade] follows it, so rooms on an older Orbit still read on or off correctly. */
|
||||
@@ -154,7 +156,9 @@ internal object Profiles {
|
||||
JSONObject().put("id", p.id).put("name", p.name).put("picture", p.picture).put("kind", p.kind.name)
|
||||
.put("channels", JSONArray(p.channels.sorted())).put("arcade", p.arcade).put("driveIn", p.driveIn).put("u", p.updatedMs)
|
||||
.also { o -> p.arcadeMode?.let { o.put("arcadeMode", it.name) }; if (p.arcadeGames.isNotEmpty()) o.put("arcadeGames", JSONArray(p.arcadeGames.sorted())) }
|
||||
.also { o -> runCatching { JSONObject(p.extra).let { x -> x.keys().forEach { k -> if (!o.has(k)) o.put(k, x.get(k)) } } } }
|
||||
}).toString()
|
||||
private val KnownProfileKeys = setOf("id", "name", "picture", "kind", "channels", "arcade", "driveIn", "u", "arcadeMode", "arcadeGames")
|
||||
fun decode(text: String): List<Profile> {
|
||||
val a = JSONArray(text)
|
||||
return (0 until a.length()).map { i ->
|
||||
@@ -163,7 +167,8 @@ internal object Profiles {
|
||||
Profile(o.getString("id"), o.getString("name").let { if (it == "Grown-ups") "Adult" else it }, o.getString("picture"), ProfileKind.valueOf(o.getString("kind")),
|
||||
ch?.let { c -> (0 until c.length()).map { c.getInt(it) }.toSet() }.orEmpty(), o.optBoolean("arcade", true), o.optBoolean("driveIn", false), o.optLong("u"),
|
||||
o.optString("arcadeMode").let { m -> KidsArcade.entries.firstOrNull { it.name == m } },
|
||||
o.optJSONArray("arcadeGames")?.let { g -> (0 until g.length()).mapNotNull { g.optString(it).takeIf { s -> s.isNotEmpty() } }.toSet() }.orEmpty())
|
||||
o.optJSONArray("arcadeGames")?.let { g -> (0 until g.length()).mapNotNull { g.optString(it).takeIf { s -> s.isNotEmpty() } }.toSet() }.orEmpty(),
|
||||
JSONObject().also { x -> o.keys().forEach { k -> if (k !in KnownProfileKeys) x.put(k, o.get(k)) } }.toString())
|
||||
}.takeIf { list -> list.any { it.kind == ProfileKind.Grown } } ?: DefaultProfiles
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,8 @@ internal object RoomSync {
|
||||
/** Opens and checks a sealed message: the pair's key, a fresh time, a nonce not seen before. Null when it fails any of them. */
|
||||
@Synchronized fun open(link: RoomLink, sealed: String, nowMs: Long): JSONObject? {
|
||||
val o = runCatching { JSONObject(String(RoomCrypto.open(link.key, sealed))) }.getOrNull() ?: return null
|
||||
if (o.optInt("v") != 1 || kotlin.math.abs(nowMs - o.optLong("at")) > ROOM_SYNC_WINDOW_MS) return null
|
||||
// A newer Orbit may say v 2 or more: what it adds is ignored here, and what this one knows is still taken (12 November 2026).
|
||||
if (o.optInt("v") < 1 || kotlin.math.abs(nowMs - o.optLong("at")) > ROOM_SYNC_WINDOW_MS) return null
|
||||
val nonce = o.optString("nonce"); if (nonce.isBlank() || nonce in seen) return null
|
||||
seen.addLast(nonce); while (seen.size > 200) seen.removeFirst()
|
||||
return o
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
/** Two TVs on different versions (12 November 2026): what a newer Orbit adds passes through an older one unharmed. */
|
||||
class ForwardCompatTest {
|
||||
@Test fun aProfileKeepsFieldsItDoesNotKnow() {
|
||||
val newer = JSONArray().put(JSONObject().put("id", "p1").put("name", "Adult").put("picture", "a").put("kind", "Grown").put("u", 5)
|
||||
.put("bedtime", "19:30").put("favouriteLayout", JSONObject().put("name", "Mosaic")))
|
||||
val decoded = Profiles.decode(newer.toString())
|
||||
val again = JSONArray(Profiles.encode(decoded)).getJSONObject(0)
|
||||
assertEquals("19:30", again.getString("bedtime"))
|
||||
assertEquals("Mosaic", again.getJSONObject("favouriteLayout").getString("name"))
|
||||
assertEquals("Adult", again.getString("name"))
|
||||
}
|
||||
|
||||
@Test fun knownFieldsAreNeverDuplicatedAsExtras() {
|
||||
val p = Profiles.decode(JSONArray().put(JSONObject().put("id", "p1").put("name", "Adult").put("picture", "a").put("kind", "Grown")).toString()).single()
|
||||
assertEquals("{}", p.extra)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user