import 'package:matrix/matrix.dart'; class AccountBundles { String prefix; List bundles; AccountBundles({this.prefix, this.bundles}); AccountBundles.fromJson(Map json) : prefix = json.tryGet('prefix'), bundles = json['bundles'] is List ? json['bundles'] .map((b) { try { return AccountBundle.fromJson(b); } catch (_) { return null; } }) .whereType() .toList() : null; Map 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 json) : name = json.tryGet('name'), priority = json.tryGet('priority'); Map toJson() => { if (name != null) 'name': name, if (priority != null) 'priority': priority, }; } const _accountBundlesType = 'im.fluffychat.account_bundles'; extension AccountBundlesExtension on Client { List get accountBundles { List 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; } }