Skip to content

Commit b454f3f

Browse files
npub12zsjdqx8dud99s9h47xmk9lq93vryf7zjrae8wdrmma52cg5yglseyulstklopez4212
andcommitted
fix(mobile): address emoji and thread review feedback
Subscribe channel timelines to live thread summaries and retain those summaries through optimistic-message rollback. Treat initial thread replies as hydration so head-first threads stay at the head, and keep full-picker recent-emoji accounting in its single owning layer. Co-authored-by: klopez4212 <[email protected]> Signed-off-by: npub12zsjdqx8dud99s9h47xmk9lq93vryf7zjrae8wdrmma52cg5yglseyulst <50a12680c76f1a52c0b7af8dbb17e02c583227c290fb93b9a3defb456114223f@buzz.block.builderlab.xyz> Signed-off-by: klopez4212 <[email protected]>
1 parent 4450e1a commit b454f3f

9 files changed

Lines changed: 192 additions & 11 deletions

‎mobile/lib/features/channels/channel_messages_provider.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import 'pending_local_messages_provider.dart';
77
import 'channel_window.dart';
88
import 'thread_replies_provider.dart';
99

10+
const _channelLiveEventKinds = [
11+
...EventKind.channelEventKinds,
12+
EventKind.channelThreadSummary,
13+
];
14+
1015
/// Provides the message list for a specific channel. Registers a live
1116
/// subscription first, then syncs history via the server-assembled channel
1217
/// window fast path, falling back to the legacy websocket history path when the
@@ -71,7 +76,7 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
7176
try {
7277
final unsubscribe = await session.subscribe(
7378
NostrFilter(
74-
kinds: EventKind.channelEventKinds,
79+
kinds: _channelLiveEventKinds,
7580
tags: {
7681
'#h': [channelId],
7782
},
@@ -325,6 +330,7 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
325330
pages: _windowStore.pages,
326331
liveOverlay: nextOverlay,
327332
liveAux: _windowStore.liveAux,
333+
liveThreadSummaries: _windowStore.liveThreadSummaries,
328334
);
329335
}
330336

‎mobile/lib/features/channels/channel_window.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ChannelWindowStore {
6363

6464
const ChannelWindowStore({
6565
required this.pages,
66-
this.liveThreadSummaries = const {},
66+
required this.liveThreadSummaries,
6767
required this.liveOverlay,
6868
required this.liveAux,
6969
});

‎mobile/lib/features/channels/message_actions.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,10 @@ class _QuickReactionRow extends ConsumerWidget {
618618
for (final entry in customEmoji) entry.shortcode.toLowerCase(): entry,
619619
};
620620

621-
void react(String value) {
622-
ref.read(recentEmojiProvider.notifier).record(value);
621+
void react(String value, {bool recordUse = true}) {
622+
// EmojiPickerSheet already records full-picker selections; quick-row
623+
// taps still enter through here and need to record their own use.
624+
if (recordUse) ref.read(recentEmojiProvider.notifier).record(value);
623625
// The sheet is on its way out, so the burst can't come from this tile —
624626
// hand it to the pill that's about to appear in the timeline.
625627
armReactionBurst(ref, message, value);
@@ -643,7 +645,10 @@ class _QuickReactionRow extends ConsumerWidget {
643645
_QuickReactionCircle(
644646
onTap: () {
645647
Navigator.of(sheetContext).pop();
646-
showEmojiPicker(context: pageContext, onSelect: react);
648+
showEmojiPicker(
649+
context: pageContext,
650+
onSelect: (value) => react(value, recordUse: false),
651+
);
647652
},
648653
child: Icon(
649654
LucideIcons.plus,

‎mobile/lib/features/channels/reaction_row.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ void showAddReactionPicker({
7777
showEmojiPicker(
7878
context: context,
7979
onSelect: (emoji) {
80-
ref.read(recentEmojiProvider.notifier).record(emoji);
8180
armReactionBurst(ref, message, emoji);
8281
ref.read(channelActionsProvider).addReaction(message.id, emoji);
8382
},

‎mobile/lib/features/channels/recent_emoji_provider.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ const defaultQuickEmojis = <String>[
2929
/// Desktop's `MAX_STORED_REACTIONS`.
3030
const _maxStoredEmoji = 24;
3131

32+
/// One persisted frequently-used emoji ranking entry.
33+
///
34+
/// [emoji] is the Unicode glyph or custom shortcode, [count] is how many times
35+
/// it has been selected, and [lastUsedAt] is the most recent selection time in
36+
/// milliseconds since the Unix epoch.
3237
class RecentEmojiEntry {
38+
/// The Unicode glyph or `:shortcode:` value that was selected.
3339
final String emoji;
40+
41+
/// Number of recorded selections for [emoji].
3442
final int count;
43+
44+
/// Most recent selection time in milliseconds since the Unix epoch.
3545
final int lastUsedAt;
3646

47+
/// Creates a persisted ranking entry for [emoji].
3748
const RecentEmojiEntry({
3849
required this.emoji,
3950
required this.count,

‎mobile/lib/features/channels/thread_detail_page.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,19 @@ class ThreadDetailPage extends HookConsumerWidget {
137137
// reversed one did, so follow the tail explicitly: when a reply arrives
138138
// while the last item is on screen, scroll it into view. If the user has
139139
// scrolled up to read, leave them where they are.
140+
final hasFetchedReplies = fetchedReplies != null;
141+
final didEstablishInitialReplies = useRef(hasFetchedReplies);
140142
final previousReplyCount = useRef(replies.length);
141143
useEffect(() {
144+
// The first authoritative query result is hydration, not a live arrival.
145+
// Establish the baseline without moving the user away from the head.
146+
if (!hasFetchedReplies) return null;
147+
if (!didEstablishInitialReplies.value) {
148+
didEstablishInitialReplies.value = true;
149+
previousReplyCount.value = replies.length;
150+
return null;
151+
}
152+
142153
final previous = previousReplyCount.value;
143154
previousReplyCount.value = replies.length;
144155
if (replies.length <= previous) return null;
@@ -163,7 +174,7 @@ class ThreadDetailPage extends HookConsumerWidget {
163174
);
164175
});
165176
return null;
166-
}, [replies.length]);
177+
}, [hasFetchedReplies, replies.length]);
167178
final readState = ref.watch(readStateProvider);
168179
final visibleReplyReadKey = replies
169180
.map((reply) => '${reply.id}:${reply.createdAt}')

‎mobile/test/features/channels/channel_detail_page_test.dart‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:convert';
23

34
import 'package:flutter/foundation.dart';
@@ -154,6 +155,7 @@ Widget _buildTestable({
154155
String? initialMessageId,
155156
String? initialThreadRootId,
156157
Map<String, List<NostrEvent>> threadReplies = const {},
158+
Map<String, Future<List<NostrEvent>>> pendingThreadReplies = const {},
157159
TextScaler textScaler = TextScaler.noScaling,
158160
RelaySessionNotifier? relaySessionNotifier,
159161
}) {
@@ -194,6 +196,10 @@ Widget _buildTestable({
194196
threadRepliesProvider(
195197
ThreadRepliesArgs(channelId: _channelId, rootId: entry.key),
196198
).overrideWith((ref) async => entry.value),
199+
for (final entry in pendingThreadReplies.entries)
200+
threadRepliesProvider(
201+
ThreadRepliesArgs(channelId: _channelId, rootId: entry.key),
202+
).overrideWith((ref) => entry.value),
197203
// Stub the relay client provider so preloadMembers doesn't crash.
198204
relayClientProvider.overrideWithValue(
199205
RelayClient(baseUrl: 'http://localhost:3000'),
@@ -2332,6 +2338,74 @@ void main() {
23322338
expect(oldestReplyY, lessThan(newestReplyY));
23332339
});
23342340

2341+
testWidgets(
2342+
'initial thread hydration keeps the head visible instead of following the tail',
2343+
(tester) async {
2344+
final rootEvent = _textMsg(
2345+
id: 'thread-root',
2346+
pubkey: 'alice',
2347+
content: 'Thread root',
2348+
createdAt: 1000,
2349+
);
2350+
final replies = [
2351+
for (var i = 0; i < 30; i++)
2352+
_textMsg(
2353+
id: 'reply-$i',
2354+
pubkey: 'bob',
2355+
content: 'Reply $i',
2356+
createdAt: 1100 + i,
2357+
extraTags: const [
2358+
['e', 'thread-root', '', 'reply'],
2359+
],
2360+
),
2361+
];
2362+
final completer = Completer<List<NostrEvent>>();
2363+
2364+
await tester.pumpWidget(
2365+
_buildTestable(
2366+
messages: [rootEvent],
2367+
pendingThreadReplies: {'thread-root': completer.future},
2368+
users: const {
2369+
'alice': UserProfile(pubkey: 'alice', displayName: 'Alice'),
2370+
'bob': UserProfile(pubkey: 'bob', displayName: 'Bob'),
2371+
},
2372+
),
2373+
);
2374+
await tester.pumpAndSettle();
2375+
2376+
final threadHead = formatTimeline([rootEvent]).single;
2377+
Navigator.of(tester.element(find.byType(ChannelDetailPage))).push(
2378+
MaterialPageRoute<void>(
2379+
builder: (_) => ThreadDetailPage(
2380+
threadHead: threadHead,
2381+
allMessages: [threadHead],
2382+
channelId: _channelId,
2383+
currentPubkey: 'self',
2384+
isMember: true,
2385+
isArchived: false,
2386+
),
2387+
),
2388+
);
2389+
await tester.pumpAndSettle();
2390+
expect(
2391+
find.byKey(const ValueKey('thread-message-group-thread-root')),
2392+
findsOneWidget,
2393+
);
2394+
2395+
completer.complete(replies);
2396+
await tester.pumpAndSettle();
2397+
2398+
expect(
2399+
find.byKey(const ValueKey('thread-message-group-thread-root')),
2400+
findsOneWidget,
2401+
);
2402+
expect(
2403+
find.byKey(const ValueKey('thread-message-group-reply-29')),
2404+
findsNothing,
2405+
);
2406+
},
2407+
);
2408+
23352409
testWidgets('a reaction landing while the thread is open shows up there', (
23362410
tester,
23372411
) async {

‎mobile/test/features/channels/channel_messages_provider_test.dart‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ void main() {
3939
.value!;
4040
expect(messages.map((event) => event.id), ['history', 'live']);
4141
expect(relaySession.operations, ['subscribe', 'query', 'fetch']);
42-
expect(
43-
relaySession.liveFilters.single.kinds,
44-
EventKind.channelEventKinds,
45-
);
42+
expect(relaySession.liveFilters.single.kinds, [
43+
...EventKind.channelEventKinds,
44+
EventKind.channelThreadSummary,
45+
]);
4646
expect(relaySession.liveFilters.single.tags['#h'], [_channelId]);
4747
expect(relaySession.liveFilters.single.limit, 200);
4848
expect(
@@ -242,6 +242,42 @@ void main() {
242242
);
243243
});
244244

245+
test(
246+
'live thread summary survives rolling back an unrelated local row',
247+
() async {
248+
final relaySession = _RecordingRelaySessionNotifier(
249+
queryResults: [
250+
[_event(id: 'root', createdAt: 10), _bounds()],
251+
],
252+
);
253+
final container = _buildContainer(relaySession);
254+
addTearDown(container.dispose);
255+
256+
container.read(channelMessagesProvider(_channelId));
257+
await relaySession.subscribed;
258+
await _pumpEventQueue();
259+
final notifier = container.read(
260+
channelMessagesProvider(_channelId).notifier,
261+
);
262+
notifier.addLocalMessage(_event(id: 'local', createdAt: 20));
263+
264+
relaySession.emit(_summary(rootId: 'root', replyCount: 2));
265+
await _pumpEventQueue();
266+
expect(notifier.threadSummaries['root']?.replyCount, 2);
267+
268+
notifier.removeLocalMessage('local');
269+
270+
expect(notifier.threadSummaries['root']?.replyCount, 2);
271+
expect(
272+
container
273+
.read(channelMessagesProvider(_channelId))
274+
.value
275+
?.map((event) => event.id),
276+
['root'],
277+
);
278+
},
279+
);
280+
245281
test('reconnect hydration cannot retain a rolled-back local row', () async {
246282
final relaySession = _RecordingRelaySessionNotifier(
247283
queryResults: [
@@ -562,6 +598,26 @@ NostrEvent _event({
562598
);
563599
}
564600

601+
NostrEvent _summary({required String rootId, required int replyCount}) {
602+
return NostrEvent(
603+
id: 'summary-$rootId-$replyCount',
604+
pubkey: 'relay',
605+
createdAt: 20,
606+
kind: EventKind.channelThreadSummary,
607+
tags: [
608+
['h', _channelId],
609+
['e', rootId],
610+
],
611+
content: jsonEncode({
612+
'reply_count': replyCount,
613+
'descendant_count': replyCount,
614+
'last_reply_at': 20,
615+
'participants': ['alice'],
616+
}),
617+
sig: 'sig',
618+
);
619+
}
620+
565621
NostrEvent _bounds({
566622
bool hasMore = false,
567623
int? cursorCreatedAt,

‎mobile/test/features/channels/emoji_picker_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:buzz/features/channels/emoji_picker.dart';
24
import 'package:buzz/features/channels/recent_emoji_provider.dart';
35
import 'package:buzz/shared/custom_emoji/custom_emoji.dart';
@@ -334,6 +336,23 @@ void main() {
334336
expect(selected, [':partyparrot:']);
335337
});
336338

339+
testWidgets('a selection records exactly one use', (tester) async {
340+
final prefs = await _prefs();
341+
final selected = await _pumpPicker(tester, prefs: prefs);
342+
343+
await tester.tap(find.byKey(const ValueKey('emoji-tile-fire')));
344+
await tester.pumpAndSettle();
345+
346+
expect(selected, ['\u{1F525}']);
347+
final stored = prefs.getString(
348+
'buzz.quick-reaction-emojis.v1:http://localhost:3000:self',
349+
);
350+
final entries = jsonDecode(stored!) as List<dynamic>;
351+
expect(entries, hasLength(1));
352+
expect(entries.single, containsPair('emoji', '\u{1F525}'));
353+
expect(entries.single, containsPair('count', 1));
354+
});
355+
337356
testWidgets('a selection adds a Frequently used section', (tester) async {
338357
final prefs = await _prefs();
339358
await _pumpPicker(tester, prefs: prefs);

0 commit comments

Comments
 (0)