Skip to content

Commit df24553

Browse files
committed
Require remote live session record to have overrideState=1 in order to override existing local state
1 parent 5323553 commit df24553

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

WWDC/LiveObserver.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,43 @@ private final class CloudKitLiveObserver: Logging {
254254
private func store(_ records: [CKRecord]) {
255255
log.debug("Storing live records")
256256

257-
storage.backgroundUpdate { realm in
257+
storage.backgroundUpdate { [weak self] realm in
258+
guard let self else { return }
259+
258260
records.forEach { record in
259261
guard let asset = SessionAsset(record: record) else { return }
260262
guard let session = realm.object(ofType: Session.self, forPrimaryKey: asset.sessionId) else { return }
261263
guard let instance = session.instances.first else { return }
262264

265+
/// Allow record to override state from the backend API only if the record's `overrideState` is set to `1`,
266+
/// otherwise existing local data from Apple's backend always wins.
267+
let canOverrideExistingState = record["overrideState"] as? Int == 1
268+
263269
if let existingAsset = realm.object(ofType: SessionAsset.self, forPrimaryKey: asset.identifier) {
264-
// update existing asset hls URL if appropriate
265-
existingAsset.remoteURL = asset.remoteURL
270+
/// Update existing asset hls URL if appropriate
271+
if canOverrideExistingState {
272+
existingAsset.remoteURL = asset.remoteURL
273+
} else {
274+
self.log.info("Ignoring remoteURL override from record for \(asset.sessionId, privacy: .public) because overrideState is not 1")
275+
}
266276
} else {
267277
// add new live asset to corresponding session
268278
session.assets.append(asset)
269279
}
270280

271281
instance.isForcedLive = (record["isLive"] as? Int == 1)
272-
instance.isCurrentlyLive = instance.isForcedLive
282+
283+
/// Allow record to override live state from not live to live, but not the other way around.
284+
if !instance.isCurrentlyLive {
285+
instance.isCurrentlyLive = instance.isForcedLive
286+
} else {
287+
/// Allow record to override live state however it wants if record's `overrideState` is `1`.
288+
if canOverrideExistingState {
289+
instance.isCurrentlyLive = instance.isForcedLive
290+
} else {
291+
self.log.info("Ignoring live state override from record for \(asset.sessionId, privacy: .public) because overrideState is not 1")
292+
}
293+
}
273294
}
274295
}
275296
}

0 commit comments

Comments
 (0)