feat(mobile): message the Bestie from Siri and Shortcuts (#9517)

Adds a **Message my Bestie** action for Siri and Shortcuts. Siri asks
what to say, confirms with the Bestie's name ("Send “…” to Astra?"), and
posts the message in your DM with the Bestie, without opening Buzz. The
Bestie's reply arrives as a normal notification, in the same DM the Home
star opens.

This is the iOS 16+ App Shortcut from [ADR
0005](https://github.com/block/buzz/blob/mobile-siri-bestie-dm/docs/adr/0005-quick-send-uses-the-system-send-message-action.md).
Follow-ups, not in this PR: the iOS 27 `messages.sendMessage` schema
intent, naming other recipients, a separate "Start a chat with my
Bestie" intent for a new Solo chat, and opening Buzz with a draft when a
send fails.

### How it works

- The App Intent runs without opening the app and hands the text to Dart
over a method channel. It waits up to 20 s for Home to say it is ready,
and runs one send at a time.
- A background launch already starts the Flutter engine and builds Home
([ADR
0004](https://github.com/block/buzz/blob/mobile-siri-bestie-dm/docs/adr/0004-background-work-runs-in-the-app-engine.md)
records the measurement). So a small `QuickSendHost`, which the app
hands Home as an invisible background widget, takes the send. Nothing is
reimplemented natively.
- Before confirming, Swift asks Dart for the saved Bestie's pubkey and
name; the send then goes to exactly that agent, so a choice this phone
hasn't synced yet is caught at the confirmation.
- The send reuses the Home star's lookup for the Bestie DM (opening it
with the relay's find-or-create DM command when it isn't loaded) and
posts with the same `SendMessage` the composer uses.
- If Buzz has been in the background past its 5 s grace period, the
relay session has closed its socket. The send reopens it, then lets the
normal background pause start over.
- A message the relay rate-limits is retried (up to 3 tries); the relay
turned it away, so a retry can't duplicate it. Any other failure isn't
retried, since the message might have landed.
- The confirmation is pinned to the community and account it was shown
in: if Buzz switches either before the message goes out, the send is
refused.
- If the send fails, or Buzz can't tell whether it landed (no answer
from the relay), the message is saved as the Bestie DM's composer draft,
after any draft already there, and Siri says so ("Buzz isn't sure that
sent. Check your Bestie DM before trying again." when the outcome is
unknown). A successful send leaves the draft alone. Limits: if the DM
can't be found or opened, there is nowhere to save it and Siri says the
message wasn't saved, and the app being killed mid-send isn't covered.
- Refusals (no Bestie, offline, empty or too long, switched community)
come back as a short message Siri reads.

### Regressions

- Nothing existing changes behavior: the host only answers this method
channel, and the action does nothing until someone runs it. Its bugs
stay inside the quick send path.
- The Home star's DM lookup moved into `cachedBestieConversation`
unchanged, so the star behaves as before.
- A composer that is open on screen now shows a draft another writer
saved for it (here, a failed quick send) instead of overwriting it with
the next keystroke. It listens without rebuilding, and its own saves are
ignored.
- A background wake pays the cost of starting the app's Dart code, which
iOS already does for this launch.

### Before / after

| Before: no Buzz actions in Shortcuts | After |
| --- | --- |
| <img width="300" alt="Shortcuts Library before"
src="https://github.com/user-attachments/assets/b0870e3d-2823-4c7d-be0e-a242a3fdafd7">
| <img width="300" alt="Shortcuts Library after, listing Buzz Dev under
Apps"
src="https://github.com/user-attachments/assets/8c092006-76b9-4313-a401-68189f5674cf">
|

| Asks what to say | Confirms (names the Bestie) | Sent |
| --- | --- | --- |
| <img width="250" alt="Prompt"
src="https://github.com/user-attachments/assets/a2eaf44a-5287-48b6-9c5a-2aaefc90c0a8">
| Shows "Send “…” to *Bestie's name*?" (capture omitted: it shows an
internal agent name) | <img width="250" alt="Sent"
src="https://github.com/user-attachments/assets/07fe66e3-4480-4d06-be52-623416e567b8">
|

The DM result and a demo video show internal names, so they're shared
internally rather than here.

### Testing

- Added test, `quick_send_test.dart` journey: Home reports ready; Siri's
Bestie lookup is refused with no Bestie, then returns the chosen
Bestie's pubkey and name; a send confirmed for another community is
refused; an empty send is refused without publishing; a send while Buzz
is backgrounded and its socket closed reconnects, opens the DM with the
Bestie, survives a rate-limited first try, posts the message in that DM
tagged to the Bestie, and hands the session back to its background
pause; a second send goes to the same DM and leaves the person's draft
there as it was; a send whose answer is lost is reported as unsure and
kept in that draft after the person's own text, with its mentions; a
socket that drops just before the message goes out keeps it too; leaving
Home reports unready. Removing the rate-limit retry, the community check
or the draft save fails it.
- Extended the composer's share-delivery journey: a draft saved by
another writer while the composer is open shows in the composer and
survives the next keystroke, and survives an ordinary send that was
pending when it arrived and then fails. Removing the composer's listener
or its revision bump fails it.
- iPhone 17 Pro simulator (iOS 27), Builderlab community with a Bestie,
automated with AXe:
- Cold: Buzz terminated, ran the action from Shortcuts, the confirmation
named the Bestie, "Sent.", and the message was in the Bestie DM with its
reply. No Solo chat was created.
- Warm: Buzz in the background for 10 s, ran it again, "Sent.", same DM.
- At-risk flows: sent a typed message from the DM composer, went back to
Home, and the Home star reopened the same DM.
- `just mobile-check mobile-test` passes.






buzz-review-completed

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
This commit is contained in:
Tom Brow
2026-11-15 09:06:25 -08:00
committed by GitHub
parent a4cac9a2d1
commit 934a16f2ff
13 changed files with 910 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
# Buzz domain language
Shared terminology for Buzz product behavior. This glossary currently covers mobile [push suppression](#push-suppression), the [notification body](#notification-body), [mobile builds](#mobile-builds) and [sharing](#sharing); it is not a complete model of Buzz.
Shared terminology for Buzz product behavior. This glossary currently covers mobile [push suppression](#push-suppression), the [notification body](#notification-body), [mobile builds](#mobile-builds), [sharing](#sharing) and [quick send](#quick-send); it is not a complete model of Buzz.
## Notifications
@@ -83,3 +83,17 @@ _Avoid_: Forward, post
A conversation offered among the share sheet's suggested destinations: an existing channel or DM, or a new Solo chat. Share targets are learned from top-level messages a person sends in a conversation; thread replies and incoming messages never count.
_Avoid_: Recipient, contact, suggestion
## Agents and conversations
### Bestie
The one agent a person has chosen as their default in a community. A community has at most one Bestie per person.
_Avoid_: Default agent, main agent, assistant
### Quick send
Sending a message to Buzz from outside the app (Siri, Shortcuts, the Action button) without opening Buzz. A quick send to the [Bestie](#bestie) posts the message in the person's DM with the Bestie.
_Avoid_: Siri message, dictation, background send

View File

@@ -0,0 +1,17 @@
---
status: proposed
---
# Background work runs in the app's own Flutter engine
When iOS wakes the mobile app in the background, for example for a [quick send](../../CONTEXT.md#quick-send), the work runs in the app's normal Dart code, in the same Flutter engine the window uses. The app's existing engine setup already supports this: a cold background launch by an App Intent starts the engine and runs `main()` before the intent's `perform()` asks Dart for anything, with no window and no change to how the engine starts. On an iPhone SE running a release build, Dart answered the intent 19 ms after `perform()` began.
## Considered options
- **Starting the engine earlier, before any window.** Unnecessary: the engine already starts on a background launch.
- **A second, lightweight engine for background work.** Rejected because it runs in its own isolate. While the app is also open, both would hold their own relay session and caches for the same community, for example its channel list and local pending messages, and could race, and avoiding that needs cross-engine coordination.
- **Reimplementing the background work natively.** Rejected because even a quick send needs the community's authenticated relay session, finding or opening the Bestie DM, and the same message tags the app writes. Duplicating that in Swift would have to stay wire-compatible with the Dart implementation.
## Consequences
A background launch still builds the widget tree, so background work can live in a widget that Home always builds and act through providers. It must not assume the app is in the foreground: once the app has been in the background past its grace period, the relay session has closed its socket, so background work reopens the session for its duration and then lets the background pause start over. A background wake pays the cost of starting the app's Dart code.

View File

@@ -0,0 +1,12 @@
---
status: proposed
---
# Quick send uses the system send-message action
On iOS 27 and later, [quick send](../../CONTEXT.md#quick-send) is an App Intent that adopts the system `messages.sendMessage` schema, which Siri and Shortcuts already understand. Its destination is optional and defaults to the [Bestie](../../CONTEXT.md#bestie), so letting people name another agent, person or conversation later only widens the destinations it accepts, while existing Action button setups and Siri phrases keep working. iOS 16 to 26 get a plain App Intent and App Shortcut that sends only to the Bestie. The plain App Shortcut, available on iOS 16 and later, ships first; the iOS 27 schema intent follows it.
## Considered options
- **A custom tool intent for the Siri AI Developer Preview.** Rejected: Apple's guidance is that a messaging app implementing `sendMessage` should not also mark it as a tool, and the preview works only in development-signed and internal TestFlight builds.
- **A Bestie-only intent with no destination.** Rejected because adding named recipients later would mean a second intent and new phrases.

View File

@@ -16,6 +16,7 @@
DAAB00000000000000000001 /* NativeTabler.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAAB00000000000000000002; };
DAAC00000000000000000001 /* HomeScreenShortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAAC00000000000000000002 /* HomeScreenShortcuts.swift */; };
5AAE00000000000000000001 /* ShareSuggestions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AAE00000000000000000002 /* ShareSuggestions.swift */; };
DAAD00000000000000000001 /* QuickSend.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAAD00000000000000000002 /* QuickSend.swift */; };
C0AF00000000000000000001 /* NativeConfirmationDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0AF00000000000000000002 /* NativeConfirmationDialog.swift */; };
C0AF00000000000000000021 /* NativePresenceMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0AF00000000000000000022 /* NativePresenceMenu.swift */; };
C0AF00000000000000000011 /* NativeAddMembersSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0AF00000000000000000012 /* NativeAddMembersSheet.swift */; };
@@ -116,6 +117,7 @@
BBAA00000000000000000002 /* HostedHpkeBackup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostedHpkeBackup.swift; sourceTree = "<group>"; };
DAAC00000000000000000002 /* HomeScreenShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeScreenShortcuts.swift; sourceTree = "<group>"; };
5AAE00000000000000000002 /* ShareSuggestions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareSuggestions.swift; sourceTree = "<group>"; };
DAAD00000000000000000002 /* QuickSend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSend.swift; sourceTree = "<group>"; };
DAAB00000000000000000002 /* NativeTabler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeTabler.swift; sourceTree = "<group>"; };
C0AF00000000000000000002 /* NativeConfirmationDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeConfirmationDialog.swift; sourceTree = "<group>"; };
C0AF00000000000000000022 /* NativePresenceMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativePresenceMenu.swift; sourceTree = "<group>"; };
@@ -315,6 +317,7 @@
DAAB00000000000000000002 /* NativeTabler.swift */,
DAAC00000000000000000002 /* HomeScreenShortcuts.swift */,
5AAE00000000000000000002 /* ShareSuggestions.swift */,
DAAD00000000000000000002 /* QuickSend.swift */,
331C809A294A618700263BE5 /* MediaSanitizer.swift */,
4A71C0022F40100100A17E01 /* InlinePhotoPicker.swift */,
4A71C0042F40200100A17E01 /* NativeAttachmentPopover.swift */,
@@ -667,6 +670,7 @@
DAAB00000000000000000001 /* NativeTabler.swift in Sources */,
DAAC00000000000000000001 /* HomeScreenShortcuts.swift in Sources */,
5AAE00000000000000000001 /* ShareSuggestions.swift in Sources */,
DAAD00000000000000000001 /* QuickSend.swift in Sources */,
331C809B294A63AB00263BE5 /* MediaSanitizer.swift in Sources */,
4A71C0012F40100100A17E01 /* InlinePhotoPicker.swift in Sources */,
4A71C0032F40200100A17E01 /* NativeAttachmentPopover.swift in Sources */,

