Add channel message filter for spam and repeated messages
Client-side filter for channel messages, with no model download: - User rules on message text and/or sender name (contains, whole word, exact), normalised for case, accents, zero-width and look-alike letters. - Automatic checks: repeated characters, same-sender repeats within 10 minutes, and the same sentence (>= 6 words) from 3+ senders within 30 minutes. Windows use send times, so synced history is not a flood. - On-device n-gram classifier trained by Mark as spam / Not spam taps. Filtered messages are kept, never notify or count as unread, are not auto-translated or used for channel previews, and are concealed in reply quotes. They show as a calm, collapsible placeholder that animates open and closed, or can be hidden completely. Includes a Filtered messages list, per-channel toggle, settings screen, translations for all locales and documentation.
This commit is contained in:
1 parent
5ef2c2cd7d
commit
1332c35884
75 files changed
+12527
-254
No files matched your search
@@ -28,11 +28,13 @@ import '../l10n/l10n.dart';
|
||||
import '../models/channel.dart';
|
||||
import '../models/channel_message.dart';
|
||||
import '../models/image_codec_support.dart' show aeicRatePointForUi;
|
||||
import '../models/message_filter.dart';
|
||||
import '../services/app_settings_service.dart'
|
||||
show AppSettingsService, ChannelNotificationMode;
|
||||
import '../services/chat_text_scale_service.dart';
|
||||
import '../services/image_chunk_transport.dart';
|
||||
import '../services/image_codec_service.dart';
|
||||
import '../services/message_filter/message_filter_service.dart';
|
||||
import '../services/received_image_store.dart';
|
||||
import '../services/translation_service.dart';
|
||||
import '../utils/image_save.dart';
|
||||
@@ -42,6 +44,8 @@ import '../widgets/received_image_message.dart';
|
||||
import '../widgets/channel_notification_mode_sheet.dart';
|
||||
import '../widgets/chat_day_separator.dart';
|
||||
import '../widgets/empty_state.dart';
|
||||
import '../widgets/filter_rule_dialog.dart';
|
||||
import '../widgets/filtered_message_ghost.dart';
|
||||
import '../widgets/chat_composer.dart';
|
||||
import '../widgets/chat_send_pipeline.dart';
|
||||
import '../widgets/chat_zoom_wrapper.dart';
|
||||
@@ -61,6 +65,7 @@ import '../theme/mesh_theme.dart';
|
||||
import '../widgets/future_timestamp_indicator.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
import 'app_settings_screen.dart';
|
||||
import 'channel_chat_transcript_grouping.dart';
|
||||
import 'channel_message_path_screen.dart';
|
||||
import 'map_screen.dart';
|
||||
import 'region_management_screen.dart';
|
||||
@@ -117,6 +122,11 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
int _imageStoreVersion = 0;
|
||||
ModalRoute<dynamic>? _route;
|
||||
|
||||
/// Filtered messages the user tapped open on this screen. Session-only by
|
||||
/// design: leaving the chat hides them again.
|
||||
final Set<String> _revealedFilteredIds = <String>{};
|
||||
int _revealVersion = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -144,9 +154,26 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
_channelSkipNextBottomSnap = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
// Estimate from visible list entries: filtered messages collapse
|
||||
// into ghost rows (or vanish in hide mode), so raw message counts
|
||||
// would undershoot.
|
||||
var unreadEntries = unread;
|
||||
var totalEntries = messages.length;
|
||||
final layout = _transcriptLayout;
|
||||
if (layout != null && layout.length > 0) {
|
||||
final entryIndex = layout.ids.indexOf(
|
||||
layout.entryIdFor(anchor!.messageId),
|
||||
);
|
||||
if (entryIndex >= 0) {
|
||||
// Entries are newest first, so the anchor has entryIndex
|
||||
// newer entries below it.
|
||||
unreadEntries = entryIndex + 1;
|
||||
totalEntries = layout.length;
|
||||
}
|
||||
}
|
||||
_scrollController.jumpToEstimatedOffset(
|
||||
unreadCount: unread,
|
||||
totalMessages: messages.length,
|
||||
unreadCount: unreadEntries,
|
||||
totalMessages: totalEntries,
|
||||
onJumped: () {
|
||||
if (!mounted) return;
|
||||
_scrollToMessage(anchor!.messageId, quiet: true);
|
||||
@@ -179,7 +206,8 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
var n = 0;
|
||||
ChannelMessage? oldest;
|
||||
for (final m in messages.reversed) {
|
||||
if (m.isOutgoing) continue;
|
||||
// Filtered messages never count as unread (see the connector).
|
||||
if (m.isOutgoing || m.isFiltered) continue;
|
||||
n++;
|
||||
oldest = m;
|
||||
if (n >= unreadCount) break;
|
||||
@@ -261,7 +289,10 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
}
|
||||
|
||||
Future<void> _scrollToMessage(String messageId, {bool quiet = false}) async {
|
||||
final key = _messageKeys[messageId];
|
||||
// A message hidden by the filter is represented by its ghost row (or the
|
||||
// nearest visible row in hide mode).
|
||||
final key =
|
||||
_messageKeys[_transcriptLayout?.entryIdFor(messageId) ?? messageId];
|
||||
if (key == null) {
|
||||
// The auto unread-jump can resolve a frame after navigating away;
|
||||
// a deactivated context can't host a snackbar.
|
||||
@@ -447,11 +478,20 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
);
|
||||
} else if (value == 'clearChat') {
|
||||
_confirmClearChat();
|
||||
} else if (value == 'messageFilter') {
|
||||
_toggleChannelFiltering();
|
||||
}
|
||||
},
|
||||
// Region lives here (and on a tap of the title) so the channel
|
||||
// name keeps its room in the app bar.
|
||||
itemBuilder: (context) => [
|
||||
if (_messageFilter case final filter?
|
||||
when filter.settings.enabled)
|
||||
CheckedPopupMenuItem(
|
||||
value: 'messageFilter',
|
||||
checked: filter.isChannelFiltered(_messageFilterChannelId),
|
||||
child: Text(context.l10n.messageFilter_channelToggle),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'region',
|
||||
child: Row(
|
||||
@@ -508,7 +548,10 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
final messages = connector.getChannelMessages(
|
||||
widget.channel,
|
||||
);
|
||||
final newest = messages.isEmpty ? null : messages.last;
|
||||
// A filtered message must not trigger holiday effects.
|
||||
final newest = connector.latestVisibleChannelMessage(
|
||||
widget.channel,
|
||||
);
|
||||
_holidayWatcher.onNewest(
|
||||
context,
|
||||
id: newest?.messageId,
|
||||
@@ -586,9 +629,10 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
} else {
|
||||
messageKey = GlobalKey();
|
||||
}
|
||||
final dividerId = _unreadDividerMessageId;
|
||||
final isUnreadAnchor =
|
||||
_unreadDividerMessageId != null &&
|
||||
row.id == _unreadDividerMessageId;
|
||||
dividerId != null &&
|
||||
row.id == layout.entryIdFor(dividerId);
|
||||
return Container(
|
||||
key: messageKey,
|
||||
child: Builder(
|
||||
@@ -598,16 +642,30 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
(service) => service.scale,
|
||||
);
|
||||
final message = row.message;
|
||||
final bubble = message != null
|
||||
? _buildMessageBubble(
|
||||
message,
|
||||
textScale,
|
||||
urlImagesEnabled,
|
||||
)
|
||||
: _buildImageBubble(
|
||||
row.image!,
|
||||
textScale,
|
||||
);
|
||||
final ghost = row.ghost;
|
||||
final Widget bubble;
|
||||
if (ghost != null) {
|
||||
bubble = _buildFilteredGhost(
|
||||
ghost,
|
||||
textScale,
|
||||
expanded: row.ghostExpanded,
|
||||
urlImagesEnabled: urlImagesEnabled,
|
||||
);
|
||||
} else if (message != null) {
|
||||
bubble = _buildMessageBubble(
|
||||
message,
|
||||
textScale,
|
||||
urlImagesEnabled,
|
||||
showFilterBanner:
|
||||
layout.filterActive &&
|
||||
message.isFiltered,
|
||||
);
|
||||
} else {
|
||||
bubble = _buildImageBubble(
|
||||
row.image!,
|
||||
textScale,
|
||||
);
|
||||
}
|
||||
final startsDay =
|
||||
index == rowCount - 1 ||
|
||||
!isSameChatDay(
|
||||
@@ -658,7 +716,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
var found = false;
|
||||
for (final m in messages) {
|
||||
if (m.messageId == message.messageId) found = true;
|
||||
if (found && !m.isOutgoing) count++;
|
||||
if (found && !m.isOutgoing && !m.isFiltered) count++;
|
||||
}
|
||||
connector.setChannelUnreadCount(widget.channel.index, count);
|
||||
}
|
||||
@@ -666,8 +724,9 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
Widget _buildMessageBubble(
|
||||
ChannelMessage message,
|
||||
double textScale,
|
||||
bool urlImagesEnabled,
|
||||
) {
|
||||
bool urlImagesEnabled, {
|
||||
bool showFilterBanner = false,
|
||||
}) {
|
||||
final settingsService = context.watch<AppSettingsService>();
|
||||
final publicIpfsGateway = settingsService.settings.publicIpfsGateway;
|
||||
final enableTracing = settingsService.settings.enableMessageTracing;
|
||||
@@ -781,6 +840,18 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
),
|
||||
if (gifId == null) const SizedBox(height: 2),
|
||||
],
|
||||
if (showFilterBanner)
|
||||
FilteredMessageBanner(
|
||||
reason: messageFilterReasonLabel(
|
||||
context.l10n,
|
||||
message.filterReason,
|
||||
message.filterDetail,
|
||||
),
|
||||
color: textColor,
|
||||
textScale: textScale,
|
||||
onNotSpam: () => _markNotSpam(message),
|
||||
onHide: () => _hideFilteredMessage(message),
|
||||
),
|
||||
if (message.replyToMessageId != null) ...[
|
||||
_buildReplyPreview(message, textScale),
|
||||
const SizedBox(height: 8),
|
||||
@@ -1153,11 +1224,52 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
final connector = context.read<MeshCoreConnector>();
|
||||
final isOwnNode = message.replyToSenderName == connector.selfName;
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final contentPreview = _buildReplyContent(
|
||||
message.replyToText ?? '',
|
||||
textScale,
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.7),
|
||||
);
|
||||
final previewColor = colorScheme.onSurface.withValues(alpha: 0.7);
|
||||
// The quoted text was copied when the reply arrived; if the quoted message
|
||||
// is hidden by the filter, don't spell it out until it is revealed.
|
||||
final quotedId = message.replyToMessageId;
|
||||
final quoted = quotedId == null
|
||||
? null
|
||||
: connector
|
||||
.getChannelMessages(widget.channel)
|
||||
.where((m) => m.messageId == quotedId)
|
||||
.lastOrNull;
|
||||
final concealed =
|
||||
quoted != null &&
|
||||
isReplyPreviewConcealed(
|
||||
quotedIsFiltered: quoted.isFiltered,
|
||||
filterActive:
|
||||
_messageFilter?.isChannelFiltered(_messageFilterChannelId) ??
|
||||
false,
|
||||
quotedRevealed: _revealedFilteredIds.contains(quoted.messageId),
|
||||
);
|
||||
final contentPreview = concealed
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.visibility_off_outlined,
|
||||
size: 16 * textScale,
|
||||
color: previewColor,
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Flexible(
|
||||
child: Text(
|
||||
context.l10n.messageFilter_ghostSingle,
|
||||
style: TextStyle(
|
||||
fontSize: 13 * textScale,
|
||||
fontStyle: FontStyle.italic,
|
||||
color: previewColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: _buildReplyContent(
|
||||
message.replyToText ?? '',
|
||||
textScale,
|
||||
color: previewColor,
|
||||
);
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => _scrollToMessage(message.replyToMessageId!),
|
||||
@@ -1701,9 +1813,32 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
store?.addListener(_onImageStoreChanged);
|
||||
_imageStoreVersion++;
|
||||
}
|
||||
MessageFilterService? filter;
|
||||
try {
|
||||
filter = context.watch<MessageFilterService>();
|
||||
} on ProviderNotFoundException {
|
||||
filter = null;
|
||||
}
|
||||
final displayMode =
|
||||
filter?.settings.displayMode ?? FilterDisplayMode.placeholder;
|
||||
final filterActive =
|
||||
filter?.isChannelFiltered(_messageFilterChannelId) ?? true;
|
||||
// Spam / Not spam replace a message in place (same list, length and
|
||||
// ends), so the memo also keys on which messages are filtered.
|
||||
final filterVersion = Object.hash(
|
||||
displayMode,
|
||||
filterActive,
|
||||
_revealVersion,
|
||||
_filteredMessagesSignature(messages),
|
||||
);
|
||||
final cached = _transcriptLayout;
|
||||
if (cached != null &&
|
||||
cached.matches(messages, _imageStoreVersion, widget.channel.index)) {
|
||||
cached.matches(
|
||||
messages,
|
||||
_imageStoreVersion,
|
||||
widget.channel.index,
|
||||
filterVersion,
|
||||
)) {
|
||||
return cached;
|
||||
}
|
||||
final images = store == null
|
||||
@@ -1714,6 +1849,10 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
images: images,
|
||||
imageVersion: _imageStoreVersion,
|
||||
channelIndex: widget.channel.index,
|
||||
filterVersion: filterVersion,
|
||||
displayMode: displayMode,
|
||||
filterActive: filterActive,
|
||||
revealedIds: _revealedFilteredIds,
|
||||
);
|
||||
// Prune stale keys (deleted/cleared messages) to avoid unbounded growth.
|
||||
final liveIds = layout.ids.toSet();
|
||||
@@ -1724,6 +1863,19 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
|
||||
void _onImageStoreChanged() => _imageStoreVersion++;
|
||||
|
||||
/// Cheap fingerprint of which messages are filtered, so the layout memo
|
||||
/// notices a message being marked spam or not spam in place.
|
||||
static int _filteredMessagesSignature(List<ChannelMessage> messages) {
|
||||
var hash = 0;
|
||||
var count = 0;
|
||||
for (final m in messages) {
|
||||
if (!m.isFiltered) continue;
|
||||
count++;
|
||||
hash = Object.hash(hash, m.messageId);
|
||||
}
|
||||
return Object.hash(count, hash);
|
||||
}
|
||||
|
||||
/// Bubble for one AEIC image, following the GIF-message precedent: minimal
|
||||
/// chrome, the renderer owns every state (receiving / decoding / failed) and
|
||||
/// the mandatory AI-reconstruction label.
|
||||
@@ -2286,6 +2438,14 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
isOutgoing: message.isOutgoing,
|
||||
) &&
|
||||
(message.translatedText?.trim().isEmpty ?? true);
|
||||
// Filter actions apply to incoming messages in a channel the filter
|
||||
// covers; structured payloads (GIFs, markers) are never filtered.
|
||||
final filter = _messageFilter;
|
||||
final showFilterActions =
|
||||
!message.isOutgoing &&
|
||||
filter != null &&
|
||||
filter.isChannelFiltered(_messageFilterChannelId) &&
|
||||
(message.isFiltered || !_isStructuredPayload(message.text));
|
||||
|
||||
showMeshSheet(
|
||||
context,
|
||||
@@ -2377,6 +2537,44 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
_markAsUnread(message);
|
||||
},
|
||||
),
|
||||
if (showFilterActions) ...[
|
||||
const Divider(height: 1),
|
||||
if (message.isFiltered)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.visibility_outlined),
|
||||
title: Text(context.l10n.messageFilter_notSpam),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
_markNotSpam(message);
|
||||
},
|
||||
)
|
||||
else
|
||||
ListTile(
|
||||
leading: const Icon(Icons.visibility_off_outlined),
|
||||
title: Text(context.l10n.messageFilter_markSpam),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
_markSpam(message);
|
||||
},
|
||||
),
|
||||
if (message.senderName.trim().isNotEmpty)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.person_off_outlined),
|
||||
title: Text(context.l10n.messageFilter_filterSender),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
_filterSender(message.senderName);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.filter_alt_outlined),
|
||||
title: Text(context.l10n.messageFilter_filterLikeThis),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
_filterMessagesLike(message);
|
||||
},
|
||||
),
|
||||
],
|
||||
const Divider(height: 1),
|
||||
ListTile(
|
||||
leading: Icon(
|
||||
@@ -2400,6 +2598,172 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
// ---- message filter -------------------------------------------------------
|
||||
|
||||
/// The message filter, or null when it is not registered (screen tests).
|
||||
MessageFilterService? get _messageFilter {
|
||||
try {
|
||||
return context.read<MessageFilterService>();
|
||||
} on ProviderNotFoundException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String get _messageFilterChannelId =>
|
||||
MeshCoreConnector.messageFilterChannelId(widget.channel);
|
||||
|
||||
/// GIFs and other structured payloads (same test as the connector's
|
||||
/// outbound check), which the filter never looks at.
|
||||
static bool _isStructuredPayload(String text) {
|
||||
final trimmed = text.trimLeft();
|
||||
return trimmed.startsWith('g:') || trimmed.startsWith('m:');
|
||||
}
|
||||
|
||||
void _setRevealed(Iterable<String> ids, {required bool revealed}) {
|
||||
setState(() {
|
||||
if (revealed) {
|
||||
_revealedFilteredIds.addAll(ids);
|
||||
} else {
|
||||
_revealedFilteredIds.removeAll(ids);
|
||||
}
|
||||
_revealVersion++;
|
||||
});
|
||||
}
|
||||
|
||||
/// "Hide" on a revealed message collapses its whole run back into one
|
||||
/// ghost row, not just that message.
|
||||
void _hideFilteredMessage(ChannelMessage message) {
|
||||
final layout = _transcriptLayout;
|
||||
final messages = context.read<MeshCoreConnector>().getChannelMessages(
|
||||
widget.channel,
|
||||
);
|
||||
final ids = layout == null
|
||||
? [message.messageId]
|
||||
: layout.revealedRunIdsFor(message.messageId, messages);
|
||||
_setRevealed(ids, revealed: false);
|
||||
}
|
||||
|
||||
Future<void> _markSpam(ChannelMessage message) async {
|
||||
final connector = context.read<MeshCoreConnector>();
|
||||
// Collapse it straight away, even if it had been revealed before.
|
||||
_setRevealed([message.messageId], revealed: false);
|
||||
await connector.markChannelMessageSpam(widget.channel, message);
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.messageFilter_markedSpam),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _markNotSpam(ChannelMessage message) async {
|
||||
final connector = context.read<MeshCoreConnector>();
|
||||
_setRevealed([message.messageId], revealed: false);
|
||||
await connector.markChannelMessageNotSpam(widget.channel, message);
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.messageFilter_markedNotSpam),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _filterSender(String senderName) async {
|
||||
final filter = _messageFilter;
|
||||
if (filter == null) return;
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: Text(dialogContext.l10n.messageFilter_filterSender),
|
||||
content: Text(
|
||||
dialogContext.l10n.messageFilter_filterSenderConfirm(senderName),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext, false),
|
||||
child: Text(dialogContext.l10n.common_cancel),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(dialogContext, true),
|
||||
child: Text(dialogContext.l10n.messageFilter_filterSender),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
await filter.addRule(
|
||||
FilterRule(
|
||||
id: MessageFilterService.newRuleId(),
|
||||
pattern: senderName,
|
||||
field: FilterRuleField.sender,
|
||||
match: FilterRuleMatch.exact,
|
||||
),
|
||||
);
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.messageFilter_ruleSaved),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _filterMessagesLike(ChannelMessage message) async {
|
||||
final filter = _messageFilter;
|
||||
if (filter == null) return;
|
||||
final rule = await showFilterRuleDialog(
|
||||
context,
|
||||
initialPattern: message.text,
|
||||
initialField: FilterRuleField.text,
|
||||
);
|
||||
if (rule == null) return;
|
||||
await filter.addRule(rule);
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.messageFilter_ruleSaved),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _toggleChannelFiltering() async {
|
||||
final filter = _messageFilter;
|
||||
if (filter == null) return;
|
||||
final channelId = _messageFilterChannelId;
|
||||
await filter.setChannelExcluded(
|
||||
channelId,
|
||||
filter.isChannelFiltered(channelId),
|
||||
);
|
||||
}
|
||||
|
||||
/// One entry for a run of hidden messages (newest first): a ghost header
|
||||
/// that animates the run's messages open and closed underneath it. Opening
|
||||
/// lasts for this visit to the chat.
|
||||
Widget _buildFilteredGhost(
|
||||
List<ChannelMessage> run,
|
||||
double textScale, {
|
||||
required bool expanded,
|
||||
required bool urlImagesEnabled,
|
||||
}) {
|
||||
final newest = run.first;
|
||||
return FilteredMessageRun(
|
||||
count: run.length,
|
||||
expanded: expanded,
|
||||
buildMessages: (context) => [
|
||||
for (final message in run.reversed)
|
||||
_buildMessageBubble(
|
||||
message,
|
||||
textScale,
|
||||
urlImagesEnabled,
|
||||
showFilterBanner: true,
|
||||
),
|
||||
],
|
||||
reason: messageFilterReasonLabel(
|
||||
context.l10n,
|
||||
newest.filterReason,
|
||||
newest.filterDetail,
|
||||
),
|
||||
textScale: textScale,
|
||||
onToggle: () =>
|
||||
_setRevealed([for (final m in run) m.messageId], revealed: !expanded),
|
||||
);
|
||||
}
|
||||
|
||||
void _showEmojiPicker(ChannelMessage message) {
|
||||
showMeshSheet(
|
||||
context,
|
||||
@@ -2822,7 +3186,7 @@ class _SwipeReplyBubbleState extends State<_SwipeReplyBubble> {
|
||||
|
||||
/// One row of the channel transcript.
|
||||
///
|
||||
/// Exactly one of [message] / [image] is non-null. Images cannot be
|
||||
/// Exactly one of [message] / [image] / [ghost] is non-null. Images cannot be
|
||||
/// `ChannelMessage`s: they arrive as GRP_DATA chunks with no text frame behind
|
||||
/// them, are keyed on (sender prefix, img id, channel) rather than a message id,
|
||||
/// and are persisted by [ReceivedImageStore] instead of the channel message
|
||||
@@ -2832,15 +3196,34 @@ class _ChannelChatRow {
|
||||
final ChannelMessage? message;
|
||||
final ReceivedImageEntry? image;
|
||||
|
||||
const _ChannelChatRow({this.message, this.image});
|
||||
/// A run of consecutive messages hidden by the message filter, newest
|
||||
/// first, shown as one ghost row.
|
||||
final List<ChannelMessage>? ghost;
|
||||
|
||||
/// [ghost] is a revealed run: its messages are shown under the header.
|
||||
final bool ghostExpanded;
|
||||
|
||||
const _ChannelChatRow({
|
||||
this.message,
|
||||
this.image,
|
||||
this.ghost,
|
||||
this.ghostExpanded = false,
|
||||
});
|
||||
|
||||
/// Local arrival time, used for ordering and day separators so a sender
|
||||
/// clock running ahead cannot pin a row to the bottom (#584).
|
||||
DateTime get receivedAt => message?.receivedAt ?? image!.firstSeen;
|
||||
/// clock running ahead cannot pin a row to the bottom (#584). A ghost run
|
||||
/// never spans two days, so its newest member stands for it.
|
||||
DateTime get receivedAt =>
|
||||
message?.receivedAt ?? ghost?.first.receivedAt ?? image!.firstSeen;
|
||||
|
||||
/// Stable identity for the scroll-to-message [GlobalKey] map. The `aeic:`
|
||||
/// prefix keeps a stream id from ever colliding with a message id.
|
||||
String get id => message?.messageId ?? 'aeic:${image!.streamId}';
|
||||
/// and `ghost:` prefixes keep these from ever colliding with a message id.
|
||||
String get id {
|
||||
final run = ghost;
|
||||
// Same id open or closed, so the run keeps its State and can animate.
|
||||
if (run != null) return 'ghost:${run.first.messageId}';
|
||||
return message?.messageId ?? 'aeic:${image!.streamId}';
|
||||
}
|
||||
}
|
||||
|
||||
/// Memoised ordering of a channel transcript, newest first.
|
||||
@@ -2848,6 +3231,10 @@ class _ChannelChatRow {
|
||||
/// Rows hold positions, not message objects: the connector replaces messages
|
||||
/// in place (status, repeats, reactions) without changing their order or id,
|
||||
/// so [rowAt] always reads the current object from the live list.
|
||||
///
|
||||
/// Messages hidden by the message filter are grouped by [groupFilteredRows]:
|
||||
/// each visible entry is one row, or one ghost standing in for a run of
|
||||
/// hidden messages (none at all in hide mode).
|
||||
class _TranscriptLayout {
|
||||
final List<ChannelMessage> _source;
|
||||
final int _sourceLength;
|
||||
@@ -2855,14 +3242,26 @@ class _TranscriptLayout {
|
||||
final String? _lastId;
|
||||
final int _imageVersion;
|
||||
final int _channelIndex;
|
||||
final int _filterVersion;
|
||||
|
||||
/// Newest first. `>= 0` is a message index; `< 0` is image `-(i + 1)`.
|
||||
final List<int> _order;
|
||||
final List<ReceivedImageEntry> _images;
|
||||
|
||||
/// Row ids, aligned with [_order].
|
||||
/// Visible entries, newest first; their rows index into [_order].
|
||||
final List<TranscriptEntry> _entries;
|
||||
|
||||
/// Whether filtered messages are being hidden in this channel. When false
|
||||
/// they render as ordinary bubbles, without the filter banner.
|
||||
final bool filterActive;
|
||||
|
||||
/// Entry ids, aligned with the visible entries.
|
||||
final List<String> ids;
|
||||
|
||||
/// Row id (message id) -> id of the entry that shows it, for rows that are
|
||||
/// not entries themselves (inside a ghost, or dropped in hide mode).
|
||||
final Map<String, String> _entryIdByRowId;
|
||||
|
||||
/// Two messages can collide on messageId (same ms + name/text hash). Only
|
||||
/// the first occurrence owns the shared GlobalKey used for
|
||||
/// scroll-to-message; duplicates get a local key so no two widgets share
|
||||
@@ -2876,9 +3275,13 @@ class _TranscriptLayout {
|
||||
this._lastId,
|
||||
this._imageVersion,
|
||||
this._channelIndex,
|
||||
this._filterVersion,
|
||||
this._order,
|
||||
this._images,
|
||||
this._entries,
|
||||
this.filterActive,
|
||||
this.ids,
|
||||
this._entryIdByRowId,
|
||||
this.keyedIndices,
|
||||
);
|
||||
|
||||
@@ -2887,6 +3290,10 @@ class _TranscriptLayout {
|
||||
required List<ReceivedImageEntry> images,
|
||||
required int imageVersion,
|
||||
required int channelIndex,
|
||||
required int filterVersion,
|
||||
required FilterDisplayMode displayMode,
|
||||
required bool filterActive,
|
||||
required Set<String> revealedIds,
|
||||
}) {
|
||||
DateTime timeOf(int code) =>
|
||||
code >= 0 ? messages[code].receivedAt : images[-code - 1].firstSeen;
|
||||
@@ -2901,12 +3308,38 @@ class _TranscriptLayout {
|
||||
// the reversed ListView.
|
||||
mergeSort<int>(order, compare: (a, b) => timeOf(a).compareTo(timeOf(b)));
|
||||
final reversed = order.reversed.toList(growable: false);
|
||||
final ids = <String>[
|
||||
final rowInfos = <TranscriptRowInfo>[
|
||||
for (final code in reversed)
|
||||
code >= 0
|
||||
? messages[code].messageId
|
||||
: 'aeic:${images[-code - 1].streamId}',
|
||||
? TranscriptRowInfo(
|
||||
id: messages[code].messageId,
|
||||
receivedAt: messages[code].receivedAt,
|
||||
isFiltered: messages[code].isFiltered,
|
||||
)
|
||||
: TranscriptRowInfo(
|
||||
id: 'aeic:${images[-code - 1].streamId}',
|
||||
receivedAt: images[-code - 1].firstSeen,
|
||||
isImage: true,
|
||||
),
|
||||
];
|
||||
final entries = groupFilteredRows(
|
||||
rowInfos,
|
||||
mode: displayMode,
|
||||
revealedIds: revealedIds,
|
||||
filterActive: filterActive,
|
||||
sameDay: isSameChatDay,
|
||||
);
|
||||
final ids = <String>[for (final entry in entries) entry.id];
|
||||
final entryIdByRowId = <String, String>{};
|
||||
final entryByRow = entryIndexByRow(entries, rowInfos.length);
|
||||
for (var row = 0; row < rowInfos.length; row++) {
|
||||
final entry = entryByRow[row];
|
||||
if (entry == null) continue;
|
||||
final rowId = rowInfos[row].id;
|
||||
final entryId = ids[entry];
|
||||
// First occurrence wins, matching the GlobalKey ownership below.
|
||||
if (rowId != entryId) entryIdByRowId.putIfAbsent(rowId, () => entryId);
|
||||
}
|
||||
final seen = <String>{};
|
||||
final keyed = <int>{};
|
||||
for (var i = 0; i < ids.length; i++) {
|
||||
@@ -2919,26 +3352,65 @@ class _TranscriptLayout {
|
||||
messages.isEmpty ? null : messages.last.messageId,
|
||||
imageVersion,
|
||||
channelIndex,
|
||||
filterVersion,
|
||||
reversed,
|
||||
images,
|
||||
entries,
|
||||
filterActive,
|
||||
ids,
|
||||
entryIdByRowId,
|
||||
keyed,
|
||||
);
|
||||
}
|
||||
|
||||
int get length => _order.length;
|
||||
int get length => _entries.length;
|
||||
|
||||
bool matches(List<ChannelMessage> messages, int imageVersion, int channel) {
|
||||
bool matches(
|
||||
List<ChannelMessage> messages,
|
||||
int imageVersion,
|
||||
int channel,
|
||||
int filterVersion,
|
||||
) {
|
||||
return identical(messages, _source) &&
|
||||
messages.length == _sourceLength &&
|
||||
imageVersion == _imageVersion &&
|
||||
channel == _channelIndex &&
|
||||
filterVersion == _filterVersion &&
|
||||
(messages.isEmpty ? null : messages.first.messageId) == _firstId &&
|
||||
(messages.isEmpty ? null : messages.last.messageId) == _lastId;
|
||||
}
|
||||
|
||||
/// Id of the visible entry that shows the row with [rowId]: the row itself,
|
||||
/// its ghost, or (hide mode) the nearest visible row.
|
||||
String entryIdFor(String rowId) => _entryIdByRowId[rowId] ?? rowId;
|
||||
|
||||
/// Ids of every message in the revealed run containing [messageId], so
|
||||
/// "Hide" on one of them collapses the whole run; just [messageId] when it
|
||||
/// is not part of a revealed run.
|
||||
List<String> revealedRunIdsFor(
|
||||
String messageId,
|
||||
List<ChannelMessage> messages,
|
||||
) {
|
||||
for (final entry in _entries) {
|
||||
if (!entry.expanded) continue;
|
||||
final ids = [
|
||||
for (final row in entry.rows)
|
||||
if (_order[row] >= 0) messages[_order[row]].messageId,
|
||||
];
|
||||
if (ids.contains(messageId)) return ids;
|
||||
}
|
||||
return [messageId];
|
||||
}
|
||||
|
||||
_ChannelChatRow rowAt(int index, List<ChannelMessage> messages) {
|
||||
final code = _order[index];
|
||||
final entry = _entries[index];
|
||||
if (entry.isGhost) {
|
||||
return _ChannelChatRow(
|
||||
ghost: [for (final row in entry.rows) messages[_order[row]]],
|
||||
ghostExpanded: entry.expanded,
|
||||
);
|
||||
}
|
||||
final code = _order[entry.row];
|
||||
return code >= 0
|
||||
? _ChannelChatRow(message: messages[code])
|
||||
: _ChannelChatRow(image: _images[-code - 1]);
|
||||
|
||||
Reference in new issue
Block a user