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:
zjs81 committed 2026-11-05 23:35:27 -07:00
1 parent 5ef2c2cd7d
commit 1332c35884
75 files changed
+12527 -254

No files matched your search

@@ -0,0 +1,57 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/models/channel_message.dart';
import 'package:meshcore_open/storage/channel_message_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
ChannelMessage _msg({String? reason, String? detail}) => ChannelMessage(
senderName: 'Alice',
text: 'hello',
timestamp: DateTime.fromMillisecondsSinceEpoch(1000),
isOutgoing: false,
messageId: 'm1',
filterReason: reason,
filterDetail: detail,
);
void main() {
test('isFiltered reflects filterReason', () {
expect(_msg().isFiltered, isFalse);
expect(_msg(reason: 'manual').isFiltered, isTrue);
});
test('copyWith sets, keeps and clears filter fields', () {
final filtered = _msg().copyWith(
filterReason: 'textRule',
filterDetail: 'spam',
);
expect(filtered.filterReason, 'textRule');
expect(filtered.filterDetail, 'spam');
final kept = filtered.copyWith(repeatCount: 2);
expect(kept.filterReason, 'textRule');
expect(kept.filterDetail, 'spam');
final cleared = filtered.copyWith(clearFilter: true);
expect(cleared.isFiltered, isFalse);
expect(cleared.filterDetail, isNull);
});
test('store round-trips filter fields and omits them when null', () async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
final store = ChannelMessageStore()..setPublicKeyHex = 'abcdef012345';
store.forgetArchive();
await store.saveChannelMessages('c1', [
_msg(reason: 'textRule', detail: 'spam'),
]);
var loaded = await store.loadChannelMessages('c1');
expect(loaded.single.filterReason, 'textRule');
expect(loaded.single.filterDetail, 'spam');
store.forgetArchive();
await store.saveChannelMessages('c2', [_msg()]);
loaded = await store.loadChannelMessages('c2');
expect(loaded.single.filterReason, isNull);
expect(loaded.single.filterDetail, isNull);
});
}