mirror of
https://github.com/block/buzz.git
synced 2026-11-13 04:41:12 +00:00
## Summary Mobile push only covered Solo chats through explicit mentions. Solo chats are private streams rather than DMs, and the push lease subscribed channel-wide only to DMs. Agent replies in your own Solo chats therefore never woke the phone. The lease now subscribes channel-wide to the viewer's own joined, unarchived Solo chats, using the same predicate as the Solo tab, including older unlabelled Solo chats listed in the encrypted Solo record. Self-authored messages stay excluded through the existing lease ignores. Muted Solo chats stay out of the lease, like muted channels, so they push only on mention. The lease waits while the Solo record loads so an unresolved record never drops legacy Solo chats. If that record fails with no cached copy, DM and mention updates proceed without the legacy chats, which the Solo tab cannot list either, and they return when it recovers. Solo chats created by someone else still push only on mention, matching how they appear in the channel list. A lease is bounded by its channel count (750), by the relay's advertised plaintext and content sizes, and by NIP-44's 65535-byte plaintext ceiling. DMs always stay, as before. Solo chats fill the remaining room most recent first, sized with the publisher's own lease encoding, the exact NIP-44 payload length and the largest endpoint the relay accepts. Sizes are computed rather than built, so a relay's advertised limits never drive an allocation. Solo chats beyond the room push only on mention instead of failing the lease update or publication, and Notifications settings says how many. A relay too small even for DMs and mentions fails push setup visibly instead of saving a lease it would reject. Publication accepts only endpoint grants in the gateway's JSON-safe alphabet, so the sizing assumption holds. Thread replies outside DMs still do not push unless they mention you. That is a separate gap. ### Related issue None found. ### Validation - Added test: the desired lease includes joined DMs and the viewer's own Solo chats, and includes legacy Solo chats from the Solo record, and excludes archived and muted Solo chats, other people's Solo chats, and unlabelled private streams. It fails without the fix. - Added test: the sync provider holds the desired lease while the Solo record loads, proceeds without legacy chats if the record fails, and adds them once it arrives. It fails if loading is treated as empty or if a failure blocks updates. - Added tests: over the channel count, and over each size limit (relay plaintext, relay content, NIP-44 plaintext), the lease keeps every DM and exactly the newest Solo chats, reports the rest as omitted, and is accepted by `publishBuzzDevPushLease` with a grant at the relay's endpoint limit, while a max-length grant that JSON would escape is rejected as not a gateway grant. A relay advertising a 2^50-byte endpoint limit is sized without allocating. Each row fails if its guard is removed. - Added test: a relay whose content or plaintext limit cannot fit even the lease without Solo chats rejects that lease at publication, and the provider fails setup instead of saving it. It fails if the guard is removed. - Added test: the NIP-44 payload length matches real encryption at every padding boundary up to 65535 bytes. - Added test: Notifications settings shows "Older Solo chats notify only on mention" with the omitted count. It fails without the row. - Mobile lint and the full mobile test suite pass. - Not exercised end to end on a device. A signed dev build was installed on a test iPhone against a personal dev relay, but the phone's dev identity owns no assistant there, so the device could not create a Solo chat without exporting its key. This will be validated in dogfood. The relay matcher already handles channel-wide `#h` leases for DMs, and Solo messages use the same kind and scoping. buzz-review-completed 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
6.0 KiB
Dart
163 lines
6.0 KiB
Dart
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import '../../features/channels/channel.dart';
|
|
import '../../features/channels/channel_mutes/channel_mutes_provider.dart';
|
|
import '../../features/channels/channels_provider.dart';
|
|
import '../auth/auth.dart';
|
|
import '../me/me_sync.dart';
|
|
import '../relay/relay_provider.dart';
|
|
import 'dev_push_lease.dart';
|
|
import 'push_subscription.dart';
|
|
import 'push_relay_capability_provider.dart';
|
|
import 'push_setup_recovery.dart';
|
|
|
|
/// Keeps the persisted desired lease and the App Group snapshot aligned with
|
|
/// the active identity, joined channels, and mute state. This is desired client
|
|
/// policy only. Relay-accepted authority is introduced by lease publication.
|
|
///
|
|
/// Yields how many Solo chats the lease had no room for.
|
|
final pushSubscriptionSyncProvider = FutureProvider.autoDispose<int>((
|
|
ref,
|
|
) async {
|
|
try {
|
|
if (ref.watch(authProvider).value?.status != AuthStatus.authenticated) {
|
|
return 0;
|
|
}
|
|
final active = ref.watch(activeCommunityProvider).value;
|
|
final descriptor = ref.watch(currentRelayPushDescriptorProvider).value;
|
|
if (active == null ||
|
|
!active.pushNotificationsEnabled ||
|
|
descriptor == null) {
|
|
return 0;
|
|
}
|
|
final channels = ref.watch(channelsProvider).value;
|
|
final mutes = ref.watch(channelMutesProvider);
|
|
if (channels == null || !mutes.isReady) {
|
|
return 0;
|
|
}
|
|
|
|
// Legacy Solo chats are listed only in the Solo record. Wait while it
|
|
// loads so an unresolved record never drops them. If it fails without a
|
|
// cached copy, keep DMs and mentions current; the Solo tab cannot list
|
|
// those chats either, and recovery republishes them.
|
|
final soloRecord = ref.watch(meGroupsProvider);
|
|
if (soloRecord.isLoading && !soloRecord.hasValue) return 0;
|
|
|
|
final desired = desiredBuzzPushSubscriptions(
|
|
community: active,
|
|
descriptor: descriptor,
|
|
channels: channels,
|
|
legacySoloChannelIds: soloRecord.value?.channels ?? const [],
|
|
mutedChannelIds: [
|
|
for (final entry in mutes.store.channels.entries)
|
|
if (entry.value.muted) entry.key,
|
|
],
|
|
);
|
|
if (desired == null) return 0;
|
|
await ref
|
|
.read(communityListProvider.notifier)
|
|
.updateDesiredPushSubscriptions(active.id, desired.subscriptions);
|
|
return desired.omittedSoloChats;
|
|
} catch (error, stack) {
|
|
reportPushSetupError('subscription synchronization', error, stack);
|
|
rethrow;
|
|
}
|
|
}, retry: pushSetupRetry);
|
|
|
|
/// The desired lease, and how many Solo chats it had no room for.
|
|
({List<BuzzPushSubscription> subscriptions, int omittedSoloChats})?
|
|
desiredBuzzPushSubscriptions({
|
|
required Community community,
|
|
required BuzzPushLeaseDescriptor descriptor,
|
|
required Iterable<Channel> channels,
|
|
required Iterable<String> mutedChannelIds,
|
|
Iterable<String> legacySoloChannelIds = const [],
|
|
}) {
|
|
final pubkey = community.pubkey ?? pubkeyFromNsec(community.nsec);
|
|
if (pubkey == null || pubkey.isEmpty) return null;
|
|
final muted = {for (final id in mutedChannelIds) id.toLowerCase()};
|
|
final candidates = _channelWideCandidates(
|
|
pubkey: pubkey,
|
|
channels: channels,
|
|
muted: muted,
|
|
legacySoloChannelIds: legacySoloChannelIds,
|
|
);
|
|
List<BuzzPushSubscription> lease(int soloCount) =>
|
|
buildDesiredBuzzPushSubscriptions(
|
|
myPubkey: pubkey,
|
|
channelIds: [...candidates.dms, ...candidates.solos.take(soloCount)],
|
|
mutedChannelIds: mutedChannelIds,
|
|
);
|
|
|
|
// Each Solo chat only adds bytes, so keep the most recent ones that fit.
|
|
var fitting = candidates.solos.length;
|
|
if (!buzzPushSubscriptionsFitLease(descriptor, lease(fitting))) {
|
|
// Without Solo chats only DMs and mentions remain. A relay too small for
|
|
// those cannot carry push, so fail setup rather than save a lease that
|
|
// publication would reject.
|
|
if (!buzzPushSubscriptionsFitLease(descriptor, lease(0))) {
|
|
throw const FormatException(
|
|
'The relay push limits are too small for a lease.',
|
|
);
|
|
}
|
|
var low = 0;
|
|
var high = fitting - 1;
|
|
while (low < high) {
|
|
final mid = (low + high + 1) ~/ 2;
|
|
if (buzzPushSubscriptionsFitLease(descriptor, lease(mid))) {
|
|
low = mid;
|
|
} else {
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
fitting = low;
|
|
}
|
|
return (
|
|
subscriptions: lease(fitting),
|
|
omittedSoloChats: candidates.omitted + candidates.solos.length - fitting,
|
|
);
|
|
}
|
|
|
|
/// Channel-wide traffic pushes only for joined DMs and the viewer's own Solo
|
|
/// chats, which are conversations with them. Other channels push through an
|
|
/// exact #p mention, so subscribing to every joined channel would over-notify.
|
|
///
|
|
/// DMs always stay, as before. Solo chats follow most recent first, within the
|
|
/// lease's channel counts; the rest push only on mention.
|
|
({List<String> dms, List<String> solos, int omitted}) _channelWideCandidates({
|
|
required String pubkey,
|
|
required Iterable<Channel> channels,
|
|
required Set<String> muted,
|
|
required Iterable<String> legacySoloChannelIds,
|
|
}) {
|
|
bool isMuted(String id) => muted.contains(id.toLowerCase());
|
|
final joined = [
|
|
for (final channel in channels)
|
|
if (channel.isMember && !channel.isArchived) channel,
|
|
];
|
|
final dms = [
|
|
for (final channel in joined)
|
|
if (channel.isDm) channel.id,
|
|
];
|
|
// Muted Solo chats stay out, like muted channels, so they push only on
|
|
// mention.
|
|
final ordered = [
|
|
for (final channel in joined)
|
|
if (!channel.isDm &&
|
|
!isMuted(channel.id) &&
|
|
channel.isPersonalChat(pubkey, legacySoloChannelIds))
|
|
channel,
|
|
]..sort(_mostRecentFirst);
|
|
final room =
|
|
buzzPushMaxChannelWideChannels - dms.where((id) => !isMuted(id)).length;
|
|
final solos = [for (final solo in ordered.take(room < 0 ? 0 : room)) solo.id];
|
|
return (dms: dms, solos: solos, omitted: ordered.length - solos.length);
|
|
}
|
|
|
|
int _mostRecentFirst(Channel a, Channel b) {
|
|
final byRecency = (b.lastMessageAt ?? b.createdAt).compareTo(
|
|
a.lastMessageAt ?? a.createdAt,
|
|
);
|
|
return byRecency != 0 ? byRecency : a.id.compareTo(b.id);
|
|
}
|