Fix false repeat flags, add repeat-sends setting, limit holiday animations

Message filter:
- Ignore copies of a message already seen (same sender, text and send
  second). Switching between companions re-synced messages the first one
  had delivered; they missed per-radio storage dedup and were flagged as
  repeats.
- Exempt replies and @[name] mentions from repeat detection.
- New "Sends to count as a repeat" setting (2-5, default 2). Each send
  must fall within 10 minutes of the previous one.
- Translations for the new strings in all locales.

Holiday animations:
- Only one plays at a time; greetings arriving while one is playing or
  within 5 seconds after it ends are dropped, not queued.
- Christmas map "self" marker: Santa hat was upside down.
This commit is contained in:
zjs81 committed 2026-11-08 01:33:05 -07:00
1 parent 59e571fb20
commit ddf0b9f553
52 files changed
+526 -24

No files matched your search

@@ -12,7 +12,17 @@ class _SeenMessage {
/// When this device saw it (local clock). Only used to forget entries.
final DateTime receivedAt;
const _SeenMessage(this.sender, this.key, this.sentAt, this.receivedAt);
/// How many sends of this text by this sender, each within the
/// same-sender window of the previous one, end with this one (1 = first).
final int sends;
const _SeenMessage(
this.sender,
this.key,
this.sentAt,
this.receivedAt, {
this.sends = 1,
});
}
/// In-memory, per-channel tracker of recently seen channel texts used to catch
@@ -28,6 +38,16 @@ class _SeenMessage {
/// so a sender whose clock is far off cannot push everyone else's entries
/// out early or keep its own around forever.
///
/// A copy with the same sender, text and send second as an entry already
/// recorded is the same message seen again (another companion syncing it,
/// a mesh repeat that slipped past storage dedup), never a repeat, and is
/// not recorded twice.
///
/// The same-sender rule flags the [sameSenderSends]th send of a text in a
/// row, each send within [sameSenderWindow] of the previous one (2 = the
/// first re-send). Higher values tolerate people re-sending when they see
/// no repeat or reply.
///
/// The same-sender rule only applies to texts of at least
/// [sameSenderMinWords] words and [sameSenderMinLength] characters: people
/// naturally repeat short replies ("ok", "yes", "test" when checking reach),
@@ -42,6 +62,7 @@ class DuplicateTracker {
this.sameSenderWindow = const Duration(minutes: 10),
this.sameSenderMinLength = 10,
this.sameSenderMinWords = 3,
this.sameSenderSends = 2,
this.crossSenderWindow = const Duration(minutes: 30),
this.crossSenderMinSenders = 3,
this.crossSenderMinLength = 10,
@@ -54,6 +75,10 @@ class DuplicateTracker {
/// Minimum length / word count for the same-sender rule (see class docs).
final int sameSenderMinLength;
final int sameSenderMinWords;
/// Which send in a row of the same text is flagged; can be overridden per
/// call so a settings change applies immediately.
final int sameSenderSends;
final Duration crossSenderWindow;
final int crossSenderMinSenders;
final int crossSenderMinLength;
@@ -95,7 +120,11 @@ class DuplicateTracker {
required String text,
required DateTime at,
DateTime? receivedAt,
int? sameSenderSends,
}) {
final flagAt = (sameSenderSends ?? this.sameSenderSends) < 2
? 2
: (sameSenderSends ?? this.sameSenderSends);
final arrived = receivedAt ?? at;
final key = MessageFilterText.normalizeForDuplicates(text);
if (key.isEmpty) return null;
@@ -103,21 +132,38 @@ class DuplicateTracker {
final entries = _seen.putIfAbsent(channelId, ListQueue<_SeenMessage>.new);
_prune(entries, arrived);
final atSecond = _second(at);
for (final entry in entries) {
if (entry.key == key &&
entry.sender == senderKey &&
_second(entry.sentAt) == atSecond) {
// The same message seen again, not a new send.
return null;
}
}
// The longest run of sends this one continues: earlier sends of this
// text by this sender within the window, each already carrying its run.
var sends = 1;
for (final entry in entries) {
if (entry.key != key || entry.sender != senderKey) continue;
if (entry.sentAt.difference(at).abs() <= sameSenderWindow &&
entry.sends + 1 > sends) {
sends = entry.sends + 1;
}
}
FilterReason? result;
if (!isAllowed(channelId, text)) {
final senders = <String>{senderKey};
var sameSender = false;
for (final entry in entries) {
if (entry.key != key) continue;
if (entry.key != key || entry.sender == senderKey) continue;
// Absolute gap between send times: delivery order does not matter.
final gap = entry.sentAt.difference(at).abs();
if (entry.sender == senderKey) {
if (gap <= sameSenderWindow) sameSender = true;
} else if (gap <= crossSenderWindow) {
if (entry.sentAt.difference(at).abs() <= crossSenderWindow) {
senders.add(entry.sender);
}
}
if (sameSender &&
if (sends >= flagAt &&
key.length >= sameSenderMinLength &&
_wordCount(key) >= sameSenderMinWords) {
result = FilterReason.duplicate;
@@ -128,11 +174,13 @@ class DuplicateTracker {
}
}
entries.addLast(_SeenMessage(senderKey, key, at, arrived));
entries.addLast(_SeenMessage(senderKey, key, at, arrived, sends: sends));
if (entries.length > maxEntriesPerChannel) entries.removeFirst();
return result;
}
static int _second(DateTime time) => time.millisecondsSinceEpoch ~/ 1000;
/// [key] is already whitespace-collapsed by `normalizeForDuplicates`.
static int _wordCount(String key) => key.isEmpty ? 0 : key.split(' ').length;
@@ -139,6 +139,10 @@ class MessageFilterService extends ChangeNotifier {
/// [sentAt] is the message's own timestamp. Duplicate windows are measured
/// between send times, so history synced in a burst after reconnecting is
/// not mistaken for a flood; without it the local clock is used.
///
/// Messages addressed to someone ([isReply], or an `@[name]` mention in
/// [text]) are exempt from duplicate detection and not recorded for it:
/// asking several people the same thing is conversation, not a repeat.
FilterVerdict? evaluate({
required String channelId,
required String sender,
@@ -146,6 +150,7 @@ class MessageFilterService extends ChangeNotifier {
DateTime? sentAt,
bool isOutgoing = false,
bool isReaction = false,
bool isReply = false,
}) {
if (!isChannelFiltered(channelId)) return null;
if (isOutgoing || isReaction) return null;
@@ -153,13 +158,17 @@ class MessageFilterService extends ChangeNotifier {
if (MessageFilterText.isStructuredPayload(text)) return null;
// Always record (even when a rule matches) so floods keep being caught.
final duplicateReason = _tracker.check(
channelId: channelId,
sender: sender,
text: text,
at: sentAt ?? _clock(),
receivedAt: _clock(),
);
final mentionsSomeone = isReply || MessageFilterText.hasMention(text);
final duplicateReason = mentionsSomeone
? null
: _tracker.check(
channelId: channelId,
sender: sender,
text: text,
at: sentAt ?? _clock(),
receivedAt: _clock(),
sameSenderSends: _settings.repeatSends,
);
final byRule = _matchRules(sender, text);
if (byRule != null) return byRule;
@@ -141,6 +141,9 @@ class MessageFilterText {
return _fold(text).replaceAll(_whitespace, ' ').trim();
}
/// True when [text] contains an `@[name]` mention.
static bool hasMention(String text) => _mention.hasMatch(text);
/// Looser form used to detect repeated messages: also drops `@[name]`
/// mentions, punctuation and emoji so trivial variations still match.
static String normalizeForDuplicates(String text) {