Files
buzz/mobile/lib/features/channels/channel_link_navigation.dart
Tom Brow 61b8455a3c fix(mobile): open unjoined channels from links and search results (#8923)
Tapping a `#channel` link to an open channel you haven't joined showed
"Channel could not be opened", because a tap could only open a channel
already in the local list. A search hit had the same gap: it did nothing
when its channel wasn't in the loaded list, for example when the
open-channel directory failed to load or hadn't loaded yet.

Both taps now go through one resolver. It looks in the loaded list
first, then reads the channel's own metadata, as `buzz://` deep links
already do.
- An open channel you haven't joined opens in preview ("Join this
channel from Manage to participate").
- A private or withheld channel stays closed and shows "Channel not
found here".
- A failed lookup shows "Could not open the channel". So does a missing
channel when the channel list failed to load, since it may be one you
belong to.

The resolver also guards two cases:
- A second tap during a slow lookup doesn't open the channel twice.
- A lookup that finishes after you switch community or identity, or
after you moved to another page, opens nothing and doesn't block a new
tap.

Fixes AIDA-2324 and AIDA-2286.
[#8764](https://github.com/block/buzz/pull/8764) already made search
load the open directory, which fixed the common search case, so the
search part here covers the remaining fallback.

### Before: a link to an unjoined open channel fails


https://github.com/user-attachments/assets/c39cf0db-8098-4010-993e-cf664230c809

### After: the same link opens the channel in preview


https://github.com/user-attachments/assets/1422e70f-39c5-4598-8232-efad190b3413

### Validation

- Added tests:
- A table-driven channel-link journey with four rows: an unjoined open
channel opens, a withheld channel and a private channel show "Channel
not found here", and a failed lookup shows "Could not open the channel".
Each row double-taps during a gated lookup and asserts a single lookup
and a single push. Failure rows then tap again to show the lock is
released.
- A lookup that settles after the user opened another page pushes
nothing.
- Search journeys: a message hit opens its unlisted channel (including
after the channel list failed to load), or explains why it can't ("Could
not open the channel" when the list failed).
- Taps whose lookup will be discarded don't block the next tap: after a
community switch away and back, or from another page after the first
page closed. Each row fails if its part of the tap lock or the
connection fence is removed.
- I reverted the production files to `main` once and confirmed every new
test fails there.
- The full mobile suite and `mobile-check` pass.
- I ran it on an iPhone 18 Pro simulator (iOS 27), signed in to
Builderlab with an account that isn't a member of the open channel
`#speed-tests`.
- A link to that channel shows the error toast on `main` and opens a
preview with this change (videos above).
- A search hit in that channel opens it with this change. On `main` it
also opens, through [#8764](https://github.com/block/buzz/pull/8764)'s
directory load.
- Not covered by tests: waiting for a channel-list load still in flight,
a short guard I checked by reading the code.
- The human app-test step doesn't apply (Tom's standing exception for
mobile client changes).

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-02 12:55:59 -08:00

116 lines
3.6 KiB
Dart

import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/widgets/feedback_snackbar.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'channel.dart';
import 'channel_detail_page.dart';
import 'channels_provider.dart';
Future<void> openChannelLink({
required BuildContext context,
required WidgetRef ref,
required String channelId,
required String currentChannelId,
}) async {
if (channelId == currentChannelId) return;
final targetChannel = await resolveChannelToOpen(context, ref, channelId);
if (targetChannel == null || !context.mounted) return;
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => ChannelDetailPage(channel: targetChannel),
),
);
}
/// Taps still resolving, so a repeated tap during a slow lookup doesn't open
/// the channel twice. Each is keyed by the page and connection it was made
/// on, so a lookup that will be discarded blocks no new tap: the relay config
/// is replaced on every community or identity switch, even back to an earlier
/// one, and a closed page's lookup is dropped.
final _resolving = <(Object?, RelayConfig, String)>{};
/// Resolves [channelId] for a tap that opens it, or shows why it can't be
/// opened and returns null.
///
/// The loaded list holds member channels and, once loaded, the open
/// directory. An open channel outside both is read from its own metadata, as
/// `buzz://` links do, and remembered so the opened page can find it.
/// Returns null without feedback for a repeated tap while the first one is
/// resolving, or when the community or identity changed or the user moved to
/// another page during the lookup.
Future<Channel?> resolveChannelToOpen(
BuildContext context,
WidgetRef ref,
String channelId,
) async {
final key = (
ModalRoute.of(context),
ref.read(relayConfigProvider),
channelId,
);
if (!_resolving.add(key)) return null;
try {
return await _resolve(context, ref, channelId);
} finally {
_resolving.remove(key);
}
}
Future<Channel?> _resolve(
BuildContext context,
WidgetRef ref,
String channelId,
) async {
final notifier = ref.read(channelsProvider.notifier);
final messenger = ScaffoldMessenger.of(context);
final connection = ref.read(relayConfigProvider);
Channel? channel;
Object? failure;
try {
// A tap can arrive before the first list load finishes. A failed load
// still leaves the channel's own metadata, so the tap is a real retry,
// but without the list a channel the user belongs to may be unreadable.
if (!ref.read(channelsProvider).hasValue) {
try {
await ref.read(channelsProvider.future);
} catch (error) {
failure = error;
}
}
channel =
notifier.channelById(channelId) ??
await notifier.fetchOpenChannel(channelId);
} catch (error) {
failure = error;
}
// The tap belongs to the connection and page it was made on: once the user
// has switched community or moved to another page, it opens nothing.
if (!context.mounted ||
!identical(ref.read(relayConfigProvider), connection) ||
!(ModalRoute.of(context)?.isCurrent ?? true)) {
return null;
}
if (channel != null) {
notifier.rememberOpened(channel);
return channel;
}
if (failure != null) {
debugPrint('channel $channelId lookup failed: $failure');
}
messenger.showFeedbackSnackBar(
SnackBar(
content: Text(
failure != null
? 'Could not open the channel'
: 'Channel not found here',
),
),
kind: ToastKind.error,
);
return null;
}