2021-12-03 17:29:32 +01:00
|
|
|
//@dart=2.12
|
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
|
|
|
class AccountBundles {
|
2021-12-03 17:29:32 +01:00
|
|
|
String? prefix;
|
|
|
|
List<AccountBundle>? bundles;
|
2021-09-19 13:48:23 +02:00
|
|
|
|
|
|
|
AccountBundles({this.prefix, this.bundles});
|
|
|
|
|
|
|
|
AccountBundles.fromJson(Map<String, dynamic> json)
|
|
|
|
: prefix = json.tryGet<String>('prefix'),
|
|
|
|
bundles = json['bundles'] is List
|
|
|
|
? json['bundles']
|
|
|
|
.map((b) {
|
|
|
|
try {
|
|
|
|
return AccountBundle.fromJson(b);
|
|
|
|
} catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.whereType<AccountBundle>()
|
|
|
|
.toList()
|
|
|
|
: null;
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
if (prefix != null) 'prefix': prefix,
|
2021-12-03 17:29:32 +01:00
|
|
|
if (bundles != null)
|
|
|
|
'bundles': bundles!.map((v) => v.toJson()).toList(),
|
2021-09-19 13:48:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccountBundle {
|
2021-12-03 17:29:32 +01:00
|
|
|
String? name;
|
|
|
|
int? priority;
|
2021-09-19 13:48:23 +02:00
|
|
|
|
|
|
|
AccountBundle({this.name, this.priority});
|
|
|
|
|
|
|
|
AccountBundle.fromJson(Map<String, dynamic> json)
|
|
|
|
: name = json.tryGet<String>('name'),
|
|
|
|
priority = json.tryGet<int>('priority');
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => <String, dynamic>{
|
|
|
|
if (name != null) 'name': name,
|
|
|
|
if (priority != null) 'priority': priority,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountBundlesType = 'im.fluffychat.account_bundles';
|
|
|
|
|
|
|
|
extension AccountBundlesExtension on Client {
|
|
|
|
List<AccountBundle> get accountBundles {
|
2021-12-03 17:29:32 +01:00
|
|
|
List<AccountBundle>? ret;
|
2021-09-19 13:48:23 +02:00
|
|
|
if (accountData.containsKey(accountBundlesType)) {
|
2021-12-03 17:29:32 +01:00
|
|
|
ret = AccountBundles.fromJson(accountData[accountBundlesType]!.content)
|
2021-09-19 13:48:23 +02:00
|
|
|
.bundles;
|
|
|
|
}
|
|
|
|
ret ??= [];
|
|
|
|
if (ret.isEmpty) {
|
|
|
|
ret.add(AccountBundle(
|
|
|
|
name: userID,
|
|
|
|
priority: 0,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:29:32 +01:00
|
|
|
Future<void> setAccountBundle(String name, [int? priority]) async {
|
2021-09-19 13:48:23 +02:00
|
|
|
final data =
|
|
|
|
AccountBundles.fromJson(accountData[accountBundlesType]?.content ?? {});
|
|
|
|
var foundBundle = false;
|
2021-12-03 17:29:32 +01:00
|
|
|
final bundles = data.bundles ??= [];
|
|
|
|
for (final bundle in bundles) {
|
2021-09-19 13:48:23 +02:00
|
|
|
if (bundle.name == name) {
|
|
|
|
bundle.priority = priority;
|
|
|
|
foundBundle = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundBundle) {
|
2021-12-03 17:29:32 +01:00
|
|
|
bundles.add(AccountBundle(name: name, priority: priority));
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
2021-12-03 17:29:32 +01:00
|
|
|
await setAccountData(userID!, accountBundlesType, data.toJson());
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeFromAccountBundle(String name) async {
|
|
|
|
if (!accountData.containsKey(accountBundlesType)) {
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
|
|
|
final data =
|
2021-12-03 17:29:32 +01:00
|
|
|
AccountBundles.fromJson(accountData[accountBundlesType]!.content);
|
2021-09-19 13:48:23 +02:00
|
|
|
if (data.bundles == null) return;
|
2021-12-03 17:29:32 +01:00
|
|
|
data.bundles!.removeWhere((b) => b.name == name);
|
|
|
|
await setAccountData(userID!, accountBundlesType, data.toJson());
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String get sendPrefix {
|
|
|
|
final data =
|
|
|
|
AccountBundles.fromJson(accountData[accountBundlesType]?.content ?? {});
|
2021-12-03 17:29:32 +01:00
|
|
|
return data.prefix!;
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
}
|