Files
buzz/mobile/lib/features/channels/dm_channel_labels.dart
morgmart 70bb7b7bec feat(mobile): name blocking agents, leave agents out of DM names (#9875)
## Problem

Two follow-ups to growable DMs (#9153), matching desktop
block/buzz-app#3744:

1. When an agent's add policy blocks a DM, mobile shows a generic error.
The person can't tell which agent blocked it or whose owner to ask.
2. DM names list every member, agents included, so a DM with one person
and their agent reads like a group, and "+N more" mixes agents with
people.

## Change

**Name the blocking agent.** The relay names the agent in that refusal
(#9601). Mobile shows the agent's name: "Only Scout's owner can add it
to a DM." or "Scout can't be added to a DM.", starting with "Couldn't
add them." when adding to an existing DM, and "this agent" when the name
can't be read. Against a relay without #9601, the error is the same as
before.

**Leave agents out of DM names.** A DM's name lists its people and never
its agents, unless no other person is in it (just you and agents), when
it names the agents. "+N more" counts people only. Member counts and
member lists still include agents. The shared DM label feeds the list,
headers, details and Activity. Agent means the profile's existing agent
flag; a profile that hasn't loaded yet counts as a person until it says
otherwise.

## Screenshots

Temporary headless renders with test data, not committed.

### Name the blocking agent

Add people, refused by an agent's owner-only setting:

| | Light | Dark |
|---|---|---|
| Narrow | ![light
narrow](https://github.com/user-attachments/assets/61bfbfe2-5882-456e-8bff-b42f7389aa06)
| ![dark
narrow](https://github.com/user-attachments/assets/d7ac0dca-9081-4cde-a403-cde492d2cf4f)
|
| Wide | ![light
wide](https://github.com/user-attachments/assets/cc9f5a0f-0f3a-4d80-8fc6-0937a2246462)
| ![dark
wide](https://github.com/user-attachments/assets/87b48014-9740-4e17-ad83-f5bac272dda8)
|

### DM names without agents

Ana alone, Ana with two agents (named "Ana", count still 3), and only
the two agents (named by them):

| | Light | Dark |
|---|---|---|
| Narrow | ![light
narrow](https://github.com/user-attachments/assets/b413dbc1-a2bc-4410-8be9-b1a219374349)
| ![dark
narrow](https://github.com/user-attachments/assets/75c9cbc3-432f-45c7-83a4-624f80752b97)
|
| Wide | ![light
wide](https://github.com/user-attachments/assets/ae57c305-f4e4-4e26-8ba0-ecbcd128137e)
| ![dark
wide](https://github.com/user-attachments/assets/a75efc90-6b20-4409-bdd5-682de1f0301f)
|

## Tests

- Blocking agent: an open and an add name the agent, fall back to "this
agent", and leave other refusals as they were.
- DM names: people and agents, only agents, more than three people and
agents, and a profile that hasn't loaded yet.

The lost-reply recovery was taken out after review. It comes back as a
relay change that answers a replayed signed command with its saved
result, so the app can resend the same event instead of guessing from
rosters.

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

---------

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 12:10:23 -08:00

169 lines
6.3 KiB
Dart

import '../../shared/identity_names/identity_names.dart';
import '../../shared/utils/string_utils.dart';
import 'channel.dart';
const int _dmParticipantPreviewLimit = 3;
bool isGenericDmChannelName(String name) {
final normalized = name.trim().toLowerCase();
if (normalized.isEmpty ||
normalized == 'dm' ||
normalized == 'direct message' ||
normalized == 'direct messages') {
return true;
}
return RegExp(r'^group dm\s*(\(\d+\))?$').hasMatch(normalized);
}
String formatDmParticipantDisplayName(List<String> displayNames) {
final visible = displayNames.take(_dmParticipantPreviewLimit).toList();
final hiddenCount = displayNames.length - visible.length;
return hiddenCount > 0
? [...visible, '+$hiddenCount more'].join(', ')
: visible.join(', ');
}
/// Contextual participant labels for a DM: its participants are the whole
/// comparison context, and relay participant names are fallbacks.
IdentityNames dmParticipantNames(Channel channel, IdentityNameSources names) =>
names.scope(
channel.participantPubkeys,
fallbackNames: {
for (var index = 0; index < channel.participantPubkeys.length; index++)
if (index < channel.participants.length)
channel.participantPubkeys[index]: channel.participants[index],
},
);
/// The DM's display label. With [names], participant labels follow the
/// contextual identity-name policy, so namesakes stay distinguishable, and
/// agents are left out unless no other person is in the DM. "+N more"
/// counts people only; member counts and lists still include agents.
String resolveDmChannelDisplayLabel(
Channel channel, {
String? currentPubkey,
IdentityNameSources? names,
}) {
if (!channel.isDm || !isGenericDmChannelName(channel.name)) {
return channel.name;
}
final normalizedCurrent = currentPubkey?.toLowerCase();
final participantNames = names == null
? null
: dmParticipantNames(channel, names);
final participants = <({String label, String? pubkey})>[
for (var index = 0; index < channel.participantPubkeys.length; index++)
(
label:
participantNames?.labelFor(channel.participantPubkeys[index]) ??
(index < channel.participants.length
? channel.participants[index]
: shortPubkey(channel.participantPubkeys[index])),
pubkey: channel.participantPubkeys[index].toLowerCase(),
),
];
final others = normalizedCurrent == null
? participants
: participants
.where((participant) => participant.pubkey != normalizedCurrent)
.toList();
// The name lists people, never agents, unless no other person is in it.
// A profile not loaded yet counts as a person until it says otherwise.
final people = others
.where(
(participant) =>
participant.pubkey == null ||
participantNames?.isAgent(participant.pubkey!) != true,
)
.toList();
final displayParticipants = people.isNotEmpty ? people : others;
final labels = <String>{
for (final participant
in displayParticipants.isNotEmpty ? displayParticipants : participants)
participant.label,
}.toList();
return labels.isNotEmpty
? formatDmParticipantDisplayName(labels)
: channel.name;
}
/// Avatar initial for the DM's visible counterpart.
///
/// Mirrors [resolveDmChannelDisplayLabel]'s participant selection: the
/// first participant that is not [currentPubkey], since member order does
/// not guarantee the counterpart is listed first — otherwise the avatar
/// could identify the current user while the label beside it identifies
/// the counterpart. When every participant is the current user (a
/// self-DM), the label names the current user too, so the avatar keys off
/// that same first participant instead of its label's first character.
///
/// A resolved display name keeps its name-derived initial. A label that
/// fell back to the compact npub form of the participant's key is keyed to
/// the hex public key instead — the npub form starts with `npub1`, so every
/// unnamed participant would otherwise render `N`.
String dmAvatarInitial(Channel channel, {String? currentPubkey}) {
final normalizedCurrent = currentPubkey?.toLowerCase();
var index = 0;
while (normalizedCurrent != null &&
index < channel.participantPubkeys.length &&
channel.participantPubkeys[index].toLowerCase() == normalizedCurrent) {
index++;
}
if (index >= channel.participantPubkeys.length) {
// No participant pubkeys (labels only): fall back to the first
// participant label, like the channel label does when the non-self
// list is empty.
if (channel.participantPubkeys.isEmpty) {
final label = channel.participants.isNotEmpty
? channel.participants.first
: '';
return label.isNotEmpty ? label[0].toUpperCase() : '?';
}
// Keys exist but every participant is the current user (a self-DM):
// the label names the current user, so key the avatar to that same
// first participant with the shared provenance rule below — the hex
// key for a compact npub label, the authored name otherwise.
index = 0;
}
final pubkey = channel.participantPubkeys[index];
final label = index < channel.participants.length
? channel.participants[index]
: shortPubkey(pubkey);
if (pubkey.isNotEmpty && label == shortPubkey(pubkey)) {
return pubkey[0].toUpperCase();
}
return label.isNotEmpty ? label[0].toUpperCase() : '?';
}
/// Sorts by the visible label, resolving each label only once per invocation.
List<Channel> sortDmChannelsByDisplayLabel(
Iterable<Channel> channels, {
String? currentPubkey,
IdentityNameSources? names,
}) {
// Keep sort keys local: the next call must reflect current viewer, channel
// participants, and community profile/owner names.
final sorted = [
for (final channel in channels)
(
channel: channel,
label: resolveDmChannelDisplayLabel(
channel,
currentPubkey: currentPubkey,
names: names,
).toLowerCase(),
),
];
sorted.sort((left, right) {
final labelCompare = left.label.compareTo(right.label);
if (labelCompare != 0) return labelCompare;
return left.channel.id.compareTo(right.channel.id);
});
return [for (final entry in sorted) entry.channel];
}