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

+15
View File
@@ -59,6 +59,12 @@ class ChannelMessage {
final String? replyToText;
final Map<String, List<String?>> reactions;
/// Set when the message filter hid this message (a FilterReason JSON name).
final String? filterReason;
/// Extra context for [filterReason], e.g. the matching rule pattern.
final String? filterDetail;
ChannelMessage({
this.senderKey,
required this.senderName,
@@ -86,6 +92,8 @@ class ChannelMessage {
this.replyToSenderName,
this.replyToText,
Map<String, List<String?>>? reactions,
this.filterReason,
this.filterDetail,
}) : messageId =
messageId ??
'${timestamp.millisecondsSinceEpoch}_${senderName.hashCode}_${text.hashCode}',
@@ -97,6 +105,8 @@ class ChannelMessage {
pathVariants,
);
bool get isFiltered => filterReason != null;
String? get senderKeyHex =>
senderKey != null ? pubKeyToHex(senderKey!) : null;
@@ -125,6 +135,9 @@ class ChannelMessage {
Object? translationModelId = _unset,
Map<String, List<String?>>? reactions,
int? channelIndex,
String? filterReason,
String? filterDetail,
bool clearFilter = false,
}) {
return ChannelMessage(
senderKey: senderKey,
@@ -161,6 +174,8 @@ class ChannelMessage {
replyToSenderName: replyToSenderName ?? this.replyToSenderName,
replyToText: replyToText ?? this.replyToText,
reactions: reactions ?? this.reactions,
filterReason: clearFilter ? null : (filterReason ?? this.filterReason),
filterDetail: clearFilter ? null : (filterDetail ?? this.filterDetail),
);
}
+225
View File
@@ -0,0 +1,225 @@
/// Data types for the on-device channel message filter.
///
/// Everything here is plain data with JSON round-tripping; the matching logic
/// lives in `lib/services/message_filter/`.
library;
/// Which part of a message a [FilterRule] looks at.
enum FilterRuleField { text, sender, both }
/// How a [FilterRule] pattern is compared against the (normalized) target.
enum FilterRuleMatch { contains, wholeWord, exact }
/// How filtered messages are presented in the channel transcript.
enum FilterDisplayMode { placeholder, hide }
/// Why a message was filtered.
enum FilterReason {
senderRule,
textRule,
repeatedCharacters,
duplicate,
crossSenderDuplicate,
learned,
manual,
}
String filterReasonToJson(FilterReason reason) => reason.name;
FilterReason? filterReasonFromJson(String? value) {
if (value == null) return null;
for (final reason in FilterReason.values) {
if (reason.name == value) return reason;
}
return null;
}
T _enumFromName<T extends Enum>(List<T> values, Object? name, T fallback) {
if (name is! String) return fallback;
for (final value in values) {
if (value.name == name) return value;
}
return fallback;
}
class FilterRule {
final String id;
final String pattern;
final FilterRuleField field;
final FilterRuleMatch match;
final bool enabled;
const FilterRule({
required this.id,
required this.pattern,
this.field = FilterRuleField.text,
this.match = FilterRuleMatch.contains,
this.enabled = true,
});
FilterRule copyWith({
String? id,
String? pattern,
FilterRuleField? field,
FilterRuleMatch? match,
bool? enabled,
}) {
return FilterRule(
id: id ?? this.id,
pattern: pattern ?? this.pattern,
field: field ?? this.field,
match: match ?? this.match,
enabled: enabled ?? this.enabled,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'pattern': pattern,
'field': field.name,
'match': match.name,
'enabled': enabled,
};
factory FilterRule.fromJson(Map<String, dynamic> json) {
return FilterRule(
id: json['id'] as String? ?? '',
pattern: json['pattern'] as String? ?? '',
field: _enumFromName(
FilterRuleField.values,
json['field'],
FilterRuleField.text,
),
match: _enumFromName(
FilterRuleMatch.values,
json['match'],
FilterRuleMatch.contains,
),
enabled: json['enabled'] as bool? ?? true,
);
}
@override
bool operator ==(Object other) =>
other is FilterRule &&
other.id == id &&
other.pattern == pattern &&
other.field == field &&
other.match == match &&
other.enabled == enabled;
@override
int get hashCode => Object.hash(id, pattern, field, match, enabled);
}
/// A labelled training example for the learned classifier.
class FilterExample {
final String sender;
final String text;
final int addedAtMs;
const FilterExample({
required this.sender,
required this.text,
required this.addedAtMs,
});
Map<String, dynamic> toJson() => {
'sender': sender,
'text': text,
'addedAtMs': addedAtMs,
};
factory FilterExample.fromJson(Map<String, dynamic> json) {
return FilterExample(
sender: json['sender'] as String? ?? '',
text: json['text'] as String? ?? '',
addedAtMs: (json['addedAtMs'] as num?)?.toInt() ?? 0,
);
}
}
class MessageFilterSettings {
final bool enabled;
final FilterDisplayMode displayMode;
final bool duplicates;
final bool repeatedCharacters;
final bool learning;
final Set<String> excludedChannelIds;
const MessageFilterSettings({
this.enabled = true,
this.displayMode = FilterDisplayMode.placeholder,
this.duplicates = true,
this.repeatedCharacters = true,
this.learning = true,
this.excludedChannelIds = const <String>{},
});
static const MessageFilterSettings defaults = MessageFilterSettings();
MessageFilterSettings copyWith({
bool? enabled,
FilterDisplayMode? displayMode,
bool? duplicates,
bool? repeatedCharacters,
bool? learning,
Set<String>? excludedChannelIds,
}) {
return MessageFilterSettings(
enabled: enabled ?? this.enabled,
displayMode: displayMode ?? this.displayMode,
duplicates: duplicates ?? this.duplicates,
repeatedCharacters: repeatedCharacters ?? this.repeatedCharacters,
learning: learning ?? this.learning,
excludedChannelIds: excludedChannelIds ?? this.excludedChannelIds,
);
}
Map<String, dynamic> toJson() => {
'enabled': enabled,
'displayMode': displayMode.name,
'duplicates': duplicates,
'repeatedCharacters': repeatedCharacters,
'learning': learning,
'excludedChannelIds': excludedChannelIds.toList()..sort(),
};
factory MessageFilterSettings.fromJson(Map<String, dynamic> json) {
final excluded = json['excludedChannelIds'];
return MessageFilterSettings(
enabled: json['enabled'] as bool? ?? true,
displayMode: _enumFromName(
FilterDisplayMode.values,
json['displayMode'],
FilterDisplayMode.placeholder,
),
duplicates: json['duplicates'] as bool? ?? true,
repeatedCharacters: json['repeatedCharacters'] as bool? ?? true,
learning: json['learning'] as bool? ?? true,
excludedChannelIds: excluded is List
? excluded.whereType<String>().toSet()
: const <String>{},
);
}
}
/// The outcome of evaluating a message that should be filtered.
class FilterVerdict {
final FilterReason reason;
final String? detail;
const FilterVerdict({required this.reason, this.detail});
@override
bool operator ==(Object other) =>
other is FilterVerdict &&
other.reason == reason &&
other.detail == detail;
@override
int get hashCode => Object.hash(reason, detail);
@override
String toString() => 'FilterVerdict($reason, $detail)';
}