Skip to content

Commit be0f654

Browse files
authored
Merge pull request #712 from insidegui/rambo/config-improvements
Event hero / live event improvements
2 parents 0b5b43c + df24553 commit be0f654

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

Packages/ConfCore/ConfCore/AppleAPIClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public final class AppleAPIClient: Logging, Signposting {
196196
}
197197

198198
currentConfigRequest?.cancel()
199-
currentConfigRequest = configResource.loadIfNeeded()
199+
currentConfigRequest = configResource.load()
200200
}
201201

202202
}

Packages/ConfCore/ConfCore/Storage.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,23 @@ public final class Storage: Logging, Signposting {
332332
return
333333
}
334334

335-
performSerializedBackgroundWrite(disableAutorefresh: false, completionBlock: completion) { backgroundRealm in
335+
performSerializedBackgroundWrite(disableAutorefresh: false, completionBlock: completion) { [weak self] backgroundRealm in
336+
guard let self else { return }
337+
336338
// We currently only care about whatever the latest event hero is.
337339
let existingHeroData = backgroundRealm.objects(EventHero.self)
338-
backgroundRealm.delete(existingHeroData)
339-
}
340+
if !existingHeroData.isEmpty {
341+
self.log.info("Removing existing event hero")
342+
backgroundRealm.delete(existingHeroData)
343+
}
340344

341-
guard let hero = response.eventHero else {
342-
log.debug("Config response didn't contain an event hero")
343-
return
344-
}
345+
if let hero = response.eventHero {
346+
self.log.info("Storing event hero \(hero.identifier, privacy: .public)")
345347

346-
performSerializedBackgroundWrite(disableAutorefresh: false, completionBlock: completion) { backgroundRealm in
347-
backgroundRealm.add(hero, update: .modified)
348+
backgroundRealm.add(hero, update: .modified)
349+
} else {
350+
self.log.info("Config response had no event hero")
351+
}
348352
}
349353
}
350354

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)