fluffychat/lib/utils/account_bundles.dart

65 lines
1.7 KiB
Dart
Raw Normal View History

import 'package:matrix/matrix.dart';
class AccountBundles {
String prefix;
List<AccountBundle> bundles;
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,
if (bundles != null) 'bundles': bundles.map((v) => v.toJson()).toList(),
};
}
class AccountBundle {
String name;
int priority;
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 {
List<AccountBundle> ret;
if (accountData.containsKey(_accountBundlesType)) {
ret = AccountBundles.fromJson(accountData[_accountBundlesType].content)
.bundles;
}
ret ??= [];
if (ret.isEmpty) {
ret.add(AccountBundle(
name: userID,
priority: 0,
));
}
return ret;
}
}