Address message filter review findings

False positives
- Same-sender repeats need at least 3 words and 10 characters, so short
  replies ("ok", "test") repeated within 10 minutes stay visible.
- Repeated characters needs a run of 20+ of one non-digit character;
  "Noooooooo!", separators, a row of emoji and long numbers are chat.

Correctness
- Rules apply to stored messages: adding a rule hides matching messages
  already in the chat, and removing or disabling one shows again what only
  it was hiding. Other filter reasons are left alone. Also applied when
  history is loaded.
- The filter runs after mesh dedup, inside _addChannelMessage, so each
  message is evaluated and counted by the duplicate tracker once.
- Channels excluded from filtering no longer feed implicit clean examples
  to the classifier.
- Unread counting and the unread anchor treat filtered messages as normal
  when filtering is off for the channel.

Performance
- Classifier training runs in a background isolate (injectable trainer for
  widget tests); app start-up no longer waits for it.
- Reply previews look up the quoted message through an id index instead
  of scanning the channel per bubble.
- The latest-messages channel sort computes each channel's latest visible
  message once.

Cleanup
- Use MessageFilterText.isStructuredPayload instead of a screen copy.

Docs: message-filter.md explains how each check works, including the
learned classifier, and drops the outdated "new messages only" limit.
This commit is contained in:
zjs81 committed 2026-11-06 00:54:09 -07:00
1 parent 4862966e47
commit a9d369f42f
50 files changed
+703 -181

No files matched your search

@@ -371,4 +371,64 @@ void main() {
addTearDown(() => _filter.setChannelExcluded(channelId, false));
expect(_connector.latestVisibleChannelMessage(channel)?.text, text);
});
test('rule changes apply to messages already in the chat', () async {
await _connect();
const text = 'Cheap antennas at the swap meet, ask me';
_connector.handleFrameForTest(
ReviewFrames.channelMessage(
channelIndex: 2,
sender: 'Vendor',
message: text,
timestampSecs: _nowSecs(),
),
);
await _waitFor(() => _find(2, text) != null);
expect(_find(2, text)!.isFiltered, isFalse);
// A new rule hides the message that already arrived...
await _filter.addRule(const FilterRule(id: 'retro', pattern: 'antennas'));
expect(
_find(2, text)!.filterReason,
filterReasonToJson(FilterReason.textRule),
);
expect(_find(2, text)!.filterDetail, 'antennas');
// ...and deleting it shows the message again.
await _filter.removeRule('retro');
expect(_find(2, text)!.isFiltered, isFalse);
// Other filter reasons are not touched by rule changes.
await _connector.markChannelMessageSpam(_channelAt(2), _find(2, text)!);
await _filter.addRule(const FilterRule(id: 'retro2', pattern: 'swap'));
await _filter.removeRule('retro2');
expect(_find(2, text)!.filterReason, 'manual');
});
test('mesh copies of one message are stored and filtered once', () async {
await _connect();
const text = 'Weather net starts at seven tonight on the hill';
final ts = _nowSecs();
final cleanBefore = _filter.recentCleanCount;
_connector.handleFrameForTest(
ReviewFrames.channelMessage(
channelIndex: 2,
sender: 'Relay',
message: text,
timestampSecs: ts,
),
);
_connector.handleFrameForTest(_logRxGroupText(_psk(2), 'Relay: $text', ts));
_connector.handleFrameForTest(_logRxGroupText(_psk(2), 'Relay: $text', ts));
await _waitFor(() => _find(2, text) != null);
await Future<void>.delayed(const Duration(milliseconds: 100));
final copies = _connector
.getChannelMessages(_channelAt(2))
.where((m) => m.text == text)
.toList();
expect(copies, hasLength(1));
// Not mistaken for the sender repeating themselves.
expect(copies.single.isFiltered, isFalse);
expect(_filter.recentCleanCount, cleanBefore + 1);
});
}