mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
Follow-up to [#9699 mention typing](https://github.com/block/buzz/pull/9699), fixing two identity problems found in its review. **Identity-link pills name the key they open.** An edit writes each mention as an identity link, `[@Name](nostr:npub1…)`, and every message from any client draws such a link as a pill. Mobile showed the label written in the link, so `[@Wes](nostr:<mallory>)` drew an ordinary `@Wes` pill that opened Mallory's profile, and two namesakes in an edited message both read `Scout`. The pill now shows the name its key resolves to in the channel (with the namesake qualifier and agent icon), as buzz-app's `ProfileMention` already does. The written label shows only when the key has no name on this device (no profile, directory or roster name), never a compact npub in its place. This holds whether or not the link's key is also tagged. A pill requests its key's profile when uncached, so a written label gives way to the key's name once it loads. Edits carry no `p` tag, so an identity-link pill resolves its own key. In channel, thread and forum rows the pill watches that one key's channel label (with the namesake qualifier and agent icon) and requests the key's profile when it builds, so only links that actually draw a pill cost a lookup, whatever Markdown surrounds them. Inbox and search previews draw identity links the same way, taking keys from the preview pieces they render. These keys never feed the text bindings, so plain `@Name` text still binds only through signed tags. **Typed names group the way the text scan reads them.** The composer grouped the roster by `toLowerCase` while the scan folds names with `foldMentionLabel`. With members `Sam` and `ſam`, typing `@ſam` sent Sam's `p` tag. Grouping, lookup and the send-time key qualifier now all fold, so namesakes under folding fail as ambiguous, as other namesakes already do. **Linked lookups stay current and bounded.** Pills and preview rows request their keys through the profile cache's revalidation path. A name restored from the last launch is revalidated, so a renamed key's link shows its current name. A lookup that fails while offline is retried once the relay reconnects, including when the reconnect comes while that lookup is still on the wire. A key the relay says has no profile is not asked for again for five minutes, and the cache remembers at most 1,000 such keys per community. Previews keep tilde and quoted code fences literal and read blockquotes as the full message does, and load only the links on lines they draw. Editing a message folds historical names the way the scan does, so a name two tagged people share under folding seeds no chip. Rendering binds signed names by the same fold, so with signed `Sam` and `ſam` a plain `@Sam` stays text instead of drawing Sam's pill. Desktop (buzz-app) already names identity-link chips from the key and needs no change. ### Validation Rows add no per-message work: message bodies are no longer scanned for links. Each drawn identity-link pill adds one label watch for its own key. Added tests: - [Channel page journey step](mobile/test/features/channels/channel_detail_page_test.dart): an edited message linking `[@Wes]` to Mallory's key draws `Mallory`, and two edited `Scout` namesakes draw distinct qualified labels, through the real channel row. - [Channel page journey step](mobile/test/features/channels/channel_detail_page_test.dart): a link to a key with no name here keeps its written label. - [Preview identity link](mobile/test/features/channels/message_content/preview_text_test.dart): an inbox/search preview shows the key's resolved name, or the written label when unknown. - [Preview keys](mobile/test/features/channels/message_content/preview_text_test.dart): a preview loads only links it draws (not ones in code or past its line limit), including labels with brackets. Full-app run (iOS simulator, debug build, signed in to a test community): posted a message with a spoofed identity link, a link to a key with no profile here, and a link inside inline code. The spoofed link drew the key's real name as a pill and opened that person's profile; the unknown key kept its written label; the code span stayed raw. The same message drew matching pills in search results and in the thread view. - [Channel row loads](mobile/test/features/channels/channel_detail_page_test.dart): a row requests profiles only for links it draws as pills, not ones in a quoted code fence or an HTML comment. - [Folded chip qualifiers](mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart): picked `@Sam` and `@ſam` chips both carry their keys on send. - [Ambiguous typed names table](mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart): `Sam` and `ſam` in either roster order now refuse `@ſam` as ambiguous. - [Restart and reconnect journey](mobile/test/features/channels/channel_detail_page_test.dart): a restored old name and an offline first lookup both resolve after the relay reconnects, on the same mounted pills, and a key with no profile is not requested again. - [Preview fences](mobile/test/features/channels/message_content/preview_text_test.dart): tilde and quoted fences keep links and Markdown literal and load no keys. - [Overtaken lookup](mobile/test/shared/profile/user_cache_provider_test.dart): a reconnect during a link's failing lookup asks once more after it settles. - [Folded signed names](mobile/test/features/channels/message_content_exact_mentions_test.dart): signed `Sam` and `ſam` in either tag order leave `@Sam` as text in the message and its preview. - [Folded edit chips](mobile/test/features/channels/message_edit_sheet_test.dart): `Sam` and `ſam` tagged in either order open as text, with no chip. - [Non-member labels](mobile/test/shared/identity_names/identity_names_test.dart): a non-member labels the same with or without the roster shortcut, including names shaped like qualifiers. ### Regressions - **Performance:** rows do no new scanning; each drawn identity-link pill adds one label watch. A non-member whose name can't collide no longer re-resolves the whole roster. - **Mention semantics:** unchanged for tagged mentions and plain text. Only identity-link pills change, from the written label to the key's resolved name. - **Composer:** names that differ only under Unicode folding (`ſ`/`s`, `µ`/`μ`, final `ς`/`σ`) are now one name when matching typed text, so a typed one between two such members asks for a pick instead of silently choosing one. Picked chips are unaffected. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz>
405 lines
13 KiB
Dart
405 lines
13 KiB
Dart
import 'package:buzz/features/channels/message_content/preview_text.dart';
|
|
import 'package:buzz/features/channels/message_media.dart';
|
|
import 'package:buzz/shared/utils/string_utils.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
const _alice =
|
|
'a11ce00000000000000000000000000000000000000000000000000000000000';
|
|
|
|
const _channelId = '0b5e2c3a-1d4f-4a6b-9c8d-7e6f5a4b3c2d';
|
|
const _channelLink = 'buzz://channel/$_channelId';
|
|
|
|
List<PreviewPiece> _pieces(
|
|
String content, {
|
|
int maxLines = 2,
|
|
Map<String, Set<String>> mentionBindings = const {},
|
|
Map<String, String> mentionNames = const {},
|
|
Map<String, String> mentionLabels = const {},
|
|
Map<String, String> customEmoji = const {},
|
|
Map<String, String> channelNames = const {},
|
|
}) => messagePreviewPieces(
|
|
content,
|
|
maxLines: maxLines,
|
|
mentionBindings: mentionBindings,
|
|
mentionNames: mentionNames,
|
|
mentionLabels: mentionLabels,
|
|
customEmoji: customEmoji,
|
|
channelNames: channelNames,
|
|
);
|
|
|
|
String _text(String content, {int maxLines = 2}) => [
|
|
for (final piece in _pieces(content, maxLines: maxLines))
|
|
if (piece is PreviewText) piece.text else '<$piece>',
|
|
].join();
|
|
|
|
void main() {
|
|
group('markup', () {
|
|
for (final (name, source, expected) in [
|
|
('plain text', 'hello world', 'hello world'),
|
|
('heading', '## Release notes', 'Release notes'),
|
|
('block quote', '> > quoted', 'quoted'),
|
|
('bullet', '- item', 'item'),
|
|
('task', '- [x] done', 'done'),
|
|
('ordered item', '12. twelfth', 'twelfth'),
|
|
('bold', 'a **bold** word', 'a bold word'),
|
|
('italic', 'an *italic* and _under_ word', 'an italic and under word'),
|
|
('strike', '~~gone~~ now', 'gone now'),
|
|
('snake_case', 'call my_func_name now', 'call my_func_name now'),
|
|
('lone asterisk', '2 * 3 = 6', '2 * 3 = 6'),
|
|
('inline code', 'run `**not bold**` here', 'run **not bold** here'),
|
|
('unpaired backtick', 'a ` b **c**', 'a ` b c'),
|
|
('double-backtick code', 'x ``**a** `b` @c`` y', 'x **a** `b` @c y'),
|
|
('unmatched backtick run', 'a `` b `c` **d**', 'a `` b c d'),
|
|
('padded code span', '`` `x` ``', '`x`'),
|
|
(
|
|
'encoded label brackets',
|
|
'[report [Q4]](https://x.y)',
|
|
'report [Q4]',
|
|
),
|
|
('link', 'see [the docs](https://example.com)', 'see the docs'),
|
|
('bare link label', '[https://x.y](https://x.y)', 'https://x.y'),
|
|
('rule', 'above\n---\nbelow', 'above\nbelow'),
|
|
('table', '| a | b |\n|---|:-:|\n| 1 | 2 |', 'a b\n1 2'),
|
|
('blank lines', 'one\n\n\ntwo', 'one\ntwo'),
|
|
('underline', 'an <u>important</u> note', 'an important note'),
|
|
('escaped emphasis', r'\*literal\* and \_x\_', '*literal* and _x_'),
|
|
('escaped backslash', r'a \\ b', r'a \ b'),
|
|
('escaped link opener', r'\[docs](https://x)', '[docs](https://x)'),
|
|
('escaped backticks', r'\`literal\` and `code`', '`literal` and code'),
|
|
]) {
|
|
test(name, () => expect(_text(source), expected));
|
|
}
|
|
|
|
test('keeps fenced code literally without its fences', () {
|
|
expect(_text('```dart\n**x** @y\n```\nafter'), '**x** @y\nafter');
|
|
});
|
|
|
|
test('a fence closes only at a bare fence line', () {
|
|
expect(_text('```\n```not a close\n```\nafter'), '```not a close\nafter');
|
|
});
|
|
|
|
test('tilde and quoted fences keep their code literal too', () {
|
|
expect(_text('~~~\n**x**\n~~~\nafter'), '**x**\nafter');
|
|
expect(_text('> ```\n> **x**\n> ```\nafter'), '**x**\nafter');
|
|
expect(_text('```\n> **x**\n```'), '> **x**');
|
|
// A quoted closer doesn't end an unquoted fence.
|
|
expect(_text('```\n> ```\n**x**\n```'), '> ```\n**x**');
|
|
// A quoted fence ends with its blockquote, at a blank line.
|
|
expect(_text('> ```\n> code\n**after**'), 'code\n**after**');
|
|
expect(_text('> ```\n> code\n\n**after**'), 'code\nafter');
|
|
// A quoted fence closes at a closer with or without the marker.
|
|
expect(_text('> ```\ncode\n```\n**after**'), 'code\nafter');
|
|
expect(_text('> ```\nx\n> ```\n> **y**'), 'x\ny');
|
|
// A fence opened inside a blockquote is quoted without its own marker.
|
|
expect(_text('> x\n~~~\n\n**after**'), 'x\nafter');
|
|
});
|
|
});
|
|
|
|
group('line limit', () {
|
|
test('the source prefix ends with the last line the preview reads', () {
|
|
const content = '# a\n\n```\ncode\n```\nb\nc\nd\ne';
|
|
expect(
|
|
messagePreviewSource(content, maxLines: 2),
|
|
'# a\n\n```\ncode\n```\nb',
|
|
);
|
|
expect(_text(messagePreviewSource(content, maxLines: 2)), 'a\ncode\nb');
|
|
});
|
|
|
|
test('the source prefix of a short message is the message', () {
|
|
expect(messagePreviewSource('one\ntwo', maxLines: 2), 'one\ntwo');
|
|
expect(messagePreviewSource('', maxLines: 2), '');
|
|
});
|
|
|
|
test('reads only maxLines + 1 kept lines', () {
|
|
final content = List.generate(1000, (i) => 'line $i').join('\n');
|
|
expect(_text(content), 'line 0\nline 1\nline 2');
|
|
});
|
|
|
|
test('dropped lines do not count', () {
|
|
expect(_text('# a\n\n---\n- b\nc\nd'), 'a\nb\nc');
|
|
});
|
|
|
|
test('stops reading after a long run of lines that do not render', () {
|
|
final content = '\n' * 1000000 + 'late';
|
|
expect(_text(content), '');
|
|
expect(
|
|
messagePreviewSource(content, maxLines: 2).length,
|
|
lessThan(20000),
|
|
);
|
|
});
|
|
|
|
test('caps a very long line, and reads only its start', () {
|
|
expect(_text('x' * 5000).length, 2000);
|
|
expect(
|
|
messagePreviewSource('x' * 128000, maxLines: 2).length,
|
|
lessThanOrEqualTo(4000),
|
|
);
|
|
});
|
|
});
|
|
|
|
group('tokens', () {
|
|
test('a bound mention becomes a chip with its display name', () {
|
|
expect(
|
|
_pieces(
|
|
'hi @alice!',
|
|
mentionBindings: {
|
|
'alice': {_alice},
|
|
},
|
|
mentionNames: {_alice: 'Alice Smith'},
|
|
),
|
|
[
|
|
const PreviewText('hi '),
|
|
const PreviewMention(
|
|
pubkey: _alice,
|
|
label: 'Alice Smith',
|
|
semanticsLabel: 'Alice Smith',
|
|
),
|
|
const PreviewText('!'),
|
|
],
|
|
);
|
|
});
|
|
|
|
test('a bound mention keeps its chip before a suffix', () {
|
|
expect(
|
|
_pieces(
|
|
"@alice's idea",
|
|
mentionBindings: {
|
|
'alice': {_alice},
|
|
},
|
|
mentionNames: {_alice: 'Alice'},
|
|
),
|
|
[
|
|
const PreviewMention(
|
|
pubkey: _alice,
|
|
label: 'Alice',
|
|
semanticsLabel: 'Alice',
|
|
),
|
|
const PreviewText("'s idea"),
|
|
],
|
|
);
|
|
});
|
|
|
|
test('an ambiguous mention stays text', () {
|
|
expect(
|
|
_pieces(
|
|
'hi @sam',
|
|
mentionBindings: {
|
|
'sam': {'a', 'b'},
|
|
},
|
|
),
|
|
[const PreviewText('hi @sam')],
|
|
);
|
|
});
|
|
|
|
test('a mention inside emphasis or a link label is still a chip', () {
|
|
final pieces = _pieces(
|
|
'**@alice** [@alice](https://x.y)',
|
|
mentionBindings: {
|
|
'alice': {_alice},
|
|
},
|
|
);
|
|
expect(pieces.whereType<PreviewMention>(), hasLength(2));
|
|
});
|
|
|
|
test('an escaped mention stays text', () {
|
|
expect(
|
|
_pieces(
|
|
r'hi \@alice',
|
|
mentionBindings: {
|
|
'alice': {_alice},
|
|
},
|
|
),
|
|
[const PreviewText('hi @alice')],
|
|
);
|
|
});
|
|
|
|
test('a mention in inline code stays text', () {
|
|
expect(
|
|
_pieces(
|
|
'`@alice`',
|
|
mentionBindings: {
|
|
'alice': {_alice},
|
|
},
|
|
),
|
|
[const PreviewText('@alice')],
|
|
);
|
|
});
|
|
|
|
test('known custom emoji become images, unknown stay text', () {
|
|
expect(
|
|
_pieces(
|
|
':nope:wave: :Wave:',
|
|
customEmoji: {'wave': 'https://e/wave.png'},
|
|
),
|
|
[
|
|
const PreviewText(':nope'),
|
|
const PreviewEmoji(shortcode: 'wave', url: 'https://e/wave.png'),
|
|
const PreviewText(' '),
|
|
const PreviewEmoji(shortcode: 'wave', url: 'https://e/wave.png'),
|
|
],
|
|
);
|
|
});
|
|
|
|
test('channel tokens become chips, but not inside words or URLs', () {
|
|
expect(
|
|
_pieces(
|
|
'see #general, a#b and x.y/#z',
|
|
channelNames: {'general': 'c1'},
|
|
),
|
|
[
|
|
const PreviewText('see '),
|
|
const PreviewChannel('general'),
|
|
const PreviewText(', a#b and x.y/#z'),
|
|
],
|
|
);
|
|
});
|
|
|
|
test('only known channels become chips', () {
|
|
expect(
|
|
_pieces(
|
|
'took over #8700, see #general and #8774\'s #gen',
|
|
channelNames: {'general': 'c1'},
|
|
),
|
|
[
|
|
const PreviewText('took over #8700, see '),
|
|
const PreviewChannel('general'),
|
|
const PreviewText(" and #8774's #gen"),
|
|
],
|
|
);
|
|
// A known prefix doesn't claim a longer unknown name; the longest known
|
|
// name wins when both are known.
|
|
expect(_pieces('#eng-ios', channelNames: {'eng': 'c1'}), [
|
|
const PreviewText('#eng-ios'),
|
|
]);
|
|
expect(
|
|
_pieces('#eng-ios', channelNames: {'eng': 'c1', 'eng-ios': 'c2'}),
|
|
[const PreviewChannel('eng-ios')],
|
|
);
|
|
expect(_pieces('see #8700 in #general'), [
|
|
const PreviewText('see #8700 in #general'),
|
|
]);
|
|
});
|
|
|
|
test('channel tokens end at punctuation that follows a word', () {
|
|
for (final (before, after) in [
|
|
('', "'s plan"),
|
|
('', '\u2019s plan'),
|
|
('"', '"'),
|
|
('\u201C', '\u201D'),
|
|
('\u2018', '\u2019'),
|
|
('\u00AB', '\u00BB'),
|
|
('', '\u2026'),
|
|
('', '\u2014next'),
|
|
('', '\u2013next'),
|
|
]) {
|
|
expect(
|
|
_pieces('see $before#general$after', channelNames: {'general': 'c1'}),
|
|
[
|
|
PreviewText('see $before'),
|
|
const PreviewChannel('general'),
|
|
PreviewText(after),
|
|
],
|
|
reason: '$before#general$after',
|
|
);
|
|
}
|
|
});
|
|
|
|
test('a buzz channel link becomes a chip with the channel name', () {
|
|
expect(
|
|
_pieces(
|
|
'[$_channelLink]($_channelLink)',
|
|
channelNames: {'general': _channelId},
|
|
),
|
|
[const PreviewChannel('general')],
|
|
);
|
|
});
|
|
|
|
test('message and repo links that show their URL get a short label', () {
|
|
final message = 'buzz://message?channel=$_channelId&id=${'ab' * 32}';
|
|
const repo = 'buzz://repo?owner=$_alice&d=buzz';
|
|
expect(
|
|
_pieces(
|
|
'[$message]($message) [$repo]($repo) [named]($message)',
|
|
channelNames: {'general': _channelId},
|
|
),
|
|
[const PreviewText('general · abababab buzz named')],
|
|
);
|
|
});
|
|
|
|
test('media links become their kind', () {
|
|
expect(_pieces('look  [clip](https://x.y/b.mp4)'), [
|
|
const PreviewText('look '),
|
|
const PreviewMedia(MessageMediaKind.image),
|
|
const PreviewText(' '),
|
|
const PreviewMedia(MessageMediaKind.video),
|
|
]);
|
|
});
|
|
});
|
|
|
|
test('an identity link previews the name its key resolves to', () {
|
|
final link = '[@Prev\\.ious](nostr:${fullNpub(_alice)})';
|
|
expect(_pieces('hi $link', mentionLabels: const {_alice: 'Current'}), [
|
|
const PreviewText('hi '),
|
|
const PreviewMention(
|
|
pubkey: _alice,
|
|
label: 'Current',
|
|
semanticsLabel: 'Current',
|
|
),
|
|
]);
|
|
// Unknown here (a nameless agent's compact npub is no name): the label
|
|
// written into the link, escapes removed.
|
|
expect(
|
|
_pieces('hi $link'),
|
|
_pieces('hi $link', mentionNames: {_alice: shortPubkey(_alice)}),
|
|
);
|
|
expect(_pieces('hi $link'), [
|
|
const PreviewText('hi '),
|
|
const PreviewMention(
|
|
pubkey: _alice,
|
|
label: 'Prev.ious',
|
|
semanticsLabel: 'Prev.ious',
|
|
),
|
|
]);
|
|
});
|
|
|
|
test('a preview loads only the identity links it draws', () {
|
|
final link = '[@Alice](nostr:${fullNpub(_alice)})';
|
|
expect(messagePreviewIdentityKeys('hi $link', maxLines: 2), {_alice});
|
|
// The last drawn line loads; the overflow line read past it doesn't.
|
|
expect(messagePreviewIdentityKeys('one\n$link', maxLines: 2), {_alice});
|
|
expect(messagePreviewIdentityKeys('one\ntwo\n$link', maxLines: 2), isEmpty);
|
|
// A label with brackets draws a pill once normalized, so it loads too.
|
|
expect(
|
|
messagePreviewIdentityKeys(
|
|
'hi [@Pat [QA]](nostr:${fullNpub(_alice)})',
|
|
maxLines: 2,
|
|
),
|
|
{_alice},
|
|
);
|
|
for (final content in [
|
|
'hi `$link`',
|
|
'```\n$link\n```',
|
|
'~~~\n$link\n~~~',
|
|
'> ```\n> $link\n> ```',
|
|
'```\n> ```\n$link\n```',
|
|
'one\ntwo\nthree\nfour\n$link',
|
|
'plain',
|
|
]) {
|
|
expect(
|
|
messagePreviewIdentityKeys(content, maxLines: 2),
|
|
isEmpty,
|
|
reason: content,
|
|
);
|
|
}
|
|
});
|
|
|
|
test('a real name equal to a compact npub still names the link', () {
|
|
final link = '[@Prev](nostr:${fullNpub(_alice)})';
|
|
final name = shortPubkey(_alice);
|
|
expect(
|
|
_pieces('hi $link', mentionLabels: {_alice: name}).last,
|
|
PreviewMention(pubkey: _alice, label: name, semanticsLabel: name),
|
|
);
|
|
});
|
|
}
|