mirror of
https://github.com/block/buzz.git
synced 2026-12-01 05:32:24 +00:00
<!-- Draft PR body for #7385 · fix(mobile): bind same-name mentions to exact selected identities · https://github.com/block/buzz/pull/7385 --> 🤖 ## Summary If two people in a channel share a display name, mobile couldn't tell them apart in mentions: picking the second could overwrite the first's selection, a rendered mention linked whichever same-name person matched first, and a later rename or a shorter name could re-bind the text to the wrong recipient. This PR binds every mention to the exact selected identity: - Each same-name selection keeps its own recipient instead of overwriting by name; conflicting picks get a qualified label like `Name (key…)`. - The longest matching label wins, so a shorter or interior name can never claim part of a longer one and steal its identity. - Rendering resolves recipients by the signed identity key from the event's tags — never from message text alone — so qualified labels stay correct regardless of tag order and survive later renames; an untagged ambiguous label blocks shorter mentions instead of silently re-binding. Ports the landed Desktop exact-recipient behavior (see `docs/mention-editor.md`). ### Related issue - Fixes: N/A. No mobile issue; Desktop's landed exact-recipient fixes are the reference this ports. - Independent base (`main`). #7387 (child) persists these exact selections in saved drafts. - Landing note: branches in this series overlap in the composer — when rebasing, keep exact/durable mention bindings, the explicit invite/reference-only choice, the account/visit/revision fences, and authorization before membership preparation and publication; don't resolve conflicts by taking either side wholesale. - Draft — not requesting merge yet; the security advisory run for this range timed out without results (no verdict). ### Testing - Regressions cover same-name collisions, prefix/overlap, removal, tag order, and renames. - At `acd841354a692243f1cb4c04059baad42f1d2abb`: `just mobile-check` and full `just mobile-test` pass (2,082 tests). The earlier full `just ci` receipt linked below is reused only for unchanged non-mobile code/tooling, not claimed as a rerun at this head. - Previous-head evidence: `just mobile-check`, the full mobile test suite, and full local `just ci` all pass — receipts in the [exact-head evidence comment](https://github.com/block/buzz/pull/7385#issuecomment-5574635836). - Verification is widget-test level; no native device or simulator run is claimed. To see it: mention two teammates with the same display name — both stay distinct, the second shows a qualified label, and the rendered message keeps both correct even after either renames. ### Screenshots Flutter production-widget test renders — not native-device screenshots or acceptance captures. | Scenario | Before | After | |---|---|---| | Qualified mention chip for same-name recipients, at 200% text scale (deliberate stress fixture) |  |  | <details> <summary>Capture provenance</summary> Rendered by the Flutter widget engine in a `flutter test` run (production widgets, production theme; no device or simulator). Before: this PR's declared base `3c7f288c60d67df78577b237e27c3dfc8831aaa1`. After: its head `39afd73b0adfde14164f4b10dbd089cb498312b6`. </details> Signed-off-by: Logan Johnson <loganj@squareup.com>
120 lines
3.6 KiB
Dart
120 lines
3.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../../shared/utils/string_utils.dart';
|
|
|
|
/// A mention autocomplete candidate. Mirrors the desktop's
|
|
/// `MentionCandidateForRanking` (desktop/src/features/messages/lib/mentionRanking.ts).
|
|
@immutable
|
|
class MentionCandidate {
|
|
final String pubkey;
|
|
|
|
/// Restored identity only: eligibility must come from current community state.
|
|
final bool requiresRevalidation;
|
|
final String? displayName;
|
|
final String? secondaryLabel;
|
|
final String? avatarUrl;
|
|
final bool isAgent;
|
|
final bool isMember;
|
|
final String? role;
|
|
final String? ownerPubkey;
|
|
|
|
const MentionCandidate({
|
|
required this.pubkey,
|
|
this.requiresRevalidation = false,
|
|
this.displayName,
|
|
this.secondaryLabel,
|
|
this.avatarUrl,
|
|
this.isAgent = false,
|
|
this.isMember = false,
|
|
this.role,
|
|
this.ownerPubkey,
|
|
});
|
|
|
|
String get label {
|
|
final name = displayName?.trim();
|
|
if (name != null && name.isNotEmpty) return name;
|
|
return shortPubkey(pubkey);
|
|
}
|
|
|
|
/// Avatar initial: display-name-derived, or keyed to the hex public key
|
|
/// so unnamed identities keep distinct initials (a compact npub would
|
|
/// render `N` for everyone).
|
|
String get initial {
|
|
final name = displayName?.trim();
|
|
if (name != null && name.isNotEmpty) return name[0].toUpperCase();
|
|
return pubkey.isNotEmpty ? pubkey[0].toUpperCase() : '?';
|
|
}
|
|
}
|
|
|
|
/// Group rank: channel members, then people, then other agents.
|
|
/// Mirrors desktop's `getMentionCandidateGroupRank` (personas are a
|
|
/// desktop-only concept; their slot between members and people is unused
|
|
/// here so numbering stays aligned).
|
|
int _groupRank(MentionCandidate candidate) {
|
|
if (candidate.isMember) return 0;
|
|
if (!candidate.isAgent) return 2;
|
|
return 3;
|
|
}
|
|
|
|
/// Match-quality score for one label. Lower is better; null means no match.
|
|
/// Mirrors desktop's `scoreMentionCandidateLabel`.
|
|
int? _scoreLabel(String label, String lowerQuery) {
|
|
final lower = label.toLowerCase();
|
|
if (lower == lowerQuery) return 0;
|
|
if (lower.startsWith(lowerQuery)) return 1;
|
|
|
|
final words = lower.split(RegExp(r'[\s\-_]+')).where((w) => w.isNotEmpty);
|
|
if (words.any((word) => word == lowerQuery)) return 2;
|
|
if (words.any((word) => word.startsWith(lowerQuery))) return 3;
|
|
|
|
return null;
|
|
}
|
|
|
|
/// Rank candidates for a mention query. Mirrors desktop's
|
|
/// `rankMentionCandidates`: sort by group, then match quality, then the
|
|
/// stable original order.
|
|
List<MentionCandidate> rankMentionCandidates(
|
|
List<MentionCandidate> candidates,
|
|
String query,
|
|
) {
|
|
final lowerQuery = query.toLowerCase();
|
|
|
|
final ranked = <(MentionCandidate, int, int, int)>[];
|
|
for (var order = 0; order < candidates.length; order++) {
|
|
final candidate = candidates[order];
|
|
|
|
final labelScores = [candidate.displayName, candidate.secondaryLabel].map((
|
|
value,
|
|
) {
|
|
final trimmed = value?.trim();
|
|
if (trimmed == null || trimmed.isEmpty) return null;
|
|
return _scoreLabel(trimmed, lowerQuery);
|
|
}).whereType<int>();
|
|
int? score = labelScores.isEmpty
|
|
? null
|
|
: labelScores.reduce((a, b) => a < b ? a : b);
|
|
|
|
if (score == null) {
|
|
final pubkeyLower = candidate.pubkey.toLowerCase();
|
|
if (pubkeyLower.startsWith(lowerQuery)) {
|
|
score = 4;
|
|
} else if (pubkeyLower.contains(lowerQuery)) {
|
|
score = 5;
|
|
}
|
|
}
|
|
|
|
if (score == null) continue;
|
|
ranked.add((candidate, _groupRank(candidate), score, order));
|
|
}
|
|
|
|
ranked.sort((a, b) {
|
|
final group = a.$2.compareTo(b.$2);
|
|
if (group != 0) return group;
|
|
final score = a.$3.compareTo(b.$3);
|
|
if (score != 0) return score;
|
|
return a.$4.compareTo(b.$4);
|
|
});
|
|
|
|
return [for (final item in ranked) item.$1];
|
|
}
|