View File

@@ -99,6 +99,7 @@ import os.log
}
}
shareSuggestions?.attach(messenger: messenger)
QuickSendBridge.shared.attach(messenger: messenger)
browserAuthBridge = BuzzBrowserAuthBridge(messenger: messenger)
hostedHpkeChannel = FlutterMethodChannel(name: "buzz/hosted_hpke", binaryMessenger: messenger)
hostedHpkeChannel?.setMethodCallHandler { call, result in

View File

@@ -0,0 +1,145 @@
import AppIntents
import Flutter
import os.log
/// Hands quick sends from Siri and Shortcuts to Dart.
///
/// A background launch starts the app's Flutter engine and builds Home, which
/// says `ready` once it can take sends. A send that arrives first waits for
/// that, up to a deadline.
final class QuickSendBridge {
static let shared = QuickSendBridge()
static let log = Logger(subsystem: "xyz.block.buzz", category: "QuickSend")
private var channel: FlutterMethodChannel?
private var ready = false
private var sending = false
private var waiters: [UUID: CheckedContinuation<Bool, Never>] = [:]
func attach(messenger: FlutterBinaryMessenger) {
let channel = FlutterMethodChannel(name: "buzz/quick_send", binaryMessenger: messenger)
self.channel = channel
ready = false
channel.setMethodCallHandler { [weak self] call, result in
guard let self else { return result(FlutterMethodNotImplemented) }
switch call.method {
case "ready":
Self.log.notice("dart ready")
self.ready = true
let waiting = self.waiters.values
self.waiters = [:]
waiting.forEach { $0.resume(returning: true) }
result(nil)
case "unready":
// Home left, for example on sign-out, and took its handler with it.
self.ready = false
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
}
/// Waits for Home's `ready`, returning false once [deadline] passes first.
@MainActor
private func waitUntilReady(_ deadline: Duration) async -> Bool {
if ready { return true }
let id = UUID()
return await withCheckedContinuation { continuation in
waiters[id] = continuation
Task { @MainActor in
try? await Task.sleep(for: deadline)
self.waiters.removeValue(forKey: id)?.resume(returning: false)
}
}
}
/// Sends [text] to the Bestie once [confirm] accepts its name, or throws a
/// message Siri can read.
@MainActor
func send(
_ text: String,
deadline: Duration = .seconds(20),
confirm: (_ bestieName: String?) async throws -> Void
) async throws {
// One at a time, so two quick sends post in the order they were confirmed.
guard !sending else { throw QuickSendError("Another message is still sending.") }
sending = true
defer { sending = false }
guard await waitUntilReady(deadline) else {
throw QuickSendError("Open Buzz, then try again.")
}
let bestie = try await call("bestie") as? [String: Any]
guard let pubkey = bestie?["pubkey"] as? String, let scope = bestie?["scope"] as? String else {
throw QuickSendError("Buzz couldn't find your Bestie.")
}
try await confirm(bestie?["name"] as? String)
let start = ContinuousClock.now
do {
_ = try await call("send", arguments: ["text": text, "bestie": pubkey, "scope": scope])
Self.log.notice("send finished after \(ContinuousClock.now - start, privacy: .public)")
} catch {
Self.log.notice("send failed after \(ContinuousClock.now - start, privacy: .public)")
throw error
}
}
@MainActor
private func call(_ method: String, arguments: Any? = nil) async throws -> Any? {
guard ready, let channel else { throw QuickSendError("Open Buzz, then try again.") }
let reply: Any? = await withCheckedContinuation { continuation in
channel.invokeMethod(method, arguments: arguments) { continuation.resume(returning: $0) }
}
if let error = reply as? FlutterError {
throw QuickSendError(error.message ?? "Buzz couldn't send that.")
}
if reply as? NSObject === FlutterMethodNotImplemented {
throw QuickSendError("Buzz isn't ready to send yet.")
}
return reply
}
}
struct QuickSendError: Error, CustomLocalizedStringResourceConvertible {
let message: String
init(_ message: String) { self.message = message }
var localizedStringResource: LocalizedStringResource { "\(message)" }
}
@available(iOS 16.0, *)
struct MessageBestieIntent: AppIntent {
static var title: LocalizedStringResource = "Message my Bestie"
static var description = IntentDescription("Sends a message in your DM with your Bestie without opening Buzz.")
static var openAppWhenRun = false
@Parameter(title: "Message", requestValueDialog: "What do you want to say?")
var message: String
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let text = message.trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty else { throw QuickSendError("Say a message to send.") }
try await QuickSendBridge.shared.send(text) { name in
try await requestConfirmation(
result: .result(dialog: "Send “\(text)” to \(name ?? "your Bestie")?"),
confirmationActionName: .send
)
}
return .result(dialog: "Sent.")
}
}
@available(iOS 16.0, *)
struct BuzzAppShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: MessageBestieIntent(),
phrases: [
"Message my Bestie in \(.applicationName)",
"Send a message to my Bestie in \(.applicationName)",
],
shortTitle: "Message my Bestie",
systemImageName: "bubble.left.and.text.bubble.right"
)
}
}

