add push rule settings

Fixes #126
This commit is contained in:
Henry Hiles 2026-10-31 15:22:13 -04:00
commit c9172e92eb
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -6,6 +6,8 @@ import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/push_rules.dart";
import "package:nexus/models/requests/update_push_rule.dart";
class AccountDataController extends Notifier<AccountData> {
@override
@ -24,24 +26,15 @@ class AccountDataController extends Notifier<AccountData> {
}
});
Future<void> setEnabledImagePacks(ImagePackRooms packs) async {
Future<void> setEnabledImagePacks(ImagePackRooms packs) {
ISet<(String, String)> keysOf(ImagePackRooms rooms) => rooms.rooms.entries
.expand((entry) => entry.value.keys.map((key) => (entry.key, key)))
.toISet();
final keys = keysOf(packs);
bool arrived(AccountData data) {
final current = keysOf(data.enabledImagePacks);
return current.length == keys.length && current.containsAll(keys);
}
final client = ref.read(ClientController.provider.notifier);
final completer = Completer<void>();
final removeListener = listenSelf((_, data) {
if (arrived(data) && !completer.isCompleted) completer.complete();
});
try {
final client = ref.read(ClientController.provider.notifier);
await Future.wait([
return _sendAndWait(
() => .wait([
if (state.imagePackRooms != null)
client.setAccountData(
.new(type: AccountData.imagePackRoomsKey, content: packs),
@ -49,8 +42,65 @@ class AccountDataController extends Notifier<AccountData> {
client.setAccountData(
.new(type: AccountData.legacyImagePackRoomsKey, content: packs),
),
]);
]),
(data) {
final current = keysOf(data.enabledImagePacks);
return current.length == keys.length && current.containsAll(keys);
},
);
}
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only
/// its actions and whether it's enabled.
Future<void> savePushRule(PushRuleKind kind, PushRule rule) {
UpdatePushRuleRequest request(UpdatePushRuleAction action) =>
.new(kind: kind, ruleId: rule.ruleId, action: action);
return _updatePushRules(
.new(switch (state.pushRules.global.find(kind, rule.ruleId)) {
null => [request(.put).copyWith(newContent: rule)],
final previous => [
if (previous.actions != rule.actions)
request(.putActions).copyWith(actions: rule.actions),
if (previous.enabled != rule.enabled)
request(rule.enabled ? .enable : .disable),
],
}),
(rules) => rules.find(kind, rule.ruleId) == rule,
);
}
Future<void> deletePushRule(PushRuleKind kind, String ruleId) =>
_updatePushRules(
.new([.new(kind: kind, ruleId: ruleId, action: .delete)]),
(rules) => rules.find(kind, ruleId) == null,
);
/// Sends [requests] in order, then waits until [arrived] is true for the
/// synced push rules.
Future<void> _updatePushRules(
IList<UpdatePushRuleRequest> requests,
bool Function(PushRuleset rules) arrived,
) => _sendAndWait(
() => .forEach(
requests,
ref.read(ClientController.provider.notifier).updatePushRule,
),
(data) => arrived(data.pushRules.global),
);
/// Runs [send], then waits until [arrived] is true for the synced data.
Future<void> _sendAndWait(
Future<void> Function() send,
bool Function(AccountData data) arrived,
) async {
final completer = Completer<void>();
final removeListener = listenSelf((_, data) {
if (arrived(data) && !completer.isCompleted) completer.complete();
});
try {
await send();
if (!arrived(state)) {
await completer.future.timeout(
const .new(seconds: 10),