mirror of
https://github.com/block/buzz.git
synced 2026-10-28 04:51:40 +00:00
Buzz push currently selects an application profile, such as `dogfood`, during client registration. We want each gateway to serve one Apple app configured by the operator instead. This removes profile selection from the client, relay, and gateway, while allowing internal and public gateways to use their own Apple identities. This PR implements that model in the Buzz source and upstream deployment chart. Discovery, enrollment, grants, and leases no longer carry an application-profile selector. Each gateway has one configured Apple application and APNs transport. The protocol changes are incompatible with the old registration format, so relays advertise `buzz-push-v1` instead of `nip-pl`. Old clients do not discover the new push capability. Updated clients use separate Keychain records and register again. The [push notes](https://github.com/block/buzz/blob/push-capability-v1/docs/buzz-push.md) describe the experimental contract. Landing this PR is the first step. Publish the matching gateway image and upstream chart, then land the [shared chart update](https://github.com/squareup/cash-helm-charts/pull/5740) and [gateway deployment update](https://github.com/squareup/builderbot-platform-core-infrastructure/pull/1085). Before cutover, stop old gateway instances and check legacy registration state. Migrations refuse to proceed if old gateway installations or active relay leases remain; they do not delete that state. Physical-device verification remains necessary before production activation. ### Validation Gateway and focused Flutter push tests pass, as do the Swift package, simulator build, and native notification/enrichment tests. A local relay advertises `buzz-push-v1` without `nip-pl` or `app_profiles`. The PostgreSQL suite passed 868 tests; its two migration-test failures were corrected and pass a focused rerun together with the gateway cutover guard. Chart rendering and release-contract checks pass. Desktop JavaScript tests, desktop and web builds, native desktop compilation, mobile analysis/tests, and desktop native tests pass. The full Rust unit lane and prescribed `just test` command pass, including workspace integration tests against task-isolated local services. The production overlay now requires an explicitly supplied APNs topic, with omission and rendered-value regression coverage. The corrected Swift checksum assertion passes the complete package suite: 78 XCTest tests and 42 Swift Testing tests. No physical-device enrollment or production cutover is claimed. Local merge validation: the embedded migration test and formatting pass after reserving profile migration 0060 behind rollback migration 0059. The local pre-push Rust lane failed ACP timing/steering tests (`idle_resets_on_stdout_activity`, `keepalive_resets_idle_past_deadline`, and the failed/missing-outcome steering tests). ACP and its dependency manifests are unchanged by this branch; all four pass in an isolated single-thread rerun. The documented local-check exception skips only the Rust pre-push lane for publication; required CI remains mandatory. Mobile and desktop pre-push lanes passed. Combined migration validation: after rollback 0059 landed, the main merge preserves profile migration 0060 and both embedded migration assertions. The focused migration test and formatting pass. The next local push again failed only the unchanged ACP idle/keepalive timing tests (1053 passed, 2 failed, 3 ignored); other lanes passed. Publication uses the same documented local Rust-lane exception; required CI remains mandatory. Follow-up review fix: removed the legacy profile column from the copied-lease fixture. All four event-follow-up contract tests pass on isolated PostgreSQL databases, covering desired and migrated schemas in dual and app-only modes. Formatting passes. Publication retains the prior passing local-lane evidence and uses the authorized pre-push exception for the known unrelated ACP timing failures; required CI remains mandatory. --------- Signed-off-by: Tom Brow <tomb@block.xyz>
386 lines
13 KiB
Swift
386 lines
13 KiB
Swift
import BuzzPushKit
|
|
import Flutter
|
|
import Foundation
|
|
import Intents
|
|
import UserNotifications
|
|
|
|
final class BuzzPushSnapshotBridge {
|
|
private let containerURL: () -> URL?
|
|
private let endpointGrantStore: BuzzPushEndpointGrantKeychainStore
|
|
private let interactionDeletionDeadline: BuzzInteractionDeletionDeadline
|
|
private let keychainAccessGroup: String?
|
|
private let queue = DispatchQueue(
|
|
label: "xyz.block.buzz.push-snapshot",
|
|
qos: .utility
|
|
)
|
|
private lazy var store: BuzzPushPresentationCacheStore? = {
|
|
guard let container = containerURL() else { return nil }
|
|
return BuzzPushPresentationCacheStore(containerURL: container)
|
|
}()
|
|
private let ageRestrictionSession = BuzzAgeRestrictionSession()
|
|
|
|
init(
|
|
appGroupIdentifier: String?,
|
|
endpointGrantStore: BuzzPushEndpointGrantKeychainStore,
|
|
keychainAccessGroup: String?,
|
|
containerURL: (() -> URL?)? = nil,
|
|
interactionDeletionDeadline: BuzzInteractionDeletionDeadline =
|
|
BuzzInteractionDeletionDeadline(
|
|
timeout: 5,
|
|
deleteAllInteractions: { completion in
|
|
INInteraction.deleteAll(completion: completion)
|
|
},
|
|
scheduleTimeout: { delay, action in
|
|
DispatchQueue.global(qos: .utility).asyncAfter(
|
|
deadline: .now() + delay,
|
|
execute: action
|
|
)
|
|
}
|
|
)
|
|
) {
|
|
self.containerURL = containerURL ?? {
|
|
guard let appGroupIdentifier else { return nil }
|
|
return FileManager.default.containerURL(
|
|
forSecurityApplicationGroupIdentifier: appGroupIdentifier)
|
|
}
|
|
self.endpointGrantStore = endpointGrantStore
|
|
self.keychainAccessGroup = keychainAccessGroup
|
|
self.interactionDeletionDeadline = interactionDeletionDeadline
|
|
}
|
|
|
|
@discardableResult
|
|
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) -> Bool {
|
|
if call.method == "restoreAgeRestrictedNotifications" {
|
|
queue.async { [self] in
|
|
ageRestrictionSession.release()
|
|
BuzzInteractionCleanupRetry(
|
|
containerURL: containerURL(), deletion: interactionDeletionDeadline
|
|
).retryPending { error in
|
|
Self.complete(result, value: error.map {
|
|
FlutterError(code: "interaction_cleanup_retry_failed",
|
|
message: "Unable to finish pending interaction cleanup.",
|
|
details: $0.localizedDescription)
|
|
})
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
if call.method == "purgeAgeRestrictedNotifications" {
|
|
purgeAgeRestrictedNotifications(result: result)
|
|
return true
|
|
}
|
|
let strictAgeGateWrite = call.method == "syncAgeGatePushSnapshot"
|
|
guard strictAgeGateWrite || call.method == "syncPushSnapshot",
|
|
let arguments = call.arguments as? [String: Any],
|
|
let section = arguments["section"] as? String
|
|
else {
|
|
return false
|
|
}
|
|
switch section {
|
|
case "communities":
|
|
syncCommunities(
|
|
arguments,
|
|
requiresStore: strictAgeGateWrite,
|
|
result: result
|
|
)
|
|
case "profiles": cacheProfiles(arguments, result: result)
|
|
case "channels": cacheChannels(arguments, result: result)
|
|
case "avatar": cacheAvatar(arguments, result: result)
|
|
default: return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
private func purgeAgeRestrictedNotifications(result: @escaping FlutterResult) {
|
|
queue.async { [weak self] in
|
|
do {
|
|
guard let self, let container = containerURL() else {
|
|
throw NSError(
|
|
domain: "BuzzPushSnapshotBridge",
|
|
code: 1,
|
|
userInfo: [
|
|
NSLocalizedDescriptionKey: "The age-restriction session container is unavailable."
|
|
]
|
|
)
|
|
}
|
|
try ageRestrictionSession.restrict(containerURL: container)
|
|
// Preserve snapshots and credentials. Restriction authority expires
|
|
// with this process even when cleanup or a later storage read fails.
|
|
let center = UNUserNotificationCenter.current()
|
|
center.removeAllDeliveredNotifications()
|
|
center.removeAllPendingNotificationRequests()
|
|
BuzzInteractionCleanupRetry(
|
|
containerURL: container, deletion: interactionDeletionDeadline
|
|
).request { error in
|
|
Self.complete(
|
|
result,
|
|
value: error.map {
|
|
FlutterError(
|
|
code: "age_restriction_purge_failed",
|
|
message: "Unable to suppress and purge restricted notifications.",
|
|
details: $0.localizedDescription
|
|
)
|
|
}
|
|
)
|
|
}
|
|
} catch {
|
|
Self.complete(
|
|
result,
|
|
value: FlutterError(
|
|
code: "age_restriction_purge_failed",
|
|
message: "Unable to suppress and purge restricted notifications.",
|
|
details: error.localizedDescription
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func syncCommunities(
|
|
_ arguments: [String: Any],
|
|
requiresStore: Bool,
|
|
result: @escaping FlutterResult
|
|
) {
|
|
guard let communities = arguments["communities"] as? [[String: Any]],
|
|
let signingKeys = arguments["signingKeys"] as? [String: String],
|
|
communities.count <= BuzzPushPresentationCacheStore.maximumCommunities,
|
|
!requiresStore || arguments["settleFence"] is Bool
|
|
else {
|
|
result(
|
|
FlutterError(
|
|
code: "invalid_arguments",
|
|
message: "Expected bounded communities and signing keys.",
|
|
details: nil
|
|
)
|
|
)
|
|
return
|
|
}
|
|
queue.async { [weak self] in
|
|
do {
|
|
guard let self else {
|
|
Self.complete(
|
|
result,
|
|
value: requiresStore
|
|
? FlutterError(
|
|
code: "snapshot_sync_unavailable",
|
|
message: "The push snapshot bridge is unavailable.",
|
|
details: nil
|
|
)
|
|
: nil
|
|
)
|
|
return
|
|
}
|
|
guard let store else {
|
|
Self.complete(
|
|
result,
|
|
value: requiresStore
|
|
? FlutterError(
|
|
code: "snapshot_sync_unavailable",
|
|
message: "The push snapshot store is unavailable.",
|
|
details: nil
|
|
)
|
|
: nil
|
|
)
|
|
return
|
|
}
|
|
// Relay-metadata enrichment is optional presentation state. A damaged
|
|
// grant cache must not block the core NSE snapshot and key update.
|
|
let grants = (try? endpointGrantStore.records()) ?? []
|
|
let enriched = communities.map { community -> [String: Any] in
|
|
var community = community
|
|
guard let relayURL = community["relayUrl"] as? String,
|
|
let relayMetadataPubkey = Self.relayMetadataPubkey(
|
|
relayURL: relayURL,
|
|
grants: grants
|
|
)
|
|
else { return community }
|
|
community["relayMetadataPubkey"] = relayMetadataPubkey
|
|
return community
|
|
}
|
|
let data = try JSONSerialization.data(withJSONObject: enriched, options: [.sortedKeys])
|
|
let decoded = try JSONDecoder().decode([PushLeaseCommunity].self, from: data)
|
|
// Ordinary snapshot maintenance never changes age access.
|
|
try store.replaceCommunities(decoded)
|
|
try BuzzPushKeychain.replace(
|
|
signingKeys: signingKeys,
|
|
accessGroup: self.keychainAccessGroup
|
|
)
|
|
Self.complete(result, value: nil)
|
|
} catch {
|
|
Self.complete(
|
|
result,
|
|
value: FlutterError(
|
|
code: "snapshot_sync_failed",
|
|
message: "Unable to sync push community state.",
|
|
details: error.localizedDescription
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
static func relayMetadataPubkey(
|
|
relayURL: String,
|
|
grants: [BuzzPushEndpointGrantRecord]
|
|
) -> String? {
|
|
guard let origin = BuzzPushPresentationCacheStore.canonicalRelayOrigin(relayURL) else {
|
|
return nil
|
|
}
|
|
return grants.filter {
|
|
BuzzPushPresentationCacheStore.canonicalRelayOrigin($0.relayOrigin) == origin
|
|
}.max {
|
|
$0.generation < $1.generation
|
|
}?.relayMetadataPubkey
|
|
}
|
|
|
|
private func cacheProfiles(_ rawArguments: Any?, result: @escaping FlutterResult) {
|
|
guard let arguments = rawArguments as? [String: Any],
|
|
let communityID = arguments["communityId"] as? String,
|
|
let rawEvents = arguments["events"] as? [[String: Any]],
|
|
rawEvents.count <= BuzzPushPresentationCacheStore.maximumProfiles
|
|
else {
|
|
result(
|
|
FlutterError(
|
|
code: "invalid_arguments",
|
|
message: "Expected communityId and profile events.",
|
|
details: nil
|
|
)
|
|
)
|
|
return
|
|
}
|
|
queue.async { [weak self] in
|
|
do {
|
|
guard let self, let community = community(id: communityID) else {
|
|
Self.complete(result, value: nil)
|
|
return
|
|
}
|
|
let events = try decodeEvents(rawEvents)
|
|
try store?.updateProfiles(
|
|
communityID: communityID,
|
|
relayOrigin: community.relayUrl,
|
|
updates: events.map { BuzzPushProfileCacheUpdate(event: $0) }
|
|
)
|
|
Self.complete(result, value: nil)
|
|
} catch {
|
|
Self.complete(
|
|
result,
|
|
value: FlutterError(
|
|
code: "profile_cache_failed",
|
|
message: "Unable to cache push sender profiles.",
|
|
details: error.localizedDescription
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func cacheChannels(_ rawArguments: Any?, result: @escaping FlutterResult) {
|
|
guard let arguments = rawArguments as? [String: Any],
|
|
let communityID = arguments["communityId"] as? String,
|
|
let rawMetadataEvents = arguments["metadataEvents"] as? [[String: Any]],
|
|
let rawMembershipEvents = arguments["membershipEvents"] as? [[String: Any]],
|
|
rawMetadataEvents.count <= BuzzPushPresentationCacheStore.maximumChannels,
|
|
rawMembershipEvents.count <= BuzzPushPresentationCacheStore.maximumChannels
|
|
else {
|
|
result(
|
|
FlutterError(
|
|
code: "invalid_arguments",
|
|
message: "Expected communityId, channel metadata, and membership events.",
|
|
details: nil
|
|
)
|
|
)
|
|
return
|
|
}
|
|
queue.async { [weak self] in
|
|
do {
|
|
guard let self, let community = community(id: communityID),
|
|
let relayMetadataPubkey = community.relayMetadataPubkey
|
|
else {
|
|
Self.complete(result, value: nil)
|
|
return
|
|
}
|
|
try store?.updateChannels(
|
|
communityID: communityID,
|
|
relayOrigin: community.relayUrl,
|
|
relayMetadataPubkey: relayMetadataPubkey,
|
|
metadataEvents: try decodeEvents(rawMetadataEvents),
|
|
membershipEvents: try decodeEvents(rawMembershipEvents)
|
|
)
|
|
Self.complete(result, value: nil)
|
|
} catch {
|
|
Self.complete(
|
|
result,
|
|
value: FlutterError(
|
|
code: "channel_cache_failed",
|
|
message: "Unable to cache push channel metadata.",
|
|
details: error.localizedDescription
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func cacheAvatar(_ rawArguments: Any?, result: @escaping FlutterResult) {
|
|
guard let arguments = rawArguments as? [String: Any],
|
|
let communityID = arguments["communityId"] as? String,
|
|
let sourceURL = arguments["sourceUrl"] as? String,
|
|
let avatar = arguments["png"] as? FlutterStandardTypedData
|
|
else {
|
|
result(
|
|
FlutterError(
|
|
code: "invalid_arguments",
|
|
message: "Expected an avatar source and PNG thumbnail.",
|
|
details: nil
|
|
)
|
|
)
|
|
return
|
|
}
|
|
let avatarData = avatar.data
|
|
queue.async { [weak self] in
|
|
do {
|
|
guard let self, let community = community(id: communityID) else {
|
|
Self.complete(result, value: false)
|
|
return
|
|
}
|
|
let updated =
|
|
try store?.updateAvatar(
|
|
communityID: communityID,
|
|
relayOrigin: community.relayUrl,
|
|
sourceURL: sourceURL,
|
|
avatarPNG: avatarData
|
|
) ?? false
|
|
Self.complete(result, value: updated)
|
|
} catch {
|
|
Self.complete(
|
|
result,
|
|
value: FlutterError(
|
|
code: "avatar_cache_failed",
|
|
message: "Unable to cache a push sender avatar.",
|
|
details: error.localizedDescription
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func community(id: String) -> PushLeaseCommunity? {
|
|
guard let container = containerURL(),
|
|
let data = try? Data(
|
|
contentsOf: container.appendingPathComponent(BuzzPushPresentationCacheStore.fileName)),
|
|
let snapshot = try? JSONDecoder().decode(BuzzPushPresentationCacheSnapshot.self, from: data)
|
|
else { return nil }
|
|
return snapshot.communities.first { $0.id == id }
|
|
}
|
|
|
|
private func decodeEvents(_ rawEvents: [[String: Any]]) throws -> [VerifiedNostrEvent] {
|
|
let data = try JSONSerialization.data(withJSONObject: rawEvents)
|
|
return try JSONDecoder().decode([VerifiedNostrEvent].self, from: data)
|
|
}
|
|
|
|
private static func complete(_ result: @escaping FlutterResult, value: Any?) {
|
|
DispatchQueue.main.async {
|
|
result(value)
|
|
}
|
|
}
|
|
}
|