don't send empty messages if only a subprofile trigger is sent

This commit is contained in:
Henry Hiles 2026-11-29 14:13:33 -05:00
commit 7435ecd5f0
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -1,4 +1,3 @@
import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:freezed_annotation/freezed_annotation.dart";
import "package:nexus/models/per_message_profiles.dart";
@ -13,9 +12,26 @@ class const RoomSubprofiles({
final StoredProfile? current,
final StoredProfile? synced,
}) with _$RoomSubprofiles {
StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull(
(profile) =>
profile.id.isNotEmpty &&
profile.triggers.any((trigger) => trigger.matches(text)),
);
// The subprofile [text] triggers, and what's left once its trigger is removed
({StoredProfile profile, String body})? triggerMatch(String text) =>
triggerOrder
.where((profile) => profile.id.isNotEmpty)
.expand(
(profile) => profile.triggers
.where((trigger) => trigger.matches(text))
.map(
(trigger) => (
profile: profile,
body: trigger.keepTrigger
? text
: text.substring(
trigger.prefix.length,
text.length - trigger.suffix.length,
),
),
),
)
.firstOrNull;
StoredProfile? triggeredBy(String text) => triggerMatch(text)?.profile;
}