mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
Lets people share screenshots, screen recordings, photos, URLs and text from any iOS app into Buzz. Today Buzz doesn't appear in the share sheet at all. - **Plain Buzz icon:** opens a new Solo chat with the items in its composer. - **Suggested conversation:** iOS offers channels and DMs the person recently sent top-level messages in. Choosing one opens that conversation with the items in its composer. When the conversation is already open, the share goes to that page's composer instead of opening a second one; if another page covers it, Buzz stays where it is and shows "Go back to that conversation to finish sharing", and the items join the draft when the person goes back. - The share extension has no UI and sends nothing. It copies the items into the app group and opens Buzz, which prefills the normal composer, so drafts, attachments, mentions and failed-send handling all stay the app's (see [the ADR](https://github.com/block/buzz/blob/mobile-share-extension-design/docs/adr/0003-share-hands-off-to-the-conversation.md)). - If the conversation already has a draft, Buzz asks whether to add the shared items to it or replace it. - A share to a conversation that's no longer available (a removed community or channel, or an unreadable share) shows "That conversation isn't available". A share while signed out shows "Sign in to share". Suggestions come from an outgoing `INSendMessageIntent` the app donates after a top-level send or a new Solo chat. The notification extension no longer donates incoming messages, so receiving a message never makes a conversation a suggestion; it still turns the push into a communication notification with the sender's avatar. Both use the same conversation identifier, now the plain community and channel IDs instead of a hash, because the extension needs to hand the identifier back to the app. | Before | After | | --- | --- | | <img alt="Share sheet without Buzz" src="https://github.com/user-attachments/assets/452fac1b-7e87-44c3-a442-099f78d0e07b" width="300"> | <img alt="Share sheet with Buzz" src="https://github.com/user-attachments/assets/54b9775d-29b4-49a5-8ab9-5fda8718a02f" width="300"> | | Buzz icon: new Solo chat | Suggested DM with a draft | After Add | | --- | --- | --- | | <img alt="New Solo chat with the shared photo" src="https://github.com/user-attachments/assets/c5da4d8b-1dd8-4e30-9e92-9dba515b3623" width="250"> | <img alt="Add to your draft prompt" src="https://github.com/user-attachments/assets/2f879f8a-3b9f-48e0-b963-2828e90dc9fe" width="250"> | <img alt="Draft with the shared link and photo added" src="https://github.com/user-attachments/assets/12832006-85c5-4447-98b5-62932552c931" width="250"> | https://github.com/user-attachments/assets/b9d380fa-e802-4ed7-89a8-592a03784c8f ### Regressions - Nothing changes for people who never use the share sheet, except that sending a top-level message or starting a Solo chat now makes one local `INInteraction.donate` call (skipped while the age restriction is on, which still purges every interaction). - Communication notifications look the same. Dropping the incoming donation removes work (and a deletion fence) from the notification extension's time budget. - The share hand-off reuses the existing deep link dispatcher. A share link is a new link type; notification and message links take the same paths as before. ### Validation - Added tests: - Dispatcher hand-off: a share to a suggested channel opens it with the shared text and files; shares to a removed community, an unlisted channel, an unknown manifest version or a missing manifest show the error and delete the pending share; share links accept only the extension's UUID ids. - Composer delivery: with an existing draft, Add appends the shared text on a new line and attaches the file, Replace clears the draft first, and the app group copy is deleted either way. - Notification presenter: specializes the notification from the intent, or falls back to the ordinary content when that throws (replaces the donation and deletion-fence tests). - Full mobile Flutter suite and BuzzPushKit Swift tests pass. - Automated app run on an iPhone 17 Pro simulator (iOS 27, dev relay test community), recorded above: sent a DM, shared a photo from Photos with the Buzz icon from a cold start (new Solo chat with the photo attached), then handed a link and photo to a DM that had a draft and chose Add. Also checked that a share's pending files are deleted after delivery. - Limitation: the iOS simulator never shows share-sheet conversation suggestions (it has no `coreduetd`), so the suggested-DM step in the video is staged by writing the same pending share and link the extension writes. Choosing a real suggestion still needs a check on a device. - Known gaps, all confined to sharing: suggestions have no avatar image yet; a suggestion for a conversation left after signing out shows "isn't available"; choosing Later in the unfinished-chat dialog keeps the share until the next new chat; and a pending share the app never receives (for example when Buzz doesn't open) stays in the app group. ### Before merge - [x] On a real iPhone, choose a suggested conversation in the share sheet and confirm the shared items open in that conversation's composer (the simulator can't show suggestions). Waived before merge: the managed test iPhone shows no share-sheet suggestions for any app, so this will be checked on a personal phone after landing. --------- Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Tom Brow <tomb@squareup.com>
830 lines
33 KiB
Swift
830 lines
33 KiB
Swift
import Foundation
|
|
|
|
#if canImport(FoundationNetworking)
|
|
import FoundationNetworking
|
|
#endif
|
|
|
|
/// Content resolved from unread Buzz events for a mutable push notification.
|
|
public struct BuzzPushResolution: Decodable, Equatable, Sendable {
|
|
/// Local sign-in that authorized this result; checked again before delivery.
|
|
public let notificationScope: BuzzNotificationCacheScope?
|
|
public let title: String
|
|
public let body: String
|
|
public let subtitle: String?
|
|
public let threadIdentifier: String?
|
|
public let navigationTarget: BuzzPushNavigationTarget?
|
|
public let senderPubkey: String?
|
|
public let senderAvatarPNG: Data?
|
|
public let conversationIdentifier: String?
|
|
public let conversationDisplayName: String?
|
|
/// Exact verified recipient count for Communication Notifications specialization.
|
|
public let conversationRecipientCount: Int?
|
|
|
|
public init(
|
|
title: String,
|
|
body: String,
|
|
subtitle: String?,
|
|
threadIdentifier: String?,
|
|
navigationTarget: BuzzPushNavigationTarget? = nil,
|
|
senderPubkey: String? = nil,
|
|
senderAvatarPNG: Data? = nil,
|
|
conversationIdentifier: String? = nil,
|
|
conversationDisplayName: String? = nil,
|
|
conversationRecipientCount: Int? = nil,
|
|
notificationScope: BuzzNotificationCacheScope? = nil
|
|
) {
|
|
self.notificationScope = notificationScope
|
|
self.title = title
|
|
self.body = body
|
|
self.subtitle = subtitle
|
|
self.threadIdentifier = threadIdentifier
|
|
self.navigationTarget = navigationTarget
|
|
self.senderPubkey = senderPubkey
|
|
self.senderAvatarPNG = senderAvatarPNG
|
|
self.conversationIdentifier = conversationIdentifier
|
|
self.conversationDisplayName = conversationDisplayName
|
|
self.conversationRecipientCount = conversationRecipientCount
|
|
}
|
|
}
|
|
|
|
/// Resolves the content used to mutate a generic Buzz push notification.
|
|
public protocol BuzzPushNotificationResolving {
|
|
func resolve(completion: @escaping (BuzzPushResolution?) -> Void)
|
|
/// Cancels any retry in flight and prevents new ones. Call when the notification is delivered.
|
|
func cancel()
|
|
/// Whether the organization session behind the resolved message, if any, still holds.
|
|
func isStillAuthorized() -> Bool
|
|
}
|
|
|
|
/// Reads configured Buzz communities and resolves their newest message not yet shown.
|
|
public final class BuzzPushNotificationResolver: BuzzPushNotificationResolving {
|
|
// Each independent refresh has a hard transfer limit, allowing bounded
|
|
// content plus JSON escaping, tags, and envelopes.
|
|
// Each record still passes the cache's separate content and signature checks.
|
|
static let maximumPresentationResponseBytes = 2 * 1_024 * 1_024
|
|
|
|
private let session: URLSession
|
|
private let makeRetrySession: () -> URLSession
|
|
// Recursive because a retry that fails synchronously completes while the lock is held,
|
|
// and that completion may finish the notification and call cancel().
|
|
private let retryLock = NSRecursiveLock()
|
|
private var isCancelled = false
|
|
private var retrySessions: [URLSession] = []
|
|
private let loadCommunitiesData: () -> Data?
|
|
private let loadPrivateKey: (String) -> String?
|
|
private let assertionLock = NSLock()
|
|
private var resolvedAssertion: BuzzRelayAssertion?
|
|
private let now: () -> Date
|
|
private let notificationCache: BuzzNotificationCache?
|
|
private let avatarLoader: BuzzNotificationAvatarLoader?
|
|
private let assertionIssuer: BuzzRelayAssertionIssuer?
|
|
|
|
/// Creates a resolver around the notification extension's App Group and Keychain I/O.
|
|
public init(
|
|
session: URLSession,
|
|
loadCommunitiesData: @escaping () -> Data?,
|
|
loadPrivateKey: @escaping (String) -> String?,
|
|
notificationCache: BuzzNotificationCache? = nil,
|
|
avatarLoader: BuzzNotificationAvatarLoader? = nil,
|
|
assertionIssuer: BuzzRelayAssertionIssuer? = nil,
|
|
now: @escaping () -> Date = Date.init,
|
|
makeRetrySession: (() -> URLSession)? = nil
|
|
) {
|
|
self.makeRetrySession = makeRetrySession ?? { URLSession(configuration: session.configuration) }
|
|
self.notificationCache = notificationCache
|
|
self.avatarLoader = avatarLoader
|
|
self.assertionIssuer = assertionIssuer
|
|
self.session = session
|
|
self.loadCommunitiesData = loadCommunitiesData
|
|
self.loadPrivateKey = loadPrivateKey
|
|
self.now = now
|
|
}
|
|
|
|
public func resolve(completion: @escaping (BuzzPushResolution?) -> Void) {
|
|
let communities = loadCommunities().filter {
|
|
$0.pubkey?.isEmpty == false
|
|
&& accountPrivateKey(for: $0) != nil
|
|
&& !$0.policies.isEmpty
|
|
}
|
|
guard !communities.isEmpty else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
let group = DispatchGroup()
|
|
let lock = NSLock()
|
|
var candidates: [(VerifiedNostrEvent, PushLeaseCommunity, BuzzRelayAssertion?)] = []
|
|
for community in communities {
|
|
group.enter()
|
|
query(community) { messages, assertion in
|
|
lock.lock()
|
|
candidates += messages.map { ($0, community, assertion) }
|
|
lock.unlock()
|
|
group.leave()
|
|
}
|
|
}
|
|
group.notify(queue: .global(qos: .userInitiated)) {
|
|
let newestFirst = candidates.sorted {
|
|
$0.0.createdAt == $1.0.createdAt ? $0.0.id < $1.0.id : $0.0.createdAt > $1.0.createdAt
|
|
}
|
|
guard let newest = self.firstUnpresented(newestFirst, communities: communities) else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
self.assertionLock.lock()
|
|
self.resolvedAssertion = newest.2
|
|
self.assertionLock.unlock()
|
|
self.resolvePresentation(event: newest.0, community: newest.1, completion: completion)
|
|
}
|
|
}
|
|
|
|
/// The push doesn't say which event it is for, so take the newest message not yet
|
|
/// shown, in any community. Without extension storage, fall back to the newest message.
|
|
/// The claim is made before presentation: if presenting then fails or expires, that
|
|
/// push shows the generic text and the message isn't offered again, which is better
|
|
/// than repeating a message in later notifications.
|
|
private func firstUnpresented(
|
|
_ newestFirst: [(VerifiedNostrEvent, PushLeaseCommunity, BuzzRelayAssertion?)],
|
|
communities: [PushLeaseCommunity]
|
|
) -> (VerifiedNostrEvent, PushLeaseCommunity, BuzzRelayAssertion?)? {
|
|
guard let notificationCache else { return newestFirst.first }
|
|
let candidates = newestFirst.compactMap { event, community, assertion in
|
|
BuzzNotificationCacheScope(community: community).map {
|
|
(candidate: (event, community, assertion), scope: $0)
|
|
}
|
|
}
|
|
do {
|
|
// Every community's history bounds the others, even one whose query failed, so a
|
|
// community with nothing shown yet can't surface its old messages.
|
|
return try notificationCache.claimFirstUnpresented(
|
|
candidates.map { (event: $0.candidate.0, scope: $0.scope) },
|
|
scopes: communities.compactMap(BuzzNotificationCacheScope.init(community:))
|
|
).map { candidates[$0].candidate }
|
|
} catch {
|
|
// Unusable storage must not hide new messages; it can only fail to dedupe them.
|
|
NSLog("Notification dedupe unavailable: %@", String(describing: error))
|
|
return candidates.first?.candidate
|
|
}
|
|
}
|
|
|
|
private func matchingPrivateKey(for community: PushLeaseCommunity) -> String? {
|
|
guard let scope = BuzzNotificationCacheScope(community: community),
|
|
loadCommunities().contains(where: { BuzzNotificationCacheScope(community: $0) == scope })
|
|
else { return nil }
|
|
return accountPrivateKey(for: community)
|
|
}
|
|
|
|
// Initial requests share the snapshot loaded by resolve. Asynchronous results
|
|
// use matchingPrivateKey to check that the account and session are still current.
|
|
private func accountPrivateKey(for community: PushLeaseCommunity) -> String? {
|
|
guard let scope = BuzzNotificationCacheScope(community: community),
|
|
let key = loadPrivateKey(community.id), scope.matchesAccountKey(key)
|
|
else { return nil }
|
|
return key
|
|
}
|
|
|
|
private func query(
|
|
_ community: PushLeaseCommunity,
|
|
completion: @escaping ([VerifiedNostrEvent], BuzzRelayAssertion?) -> Void
|
|
) {
|
|
guard let privateKey = accountPrivateKey(for: community), community.pubkey?.isEmpty == false else {
|
|
completion([], nil)
|
|
return
|
|
}
|
|
guard
|
|
!community.policies.isEmpty,
|
|
let relayURL = community.relayURL,
|
|
let url = URL(string: "/query", relativeTo: relayURL),
|
|
let body = try? JSONSerialization.data(
|
|
withJSONObject: community.policies.map { $0.filter.queryFilter(since: nil, limit: 10) }
|
|
)
|
|
else {
|
|
completion([], nil)
|
|
return
|
|
}
|
|
withFederatedIdentity(relayURL: relayURL, privateKey: privateKey) {
|
|
(assertion, finish: @escaping ((Data?, URLResponse?), Bool) -> Void) in
|
|
self.send(
|
|
url: url, body: body, privateKey: privateKey, federatedIdentity: assertion?.header,
|
|
session: self.session,
|
|
canRetry: {
|
|
self.matchingPrivateKey(for: community) == privateKey && self.isCurrent(assertion)
|
|
}
|
|
) { data, response in
|
|
finish((data, response), BuzzRelayAssertionIssuer.isChallenge(response))
|
|
}
|
|
} completion: { result, assertion in
|
|
let (data, response) = result
|
|
guard self.matchingPrivateKey(for: community) != nil, self.isCurrent(assertion),
|
|
let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode),
|
|
let data, let events = try? JSONDecoder().decode([VerifiedNostrEvent].self, from: data)
|
|
else {
|
|
completion([], assertion)
|
|
return
|
|
}
|
|
completion(
|
|
Self.messages(
|
|
events: events.filter { event in
|
|
event.hasValidIDAndSignature()
|
|
&& community.policies.contains { policy in
|
|
PushLeaseMatcher.matches(event: event, policy: policy)
|
|
}
|
|
},
|
|
community: community
|
|
), assertion)
|
|
}
|
|
}
|
|
|
|
/// Runs `attempt` with the relay's NIP-FI assertion, if any. A hosted relay is tried without
|
|
/// one first; only if it challenges does the extension ask Builderlab, then retry once.
|
|
private func withFederatedIdentity<Output>(
|
|
relayURL: URL, privateKey: String,
|
|
attempt: @escaping (BuzzRelayAssertion?, @escaping (Output, Bool) -> Void) -> Void,
|
|
completion: @escaping (Output, BuzzRelayAssertion?) -> Void
|
|
) {
|
|
federatedIdentity(relayURL: relayURL, privateKey: privateKey) { assertion in
|
|
attempt(assertion) { output, challenged in
|
|
guard challenged, assertion == nil, let assertionIssuer = self.assertionIssuer else {
|
|
completion(output, assertion)
|
|
return
|
|
}
|
|
assertionIssuer.header(relayURL: relayURL, privateKeyHex: privateKey, challenged: true) {
|
|
answer in
|
|
guard let answer else {
|
|
completion(output, nil)
|
|
return
|
|
}
|
|
attempt(answer) { output, _ in completion(output, answer) }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The NIP-FI assertion an organization relay requires, or nil for any other relay.
|
|
private func federatedIdentity(
|
|
relayURL: URL, privateKey: String, completion: @escaping (BuzzRelayAssertion?) -> Void
|
|
) {
|
|
guard let assertionIssuer else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
assertionIssuer.header(relayURL: relayURL, privateKeyHex: privateKey, completion: completion)
|
|
}
|
|
|
|
/// Whether the organization session behind the resolved message, if any, is still the shared
|
|
/// one. The extension checks this again before delivering, as it does the account scope.
|
|
public func isStillAuthorized() -> Bool {
|
|
assertionLock.lock()
|
|
let assertion = resolvedAssertion
|
|
assertionLock.unlock()
|
|
return isCurrent(assertion)
|
|
}
|
|
|
|
/// Responses fetched with an assertion are accepted only while its organization session lasts.
|
|
private func isCurrent(_ assertion: BuzzRelayAssertion?) -> Bool {
|
|
guard let assertion else { return true }
|
|
return assertionIssuer?.isCurrent(assertion) ?? false
|
|
}
|
|
|
|
/// Sends one signed `/query`. A timeout or dropped connection is retried once on a new
|
|
/// session, because the failed attempt may have used a pooled connection that went stale
|
|
/// when the network path changed. Each attempt is signed afresh so the relay's NIP-98
|
|
/// replay guard accepts it. `canRetry` rechecks the account first; nil means no retry.
|
|
public func cancel() {
|
|
retryLock.lock()
|
|
isCancelled = true
|
|
let sessions = retrySessions
|
|
retrySessions = []
|
|
retryLock.unlock()
|
|
sessions.forEach { $0.invalidateAndCancel() }
|
|
}
|
|
|
|
private func send(
|
|
url: URL, body: Data, privateKey: String, federatedIdentity: String?, session: URLSession,
|
|
canRetry: (() -> Bool)?,
|
|
completion: @escaping (Data?, URLResponse?) -> Void
|
|
) {
|
|
var request = URLRequest(url: url)
|
|
request.httpMethod = "POST"
|
|
request.httpBody = body
|
|
request.timeoutInterval = 8
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
guard
|
|
let auth = try? NostrHTTPAuth.authorizationHeader(
|
|
url: url, method: "POST", body: body, privateKeyHex: privateKey
|
|
)
|
|
else {
|
|
completion(nil, nil)
|
|
return
|
|
}
|
|
request.setValue(auth, forHTTPHeaderField: "Authorization")
|
|
if let federatedIdentity {
|
|
request.setValue(federatedIdentity, forHTTPHeaderField: "Nostr-Federated-Identity")
|
|
}
|
|
let task = session.dataTask(with: request) { data, response, error in
|
|
guard let error = error as? URLError,
|
|
[.timedOut, .networkConnectionLost].contains(error.code), canRetry?() == true
|
|
else {
|
|
completion(data, response)
|
|
return
|
|
}
|
|
// Creating the retry under the lock means cancel() either stops it or sees its session.
|
|
self.retryLock.lock()
|
|
guard !self.isCancelled else {
|
|
self.retryLock.unlock()
|
|
completion(data, response)
|
|
return
|
|
}
|
|
defer { self.retryLock.unlock() }
|
|
let retrySession = self.makeRetrySession()
|
|
self.retrySessions.append(retrySession)
|
|
self.send(
|
|
url: url, body: body, privateKey: privateKey, federatedIdentity: federatedIdentity,
|
|
session: retrySession, canRetry: nil
|
|
) { data, response in
|
|
retrySession.finishTasksAndInvalidate()
|
|
completion(data, response)
|
|
}
|
|
}
|
|
// A redirect would forward the federated identity header to its target, so it is refused.
|
|
task.delegate = BuzzRedirectRefusal.shared
|
|
task.resume()
|
|
}
|
|
|
|
private func resolvePresentation(
|
|
event: VerifiedNostrEvent,
|
|
community: PushLeaseCommunity,
|
|
completion: @escaping (BuzzPushResolution?) -> Void
|
|
) {
|
|
guard matchingPrivateKey(for: community) != nil else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
if let notificationCache {
|
|
guard let scope = BuzzNotificationCacheScope(community: community) else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
let operation = BuzzNotificationEnrichmentOperation(
|
|
event: event, community: community, scope: scope, cache: notificationCache,
|
|
avatarLoader: avatarLoader, now: now,
|
|
isCurrent: { [self] in
|
|
matchingPrivateKey(for: community) != nil && isStillAuthorized()
|
|
},
|
|
refresh: { [self] profile, channel, done in
|
|
refreshPresentation(event: event, community: community,
|
|
refreshProfile: profile, refreshChannel: channel, completion: done)
|
|
}, completion: completion)
|
|
operation.start()
|
|
return
|
|
}
|
|
// Without extension storage, keep the verified message but omit enrichment.
|
|
completion(Self.makeResolution(event: event, community: community, profile: nil, channel: nil))
|
|
}
|
|
|
|
private func refreshPresentation(
|
|
event: VerifiedNostrEvent, community: PushLeaseCommunity,
|
|
refreshProfile: Bool, refreshChannel: Bool,
|
|
completion: @escaping (BuzzNotificationEnrichmentOperation.RefreshResult) -> Void
|
|
) {
|
|
// A full channel roster can be much larger than a profile. Keep these
|
|
// transfers independent so its size or latency cannot hide the sender.
|
|
if refreshProfile {
|
|
queryPresentation(
|
|
filters: [["kinds": [0], "authors": [event.pubkey.lowercased()], "limit": 1]],
|
|
community: community
|
|
) { events in
|
|
completion(.profile(Self.newest(events.filter {
|
|
$0.kind == 0 && $0.pubkey.lowercased() == event.pubkey.lowercased()
|
|
})))
|
|
}
|
|
}
|
|
if refreshChannel {
|
|
guard let channelID = Self.tagValue("h", in: event),
|
|
let authority = community.relayMetadataPubkey?.lowercased()
|
|
else {
|
|
completion(.channel(nil, nil))
|
|
return
|
|
}
|
|
let group = DispatchGroup()
|
|
let lock = NSLock()
|
|
var records: [Int: VerifiedNostrEvent] = [:]
|
|
for kind in [39000, 39002] {
|
|
group.enter()
|
|
queryPresentation(filters: [[
|
|
"kinds": [kind], "authors": [authority], "#d": [channelID], "limit": 1,
|
|
]], community: community) { events in
|
|
let record = Self.newest(events.filter {
|
|
$0.kind == kind && $0.pubkey.lowercased() == authority
|
|
&& Self.tagValue("d", in: $0) == channelID
|
|
})
|
|
lock.lock()
|
|
records[kind] = record
|
|
lock.unlock()
|
|
group.leave()
|
|
}
|
|
}
|
|
group.notify(queue: .global(qos: .utility)) {
|
|
completion(.channel(records[39000], records[39002]))
|
|
}
|
|
}
|
|
}
|
|
|
|
private func queryPresentation(
|
|
filters: [[String: Any]], community: PushLeaseCommunity,
|
|
completion: @escaping ([VerifiedNostrEvent]) -> Void
|
|
) {
|
|
guard let privateKey = matchingPrivateKey(for: community),
|
|
let relayURL = community.relayURL,
|
|
let url = URL(string: "/query", relativeTo: relayURL),
|
|
let body = try? JSONSerialization.data(withJSONObject: filters)
|
|
else {
|
|
completion([])
|
|
return
|
|
}
|
|
withFederatedIdentity(relayURL: relayURL, privateKey: privateKey) {
|
|
(assertion, finish: @escaping (Result<Data, Error>?, Bool) -> Void) in
|
|
var request = URLRequest(url: url)
|
|
request.httpMethod = "POST"
|
|
request.httpBody = body
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
guard let auth = try? NostrHTTPAuth.authorizationHeader(
|
|
url: url, method: "POST", body: body, privateKeyHex: privateKey)
|
|
else {
|
|
finish(nil, false)
|
|
return
|
|
}
|
|
request.setValue(auth, forHTTPHeaderField: "Authorization")
|
|
if let assertion {
|
|
request.setValue(assertion.header, forHTTPHeaderField: "Nostr-Federated-Identity")
|
|
}
|
|
BuzzNotificationDownload.start(
|
|
request: request, configuration: self.session.configuration,
|
|
maximumBytes: Self.maximumPresentationResponseBytes
|
|
) { result in
|
|
guard case .failure(BuzzNotificationDownload.Failure.challenged) = result else {
|
|
finish(result, false)
|
|
return
|
|
}
|
|
finish(result, true)
|
|
}
|
|
} completion: { result, assertion in
|
|
guard self.matchingPrivateKey(for: community) != nil, self.isCurrent(assertion),
|
|
case .success(let data)? = result,
|
|
let events = try? JSONDecoder().decode([VerifiedNostrEvent].self, from: data)
|
|
else {
|
|
completion([])
|
|
return
|
|
}
|
|
completion(events.filter { $0.hasValidIDAndSignature() })
|
|
}
|
|
}
|
|
|
|
static func decodeResolution(
|
|
events: [VerifiedNostrEvent], community: PushLeaseCommunity
|
|
) -> (BuzzPushResolution, VerifiedNostrEvent)? {
|
|
let event = newestMessage(events: events, community: community)
|
|
guard let event else { return nil }
|
|
guard
|
|
let resolution = makeResolution(
|
|
event: event,
|
|
community: community,
|
|
profile: nil,
|
|
channel: nil
|
|
)
|
|
else { return nil }
|
|
return (resolution, event)
|
|
}
|
|
|
|
private static func newestMessage(
|
|
events: [VerifiedNostrEvent],
|
|
community: PushLeaseCommunity
|
|
) -> VerifiedNostrEvent? {
|
|
messages(events: events, community: community).first
|
|
}
|
|
|
|
/// Messages from others that a notification can show, newest first.
|
|
private static func messages(
|
|
events: [VerifiedNostrEvent],
|
|
community: PushLeaseCommunity
|
|
) -> [VerifiedNostrEvent] {
|
|
guard let mine = community.pubkey?.lowercased() else { return [] }
|
|
return events.filter {
|
|
$0.pubkey.lowercased() != mine && [9, 40002, 45001, 45003].contains($0.kind)
|
|
}.sorted {
|
|
$0.createdAt == $1.createdAt ? $0.id < $1.id : $0.createdAt > $1.createdAt
|
|
}
|
|
}
|
|
|
|
static func makeResolution(
|
|
event: VerifiedNostrEvent,
|
|
community: PushLeaseCommunity,
|
|
profile: BuzzPushCachedProfile?,
|
|
channel: BuzzPushCachedChannel?
|
|
) -> BuzzPushResolution? {
|
|
let body = previewBody(event.content)
|
|
guard !body.isEmpty else { return nil }
|
|
let channelID = tagValue("h", in: event)
|
|
let conversationIdentifier = channelID.map {
|
|
BuzzPushPresentationIdentity.conversation(communityID: community.id, channelID: $0)
|
|
}
|
|
let conversation = channel.flatMap {
|
|
communicationConversation(event: event, community: community, channel: $0)
|
|
}
|
|
return BuzzPushResolution(
|
|
title: profile?.displayName ?? "",
|
|
body: body,
|
|
subtitle: channel.flatMap {
|
|
channelDisplayName(channel: $0, currentUser: community.pubkey?.lowercased())
|
|
},
|
|
threadIdentifier: channelID.map {
|
|
BuzzPushPresentationIdentity.notificationThread(communityID: community.id, channelID: $0)
|
|
} ?? community.id,
|
|
navigationTarget: channelID.map {
|
|
BuzzPushNavigationTarget(
|
|
eventID: event.id,
|
|
communityID: community.id,
|
|
channelID: $0,
|
|
threadRootID: BuzzPushNavigationTarget.threadRootID(in: event.tags)
|
|
)
|
|
},
|
|
senderPubkey: event.pubkey.lowercased(),
|
|
senderAvatarPNG: profile?.avatarPNG,
|
|
conversationIdentifier: conversationIdentifier,
|
|
conversationDisplayName: conversation?.displayName,
|
|
conversationRecipientCount: conversation?.recipientCount,
|
|
notificationScope: BuzzNotificationCacheScope(community: community)
|
|
)
|
|
}
|
|
|
|
static func ephemeralChannel(
|
|
metadataEvent: VerifiedNostrEvent?,
|
|
membershipEvent: VerifiedNostrEvent?,
|
|
cached: BuzzPushCachedChannel?,
|
|
communityID: String,
|
|
relayOrigin: String,
|
|
relayMetadataPubkey: String,
|
|
cachedAt: Int
|
|
) -> BuzzPushCachedChannel? {
|
|
guard metadataEvent != nil || cached != nil else { return nil }
|
|
let channelID = metadataEvent.flatMap { tagValue("d", in: $0) } ?? cached?.channelID
|
|
guard let channelID, !channelID.isEmpty,
|
|
let eventID = metadataEvent?.id ?? cached?.eventID,
|
|
let eventCreatedAt = metadataEvent?.createdAt ?? cached?.eventCreatedAt,
|
|
let metadataCachedAt = metadataEvent == nil ? cached?.cachedAt : cachedAt
|
|
else { return nil }
|
|
let membership = membershipEvent.flatMap {
|
|
BuzzPushPresentationCacheStore.normalizedChannelMembership(
|
|
$0,
|
|
communityID: communityID,
|
|
channelID: channelID
|
|
)
|
|
}
|
|
let acceptedMembershipEvent = membership == nil ? nil : membershipEvent
|
|
return BuzzPushCachedChannel(
|
|
communityID: communityID,
|
|
relayOrigin: relayOrigin,
|
|
channelID: channelID,
|
|
relayMetadataPubkey: relayMetadataPubkey,
|
|
displayName: metadataEvent.map {
|
|
BuzzPushPresentationCacheStore.normalizedDisplayName(tagValue("name", in: $0))
|
|
} ?? cached?.displayName,
|
|
channelType: metadataEvent.map {
|
|
BuzzPushPresentationCacheStore.normalizedChannelType(tagValue("t", in: $0))
|
|
} ?? cached?.channelType,
|
|
isSolo: metadataEvent.map {
|
|
BuzzPushPresentationCacheStore.isSoloChannel($0)
|
|
} ?? cached?.isSolo,
|
|
creatorPubkey: metadataEvent.map {
|
|
BuzzPushPresentationCacheStore.normalizedCreator($0)
|
|
} ?? cached?.creatorPubkey,
|
|
memberCount: membership?.count ?? cached?.memberCount,
|
|
memberDigests: membership?.digests ?? cached?.memberDigests,
|
|
membershipEventID: acceptedMembershipEvent?.id ?? cached?.membershipEventID,
|
|
membershipEventCreatedAt: acceptedMembershipEvent?.createdAt
|
|
?? cached?.membershipEventCreatedAt,
|
|
membershipCachedAt: acceptedMembershipEvent == nil
|
|
? cached?.membershipCachedAt : cachedAt,
|
|
eventID: eventID,
|
|
eventCreatedAt: eventCreatedAt,
|
|
cachedAt: metadataCachedAt
|
|
)
|
|
}
|
|
|
|
private static func communicationConversation(
|
|
event: VerifiedNostrEvent,
|
|
community: PushLeaseCommunity,
|
|
channel: BuzzPushCachedChannel
|
|
) -> (displayName: String?, recipientCount: Int)? {
|
|
guard let currentUser = community.pubkey?.lowercased(),
|
|
let memberCount = channel.memberCount,
|
|
let memberDigests = channel.memberDigests,
|
|
memberDigests.count == memberCount
|
|
else { return nil }
|
|
let currentUserDigest = BuzzPushPresentationIdentity.channelMember(
|
|
communityID: community.id,
|
|
channelID: channel.channelID,
|
|
pubkey: currentUser
|
|
)
|
|
guard memberDigests.contains(currentUserDigest) else { return nil }
|
|
let senderDigest = BuzzPushPresentationIdentity.channelMember(
|
|
communityID: community.id,
|
|
channelID: channel.channelID,
|
|
pubkey: event.pubkey
|
|
)
|
|
let senderIsMember = memberDigests.contains(senderDigest)
|
|
let recipientCount = memberCount - (senderIsMember ? 1 : 0)
|
|
guard recipientCount > 0 else { return nil }
|
|
|
|
if channel.channelType == "dm" {
|
|
guard memberCount == 2, senderIsMember else { return nil }
|
|
return (nil, recipientCount)
|
|
}
|
|
guard let displayName = channelDisplayName(channel: channel, currentUser: currentUser)
|
|
else { return nil }
|
|
return (displayName, recipientCount)
|
|
}
|
|
|
|
/// The conversation name shown with a channel message: `#name` for team
|
|
/// channels and forums, the bare name for your own Solo chats, and `nil`
|
|
/// for DMs and unrecognized channel types.
|
|
private static func channelDisplayName(
|
|
channel: BuzzPushCachedChannel,
|
|
currentUser: String?
|
|
) -> String? {
|
|
guard let channelType = channel.channelType,
|
|
["stream", "forum"].contains(channelType),
|
|
let displayName = channel.displayName
|
|
else { return nil }
|
|
// Your own Solo chats are personal conversations, not channels, so they
|
|
// get no `#`. Like the app (`Channel.isPersonalChat`), someone else's
|
|
// Solo chat you belong to stays an ordinary channel.
|
|
if channel.isSolo == true, channel.creatorPubkey == currentUser {
|
|
return displayName
|
|
}
|
|
return displayName.hasPrefix("#") ? displayName : "#\(displayName)"
|
|
}
|
|
|
|
private static func newest(_ events: [VerifiedNostrEvent]) -> VerifiedNostrEvent? {
|
|
events.sorted {
|
|
$0.createdAt == $1.createdAt ? $0.id < $1.id : $0.createdAt > $1.createdAt
|
|
}.first
|
|
}
|
|
|
|
static func tagValue(_ name: String, in event: VerifiedNostrEvent) -> String? {
|
|
event.tags.first { $0.count >= 2 && $0[0] == name }?[1]
|
|
}
|
|
|
|
/// Longest body iOS shows, in UTF-16 code units, even when a notification
|
|
/// is expanded. It cuts longer bodies without an ellipsis, so we stop one
|
|
/// unit short and add our own. See docs/adr/0002-notification-message-text.md.
|
|
static let previewBodyLimit = 256
|
|
|
|
/// Most of a message's content, in UTF-16 code units, that the body is
|
|
/// built from. The regexes below can take quadratic time on unclosed
|
|
/// markup, and content can be up to 256 KB, so a long message is cut
|
|
/// first. This only changes the result when stripped markup and
|
|
/// whitespace in the prefix exceed about 3.8K units.
|
|
static let previewInputLimit = 4096
|
|
|
|
/// A fenced span on one line, a fenced code block, or an inline code span.
|
|
/// A block's opening line (a fence of three or more backticks, then an
|
|
/// optional language) is dropped, and the block closes only at a fence at
|
|
/// least as long that ends its line, so backticks inside the code (a "```"
|
|
/// string literal) don't end it. A fence with no closing fence runs to the
|
|
/// end of the text, as in CommonMark, which also covers a block cut by
|
|
/// `previewInputLimit`. Group 2 is the block's fence; the others hold code.
|
|
private static let previewCode = try! NSRegularExpression(
|
|
pattern: #"```([^\n]*?)(?:```|\z)|(`{3,})[^\n]*\n([\s\S]*?)(?:\n?\2`*[ \t]*(?=\n|\z)|\z)|`([^`\n]*)`"#)
|
|
|
|
/// Stands in for a code span while the prose rules run over the whole
|
|
/// text, so a link label or a bold run can contain code. Private-use
|
|
/// characters, removed from the content first, so they can't collide.
|
|
private static let previewCodeOpen = "\u{E000}"
|
|
private static let previewCodeClose = "\u{E001}"
|
|
private static let previewCodePlaceholder = try! NSRegularExpression(
|
|
pattern: "\u{E000}([0-9]+)\u{E001}")
|
|
|
|
/// Plain-text notification body for a Markdown message. Code and URLs
|
|
/// stay as written, line breaks are kept, and the Markdown syntax for code,
|
|
/// links, images, bold and strikethrough is removed and runs of spaces are
|
|
/// collapsed outside code.
|
|
static func previewBody(_ content: String) -> String {
|
|
var input = content
|
|
let cut = input.utf16.count > previewInputLimit
|
|
if cut {
|
|
var units = 0
|
|
input = String(input.prefix { character in
|
|
units += character.utf16.count
|
|
return units <= previewInputLimit
|
|
})
|
|
}
|
|
input = input
|
|
.replacingOccurrences(of: "\r\n", with: "\n")
|
|
.replacingOccurrences(of: previewCodeOpen, with: "")
|
|
.replacingOccurrences(of: previewCodeClose, with: "")
|
|
// Work in UTF-16 ranges: a match may start or end inside a grapheme
|
|
// cluster (a combining mark after a backtick), where String indices
|
|
// don't exist.
|
|
let text = input as NSString
|
|
var codes: [String] = []
|
|
var prose = ""
|
|
var position = 0
|
|
for match in previewCode.matches(in: input, range: NSRange(location: 0, length: text.length)) {
|
|
prose += text.substring(with: NSRange(location: position, length: match.range.location - position))
|
|
let body = [1, 3, 4].map(match.range(at:)).first { $0.location != NSNotFound }!
|
|
prose += previewCodeOpen + String(codes.count) + previewCodeClose
|
|
codes.append(text.substring(with: body))
|
|
position = NSMaxRange(match.range)
|
|
}
|
|
prose += text.substring(from: position)
|
|
let stripped = previewProse(prose) as NSString
|
|
var result = ""
|
|
position = 0
|
|
for match in previewCodePlaceholder.matches(
|
|
in: stripped as String, range: NSRange(location: 0, length: stripped.length))
|
|
{
|
|
result += stripped.substring(with: NSRange(location: position, length: match.range.location - position))
|
|
result += codes[Int(stripped.substring(with: match.range(at: 1)))!]
|
|
position = NSMaxRange(match.range)
|
|
}
|
|
result += stripped.substring(from: position)
|
|
result = result.replacingOccurrences(
|
|
of: #"[ \t]+\n"#, with: "\n", options: .regularExpression)
|
|
result = result.replacingOccurrences(
|
|
of: #"\n{3,}"#, with: "\n\n", options: .regularExpression)
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard cut || result.utf16.count > previewBodyLimit else { return result }
|
|
var kept = ""
|
|
var units = 0
|
|
for character in result {
|
|
units += character.utf16.count
|
|
guard units < previewBodyLimit else { break }
|
|
kept.append(character)
|
|
}
|
|
return kept.trimmingCharacters(in: .whitespacesAndNewlines) + "…"
|
|
}
|
|
|
|
private static func previewProse(_ prose: String) -> String {
|
|
prose
|
|
.replacingOccurrences(
|
|
of: #"!?\[([^\]]*)\]\([^)]*\)"#, with: "$1", options: .regularExpression)
|
|
.replacingOccurrences(
|
|
of: #"(\*\*|~~)(\S(?:.*?\S)?)\1"#, with: "$2", options: .regularExpression)
|
|
.replacingOccurrences(of: #"[ \t]+"#, with: " ", options: .regularExpression)
|
|
}
|
|
|
|
/// Compact sender label for unnamed senders: the first 8 and last 4
|
|
/// characters of the sender's full npub, matching the compact npub shape
|
|
/// used across Buzz. A key that cannot be encoded as an npub falls back to
|
|
/// the neutral "Someone" identity so malformed payloads leak no key
|
|
/// material while the notification keeps a useful title.
|
|
static func shortPubkey(_ pubkey: String) -> String {
|
|
guard let npub = Bech32.canonicalNpub(from: pubkey) else { return "Someone" }
|
|
return npub.count > 12
|
|
? String(npub.prefix(8)) + "…" + String(npub.suffix(4)) : npub
|
|
}
|
|
|
|
private func loadCommunities() -> [PushLeaseCommunity] {
|
|
guard let data = loadCommunitiesData(),
|
|
let decoded = try? JSONDecoder().decode(PushLeaseSnapshot.self, from: data)
|
|
else { return [] }
|
|
return decoded.communities
|
|
}
|
|
}
|
|
|
|
extension PushLeaseCommunity {
|
|
var relayURL: URL? {
|
|
guard var components = URLComponents(string: relayUrl),
|
|
components.host?.isEmpty == false,
|
|
components.user == nil,
|
|
components.password == nil,
|
|
components.query == nil,
|
|
components.fragment == nil,
|
|
components.path.isEmpty || components.path == "/"
|
|
else { return nil }
|
|
components.scheme =
|
|
switch components.scheme?.lowercased() {
|
|
case "wss": "https"
|
|
case "ws": "http"
|
|
case "https": "https"
|
|
case "http": "http"
|
|
default: nil
|
|
}
|
|
components.path = ""
|
|
return components.url
|
|
}
|
|
}
|
|
|
|
/// Refuses every redirect, so a signed relay request never forwards its proof or assertion.
|
|
final class BuzzRedirectRefusal: NSObject, URLSessionTaskDelegate, Sendable {
|
|
static let shared = BuzzRedirectRefusal()
|
|
|
|
func urlSession(
|
|
_ session: URLSession, task: URLSessionTask,
|
|
willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest,
|
|
completionHandler: @escaping (URLRequest?) -> Void
|
|
) {
|
|
completionHandler(nil)
|
|
}
|
|
}
|