fix(mobile): bind notification replies to the account they were typed for (#9845)

A notification reply goes out as whatever account its community signs
with when the reply is sent. No sign-in path replaces a community's
account in place today: signing in again as another account adds a new
community, and hosted re-entry refuses a different key. But nothing tied
a reply to the account its notification was for, so a future in-place
credential replacement would let a reply typed for the earlier account
go out as the new one, or land in its drafts.

Each reply is now tied to the account its notification was for:

- The notification service extension records that account in the reply
context, and iOS carries it through the reply journal, so a reply
recovered on a later launch keeps it too.
- The app sends a reply, or keeps it as a draft, only while the
community still signs as that account (the key it signs with). It checks
again after connecting.
- Otherwise the reply ends as lost: iOS shows "Reply not sent" with the
text, and no draft is kept. This is the same path a reply takes when its
community has been removed ([ADR
0004](docs/adr/0004-notification-reply.md), updated here).
- A reply to a notification posted by an earlier build has no recorded
account, so it is checked only by its community, as before.

### Regressions

- A community whose notification snapshot has no account key offers no
reply action. Every signed-in community has one, since the snapshot
falls back to the key derived from its secret.
- No extra work on the send path beyond a key comparison.

### Validation

-
[notification_reply_test.dart](https://github.com/block/buzz/blob/notification-reply-account/mobile/test/features/channels/notification_reply_test.dart):
a live reply for another account and a recovered one for another
community end as lost with their text kept nowhere; a reply whose
community's signing key is replaced while the relay connects (same
community and stored key) is neither sent nor drafted; a reply from an
earlier build without an account is sent.
-
[BuzzNotificationReplyTests.swift](https://github.com/block/buzz/blob/notification-reply-account/mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzNotificationReplyTests.swift):
the resolver records the notification's account, lowercased. An empty
account is rejected, and the account survives the journal round trip.
- No simulator run: the reply action needs a real push resolved by the
notification service extension, which the simulator can't receive. The
change is covered at both layers by the tests above, and the iOS app
builds.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-11-20 20:23:59 -08:00
committed by GitHub
parent 52cbc61131
commit d1236080eb
7 changed files with 148 additions and 24 deletions

View File

@@ -20,14 +20,20 @@ public struct BuzzNotificationReplyContext: Codable, Equatable, Sendable {
/// Whether channel metadata showed a Solo chat, which takes no thread
/// replies; nil when it was unknown.
public let inSolo: Bool?
/// The account the notification was for, which the reply must be sent as,
/// guarding against a community's credentials being replaced in place. nil
/// for a notification an earlier build posted.
public let recipientPubkey: String?
public init(
senderPubkey: String, threadRootID: String? = nil, messageCreatedAt: Int, inSolo: Bool? = nil
senderPubkey: String, threadRootID: String? = nil, messageCreatedAt: Int, inSolo: Bool? = nil,
recipientPubkey: String? = nil
) {
self.senderPubkey = senderPubkey
self.threadRootID = threadRootID
self.messageCreatedAt = messageCreatedAt
self.inSolo = inSolo
self.recipientPubkey = recipientPubkey
}
/// Kinds the app can answer with an ordinary channel or DM message.
@@ -35,14 +41,15 @@ public struct BuzzNotificationReplyContext: Codable, Equatable, Sendable {
static let replyableKinds: Set<Int> = [9, 40002]
static func make(
for event: VerifiedNostrEvent, inSolo: Bool? = nil
for event: VerifiedNostrEvent, recipientPubkey: String, inSolo: Bool? = nil
) -> BuzzNotificationReplyContext? {
guard replyableKinds.contains(event.kind) else { return nil }
return BuzzNotificationReplyContext(
senderPubkey: event.pubkey.lowercased(),
threadRootID: nip10RootID(in: event.tags),
messageCreatedAt: event.createdAt,
inSolo: inSolo
inSolo: inSolo,
recipientPubkey: recipientPubkey.lowercased()
)
}
@@ -72,6 +79,7 @@ public struct BuzzNotificationReplyContext: Codable, Equatable, Sendable {
var value = ["sender_pubkey": senderPubkey, "created_at": String(messageCreatedAt)]
value["thread_root_id"] = threadRootID
value["in_solo"] = inSolo.map { $0 ? "true" : "false" }
value["recipient_pubkey"] = recipientPubkey
return value
}
@@ -80,13 +88,18 @@ public struct BuzzNotificationReplyContext: Codable, Equatable, Sendable {
from userInfo: [AnyHashable: Any]
) -> BuzzNotificationReplyContext? {
guard let raw = userInfo[userInfoKey] as? [String: Any],
Set(raw.keys).isSubset(of: ["sender_pubkey", "thread_root_id", "created_at", "in_solo"]),
Set(raw.keys).isSubset(of: [
"sender_pubkey", "thread_root_id", "created_at", "in_solo", "recipient_pubkey",
]),
let sender = raw["sender_pubkey"] as? String, !sender.isEmpty,
let createdAt = (raw["created_at"] as? String).flatMap(Int.init), createdAt > 0
else { return nil }
if let rawRoot = raw["thread_root_id"] {
guard let root = rawRoot as? String, !root.isEmpty else { return nil }
}
if let rawRecipient = raw["recipient_pubkey"] {
guard let recipient = rawRecipient as? String, !recipient.isEmpty else { return nil }
}
var inSolo: Bool?
if let rawSolo = raw["in_solo"] {
switch rawSolo as? String {
@@ -97,7 +110,8 @@ public struct BuzzNotificationReplyContext: Codable, Equatable, Sendable {
}
return BuzzNotificationReplyContext(
senderPubkey: sender, threadRootID: raw["thread_root_id"] as? String,
messageCreatedAt: createdAt, inSolo: inSolo)
messageCreatedAt: createdAt, inSolo: inSolo,
recipientPubkey: raw["recipient_pubkey"] as? String)
}
}
@@ -169,6 +183,7 @@ public struct BuzzNotificationReplyRequest: Equatable, Sendable {
arguments["messageCreatedAt"] = String(context.messageCreatedAt)
arguments["deadline"] = String(Int64(deadline.timeIntervalSince1970 * 1000))
arguments["inSolo"] = context.inSolo == true ? "true" : nil
arguments["recipientPubkey"] = context.recipientPubkey
return arguments
}
@@ -187,7 +202,8 @@ public struct BuzzNotificationReplyRequest: Equatable, Sendable {
threadRootID: arguments["threadRootId"]),
context: BuzzNotificationReplyContext(
senderPubkey: sender, threadRootID: arguments["replyRootId"],
messageCreatedAt: createdAt, inSolo: arguments["inSolo"] == "true" ? true : nil),
messageCreatedAt: createdAt, inSolo: arguments["inSolo"] == "true" ? true : nil,
recipientPubkey: arguments["recipientPubkey"]),
threadIdentifier: "",
deadline: arguments["deadline"].flatMap(Double.init).map {
Date(timeIntervalSince1970: $0 / 1000)

View File

@@ -557,7 +557,11 @@ public final class BuzzPushNotificationResolver: BuzzPushNotificationResolving {
conversationDisplayName: conversation?.displayName,
conversationRecipientCount: conversation?.recipientCount,
replyContext: channelID == nil
? nil : BuzzNotificationReplyContext.make(for: event, inSolo: channel?.isSolo),
? nil
: community.pubkey.flatMap {
BuzzNotificationReplyContext.make(
for: event, recipientPubkey: $0, inSolo: channel?.isSolo)
},
notificationScope: BuzzNotificationCacheScope(community: community)
)
}

View File

@@ -6,6 +6,7 @@ import Testing
private let root = String(repeating: "a", count: 64)
private let parent = String(repeating: "b", count: 64)
private let sender = String(repeating: "C", count: 64)
private let recipient = String(repeating: "F", count: 64)
private func event(kind: Int, tags: [[String]] = []) -> VerifiedNostrEvent {
VerifiedNostrEvent(
@@ -36,8 +37,10 @@ private func event(kind: Int, tags: [[String]] = []) -> VerifiedNostrEvent {
func `Reply context carries the sender and NIP-10 thread root`(
tags: [[String]], expectedRoot: String?
) throws {
let context = try #require(BuzzNotificationReplyContext.make(for: event(kind: 9, tags: tags)))
let context = try #require(
BuzzNotificationReplyContext.make(for: event(kind: 9, tags: tags), recipientPubkey: recipient))
#expect(context.senderPubkey == sender.lowercased())
#expect(context.recipientPubkey == recipient.lowercased())
#expect(context.threadRootID == expectedRoot)
#expect(context.messageCreatedAt == 1_000)
#expect(
@@ -47,7 +50,8 @@ func `Reply context carries the sender and NIP-10 thread root`(
@Test(arguments: [45001, 45003, 7])
func `Forum posts and other kinds offer no reply`(kind: Int) {
#expect(BuzzNotificationReplyContext.make(for: event(kind: kind)) == nil)
#expect(
BuzzNotificationReplyContext.make(for: event(kind: kind), recipientPubkey: recipient) == nil)
}
@Test func `Reply context rejects unexpected or malformed fields`() {
@@ -65,16 +69,21 @@ func `Forum posts and other kinds offer no reply`(kind: Int) {
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "pk", "created_at": "1", "thread_root_id": ""]]) == nil)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "pk", "created_at": "1", "recipient_pubkey": ""]]) == nil)
}
@Test func `Resolution offers reply only for a channel message`() {
let community = PushLeaseCommunity(
id: "community", name: "Community", relayUrl: "https://relay.example.org",
pubkey: String(repeating: "f", count: 64), policies: [])
pubkey: recipient, policies: [])
let inChannel = BuzzPushNotificationResolver.makeResolution(
event: event(kind: 9, tags: [["h", "channel"]]), community: community, profile: nil,
channel: nil)
#expect(inChannel?.replyContext?.senderPubkey == sender.lowercased())
// The reply is bound to the account the notification was for.
#expect(inChannel?.replyContext?.recipientPubkey == recipient.lowercased())
let noChannel = BuzzPushNotificationResolver.makeResolution(
event: event(kind: 9), community: community, profile: nil, channel: nil)
#expect(noChannel != nil)
@@ -200,7 +209,8 @@ func `Forum posts and other kinds offer no reply`(kind: Int) {
target: BuzzPushNavigationTarget(
eventID: parent, communityID: "c", channelID: "ch", threadRootID: "root"),
context: BuzzNotificationReplyContext(
senderPubkey: "pk", threadRootID: "root", messageCreatedAt: 7, inSolo: true),
senderPubkey: "pk", threadRootID: "root", messageCreatedAt: 7, inSolo: true,
recipientPubkey: "me"),
threadIdentifier: "t",
deadline: Date(timeIntervalSince1970: 1_700_000_000.5))
let rebuilt = BuzzNotificationReplyRequest(