Files
buzz/mobile/lib/shared/verified_profiles/verified_profile_directory.dart
Logan Johnson c39ee507b2 fix(mobile): find verified people who have no profile (#9086)
## What changes

Mobile now finds a person who has a relay-verified profile (NIP-FI, kind
30382) but never published a kind:0 profile. This happens, for example,
when someone skips profile setup. The relay's `POST /profiles/search`
already finds such people by their verified name. Each one comes back as
a kind:30382 head with no kind:0. Mobile kept only kind:0 results, so
these people did not appear.

- `RelaySessionNotifier.searchProfiles` keeps kind:30382 heads from the
endpoint.
- `VerifiedProfilesNotifier.searched` turns a search page into people:
- If the relay authority is not known yet, it first waits for NIP-11
discovery. Otherwise, a search that finished first would drop these
people for that query.
- It checks each head's signature with the directory's off-isolate proof
checker.
- Proven heads go into the verified directory, so the rows show verified
names right away.
- `searchedPeople` (in `verified_profile.dart`) builds one row per
person, in the relay's order:
  - If the person has a kind:0, the newest kind:0 is used, as before.
- If not, it uses the proven head that the authority signed, provided it
parses as an active verified profile.
- These cases add no row:
  - a head signed by another key;
  - a head that names the relay key but has a bad signature;
  - a retraction;
  - a malformed subject.
- **Behavior change for all mobile mentions:** every verified person now
matches in mention ranking by their verified full name and username, as
well as by display name, including people who have a kind:0. The relay's
search matches these same three names. Without this, a person whose
display name is their username (`glilienfeld`, full name "Gabi
Lilienfeld") came back from the relay for "gabi", and then the local
filter dropped them.
- Callers:
  - Mention search, which also serves Add members and New DM.
  - The user rows in global search, through `directoryUsersFromSearch`.

The desktop app has the same fix for Manage members:
https://github.com/block/buzz-app/pull/2554

## Screenshots

These come from the real Add members sheet on an Android emulator. Only
the relay session, verified-profile discovery and the member actions are
faked. The names are made up. Jordan Park has a kind:0. jrivera has only
a verified profile, with the full name "Jordan Rivera". The search is
"jordan". The row shows the verified display name, which here is the
same as the username.

| Search finds jrivera | After tapping + on jrivera |
| --- | --- |
| <img width="300" alt="jrivera is found next to Jordan Park"
src="https://github.com/user-attachments/assets/d93b77b1-1bc7-4d09-afc1-f359616a192a"
/> | <img width="300" alt="jrivera is selected to add"
src="https://github.com/user-attachments/assets/ce6c7a71-b098-434e-8b0e-ed0de8d1a5a2"
/> |

## Tests

- `add_members_sheet_names_test.dart`: a journey in which Gabi has
`display_name` = `glilienfeld` and `full_name` = "Gabi Lilienfeld", and
NIP-11 discovery is slow.
  - While discovery is pending, typing "gabi" shows nothing yet.
  - When discovery finishes, Gabi appears with no further keystroke.
- A head from another key does not appear, and neither does a head that
names the relay key but has a bad signature.
  - **Add member** adds Gabi's key.
- I removed each fix in turn: the full-name alias, the signature proof,
and the wait for discovery. The journey fails each time, for the
expected reason.
- `verified_profile_test.dart`: a table of `searchedPeople` cases.
  - A kind:0 wins over a head, and the newest kind:0 counts.
- These find nobody: another signer, a retraction, an uppercase subject,
a non-profile kind, and any head when the authority is unknown.
- `relay_session_test.dart` and `channel_management_provider_test.dart`:
rows for the kept heads and the directory row.
- `flutter analyze` is clean, and the full `flutter test` passes on
6efef79cc: 4,462 tests.

**Note:** mobile on `main` does not compile at e558f15fa.
`account_identity_store.dart` calls the removed `_keys`. That fix is
https://github.com/block/buzz/pull/9085. The local runs above had that
one-line fix applied but not committed. Until it lands, mobile CI on
this PR will fail.

## Not covered

- **Search during a reconnect:** discovery starts only when the relay
session is connected. If an HTTP search succeeds while the session is
reconnecting, that search leaves these people out.
- **Discovery failure:** if NIP-11 discovery fails, the search also
leaves these people out.
- In both cases the mention cache keeps that partial page for the query,
and the next search tries discovery again. People who have a kind:0 show
as before. Showing the left-out people when the authority arrives later
would need the cached results to be rebuilt; that is a possible
follow-up.

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

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 18:53:40 -05:00

575 lines
19 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../relay/relay.dart';
import 'verified_profile.dart';
import 'verified_profile_discovery_fetch.dart';
import 'verified_profile_proof.dart';
const _wantedLimit = 1024;
/// Records kept after their subject stops being tracked. A roster larger than
/// [_wantedLimit] keeps evicting and re-tracking the same people; their last
/// known names stay on screen, unchecked, instead of blinking to fallbacks.
const _cachedHeadLimit = 4 * _wantedLimit;
const _eoseTimeout = Duration(seconds: 10);
const _discoveryRefresh = Duration(minutes: 5);
/// A relay returns one current projection per subject; a few duplicates are
/// tolerated, a flood before EOSE fails the batch instead of buffering it.
const _maxEarlyPerSubject = 4;
class _Batch {
_Batch(this.subjects);
final List<String> subjects;
final List<NostrEvent> early = [];
/// Live events awaiting proof, newest per subject, so a fast stream cannot
/// queue unbounded work.
final Map<String, NostrEvent> pending = {};
Future<void> tail = Future.value();
/// The relay retried this subscription. Its replay resumes from the last
/// event and skips events already delivered, so it is not a snapshot: the
/// next sync replaces the batch with a fresh subscription.
bool stale = false;
/// Bumped by each retry, so a job that awaited proof across one doesn't
/// promote records the retry left unchecked.
int retries = 0;
/// Subjects that were checked when the retry began, checked again at the
/// replay's EOSE.
final Set<String> resume = {};
void Function()? unsubscribe;
/// Retires this batch so the next sync can reopen it.
void Function() fail = () {};
Timer? timeout;
bool active = true;
bool ready = false;
int candidates = 0;
void stop() {
active = false;
timeout?.cancel();
unsubscribe?.call();
unsubscribe = null;
}
}
/// Verified profiles for the community's currently relevant subjects, keyed by
/// lowercase pubkey. Empty unless a successful discovery enables the feature.
///
/// Records survive disconnects as `checked: false` (cached) and are only
/// reconciled by a completed subscription on the current connection.
class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
late final Map<String, int> _wanted = newInterestMap();
final Map<String, NostrEvent> _heads = {};
final Set<String> _checked = {};
final Map<String, _Batch> _batches = {};
VerifiedProfileAuthority? _authority;
bool _enabled = false;
bool _discoveryFresh = false;
bool _connected = false;
bool _discoveryInFlight = false;
Completer<void>? _discoveryDone;
bool _scheduled = false;
int _generation = 0;
int _interest = 0;
(String, String?)? _scope;
Timer? _refreshTimer;
/// The interest map, insertion-ordered by interest. Tests override this to
/// count how many entries eviction visits.
@visibleForTesting
Map<String, int> newInterestMap() => {};
@override
Map<String, VerifiedProfile> build() {
final scope = ref.watch(
relayConfigProvider.select((config) => (config.baseUrl, config.nsec)),
);
final status = ref.watch(
relaySessionProvider.select((session) => session.status),
);
if (_scope != null && _scope != scope) _resetCommunity();
_scope = scope;
final generation = ++_generation;
_connected = status == SessionStatus.connected;
_stopBatches();
_discoveryFresh = false;
_discoveryInFlight = false;
ref.onDispose(() {
_generation++;
_stopBatches();
_refreshTimer?.cancel();
_refreshTimer = null;
});
if (_connected && _wanted.isNotEmpty) {
Future.microtask(() => _onConnected(generation));
}
return _snapshot();
}
void _resetCommunity() {
_wanted.clear();
_heads.clear();
_authority = null;
_enabled = false;
}
void _stopBatches() {
for (final batch in _batches.values) {
batch.stop();
}
_batches.clear();
_checked.clear();
}
/// The people a profile search found ([searchedPeople]). Waits for
/// discovery when the relay's authority is not known yet, and keeps only
/// heads with a valid signature. A kept head also names its subject here,
/// so the search's rows show their verified names at once.
Future<List<SearchedPerson>> searched(List<NostrEvent> events) {
final heads = [
for (final event in events)
if (event.kind == verifiedProfileKind) event,
];
// Without heads there is nothing to wait for or prove: answer at once.
if (heads.isEmpty) return SynchronousFuture(searchedPeople(events, null));
return _searchedWithHeads(events, heads);
}
Future<List<SearchedPerson>> _searchedWithHeads(
List<NostrEvent> events,
List<NostrEvent> heads,
) async {
final generation = _generation;
if (_authority == null && !_discoveryFresh) {
unawaited(_refreshDiscovery(generation));
await _discoveryDone?.future;
}
final authority = _enabled ? _authority : null;
if (authority == null) return searchedPeople(events, null);
final proven = Set<NostrEvent>.identity()..addAll(await _prove(heads));
if (generation == _generation && authority == _authority && ref.mounted) {
track([
for (final head in proven) ?verifiedProfileSubject(head, authority),
]);
_accept(proven.toList(), authority);
_publish();
}
return searchedPeople([
for (final event in events)
if (event.kind != verifiedProfileKind || proven.contains(event)) event,
], authority);
}
/// Marks [pubkeys] relevant. Repeated calls coalesce into one scheduling pass.
void track(Iterable<String> pubkeys) {
final valid = RegExp(r'^[0-9a-f]{64}$');
var added = false;
for (final raw in pubkeys) {
final key = raw.trim().toLowerCase();
if (!valid.hasMatch(key)) continue;
added |= !_wanted.containsKey(key);
_wanted[key] ??= ++_interest;
}
// A key's interest is set once, when it is inserted, so the map's
// insertion order is interest order and the oldest keys come first. Take
// them in one pass: a scan per eviction made one 4,000-member roster take
// ~300 ms on device, and even `keys.first` per eviction is quadratic,
// because each new iterator skips the slots earlier removals left behind.
final overflow = _wanted.length - _wantedLimit;
if (overflow > 0) {
for (final oldest in _wanted.keys.take(overflow).toList()) {
_wanted.remove(oldest);
_checked.remove(oldest);
}
_trimCachedHeads();
added = true;
}
if (!added) return;
if (_authority == null && !_discoveryInFlight && _connected) {
unawaited(_refreshDiscovery(_generation));
} else {
_schedule(_generation);
}
}
/// Drops the oldest untracked records past [_cachedHeadLimit], in one pass.
void _trimCachedHeads() {
var excess = _heads.length - _cachedHeadLimit;
if (excess <= 0) return;
final drop = <String>[];
for (final key in _heads.keys) {
if (_wanted.containsKey(key)) continue;
drop.add(key);
if (--excess == 0) break;
}
drop.forEach(_heads.remove);
}
Future<void> _onConnected(int generation) async {
if (generation != _generation) return;
await _refreshDiscovery(generation);
}
void _armRefresh(int generation) {
_refreshTimer?.cancel();
_refreshTimer = null;
if (generation != _generation || !_connected || _wanted.isEmpty) return;
_refreshTimer = Timer(_discoveryRefresh, () {
unawaited(_refreshDiscovery(generation));
});
}
Future<void> _refreshDiscovery(int generation) async {
if (_discoveryInFlight || generation != _generation || !_connected) return;
_discoveryInFlight = true;
final done = _discoveryDone = Completer<void>();
try {
final result = await ref.read(verifiedProfileDiscoveryFetchProvider)();
if (generation != _generation) return;
_discoveryFresh = true;
switch (result) {
case VerifiedProfilesDisabled():
// Hide marks but keep the historical records.
_enabled = false;
_stopBatches();
case VerifiedProfilesEnabled(:final authority):
if (_authority != null && _authority!.pubkey != authority.pubkey) {
_stopBatches();
_heads.clear();
}
_authority = authority;
_enabled = true;
_schedule(generation);
}
} catch (error) {
// Failed or malformed discovery is not a disable: keep the last state,
// but it is no longer fresh.
if (generation == _generation) _discoveryFresh = false;
debugPrint('[VerifiedProfiles] discovery failed: $error');
} finally {
if (identical(_discoveryDone, done)) _discoveryDone = null;
done.complete();
if (generation == _generation) {
_discoveryInFlight = false;
_armRefresh(generation);
_publish();
}
}
}
void _schedule(int generation) {
if (_scheduled) return;
_scheduled = true;
scheduleMicrotask(() {
_scheduled = false;
if (generation == _generation) _syncBatches(generation);
});
}
void _syncBatches(int generation) {
final authority = _authority;
if (!_connected || !_enabled || authority == null) {
_publish();
return;
}
final subjects = _wanted.keys.toList();
final next = <String, List<String>>{};
for (var i = 0; i < subjects.length; i += verifiedProfileBatchSize) {
final end = i + verifiedProfileBatchSize;
final chunk = subjects.sublist(
i,
end < subjects.length ? end : subjects.length,
);
next[chunk.join(':')] = chunk;
}
for (final id in _batches.keys.toList()) {
// A retried batch is replaced so a fresh snapshot can prove absence.
if (next.containsKey(id) && !_batches[id]!.stale) continue;
final batch = _batches.remove(id)!..stop();
_checked.removeAll(batch.subjects);
}
for (final MapEntry(key: id, value: chunk) in next.entries) {
if (!_batches.containsKey(id)) {
unawaited(_open(id, chunk, authority, generation));
}
}
if (_refreshTimer == null) _armRefresh(generation);
_publish();
}
Future<void> _open(
String id,
List<String> chunk,
VerifiedProfileAuthority authority,
int generation,
) async {
final batch = _Batch(chunk);
_batches[id] = batch;
void fail() {
if (!batch.active) return;
batch.stop();
if (_batches[id] == batch) _batches.remove(id);
_checked.removeAll(chunk);
_publish();
// No hot retry: the next discovery refresh, reconnect or tracked
// subject reopens it.
}
batch.fail = fail;
batch.timeout = Timer(_eoseTimeout, fail);
try {
final unsubscribe = await ref
.read(relaySessionProvider.notifier)
.subscribeWithStatus(
NostrFilter(
kinds: const [verifiedProfileKind],
authors: [authority.pubkey],
tags: {'#d': chunk},
limit: chunk.length,
),
(event) => _onEvent(batch, event, authority, generation),
onStatusChanged: (status) => switch (status) {
RelaySubscriptionStatus.ready => _onEose(
batch,
authority,
generation,
),
RelaySubscriptionStatus.retrying => _onRetry(batch),
},
onClosed: (_) => fail(),
);
if (!batch.active || generation != _generation) {
unsubscribe();
} else {
batch.unsubscribe = unsubscribe;
}
} catch (error) {
debugPrint('[VerifiedProfiles] subscribe failed: $error');
fail();
}
}
void _onEvent(
_Batch batch,
NostrEvent event,
VerifiedProfileAuthority authority,
int generation,
) {
if (!batch.active || generation != _generation) return;
final subject = verifiedProfileSubject(event, authority);
if (subject == null || !batch.subjects.contains(subject)) return;
if (!batch.ready) {
if (batch.early.length >= batch.subjects.length * _maxEarlyPerSubject) {
debugPrint('[VerifiedProfiles] too many events before EOSE');
batch.fail();
return;
}
batch.candidates++;
batch.early.add(event);
return;
}
final queued = batch.pending.isNotEmpty;
if (supersedes(event, batch.pending[subject])) {
batch.pending[subject] = event;
}
if (queued) return;
_enqueue(batch, generation, () async {
final events = batch.pending.values.toList();
batch.pending.clear();
final proven = await _prove(events);
if (_live(batch, generation)) {
_accept(proven, authority);
_publish();
}
});
}
/// The relay closed and is retrying the subscription. Its records are cached
/// until the replay's EOSE; replayed events, including retractions, apply as
/// live updates. Absence is only proven by the fresh subscription the next
/// sync opens, so a retrying relay is never resubscribed hot.
void _onRetry(_Batch batch) {
if (!batch.active) return;
batch.stale = true;
batch.retries++;
if (!batch.ready) return;
batch.resume.addAll(batch.subjects.where(_checked.contains));
_checked.removeAll(batch.subjects);
_publish();
}
void _onEose(
_Batch batch,
VerifiedProfileAuthority authority,
int generation,
) {
if (!batch.active || generation != _generation) return;
if (batch.ready) {
// The end of a retry's replay: queued live updates apply first.
if (batch.resume.isEmpty) return;
final resume = Set.of(batch.resume);
batch.resume.clear();
final retries = batch.retries;
_enqueue(batch, generation, () async {
if (batch.retries != retries) return;
_checked.addAll(resume);
_publish();
});
return;
}
batch.ready = true;
batch.timeout?.cancel();
batch.timeout = null;
final early = List.of(batch.early);
batch.early.clear();
final candidates = batch.candidates;
final retries = batch.retries;
_enqueue(batch, generation, () async {
final proven = await _prove(early);
if (!_live(batch, generation)) return;
_accept(proven, authority);
// A retry while proof ran left these subjects for the replay or the
// replacement subscription to reconcile.
if (batch.retries != retries) {
_publish();
return;
}
final seen = {
for (final event in proven) verifiedProfileSubject(event, authority)!,
};
// A full response that omits subjects may be truncated: only a short
// response proves absence. A retried response resumed after the events
// already delivered, so it never does.
final complete =
retries == 0 &&
(candidates < batch.subjects.length ||
seen.length == batch.subjects.length);
for (final subject in batch.subjects) {
if (!complete && !seen.contains(subject)) continue;
_checked.add(subject);
if (!seen.contains(subject)) _heads.remove(subject);
}
_publish();
});
}
bool _live(_Batch batch, int generation) =>
batch.active && generation == _generation && ref.mounted;
void _enqueue(_Batch batch, int generation, Future<void> Function() job) {
batch.tail = batch.tail.then((_) async {
if (!_live(batch, generation)) return;
try {
await job();
} catch (error) {
// Retire the batch so a later sync reconciles it again.
debugPrint('[VerifiedProfiles] batch job failed: $error');
batch.fail();
}
});
}
Future<List<NostrEvent>> _prove(List<NostrEvent> events) async {
if (events.isEmpty) return const [];
final results = await ref.read(verifiedProfileProofCheckerProvider)(events);
return [
for (var i = 0; i < events.length; i++)
if (results[i]) events[i],
];
}
void _accept(List<NostrEvent> events, VerifiedProfileAuthority authority) {
for (final event in events) {
final subject = verifiedProfileSubject(event, authority);
if (subject == null || !_wanted.containsKey(subject)) continue;
if (supersedes(event, _heads[subject])) _heads[subject] = event;
}
}
/// Validates one head. Tests override this to count parses.
@visibleForTesting
VerifiedProfile? parseHead(
NostrEvent event,
VerifiedProfileAuthority authority, {
required bool checked,
}) => parseVerifiedProfile(event, authority, checked: checked);
/// The last parse of each subject's head. Every publish rebuilds the whole
/// map, so without it each batch re-validated every head already known.
Map<String, _ParsedHead> _parsed = const {};
Map<String, VerifiedProfile> _snapshot() {
final authority = _authority;
if (!_enabled || authority == null) {
_parsed = const {};
return const {};
}
final parsed = <String, _ParsedHead>{};
final profiles = <String, VerifiedProfile>{};
for (final MapEntry(:key, :value) in _heads.entries) {
final checked = _discoveryFresh && _checked.contains(key);
final previous = _parsed[key];
final head =
previous != null &&
identical(previous.event, value) &&
previous.authority == authority
? previous
: _ParsedHead(
value,
authority,
parseHead(value, authority, checked: checked),
);
parsed[key] = head;
if (head.withChecked(checked) case final profile?) {
profiles[key] = profile;
}
}
_parsed = parsed;
return profiles;
}
void _publish() {
if (!ref.mounted) return;
final next = _snapshot();
if (!mapEquals(state, next)) state = next;
}
}
/// A head's parse, kept while the head and authority stand. Only `checked`
/// varies between publishes, so it is applied without validating again.
class _ParsedHead {
_ParsedHead(this.event, this.authority, this._profile);
final NostrEvent event;
final VerifiedProfileAuthority authority;
VerifiedProfile? _profile;
VerifiedProfile? withChecked(bool checked) {
final profile = _profile;
if (profile == null || profile.checked == checked) return profile;
return _profile = VerifiedProfile(
displayName: profile.displayName,
username: profile.username,
fullName: profile.fullName,
issuerLabel: profile.issuerLabel,
verifiedAt: profile.verifiedAt,
checked: checked,
multipleIssuers: profile.multipleIssuers,
);
}
}
final verifiedProfilesProvider =
NotifierProvider<VerifiedProfilesNotifier, Map<String, VerifiedProfile>>(
VerifiedProfilesNotifier.new,
);