forked from Nexus/nexus
don't send empty messages if only a subprofile trigger is sent
This commit is contained in:
parent
9f2d223809
commit
7435ecd5f0
2 changed files with 46 additions and 18 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import "package:collection/collection.dart";
|
|
||||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||||
import "package:freezed_annotation/freezed_annotation.dart";
|
import "package:freezed_annotation/freezed_annotation.dart";
|
||||||
import "package:nexus/models/per_message_profiles.dart";
|
import "package:nexus/models/per_message_profiles.dart";
|
||||||
|
|
@ -13,9 +12,26 @@ class const RoomSubprofiles({
|
||||||
final StoredProfile? current,
|
final StoredProfile? current,
|
||||||
final StoredProfile? synced,
|
final StoredProfile? synced,
|
||||||
}) with _$RoomSubprofiles {
|
}) with _$RoomSubprofiles {
|
||||||
StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull(
|
// The subprofile [text] triggers, and what's left once its trigger is removed
|
||||||
(profile) =>
|
({StoredProfile profile, String body})? triggerMatch(String text) =>
|
||||||
profile.id.isNotEmpty &&
|
triggerOrder
|
||||||
profile.triggers.any((trigger) => trigger.matches(text)),
|
.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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -436,18 +436,30 @@ class const Composer(
|
||||||
attachment != null && attachment.$2 == null) {
|
attachment != null && attachment.$2 == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ref.read(SettingsController.provider).value case Settings(
|
final subprofiles = ref.read(SubprofilesController.provider(roomId));
|
||||||
subprofiles: true,
|
final match = subprofiles.triggerMatch(controller.text);
|
||||||
latchSubprofiles: true,
|
void switchSubprofile(String id) {
|
||||||
)) {
|
if (id == subprofiles.current?.id) return;
|
||||||
final subprofiles = ref.read(SubprofilesController.provider(roomId));
|
ref
|
||||||
if (subprofiles.triggeredBy(controller.text) case final triggered?
|
.read(AccountDataController.provider.notifier)
|
||||||
when triggered.id != subprofiles.current?.id) {
|
.setRoomProfile(roomId, id)
|
||||||
ref
|
.onError(showError);
|
||||||
.read(AccountDataController.provider.notifier)
|
}
|
||||||
.setRoomProfile(roomId, triggered.id)
|
|
||||||
.onError(showError);
|
// A trigger on its own would send an empty message, so it switches to
|
||||||
}
|
// its subprofile instead
|
||||||
|
if (match != null && match.body.trim().isEmpty && attachment == null) {
|
||||||
|
switchSubprofile(match.profile.id);
|
||||||
|
controller.text = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// gomuks sends this message as the triggered subprofile itself, this
|
||||||
|
// keeps sending as it afterwards
|
||||||
|
if (ref.read(SettingsController.provider).value
|
||||||
|
case Settings(subprofiles: true, latchSubprofiles: true)
|
||||||
|
when match != null) {
|
||||||
|
switchSubprofile(match.profile.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
onSend(
|
onSend(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue