asnasd/lib/controllers/account_data.dart

204 lines
6.7 KiB
Dart

import "dart:async";
import "dart:convert";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/pending_subprofiles.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/per_message_profiles.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/push_rules.dart";
import "package:nexus/models/requests/update_push_rule.dart";
import "package:nexus/models/room_notification_level.dart";
import "package:nexus/models/sync_data.dart";
class AccountDataController extends Notifier<AccountData> {
@override
AccountData build() => .new();
void update(IMap<String, IMap<String, dynamic>> newAccountData) =>
state = newAccountData.entries.fold(state, (data, entry) {
try {
return .fromJson({
...json.decode(json.encode(data.toJson())),
entry.key: entry.value["content"],
});
} catch (error) {
debugPrint("Ignoring invalid ${entry.key} account data: $error");
return data;
}
});
void handleSync(SyncData sync) {
if (sync.clearState) state = .new();
update(sync.accountData);
}
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);
final client = ref.read(ClientController.provider.notifier);
return _sendAndWait(
() => .wait([
if (state.imagePackRooms != null)
client.setAccountData(
.new(type: AccountData.imagePackRoomsKey, content: packs),
),
client.setAccountData(
.new(type: AccountData.legacyImagePackRoomsKey, content: packs),
),
]),
(data) {
final current = keysOf(data.enabledImagePacks);
return current.length == keys.length && current.containsAll(keys);
},
);
}
Future<void> setIgnored(String userId, {required bool ignored}) =>
_sendAndWait(
() => ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(
type: AccountData.ignoredUserListKey,
content: state.ignoredUserList.withUser(
userId,
ignored: ignored,
),
),
),
(data) => data.ignoredUserList.contains(userId) == ignored,
);
Future<void> setPerMessageProfiles(PerMessageProfiles profiles) =>
_sendAndWait(
() => ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(type: AccountData.perMessageProfilesKey, content: profiles),
),
(data) => data.perMessageProfiles == profiles,
);
Future<void> setRoomProfile(String roomId, String profileId) {
const type = AccountData.perMessageProfilesKey;
const key = "default_profile_id";
final current = ref
.read(RoomsController.provider)[roomId]
?.accountData[type];
return ref.read(PendingSubprofilesController.provider.notifier).track(
roomId,
profileId,
() async {
await ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(
type: type,
roomId: roomId,
content: {...?current, key: profileId},
),
);
await ref
.read(RoomsController.provider.notifier)
.awaitRooms(
(rooms) => rooms[roomId]?.accountData[type]?[key] == profileId,
);
},
);
}
/// 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,
);
/// Replaces the push rules for the room with [roomId] with those [level]
/// needs, deleting them first so disabled ones don't stay disabled.
Future<void> setRoomNotificationLevel(
String roomId,
RoomNotificationLevel level,
) async {
final rules = state.pushRules.global;
if (RoomNotificationLevel.of(rules, roomId) == level) return;
return _updatePushRules(
.new([
...[PushRuleKind.override, PushRuleKind.room]
.where((kind) => rules.find(kind, roomId) != null)
.map((kind) => .new(kind: kind, ruleId: roomId, action: .delete)),
if (level.ruleFor(roomId) case (:final kind, :final rule))
.new(kind: kind, ruleId: roomId, action: .put, newContent: rule),
]),
(rules) => RoomNotificationLevel.of(rules, roomId) == level,
);
}
/// 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),
onTimeout: () {},
);
}
} finally {
removeListener();
}
}
static final provider = NotifierProvider<AccountDataController, AccountData>(
AccountDataController.new,
);
}