Files
buzz/mobile/lib/features/activity/inbox_read_state.dart
morgmart 97bec174a4 fix(mobile): only dot channel-sent replies in the reader's threads (#9696)
## What changed

Mobile matches block/buzz-app#3938. A reply someone sends with **Also
send to channel** used to light the channel's attention dot and count
toward the app icon badge for everyone in the channel, even people who
never joined that thread.

Now it follows the same rule as any other reply:

- **Dot** if it mentions you, or it's in a thread you're part of (you
started it, replied in it, or follow it).
- **Bold only** otherwise, like a new top-level post. It no longer
counts toward the app icon badge.

Reading is unchanged. These replies are still marked read individually,
as desktop reads them, so reaching the bottom of the channel doesn't
silently mark them read on one device but not the other.

## How

- The "is this one of my threads" check that already decides which
ordinary replies count is now shared, and channel-sent replies go
through it when deciding attention.
- On mobile, "earns a dot" and "is read by reaching the bottom of the
channel" were one flag. Dropping a channel-sent reply's priority would
have also changed how it gets read, drifting from desktop. The dot now
comes from its own field; every other kind of message gets exactly the
same dot and read behavior as before.

## Tests

New cases drive live events through the channel list. For each, they
check the observed unread entry, whether its thread shows in Activity,
the app icon badge, the channel list's bold vs dot, and reading:

- reply sent to the channel in someone else's thread: bold, no dot, not
in Activity, counts as ordinary unread on the icon (same as a top-level
post)
- same, but mentioning the reader: dot, in Activity
- in a thread the reader follows: dot, in Activity
- in a thread the reader started: dot, in Activity
- in a thread the reader replied in: dot, in Activity

In every case, a channel catch-up mark leaves the reply unread and its
own read mark clears it.

The "someone else's thread" case fails on `main` (it gets a dot). The
pre-push mobile checks (format, analyze, full mobile test suite) passed
on this head.

## Known gaps (not changed here)

- Mobile only knows the threads you're part of from posts it has seen on
this phone. A fresh install doesn't know about older threads you joined
elsewhere, and a reply's first classification sticks even if later
loading shows you joined that thread. Desktop looks these up on the
relay.
- Mobile has no explicit "Unfollow" for a thread you started or replied
in (the menu only offers Follow there), so you can't silence one the way
desktop can. This applies to all replies, not just ones sent to the
channel.
- A reply sent to the channel still counts as unread in a muted channel.
That was already true before this change.

No screenshots: no visual element changes, only which existing state
(bold or dot) a channel shows.


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

---------

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-20 09:32:03 -08:00

124 lines
4.5 KiB
Dart

import '../../shared/read_state/read_state_format.dart';
import 'feed_item.dart';
import 'inbox_item.dart';
/// The newest read marker that reads one grouped [event], following the
/// unread badge's `observedUnreadEventReadAt`: the channel mark, the
/// event's own `msg:` mark, and for a thread reply `thread:<root>` and
/// `thread-activity:<root>`. Returns null for an event with no channel.
///
/// `activity:<channel>` never counts here. Activity rows hold only mentions,
/// needs-action and agent events addressed to the reader, eligible thread
/// replies, and DM messages. That catch-up mark reads none of those.
int? inboxEventReadAt(
FeedItem event, {
required int? Function(String contextId) markerOf,
}) {
final channelId = event.channelId;
if (channelId == null) return null;
final rootId = isThreadReply(event.tags)
? threadReferenceOf(event.tags).rootId
: null;
return maxReadAt([
markerOf(channelId),
markerOf(msgContextKey(event.id)),
if (rootId != null) ...[
markerOf(threadContextKey(rootId)),
markerOf(threadActivityContextKey(rootId)),
],
]);
}
/// Whether the marks read one grouped [event].
bool isInboxEventRead(
FeedItem event, {
required int? Function(String contextId) markerOf,
}) {
final readAt = inboxEventReadAt(event, markerOf: markerOf);
return readAt != null && event.createdAt <= readAt;
}
/// The newest marker covering any grouped event of [item], or 0.
int inboxItemReadAt(
InboxItem item, {
required int? Function(String contextId) markerOf,
}) =>
maxReadAt([
for (final event in _groupedEvents(item))
inboxEventReadAt(event, markerOf: markerOf),
]) ??
0;
/// Whether the row is read ("done"): a local unread override wins until any
/// device reads past the marker it was set at ([rawMarkerOf], the stored
/// markers without the moving retention floor, defaulting to [markerOf]);
/// otherwise the row is done
/// when the marks read every grouped event. Each event is checked on its
/// own, because reading a channel marks mentions with per-message marks, so
/// reading the newest says nothing about the rest. Channel-less rows fall
/// back to the local done set.
bool isInboxItemDone(
InboxItem item, {
required int? Function(String contextId) markerOf,
int? Function(String contextId)? rawMarkerOf,
required Map<String, int> localUnreadOverrides,
required Set<String> localDoneSet,
}) {
final ids = groupedInboxItemIds(item);
final baselines = [for (final id in ids) ?localUnreadOverrides[id]];
if (baselines.isNotEmpty &&
inboxItemReadAt(item, markerOf: rawMarkerOf ?? markerOf) <=
baselines.reduce((a, b) => a > b ? a : b)) {
return false;
}
if (item.item.channelId == null) return localDoneSet.contains(item.id);
return _groupedEvents(
item,
).every((event) => isInboxEventRead(event, markerOf: markerOf));
}
Iterable<FeedItem> _groupedEvents(InboxItem item) => {
for (final event in [item.item, ...item.groupItems]) event.id: event,
}.values;
/// Whether marking a channel row read reads [event] only by its own `msg:`
/// mark: an unmentioned reply also sent to the channel. The channel's unread
/// registry reads it by that mark alone, and the channel marker would also
/// read every older message in the channel. A mention keeps the channel
/// marker path it had before these replies joined Activity.
bool readsByOwnMark(FeedItem event) =>
event.category != 'mention' && isChannelSentReply(event.tags);
/// All event ids identified with the row — desktop's `getGroupedInboxItemIds`.
List<String> groupedInboxItemIds(InboxItem item) {
return {item.id, item.item.id, ...item.groupItems.map((i) => i.id)}.toList();
}
/// The channel-level timestamp that marking a thread row read should also
/// advance — desktop's `getGroupedChannelReadTimestamp`. Only top-level
/// (non-thread-reply) grouped events count, so marking a thread read cannot
/// swallow unrelated channel messages. Replies that [readsByOwnMark] get
/// their own marks instead.
({String channelId, int timestamp})? groupedChannelReadTimestamp(
InboxItem item,
) {
final channelId = item.item.channelId;
if (channelId == null) return null;
int? timestamp;
for (final groupItem in item.groupItems) {
if (groupItem.channelId != channelId ||
isThreadReply(groupItem.tags) ||
readsByOwnMark(groupItem)) {
continue;
}
if (timestamp == null || groupItem.createdAt > timestamp) {
timestamp = groupItem.createdAt;
}
}
return timestamp == null
? null
: (channelId: channelId, timestamp: timestamp);
}