mirror of
https://github.com/block/buzz.git
synced 2026-11-24 02:02:07 +00:00
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>
292 lines
12 KiB
Swift
292 lines
12 KiB
Swift
import Foundation
|
|
import Testing
|
|
|
|
@testable import BuzzPushKit
|
|
|
|
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(
|
|
id: String(repeating: "d", count: 64),
|
|
pubkey: sender,
|
|
createdAt: 1_000,
|
|
kind: kind,
|
|
tags: tags,
|
|
content: "message",
|
|
sig: String(repeating: "e", count: 128)
|
|
)
|
|
}
|
|
|
|
@Test(arguments: [
|
|
// Top-level message: no thread root.
|
|
([[String]](), nil as String?),
|
|
// Direct reply to a thread head: the lone reply marker names the root.
|
|
([["e", root, "", "reply"]], root),
|
|
// Nested reply: the root marker wins over the direct parent.
|
|
([["e", root, "", "root"], ["e", parent, "", "reply"]], root),
|
|
// A malformed root marker falls back to the valid reply marker.
|
|
([["e", "not-hex", "", "root"], ["e", parent, "", "reply"]], parent),
|
|
// Uppercase IDs are normalized.
|
|
([["e", root.uppercased(), "", "root"], ["e", parent, "", "reply"]], root),
|
|
// A root marker without a valid reply marker is not a reply, as in the app.
|
|
([["e", root, "", "root"]], nil),
|
|
])
|
|
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), recipientPubkey: recipient))
|
|
#expect(context.senderPubkey == sender.lowercased())
|
|
#expect(context.recipientPubkey == recipient.lowercased())
|
|
#expect(context.threadRootID == expectedRoot)
|
|
#expect(context.messageCreatedAt == 1_000)
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [BuzzNotificationReplyContext.userInfoKey: context.userInfoValue]) == context)
|
|
}
|
|
|
|
@Test(arguments: [45001, 45003, 7])
|
|
func `Forum posts and other kinds offer no reply`(kind: Int) {
|
|
#expect(
|
|
BuzzNotificationReplyContext.make(for: event(kind: kind), recipientPubkey: recipient) == nil)
|
|
}
|
|
|
|
@Test func `Reply context rejects unexpected or malformed fields`() {
|
|
let key = BuzzNotificationReplyContext.userInfoKey
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [key: ["sender_pubkey": "", "created_at": "1"]]) == nil)
|
|
#expect(BuzzNotificationReplyContext.decodeIfPresent(from: [key: ["sender_pubkey": "pk"]]) == nil)
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [key: ["sender_pubkey": "pk", "created_at": "soon"]]) == nil)
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [key: ["sender_pubkey": "pk", "created_at": "1", "extra": "x"]]) == nil)
|
|
#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: 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)
|
|
#expect(noChannel?.replyContext == nil)
|
|
}
|
|
|
|
@Test func `Reply request needs text, a target and a reply context`() {
|
|
let target = BuzzPushNavigationTarget(
|
|
eventID: parent, communityID: "community", channelID: "channel")
|
|
let context = BuzzNotificationReplyContext(senderPubkey: "pk", threadRootID: root, messageCreatedAt: 1)
|
|
let userInfo: [AnyHashable: Any] = [
|
|
BuzzPushNavigationTarget.userInfoKey: target.userInfoValue,
|
|
BuzzNotificationReplyContext.userInfoKey: context.userInfoValue,
|
|
]
|
|
let request = BuzzNotificationReplyRequest.make(
|
|
id: "1", text: "hi", userInfo: userInfo, threadIdentifier: "thread")
|
|
#expect(request?.target == target)
|
|
#expect(request?.context == context)
|
|
#expect(
|
|
BuzzNotificationReplyRequest.make(
|
|
id: "1", text: " \n", userInfo: userInfo, threadIdentifier: "thread") == nil)
|
|
#expect(
|
|
BuzzNotificationReplyRequest.make(
|
|
id: "1", text: "hi",
|
|
userInfo: [BuzzPushNavigationTarget.userInfoKey: target.userInfoValue],
|
|
threadIdentifier: "thread") == nil)
|
|
}
|
|
|
|
@Test func `Each buffered reply is taken once and finishes once`() {
|
|
let buffer = BuzzNotificationReplyBuffer()
|
|
let target = BuzzPushNavigationTarget(eventID: parent, communityID: "c", channelID: "ch")
|
|
let context = BuzzNotificationReplyContext(senderPubkey: "pk", messageCreatedAt: 1)
|
|
func request(_ id: String) -> BuzzNotificationReplyRequest {
|
|
BuzzNotificationReplyRequest(
|
|
id: id, text: id, target: target, context: context, threadIdentifier: "t")
|
|
}
|
|
var outcomes: [String: [BuzzNotificationReplyOutcome]] = [:]
|
|
for id in ["1", "2", "3", "4"] {
|
|
buffer.add(request(id)) { outcomes[id, default: []].append($0) }
|
|
}
|
|
|
|
#expect(buffer.take().map(\.id) == ["1", "2", "3", "4"])
|
|
#expect(buffer.take().isEmpty)
|
|
// A Solo chat keeps an expired reply's draft in the chat itself.
|
|
buffer.add(
|
|
BuzzNotificationReplyRequest(
|
|
id: "5", text: "5", target: target,
|
|
context: BuzzNotificationReplyContext(senderPubkey: "pk", messageCreatedAt: 1, inSolo: true),
|
|
threadIdentifier: "t")
|
|
) { outcomes["5", default: []].append($0) }
|
|
|
|
// Taken, a reply is still held until it finishes; then it's no longer
|
|
// allowed to go out.
|
|
#expect(buffer.holds(id: "1"))
|
|
#expect(buffer.complete(id: "1", outcome: .sent))
|
|
#expect(!buffer.holds(id: "1"))
|
|
// A deadline after the app reported the outcome changes nothing.
|
|
buffer.expire(id: "1", mayHavePublished: false)
|
|
#expect(!buffer.complete(id: "1", outcome: .sent))
|
|
buffer.complete(id: "2", outcome: .notSent(draftThreadRootID: "head"))
|
|
buffer.complete(id: "3", outcome: .unknown)
|
|
buffer.expire(id: "4", mayHavePublished: false)
|
|
#expect(!buffer.holds(id: "4"))
|
|
buffer.expire(id: "5", mayHavePublished: false)
|
|
// One the app may already have published can't be called not sent.
|
|
buffer.add(request("6")) { outcomes["6", default: []].append($0) }
|
|
buffer.expire(id: "6", mayHavePublished: true)
|
|
#expect(!buffer.complete(id: "unknown", outcome: .sent))
|
|
|
|
#expect(
|
|
outcomes == [
|
|
"1": [.sent], "2": [.notSent(draftThreadRootID: "head")], "3": [.unknown],
|
|
// Expired, it opens the subthread under the notified message, where the
|
|
// app keeps its draft.
|
|
"4": [.notSent(draftThreadRootID: parent)], "5": [.notSent(draftThreadRootID: nil)],
|
|
"6": [.unknown],
|
|
])
|
|
}
|
|
|
|
@Test func `A reply stays journaled until reported, across a relaunch`() throws {
|
|
let suite = "reply-journal-\(UUID().uuidString)"
|
|
let defaults = try #require(UserDefaults(suiteName: suite))
|
|
defer { defaults.removePersistentDomain(forName: suite) }
|
|
let journal = BuzzNotificationReplyJournal(defaults: defaults)
|
|
journal.record(["id": "1", "text": "one"])
|
|
journal.record(["id": "2", "text": "two"])
|
|
|
|
#expect(journal.take().map { $0["id"] } == ["1", "2"])
|
|
// The app is already working on these.
|
|
#expect(journal.take().isEmpty)
|
|
journal.settle(id: "1")
|
|
journal.record(["id": "3", "text": "three"])
|
|
#expect(journal.take().map { $0["id"] } == ["3"])
|
|
|
|
// The app got as far as maybe publishing 3, so expiry reports it unknown.
|
|
#expect(!journal.isPublishing(id: "3"))
|
|
journal.markPublishing(id: "3")
|
|
#expect(journal.isPublishing(id: "3"))
|
|
#expect(!journal.isPublishing(id: "2"))
|
|
|
|
// The app was stopped before reporting 2 and 3, so a relaunch hands them
|
|
// over again, marked so the app never sends them; one recorded after the
|
|
// relaunch is fresh.
|
|
let relaunched = BuzzNotificationReplyJournal(defaults: defaults)
|
|
relaunched.record(["id": "4", "text": "four"])
|
|
#expect(
|
|
relaunched.take() == [
|
|
["id": "2", "text": "two", "recovered": "true"],
|
|
["id": "3", "text": "three", "publishing": "true", "recovered": "true"],
|
|
["id": "4", "text": "four"],
|
|
])
|
|
// The relaunch can still say that 3 may not have sent, then settle it.
|
|
#expect(relaunched.recorded(id: "3") == ["id": "3", "text": "three", "publishing": "true"])
|
|
#expect(relaunched.isPublishing(id: "3"))
|
|
for id in ["2", "3", "4"] { relaunched.settle(id: id) }
|
|
#expect(relaunched.recorded(id: "3") == nil)
|
|
#expect(BuzzNotificationReplyJournal(defaults: defaults).take().isEmpty)
|
|
}
|
|
|
|
@Test func `A journaled reply rebuilds the request it came from`() {
|
|
let request = BuzzNotificationReplyRequest(
|
|
id: "1", text: "hi",
|
|
target: BuzzPushNavigationTarget(
|
|
eventID: parent, communityID: "c", channelID: "ch", threadRootID: "root"),
|
|
context: BuzzNotificationReplyContext(
|
|
senderPubkey: "pk", threadRootID: "root", messageCreatedAt: 7, inSolo: true,
|
|
recipientPubkey: "me"),
|
|
threadIdentifier: "t",
|
|
deadline: Date(timeIntervalSince1970: 1_700_000_000.5))
|
|
let rebuilt = BuzzNotificationReplyRequest(
|
|
journalArguments: request.journalArguments.merging(["recovered": "true"]) { _, new in new })
|
|
// The notification it came from, and its thread, are gone.
|
|
#expect(
|
|
rebuilt
|
|
== BuzzNotificationReplyRequest(
|
|
id: request.id, text: request.text, target: request.target, context: request.context,
|
|
threadIdentifier: "", deadline: request.deadline))
|
|
#expect(BuzzNotificationReplyRequest(journalArguments: ["id": "1", "text": "hi"]) == nil)
|
|
}
|
|
|
|
@Test func `Reply context carries whether the chat is Solo`() throws {
|
|
for inSolo in [true, false, nil] as [Bool?] {
|
|
let context = BuzzNotificationReplyContext(
|
|
senderPubkey: "pk", messageCreatedAt: 1, inSolo: inSolo)
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [BuzzNotificationReplyContext.userInfoKey: context.userInfoValue]) == context)
|
|
}
|
|
#expect(
|
|
BuzzNotificationReplyContext.decodeIfPresent(
|
|
from: [
|
|
BuzzNotificationReplyContext.userInfoKey: [
|
|
"sender_pubkey": "pk", "created_at": "1", "in_solo": "yes",
|
|
]
|
|
]) == nil)
|
|
}
|
|
|
|
@Test func `Selected community round-trips and clears`() throws {
|
|
let suite = "BuzzNotificationReplyTests.\(UUID().uuidString)"
|
|
let defaults = try #require(UserDefaults(suiteName: suite))
|
|
defer { defaults.removePersistentDomain(forName: suite) }
|
|
#expect(BuzzSelectedCommunity.id(in: defaults) == nil)
|
|
BuzzSelectedCommunity.set("community", in: defaults)
|
|
#expect(BuzzSelectedCommunity.id(in: defaults) == "community")
|
|
BuzzSelectedCommunity.set(nil, in: defaults)
|
|
#expect(BuzzSelectedCommunity.id(in: defaults) == nil)
|
|
}
|
|
|
|
@Test func `A sent reply clears the notifications its read marker covers`() {
|
|
let target = BuzzPushNavigationTarget(eventID: parent, communityID: "c", channelID: "ch")
|
|
func request(root: String?) -> BuzzNotificationReplyRequest {
|
|
BuzzNotificationReplyRequest(
|
|
id: "1", text: "hi", target: target,
|
|
context: BuzzNotificationReplyContext(
|
|
senderPubkey: "pk", threadRootID: root, messageCreatedAt: 10),
|
|
threadIdentifier: "conversation")
|
|
}
|
|
func notification(root: String?, at createdAt: Int) -> [AnyHashable: Any] {
|
|
[
|
|
BuzzNotificationReplyContext.userInfoKey: BuzzNotificationReplyContext(
|
|
senderPubkey: "pk", threadRootID: root, messageCreatedAt: createdAt
|
|
).userInfoValue
|
|
]
|
|
}
|
|
let rows: [(request: String?, notified: String?, createdAt: Int, cleared: Bool)] = [
|
|
// A top-level reply reads the whole conversation, threads included.
|
|
(nil, nil, 5, true),
|
|
(nil, "thread", 5, true),
|
|
(nil, "thread", 11, false),
|
|
// A reply in a thread reads only that thread.
|
|
("thread", "thread", 5, true),
|
|
("thread", nil, 5, false),
|
|
("thread", "other", 5, false),
|
|
]
|
|
for row in rows {
|
|
#expect(
|
|
request(root: row.request).clears(
|
|
threadIdentifier: "conversation",
|
|
userInfo: notification(root: row.notified, at: row.createdAt)) == row.cleared)
|
|
}
|
|
#expect(
|
|
!request(root: nil).clears(
|
|
threadIdentifier: "elsewhere", userInfo: notification(root: nil, at: 5)))
|
|
#expect(!request(root: nil).clears(threadIdentifier: "conversation", userInfo: [:]))
|
|
}
|