View File

@@ -1,6 +1,7 @@
import 'features/activity/activity_provider.dart';
import 'features/channels/bestie_conversation.dart';
import 'features/channels/default_agent_launcher.dart';
import 'features/channels/quick_send_host.dart';
import 'features/agents/agents_page.dart';
import 'dart:async';
@@ -15,6 +16,7 @@ import 'shared/community/paired_community_landing.dart';
import 'shared/community/community_loading_surface.dart';
import 'package:app_badge_plus/app_badge_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
@@ -451,6 +453,9 @@ class App extends HookConsumerWidget {
newChatAction: chat,
newWorkspaceAction: workspace,
),
backgroundHost: defaultTargetPlatform == TargetPlatform.iOS
? const QuickSendHost()
: null,
settingsPageBuilder: _buildSettingsPage,
communityInvitePageBuilder: (_) => const CommunityInvitePage(),
communityAppearancePageBuilder: (_) => const ThemePickerPage(),

View File

@@ -12,6 +12,26 @@ import 'channel_detail_page.dart';
import 'channels_provider.dart';
import 'channel_management_provider.dart';
/// The loaded direct conversation with the Bestie [pubkey], if any.
Channel? cachedBestieConversation(WidgetRef ref, String pubkey) {
final channelState = ref.read(channelsProvider);
// AsyncValue may retain the previous community's list during reload.
final channels = channelState.isLoading || channelState.hasError
? const <Channel>[]
: channelState.value ?? const <Channel>[];
final notifier = ref.read(channelsProvider.notifier);
return channels
.where(
(channel) => isActiveDirectConversation(
channel,
notifier.cachedMembersForChannel(channel.id),
ref.read(myPubkeyProvider),
pubkey,
),
)
.firstOrNull;
}
/// The open whose conversation lookup is in flight.
Object? _lookup;
@@ -33,22 +53,7 @@ Future<void> openBestieConversation(
final scope = ref.read(meSyncProvider);
try {
final channelState = ref.read(channelsProvider);
// AsyncValue may retain the previous community's list during reload.
final channels = channelState.isLoading || channelState.hasError
? const <Channel>[]
: channelState.value ?? const <Channel>[];
final notifier = ref.read(channelsProvider.notifier);
final cached = channels
.where(
(channel) => isActiveDirectConversation(
channel,
notifier.cachedMembersForChannel(channel.id),
ref.read(myPubkeyProvider),
pubkey,
),
)
.firstOrNull;
final cached = cachedBestieConversation(ref, pubkey);
onResolving?.call(cached == null);
final channel =
cached ??

View File

@@ -182,7 +182,30 @@ void _useComposeDraftLifecycle({
}
controller.addListener(persistDraft);
return () => controller.removeListener(persistDraft);
// Another writer, such as a failed quick send from Siri, can save this
// draft while the composer is open; show its text rather than overwrite
// it with the next keystroke. Own saves match lastPersistedText.
final external = ref.listenManual(
composeDraftsProvider.select(
(drafts) => drafts.where((d) => d.key == draftKey).firstOrNull?.text,
),
(_, text) {
if (!identical(currentOwner.value, owner)) return;
if (text == null ||
text == lastPersistedText ||
text == controller.text) {
return;
}
// A newer draft: an ordinary send that fails must not restore over it.
draftRevision.value += 1;
lastPersistedText = text;
controller.text = text;
},
);
return () {
controller.removeListener(persistDraft);
external.close();
};
}, [controller, draftKey, draftIdentity, onDraftIdentityChanged]);
_useComposerShareDelivery(
context: context,

View File

@@ -0,0 +1,234 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../shared/me/default_agent.dart';
import '../../shared/profile/user_cache_provider.dart';
import '../../shared/relay/relay.dart';
import '../activity/compose_drafts_provider.dart';
import 'bestie_conversation.dart';
import 'channel.dart';
import 'channel_management_provider.dart';
import 'send_message_provider.dart';
const _quickSendChannel = MethodChannel('buzz/quick_send');
/// A quick send Buzz turned down, with a reason Siri reads out.
class _QuickSendRefused implements Exception {
const _QuickSendRefused(this.message);
final String message;
}
/// Answers quick sends from Siri and Shortcuts while Home is mounted.
///
/// iOS can run a quick send with Buzz in the background; the app still
/// builds Home then, so this leaf is where the send gets the community's
/// session and the Bestie's DM. It tells the platform once it can take sends.
class QuickSendHost extends HookConsumerWidget {
const QuickSendHost({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
// Keeps the Bestie choice loaded for a send that arrives in the background.
ref.watch(defaultAgentProvider);
useEffect(() {
_quickSendChannel.setMethodCallHandler((call) async {
try {
return switch ((call.method, call.arguments)) {
('bestie', _) => await _bestie(ref),
(
'send',
{
'text': final String text,
'bestie': final String to,
'scope': final String scope,
},
) =>
await _quickSend(ref, text.trim(), to, scope),
_ => throw MissingPluginException(),
};
} on _QuickSendRefused catch (failure) {
throw PlatformException(code: 'failed', message: failure.message);
} on MissingPluginException {
rethrow;
} catch (failure) {
debugPrint('Quick send failed: $failure');
throw PlatformException(
code: 'failed',
message: "Buzz couldn't send that. Open Buzz to try again.",
);
}
});
unawaited(_quickSendChannel.invokeMethod<void>('ready'));
return () {
_quickSendChannel.setMethodCallHandler(null);
unawaited(_quickSendChannel.invokeMethod<void>('unready'));
};
}, const []);
return const SizedBox.shrink();
}
}
/// The Bestie a quick send will go to, named so the person can confirm it.
///
/// This is the saved choice, which may predate a change made on another
/// device that this one hasn't synced; naming it lets the person catch that.
Future<Map<String, String?>> _bestie(WidgetRef ref) async {
// Captured first: a switch during the awaits must not pair this
// community's scope with another's Bestie.
final scope = _scope(ref);
final bestie = await ref
.read(defaultAgentProvider.future)
.timeout(const Duration(seconds: 10));
if (bestie == null) {
throw const _QuickSendRefused('Choose a Bestie in Buzz first.');
}
if (ref.read(userCacheProvider)[bestie] == null) {
// Briefly: Siri can say "your Bestie" rather than keep the person waiting.
await ref
.read(userCacheProvider.notifier)
.preload([bestie])
.timeout(const Duration(seconds: 2), onTimeout: () => false);
}
_ensureScope(ref, scope);
return {
'pubkey': bestie,
'name': ref.read(userCacheProvider)[bestie]?.label,
'scope': scope,
};
}
/// The account and community a confirmed Bestie belongs to.
String _scope(WidgetRef ref) =>
'${ref.read(relayConfigProvider).baseUrl} ${ref.read(myPubkeyProvider)}';
/// Refuses a send whose confirmation was for another account or community.
void _ensureScope(WidgetRef ref, String scope) {
if (_scope(ref) != scope) {
throw const _QuickSendRefused(
'Buzz switched communities. Try again to confirm the new Bestie.',
);
}
}
/// Posts [content] in the direct conversation with [bestie].
///
/// When the send fails or Buzz can't tell whether it landed, the message is
/// kept as that DM's composer draft so it isn't lost.
Future<void> _quickSend(
WidgetRef ref,
String content,
String bestie,
String scope,
) async {
_ensureScope(ref, scope);
// Bound to this community: each refuses to act after a switch.
final actions = ref.read(channelActionsProvider);
final send = ref.read(sendMessageProvider);
final drafts = ref.read(composeDraftsProvider.notifier);
if (content.isEmpty) throw const _QuickSendRefused('Say a message.');
if (utf8.encode(content).length > 8192) {
throw const _QuickSendRefused('That message is too long.');
}
var dm = cachedBestieConversation(ref, bestie);
// After its background grace period the session has closed the socket.
// Reopen it for this send, then let the background pause start over.
final session = ref.read(relaySessionProvider.notifier);
final paused = switch (ref.read(appLifecycleProvider)) {
AppLifecycleState.paused || AppLifecycleState.detached => true,
_ => false,
};
if (paused) session.onAppResumed();
try {
if (!await _connected(ref)) {
if (dm == null) {
throw const _QuickSendRefused(
"Buzz couldn't reach your community, so your message wasn't saved.",
);
}
_ensureScope(ref, scope);
_keepDraft(drafts, dm, content);
throw const _QuickSendRefused(
"Buzz couldn't reach your community. Your message is a draft in "
'your Bestie DM.',
);
}
_ensureScope(ref, scope);
try {
dm ??= await actions.openDm(pubkeys: [bestie], waitForChannelList: false);
} catch (failure) {
debugPrint('Quick send lookup failed: $failure');
throw const _QuickSendRefused(
"Buzz couldn't open your Bestie DM, so your message wasn't saved.",
);
}
for (var attempt = 1; ; attempt++) {
try {
await send(channelId: dm.id, content: content, channel: dm);
return;
} catch (failure) {
// Catching up after a reconnect can use the relay's write quota. The
// relay turned that message away, so sending again can't duplicate
// it; any other failure might have landed and isn't retried.
final limited = '$failure'.contains('rate-limited:');
if (limited && attempt < 3) continue;
debugPrint('Quick send failed: $failure');
// A switch mid-send means this account's drafts are no longer here.
_ensureScope(ref, scope);
_keepDraft(drafts, dm, content);
throw _QuickSendRefused(
limited
? "Buzz couldn't send that yet. Your message is a draft in your "
'Bestie DM.'
: "Buzz isn't sure that sent. Check your Bestie DM before "
'trying again.',
);
}
}
} finally {
if (paused && ref.read(appLifecycleProvider) != AppLifecycleState.resumed) {
session.onAppPaused();
}
}
}
/// Keeps [content] in [dm]'s composer draft, after any text already there.
void _keepDraft(ComposeDraftsNotifier drafts, Channel dm, String content) {
final key = composeDraftKey(dm.id);
final before = drafts.draftFor(key);
drafts.save(
key: key,
channelId: dm.id,
text: before == null ? content : '${before.text}\n\n$content',
mentionKeys: before?.mentionKeys ?? const {},
);
}
/// Waits briefly for the relay session a cold background launch is opening.
Future<bool> _connected(WidgetRef ref) async {
if (ref.read(relaySessionProvider).status == SessionStatus.connected) {
return true;
}
final connected = Completer<void>();
final subscription = ref.listenManual(
relaySessionProvider.select((s) => s.status),
(_, status) {
if (status == SessionStatus.connected && !connected.isCompleted) {
connected.complete();
}
},
fireImmediately: true,
);
try {
await connected.future.timeout(const Duration(seconds: 10));
return true;
} on TimeoutException {
return false;
} finally {
subscription.close();
}
}

View File

@@ -39,6 +39,7 @@ class HomePage extends HookConsumerWidget {
required this.openBestieConversation,
this.communityInvitePageBuilder,
this.mePageBuilder,
this.backgroundHost,
this.communityAppearancePageBuilder,
super.key,
});
@@ -58,6 +59,9 @@ class HomePage extends HookConsumerWidget {
ValueNotifier<VoidCallback?>,
)?
mePageBuilder;
/// An invisible widget that serves background work while Home is up.
final Widget? backgroundHost;
final WidgetBuilder? communityInvitePageBuilder;
final WidgetBuilder? communityAppearancePageBuilder;
@@ -393,6 +397,7 @@ class HomePage extends HookConsumerWidget {
openBestieConversation: openBestieConversation,
),
),
?backgroundHost,
MediaQueryOverride(
transform: (media) => media.copyWith(
padding: media.padding.copyWith(

View File

@@ -52,12 +52,14 @@ void shareDeliveryTests() {
).read(composeDraftsProvider.notifier).textFor('channel-1');
String? sent;
Completer<void>? sending;
await tester.pumpWidget(
_buildComposeBar(
uploadService: _testUploadService(nostr.Keys.generate().nsec),
onSend:
(content, mentions, {mediaTags = const <List<String>>[]}) async {
sent = content;
await sending?.future;
},
),
);
@@ -143,6 +145,47 @@ void shareDeliveryTests() {
await tester.pumpAndSettle();
expect(find.byTooltip('Remove attachment'), findsOneWidget);
expect(find.text('Couldn’t add some of what you shared.'), findsOneWidget);
// A quick send from Siri that fails keeps its text in the open
// composer's draft; the composer shows it rather than overwrite it.
if (find.byType(TextField).evaluate().isEmpty) {
await tester.tap(find.text('Still here'));
await tester.pumpAndSettle();
}
final drafts = ProviderScope.containerOf(
tester.element(find.byType(ComposeBar)),
).read(composeDraftsProvider.notifier);
drafts.save(
key: 'channel-1',
channelId: 'channel-1',
text: 'Still here\n\nFrom Siri',
);
await tester.pumpAndSettle();
expect(
tester.widget<TextField>(find.byType(TextField)).controller?.text,
'Still here\n\nFrom Siri',
);
await tester.enterText(find.byType(TextField), 'Still here\n\nFrom Siri!');
await tester.pumpAndSettle();
expect(draftText(), 'Still here\n\nFrom Siri!');
// Siri's text arriving while an ordinary send is still out survives that
// send failing: the composer doesn't put the sent text back over it.
await tester.tap(find.byTooltip('Remove attachment'));
await tester.pumpAndSettle();
sending = Completer<void>();
await tester.tap(find.byIcon(BuzzIcons.arrowUp));
await tester.pump();
expect(sent, 'Still here\n\nFrom Siri!');
drafts.save(key: 'channel-1', channelId: 'channel-1', text: 'Again');
await tester.pump();
sending.completeError(Exception('offline'));
await tester.pumpAndSettle();
expect(draftText(), 'Again');
expect(
tester.widget<TextField>(find.byType(TextField)).controller?.text,
'Again',
);
});
testWidgets('a share to the open conversation joins the composer on screen', (

View File

@@ -0,0 +1,384 @@
import 'dart:async';
import 'dart:convert';
import 'package:buzz/features/activity/compose_drafts_provider.dart';
import 'package:buzz/features/channels/channel.dart';
import 'package:buzz/features/channels/channels_provider.dart';
import 'package:buzz/features/channels/quick_send_host.dart';
import 'package:buzz/shared/me/default_agent.dart';
import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import 'package:pointycastle/digests/sha256.dart';
import 'package:shared_preferences/shared_preferences.dart';
class _EmptyChannels extends ChannelsNotifier {
@override
Future<List<Channel>> build() async => [];
}
class _Lifecycle extends AppLifecycleNotifier {
@override
AppLifecycleState build() => AppLifecycleState.resumed;
void background() => state = AppLifecycleState.paused;
}
class _Config extends RelayConfigNotifier {
_Config(this.config);
final RelayConfig config;
@override
RelayConfig build() => config;
}
/// A relay that is not connected until the test says so, accepts every
/// publish and serves back the DM its open command finds or creates.
class _Relay extends RelaySessionNotifier {
_Relay(this.owner, this.profile);
final nostr.Keys owner;
final NostrEvent profile;
final published = <NostrEvent>[];
/// Rejects the next message the way an over-quota relay does.
var rateLimitNextMessage = false;
/// Loses the next message's answer, so it may or may not have landed.
var timeOutNextMessage = false;
/// Drops the socket just before the next message goes out.
var dropBeforeNextMessage = false;
final _stored = <NostrEvent>[];
static const dmId = 'bestie-dm';
@override
SessionState build() =>
const SessionState(status: SessionStatus.disconnected);
void connect() => state = const SessionState(status: SessionStatus.connected);
/// Background pauses started; the real session closes the socket after one.
var pauses = 0;
@override
void onAppPaused() => pauses++;
@override
void onAppResumed() => connect();
NostrEvent _sign(int kind, List<List<String>> tags) => NostrEvent.fromJson(
nostr.Event.from(
kind: kind,
content: '',
secretKey: owner.secret,
tags: tags,
).toMap(),
);
@override
Future<List<NostrEvent>> queryRelay(
List<NostrFilter> filters, {
Duration timeout = const Duration(seconds: 8),
}) async => [profile];
@override
Future<List<NostrEvent>> fetchHistory(
NostrFilter filter, {
Duration timeout = const Duration(seconds: 8),
}) async {
if (filter.kinds.contains(0)) return [profile];
final ds = filter.tags['#d'];
return _stored.reversed
.where(
(e) =>
filter.kinds.contains(e.kind) &&
(ds == null || ds.contains(e.getTagValue('d'))),
)
.take(filter.limit)
.toList();
}
@override
Future<void Function()> subscribeWithStatus(
NostrFilter filter,
void Function(NostrEvent) onEvent, {
void Function(String message)? onClosed,
required void Function(RelaySubscriptionStatus status) onStatusChanged,
}) async => () {};
@override
Future<NostrEvent> publish(
NostrEvent event, {
Duration timeout = const Duration(seconds: 8),
}) async {
if (event.kind == 9 && rateLimitNextMessage) {
rateLimitNextMessage = false;
throw Exception('rate-limited: quota exceeded; retry in 0s');
}
if (event.kind == 9 && dropBeforeNextMessage) {
dropBeforeNextMessage = false;
throw StateError('Relay session is not connected');
}
if (event.kind == 9 && timeOutNextMessage) {
timeOutNextMessage = false;
throw TimeoutException('no OK');
}
published.add(event);
if (event.kind != 41010) return event;
// Opening is find-or-create: the relay answers with the one DM.
if (!_stored.any((e) => e.getTagValue('d') == dmId)) {
final peer = event.getTagValue('p')!;
_stored.add(
_sign(39000, [
['d', dmId],
['name', 'DM'],
['t', 'dm'],
['private'],
['hidden'],
['p', owner.public],
['p', peer],
]),
);
_stored.add(
_sign(39002, [
['d', dmId],
['p', owner.public, '', 'member'],
['p', peer, '', 'bot'],
]),
);
}
return NostrEvent(
id: event.id,
pubkey: event.pubkey,
createdAt: event.createdAt,
kind: event.kind,
tags: event.tags,
content: 'response:${jsonEncode({'channel_id': dmId, 'created': true})}',
sig: event.sig,
);
}
}
void main() {
testWidgets('Siri quick send posts in the Bestie DM', (tester) async {
const channel = MethodChannel('buzz/quick_send');
final platformCalls = <String>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
platformCalls.add(call.method);
return null;
});
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
channel,
null,
),
);
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final owner = nostr.Keys.generate(), bestie = nostr.Keys.generate();
final digest = SHA256Digest()
.process(
Uint8List.fromList(utf8.encode('nostr:agent-auth:${bestie.public}:')),
)
.map((v) => v.toRadixString(16).padLeft(2, '0'))
.join();
final profile = NostrEvent.fromJson(
nostr.Event.from(
kind: 0,
content: jsonEncode({'display_name': 'Bestie'}),
secretKey: bestie.secret,
tags: [
[
'auth',
owner.public,
'',
nostr.Schnorr.sign(secretKey: owner.secret, message: digest),
],
],
).toMap(),
);
final relay = _Relay(owner, profile);
String? chosenBestie;
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
relayConfigProvider.overrideWith(
() => _Config(
RelayConfig(baseUrl: 'https://relay.test', nsec: owner.nsec),
),
),
relaySessionProvider.overrideWith(() => relay),
defaultAgentProvider.overrideWith(
(ref) => Stream.value(chosenBestie),
),
channelsProvider.overrideWith(_EmptyChannels.new),
appLifecycleProvider.overrideWith(_Lifecycle.new),
],
child: MaterialApp(
theme: AppTheme.light(),
home: const QuickSendHost(),
),
),
);
await tester.pumpAndSettle();
expect(platformCalls, ['ready'], reason: 'Siri can send once Home is up');
// Siri calls the host the way QuickSend.swift does and gets its reply.
Future<(Object?, PlatformException?)> siri(
String method, [
Object? arguments,
]) async {
Object? result;
PlatformException? failure;
var replied = false;
tester.binding.defaultBinaryMessenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(MethodCall(method, arguments)),
(reply) {
try {
result = channel.codec.decodeEnvelope(reply!);
} on PlatformException catch (e) {
failure = e;
}
replied = true;
},
);
// Ownership verification runs in a real isolate.
for (var i = 0; i < 100 && !replied; i++) {
await tester.pump(const Duration(milliseconds: 50));
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
}
expect(replied, isTrue, reason: '$method never answered');
return (result, failure);
}
final container = ProviderScope.containerOf(
tester.element(find.byType(QuickSendHost)),
);
container.read(relaySessionProvider);
relay.connect();
await tester.pump();
final (_, noBestie) = await siri('bestie');
expect(noBestie?.code, 'failed');
expect(noBestie?.message, 'Choose a Bestie in Buzz first.');
chosenBestie = bestie.public;
container.invalidate(defaultAgentProvider);
await tester.pump();
final (named, _) = await siri('bestie');
final scope = (named! as Map)['scope'];
expect(named, {'pubkey': bestie.public, 'name': 'Bestie', 'scope': scope});
// A confirmation from before a community switch can't send here.
final (_, switched) = await siri('send', {
'text': 'Remind me to call Sam',
'bestie': bestie.public,
'scope': 'https://other.test $scope',
});
expect(switched?.message, contains('switched communities'));
final (_, blank) = await siri('send', {
'text': ' \n ',
'bestie': bestie.public,
'scope': scope,
});
expect(blank?.code, 'failed');
expect(blank?.message, 'Say a message.');
expect(relay.published, isEmpty);
// Buzz sat in the background long enough for its socket to close.
(container.read(appLifecycleProvider.notifier) as _Lifecycle).background();
relay.state = const SessionState(status: SessionStatus.disconnected);
final pausesBefore = relay.pauses;
// Its catch-up used the write quota, so the first try is turned away.
relay.rateLimitNextMessage = true;
await tester.pumpAndSettle();
final (_, failure) = await siri('send', {
'text': ' Remind me to call Sam ',
'bestie': bestie.public,
'scope': scope,
});
expect(failure, isNull);
expect(
relay.pauses,
pausesBefore + 1,
reason: 'The send hands the session back to its background pause',
);
final open = relay.published.singleWhere((e) => e.kind == 41010);
expect(open.pubkey, owner.public);
expect(open.tags, [
['p', bestie.public],
]);
final first = relay.published.singleWhere((e) => e.kind == 9);
expect(first.content, 'Remind me to call Sam');
expect(first.pubkey, owner.public);
expect(first.getTagValue('h'), _Relay.dmId);
expect(first.tags, contains(equals(['p', bestie.public])));
// A sent message leaves the DM's draft as it was.
final drafts = container.read(composeDraftsProvider.notifier);
drafts.save(
key: _Relay.dmId,
channelId: _Relay.dmId,
text: 'Half a @Sam',
mentionKeys: {'Sam': 'sam-pubkey'},
);
// A second quick send goes to the same DM.
final (_, again) = await siri('send', {
'text': 'And buy milk',
'bestie': bestie.public,
'scope': scope,
});
expect(again, isNull);
final messages = relay.published.where((e) => e.kind == 9).toList();
expect(messages.map((e) => e.content), [
'Remind me to call Sam',
'And buy milk',
]);
expect(messages.map((e) => e.getTagValue('h')).toSet(), {_Relay.dmId});
expect(drafts.textFor(_Relay.dmId), 'Half a @Sam');
// When Buzz can't tell whether a send landed, the text stays in the DM's
// draft, after the person's own, and Siri says to check before resending.
relay.timeOutNextMessage = true;
final (_, unsure) = await siri('send', {
'text': 'Call back',
'bestie': bestie.public,
'scope': scope,
});
expect(
unsure?.message,
"Buzz isn't sure that sent. Check your Bestie DM before trying again.",
);
final kept = drafts.draftFor(_Relay.dmId)!;
expect(kept.text, 'Half a @Sam\n\nCall back');
expect(kept.mentionKeys, {'Sam': 'sam-pubkey'});
// A socket that drops just before the message goes out keeps it too.
relay.dropBeforeNextMessage = true;
final (_, dropped) = await siri('send', {
'text': 'Then Sam',
'bestie': bestie.public,
'scope': scope,
});
expect(dropped?.message, contains('Check your Bestie DM'));
expect(drafts.textFor(_Relay.dmId), 'Half a @Sam\n\nCall back\n\nThen Sam');
// Leaving Home, for example on sign-out, takes the handler with it.
await tester.pumpWidget(const SizedBox());
expect(platformCalls, ['ready', 'unready']);
// Let the sent messages' local animations finish.
await tester.pump(const Duration(seconds: 10));
});
}