iOS app asks ipx in protobuf, not JSON (#469)

The badge's and CarPlay's reads (/api/feeds, the entry lists) and the
player's writes (position, flags) now go as application/x-protobuf, the
messages generated from crates/wire/proto by ios/scripts/proto.sh into
Sources/Wire, committed so the build needs no protoc. SwiftProtobuf is
pinned to the generator's version, 1.41.0, whose code refuses another.

A read whose answer is not protobuf counts as no answer: a server from
before #465 answers JSON whatever it is asked, and JSON can decode as a
message of nothing, which would have set the badge to zero.

The outbox keeps its stored key, `json`, for the body, and marks the
new writes protobuf, so places saved offline by an older build are
still sent, as JSON.

Built for the simulator with Xcode 27.2; not run against a server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ray Slakinski
2026-11-16 19:00:10 -05:00
parent a96be2f14c
commit baa6661622
13 changed files with 6118 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
import AVFoundation
import MediaPlayer
import SwiftProtobuf
import UIKit
/// The phone's player: the one that plays on with the screen locked, and the one the lock screen
@@ -269,14 +270,15 @@ final class Playback: NSObject {
s.position = Double(secs)
if let d = duration { s.duration = d }
}
var body: [String: Any] = ["secs": secs]
if let d = duration { body["duration"] = Int(d.rounded()) }
var body = Ipx_Position()
body.secs = Int64(secs)
if let d = duration { body.duration = Int64(d.rounded()) }
// DeadAir's copy runs on a shorter clock; the server has to know which one this is.
if item.url.path.contains("/deadair") { body["deadair"] = item.enc }
if item.url.path.contains("/deadair") { body.deadair = Int64(item.enc) }
let path = entryPath(item, "position")
// Not arrived, kept for when the connection is back: offline, the ten-second retry would
// only fail again, and the place reached on the last pause would be lost.
Task { if await request("POST", path, body) == nil { Outbox.add(path: path, json: body) } }
Task { if await request("POST", path, body) == nil { Outbox.add(path: path, body) } }
}
// MARK: - as it plays
@@ -314,7 +316,9 @@ final class Playback: NSObject {
markedRead = true
let path = entryPath(item, "flags")
Task {
if await request("POST", path, ["read": true]) == nil { Outbox.add(path: path, json: ["read": true]) }
var read = Ipx_Flags()
read.read = true
if await request("POST", path, read) == nil { Outbox.add(path: path, read) }
await Badge.refresh()
}
}
@@ -406,14 +410,14 @@ final class Playback: NSObject {
/// One call to ipx, signed in by the credentials or else the web view's cookies; its status, or nil if it never
/// arrived. A position that does not save is saved again ten seconds on.
@discardableResult
private func request(_ method: String, _ path: String, _ json: [String: Any]? = nil) async -> Int? {
private func request(_ method: String, _ path: String, _ body: (any SwiftProtobuf.Message)? = nil) async -> Int? {
guard let url = Server.resolveForPlayer(path) else { return nil }
var req = URLRequest(url: url)
req.httpMethod = method
Credentials.headers.forEach { req.setValue($1, forHTTPHeaderField: $0) }
if let json {
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.httpBody = try? JSONSerialization.data(withJSONObject: json)
if let body {
req.setValue(API.protobuf, forHTTPHeaderField: "Content-Type")
req.httpBody = try? body.serializedData()
}
guard let (_, resp) = try? await session.data(for: req) else { return nil }
return (resp as? HTTPURLResponse)?.statusCode