chore: strict casts
This commit is contained in:
parent
6a225e3b8c
commit
a525bb120a
|
|
@ -11,7 +11,7 @@ linter:
|
|||
|
||||
analyzer:
|
||||
language:
|
||||
strict-casts: false
|
||||
strict-casts: true
|
||||
strict-inference: true
|
||||
strict-raw-types: true
|
||||
errors:
|
||||
|
|
|
|||
|
|
@ -31,12 +31,12 @@ import 'package:matrix_api_lite/matrix_api_lite.dart';
|
|||
|
||||
Map<String, dynamic> decodeJson(dynamic data) {
|
||||
if (data is String) {
|
||||
return json.decode(data);
|
||||
return json.decode(data) as Map<String, dynamic>;
|
||||
}
|
||||
if (data is Map && data.isEmpty) {
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
return data;
|
||||
return data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
T? tryCast<T>(dynamic object) => object is T ? object : null;
|
||||
|
|
|
|||
|
|
@ -47,7 +47,10 @@ class MatrixApi extends Api {
|
|||
@override
|
||||
Never unexpectedResponse(http.BaseResponse response, Uint8List body) {
|
||||
if (response.statusCode >= 400 && response.statusCode < 500) {
|
||||
throw MatrixException.fromJson(json.decode(utf8.decode(body)));
|
||||
final resp = json.decode(utf8.decode(body));
|
||||
if (resp is Map<String, dynamic>) {
|
||||
throw MatrixException.fromJson(resp);
|
||||
}
|
||||
}
|
||||
super.unexpectedResponse(response, body);
|
||||
}
|
||||
|
|
@ -164,7 +167,7 @@ class MatrixApi extends Api {
|
|||
},
|
||||
},
|
||||
);
|
||||
return Map<String, int>.from(response['one_time_key_counts']);
|
||||
return Map<String, int>.from(response['one_time_key_counts'] as Map);
|
||||
}
|
||||
|
||||
/// This endpoint allows the creation, modification and deletion of pushers
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ class AuthenticationData {
|
|||
AuthenticationData({this.type, this.session});
|
||||
|
||||
AuthenticationData.fromJson(Map<String, dynamic> json)
|
||||
: type = json['type'],
|
||||
session = json['session'];
|
||||
: type = json['type'] as String?,
|
||||
session = json['session'] as String?;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class AuthenticationIdentifier {
|
|||
AuthenticationIdentifier({required this.type});
|
||||
|
||||
AuthenticationIdentifier.fromJson(Map<String, dynamic> json)
|
||||
: type = json['type'];
|
||||
: type = json['type'] as String;
|
||||
|
||||
factory AuthenticationIdentifier.subFromJson(Map<String, dynamic> json) {
|
||||
switch (json['type']) {
|
||||
|
|
|
|||
|
|
@ -41,8 +41,9 @@ class AuthenticationPassword extends AuthenticationData {
|
|||
);
|
||||
|
||||
AuthenticationPassword.fromJson(Map<String, dynamic> json)
|
||||
: password = json['password'],
|
||||
identifier = AuthenticationIdentifier.subFromJson(json['identifier']),
|
||||
: password = json['password'] as String,
|
||||
identifier = AuthenticationIdentifier.subFromJson(
|
||||
json['identifier'] as Map<String, dynamic>),
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ class AuthenticationPhoneIdentifier extends AuthenticationIdentifier {
|
|||
: super(type: AuthenticationIdentifierTypes.phone);
|
||||
|
||||
AuthenticationPhoneIdentifier.fromJson(Map<String, dynamic> json)
|
||||
: country = json['country'],
|
||||
phone = json['phone'],
|
||||
: country = json['country'] as String,
|
||||
phone = json['phone'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class AuthenticationRecaptcha extends AuthenticationData {
|
|||
);
|
||||
|
||||
AuthenticationRecaptcha.fromJson(Map<String, dynamic> json)
|
||||
: response = json['response'],
|
||||
: response = json['response'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ class AuthenticationThirdPartyIdentifier extends AuthenticationIdentifier {
|
|||
: super(type: AuthenticationIdentifierTypes.thirdParty);
|
||||
|
||||
AuthenticationThirdPartyIdentifier.fromJson(Map<String, dynamic> json)
|
||||
: medium = json['medium'],
|
||||
address = json['address'],
|
||||
: medium = json['medium'] as String,
|
||||
address = json['address'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -66,10 +66,10 @@ class ThreepidCreds {
|
|||
this.idAccessToken});
|
||||
|
||||
ThreepidCreds.fromJson(Map<String, dynamic> json)
|
||||
: sid = json['sid'],
|
||||
clientSecret = json['client_secret'],
|
||||
idServer = json['id_server'],
|
||||
idAccessToken = json['id_access_token'];
|
||||
: sid = json['sid'] as String,
|
||||
clientSecret = json['client_secret'] as String,
|
||||
idServer = json['id_server'] as String?,
|
||||
idAccessToken = json['id_access_token'] as String?;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@ class AuthenticationToken extends AuthenticationData {
|
|||
);
|
||||
|
||||
AuthenticationToken.fromJson(Map<String, dynamic> json)
|
||||
: token = json['token'],
|
||||
txnId = json['txn_id'],
|
||||
: token = json['token'] as String,
|
||||
txnId = json['txn_id'] as String?,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class AuthenticationUserIdentifier extends AuthenticationIdentifier {
|
|||
: super(type: AuthenticationIdentifierTypes.userId);
|
||||
|
||||
AuthenticationUserIdentifier.fromJson(Map<String, dynamic> json)
|
||||
: user = json['user'],
|
||||
: user = json['user'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ class BasicEvent {
|
|||
});
|
||||
|
||||
BasicEvent.fromJson(Map<String, dynamic> json)
|
||||
: type = json['type'],
|
||||
: type = json['type'] as String,
|
||||
content = (json['content'] as Map<String, dynamic>).copy();
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ class BasicEventWithSender extends BasicEvent {
|
|||
: super(type: type, content: content);
|
||||
|
||||
BasicEventWithSender.fromJson(Map<String, dynamic> json)
|
||||
: senderId = json['sender'],
|
||||
: senderId = json['sender'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class BasicRoomEvent extends BasicEvent {
|
|||
);
|
||||
|
||||
BasicRoomEvent.fromJson(Map<String, dynamic> json)
|
||||
: roomId = json['room_id'],
|
||||
: roomId = json['room_id'] as String?,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -39,8 +39,8 @@ class ChildrenState extends StrippedStateEvent {
|
|||
stateKey: stateKey);
|
||||
|
||||
ChildrenState.fromJson(Map<String, dynamic> json)
|
||||
: originServerTs =
|
||||
DateTime.fromMillisecondsSinceEpoch(json['origin_server_ts']),
|
||||
: originServerTs = DateTime.fromMillisecondsSinceEpoch(
|
||||
json['origin_server_ts'] as int),
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -78,15 +78,19 @@ class ImagePackContent {
|
|||
(e) => !['images', 'pack', 'emoticons', 'short'].contains(e.key))),
|
||||
pack = ImagePackPackContent.fromJson(
|
||||
json.tryGetMap<String, dynamic>('pack') ?? {}),
|
||||
images = json.tryGetMap<String, dynamic>('images')?.catchMap(
|
||||
(k, v) => MapEntry(k, ImagePackImageContent.fromJson(v))) ??
|
||||
images = json.tryGetMap<String, dynamic>('images')?.catchMap((k, v) =>
|
||||
MapEntry(
|
||||
k,
|
||||
ImagePackImageContent.fromJson(
|
||||
v as Map<String, dynamic>))) ??
|
||||
// the "emoticons" key needs a small migration on the key, ":string:" --> "string"
|
||||
json.tryGetMap<String, dynamic>('emoticons')?.catchMap((k, v) =>
|
||||
MapEntry(
|
||||
k.startsWith(':') && k.endsWith(':')
|
||||
? k.substring(1, k.length - 1)
|
||||
: k,
|
||||
ImagePackImageContent.fromJson(v))) ??
|
||||
ImagePackImageContent.fromJson(
|
||||
v as Map<String, dynamic>))) ??
|
||||
// the "short" key was still just a map from shortcode to mxc uri
|
||||
json.tryGetMap<String, String>('short')?.catchMap((k, v) =>
|
||||
MapEntry(
|
||||
|
|
@ -118,7 +122,7 @@ class ImagePackImageContent {
|
|||
ImagePackImageContent.fromJson(Map<String, dynamic> json)
|
||||
: _json = Map.fromEntries(json.entries
|
||||
.where((e) => !['url', 'body', 'info'].contains(e.key))),
|
||||
url = Uri.parse(json['url']),
|
||||
url = Uri.parse(json['url'] as String),
|
||||
body = json.tryGet('body'),
|
||||
info = json.tryGetMap<String, dynamic>('info'),
|
||||
usage = imagePackUsageFromJson(json.tryGetList<String>('usage'));
|
||||
|
|
|
|||
|
|
@ -49,7 +49,8 @@ class RoomEncryptedContent {
|
|||
// filter out invalid/incomplete CiphertextInfos
|
||||
ciphertextOlm = json
|
||||
.tryGet<Map<String, dynamic>>('ciphertext', TryGet.silent)
|
||||
?.catchMap((k, v) => MapEntry(k, CiphertextInfo.fromJson(v)));
|
||||
?.catchMap((k, v) => MapEntry(
|
||||
k, CiphertextInfo.fromJson(v as Map<String, dynamic>)));
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -81,8 +82,8 @@ class CiphertextInfo {
|
|||
int type;
|
||||
|
||||
CiphertextInfo.fromJson(Map<String, dynamic> json)
|
||||
: body = json['body'],
|
||||
type = json['type'];
|
||||
: body = json['body'] as String,
|
||||
type = json['type'] as int;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -50,13 +50,13 @@ class MatrixEvent extends StrippedStateEvent {
|
|||
stateKey: stateKey);
|
||||
|
||||
MatrixEvent.fromJson(Map<String, dynamic> json)
|
||||
: eventId = json['event_id'],
|
||||
roomId = json['room_id'],
|
||||
originServerTs =
|
||||
DateTime.fromMillisecondsSinceEpoch(json['origin_server_ts']),
|
||||
: eventId = json['event_id'] as String,
|
||||
roomId = json['room_id'] as String?,
|
||||
originServerTs = DateTime.fromMillisecondsSinceEpoch(
|
||||
json['origin_server_ts'] as int),
|
||||
unsigned = (json['unsigned'] as Map<String, dynamic>?)?.copy(),
|
||||
prevContent = (json['prev_content'] as Map<String, dynamic>?)?.copy(),
|
||||
redacts = json['redacts'],
|
||||
redacts = json['redacts'] as String?,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -65,12 +65,12 @@ class MatrixException implements Exception {
|
|||
|
||||
/// The unique identifier for this error.
|
||||
String get errcode =>
|
||||
raw['errcode'] ??
|
||||
raw['errcode'] as String? ??
|
||||
(requireAdditionalAuthentication ? 'M_FORBIDDEN' : 'M_UNKNOWN');
|
||||
|
||||
/// A human readable error description.
|
||||
String get errorMessage =>
|
||||
raw['error'] ??
|
||||
raw['error'] as String? ??
|
||||
(requireAdditionalAuthentication
|
||||
? 'Require additional authentication'
|
||||
: 'Unknown error');
|
||||
|
|
@ -79,7 +79,7 @@ class MatrixException implements Exception {
|
|||
http.Response? response;
|
||||
|
||||
MatrixException(http.Response this.response)
|
||||
: raw = json.decode(response.body);
|
||||
: raw = json.decode(response.body) as Map<String, dynamic>;
|
||||
|
||||
MatrixException.fromJson(Map<String, dynamic> content) : raw = content;
|
||||
|
||||
|
|
@ -91,11 +91,11 @@ class MatrixException implements Exception {
|
|||
(e) => e.toString() == 'MatrixError.${(raw["errcode"] ?? "")}',
|
||||
orElse: () => MatrixError.M_UNKNOWN);
|
||||
|
||||
int? get retryAfterMs => raw['retry_after_ms'];
|
||||
int? get retryAfterMs => raw['retry_after_ms'] as int?;
|
||||
|
||||
/// This is a session identifier that the client must pass back to the homeserver, if one is provided,
|
||||
/// in subsequent attempts to authenticate in the same API call.
|
||||
String? get session => raw['session'];
|
||||
String? get session => raw['session'] as String?;
|
||||
|
||||
/// Returns true if the server requires additional authentication.
|
||||
bool get requireAdditionalAuthentication => response != null
|
||||
|
|
@ -119,11 +119,12 @@ class MatrixException implements Exception {
|
|||
/// This section contains any information that the client will need to know in order to use a given type
|
||||
/// of authentication. For each authentication type presented, that type may be present as a key in this
|
||||
/// dictionary. For example, the public part of an OAuth client ID could be given here.
|
||||
Map<String, dynamic>? get authenticationParams => raw['params'];
|
||||
Map<String, dynamic>? get authenticationParams =>
|
||||
raw['params'] as Map<String, dynamic>?;
|
||||
|
||||
/// Returns the list of already completed authentication flows from previous requests.
|
||||
List<String> get completedAuthenticationFlows =>
|
||||
List<String>.from(raw['completed'] ?? []);
|
||||
List<String>.from(raw['completed'] as List<String>? ?? []);
|
||||
}
|
||||
|
||||
/// For each endpoint, a server offers one or more 'flows' that the client can use
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import '../utils/map_copy_extension.dart';
|
||||
import 'package:matrix_api_lite/matrix_api_lite.dart';
|
||||
|
||||
abstract class MatrixSignableKey {
|
||||
String userId;
|
||||
|
|
@ -39,14 +39,11 @@ abstract class MatrixSignableKey {
|
|||
|
||||
MatrixSignableKey.fromJson(Map<String, dynamic> json)
|
||||
: _json = json,
|
||||
userId = json['user_id'],
|
||||
keys = Map<String, String>.from(json['keys']),
|
||||
userId = json['user_id'] as String,
|
||||
keys = Map<String, String>.from(json['keys'] as Map<String, dynamic>),
|
||||
// we need to manually copy to ensure that our map is Map<String, Map<String, String>>
|
||||
signatures = json['signatures'] is Map
|
||||
? Map<String, Map<String, String>>.from((json['signatures'] as Map)
|
||||
.map((k, v) => MapEntry(k, Map<String, String>.from(v))))
|
||||
: null,
|
||||
unsigned = (json['unsigned'] as Map<String, dynamic>?)?.copy();
|
||||
signatures = json.tryGetMap<String, Map<String, String>>('signatures'),
|
||||
unsigned = json.tryGetMap<String, dynamic>('unsigned');
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = _json ?? <String, dynamic>{};
|
||||
|
|
@ -81,7 +78,7 @@ class MatrixCrossSigningKey extends MatrixSignableKey {
|
|||
|
||||
@override
|
||||
MatrixCrossSigningKey.fromJson(Map<String, dynamic> json)
|
||||
: usage = List<String>.from(json['usage']),
|
||||
: usage = json.tryGetList<String>('usage') ?? [],
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
@ -97,7 +94,7 @@ class MatrixDeviceKeys extends MatrixSignableKey {
|
|||
List<String> algorithms;
|
||||
|
||||
String? get deviceDisplayName =>
|
||||
unsigned != null ? unsigned!['device_display_name'] : null;
|
||||
unsigned?.tryGet<String>('device_display_name');
|
||||
|
||||
MatrixDeviceKeys(
|
||||
String userId,
|
||||
|
|
@ -113,8 +110,8 @@ class MatrixDeviceKeys extends MatrixSignableKey {
|
|||
|
||||
@override
|
||||
MatrixDeviceKeys.fromJson(Map<String, dynamic> json)
|
||||
: algorithms = (json['algorithms'] as List).cast<String>(),
|
||||
deviceId = json['device_id'],
|
||||
: algorithms = json.tryGetList<String>('algorithms') ?? [],
|
||||
deviceId = json['device_id'] as String,
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ class Presence extends BasicEventWithSender {
|
|||
PresenceContent presence;
|
||||
|
||||
Presence.fromJson(Map<String, dynamic> json)
|
||||
: presence = PresenceContent.fromJson(json['content']),
|
||||
: presence =
|
||||
PresenceContent.fromJson(json['content'] as Map<String, dynamic>),
|
||||
super.fromJson(json);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import '../generated/model.dart';
|
||||
import 'package:matrix_api_lite/matrix_api_lite.dart';
|
||||
|
||||
class PresenceContent {
|
||||
PresenceType presence;
|
||||
|
|
@ -32,9 +32,9 @@ class PresenceContent {
|
|||
PresenceContent.fromJson(Map<String, dynamic> json)
|
||||
: presence = PresenceType.values.firstWhere(
|
||||
(p) => p.toString().split('.').last == json['presence']),
|
||||
lastActiveAgo = json['last_active_ago'],
|
||||
statusMsg = json['status_msg'],
|
||||
currentlyActive = json['currently_active'];
|
||||
lastActiveAgo = json.tryGet<int>('last_active_ago'),
|
||||
statusMsg = json.tryGet<String>('status_msg'),
|
||||
currentlyActive = json.tryGet<bool>('currently_active');
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import 'package:matrix_api_lite/matrix_api_lite.dart';
|
||||
|
||||
class RoomKeysSingleKey {
|
||||
int firstMessageIndex;
|
||||
int forwardedCount;
|
||||
|
|
@ -34,10 +36,10 @@ class RoomKeysSingleKey {
|
|||
required this.sessionData});
|
||||
|
||||
RoomKeysSingleKey.fromJson(Map<String, dynamic> json)
|
||||
: firstMessageIndex = json['first_message_index'],
|
||||
forwardedCount = json['forwarded_count'],
|
||||
isVerified = json['is_verified'],
|
||||
sessionData = json['session_data'];
|
||||
: firstMessageIndex = json['first_message_index'] as int,
|
||||
forwardedCount = json['forwarded_count'] as int,
|
||||
isVerified = json['is_verified'] as bool,
|
||||
sessionData = json['session_data'] as Map<String, dynamic>;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -55,8 +57,8 @@ class RoomKeysRoom {
|
|||
RoomKeysRoom({required this.sessions});
|
||||
|
||||
RoomKeysRoom.fromJson(Map<String, dynamic> json)
|
||||
: sessions = (json['sessions'] as Map)
|
||||
.map((k, v) => MapEntry(k, RoomKeysSingleKey.fromJson(v)));
|
||||
: sessions = (json['sessions'] as Map<String, dynamic>).map((k, v) =>
|
||||
MapEntry(k, RoomKeysSingleKey.fromJson(v as Map<String, dynamic>)));
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -70,8 +72,9 @@ class RoomKeysUpdateResponse {
|
|||
int count;
|
||||
|
||||
RoomKeysUpdateResponse.fromJson(Map<String, dynamic> json)
|
||||
: etag = json['etag'], // synapse replies an int but docs say string?
|
||||
count = json['count'];
|
||||
: etag = json.tryGet<String>('etag') ??
|
||||
'', // synapse replies an int but docs say string?
|
||||
count = json.tryGet<int>('count') ?? 0;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -28,10 +28,10 @@ class RoomSummary {
|
|||
|
||||
RoomSummary.fromJson(Map<String, dynamic> json)
|
||||
: mHeroes = json['m.heroes'] != null
|
||||
? List<String>.from(json['m.heroes'])
|
||||
? List<String>.from(json['m.heroes'] as List)
|
||||
: null,
|
||||
mJoinedMemberCount = json['m.joined_member_count'],
|
||||
mInvitedMemberCount = json['m.invited_member_count'];
|
||||
mJoinedMemberCount = json['m.joined_member_count'] as int?,
|
||||
mInvitedMemberCount = json['m.invited_member_count'] as int?;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import 'basic_event_with_sender.dart';
|
||||
import 'package:matrix_api_lite/matrix_api_lite.dart';
|
||||
|
||||
class StrippedStateEvent extends BasicEventWithSender {
|
||||
String? stateKey;
|
||||
|
|
@ -34,7 +34,7 @@ class StrippedStateEvent extends BasicEventWithSender {
|
|||
: super(type: type, content: content, senderId: senderId);
|
||||
|
||||
StrippedStateEvent.fromJson(Map<String, dynamic> json)
|
||||
: stateKey = json['state_key'],
|
||||
: stateKey = json.tryGet<String>('state_key'),
|
||||
super.fromJson(json);
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -21,13 +21,7 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import 'basic_event.dart';
|
||||
import 'basic_event_with_sender.dart';
|
||||
import 'basic_room_event.dart';
|
||||
import 'matrix_event.dart';
|
||||
import 'presence.dart';
|
||||
import 'room_summary.dart';
|
||||
import 'stripped_state_event.dart';
|
||||
import 'package:matrix_api_lite/matrix_api_lite.dart';
|
||||
|
||||
class SyncUpdate {
|
||||
String nextBatch;
|
||||
|
|
@ -51,32 +45,34 @@ class SyncUpdate {
|
|||
});
|
||||
|
||||
SyncUpdate.fromJson(Map<String, dynamic> json)
|
||||
: nextBatch = json['next_batch'],
|
||||
rooms =
|
||||
json['rooms'] != null ? RoomsUpdate.fromJson(json['rooms']) : null,
|
||||
presence = ((json['presence'] as Map?)?['events'] as List?)
|
||||
?.map((i) => Presence.fromJson(i))
|
||||
: nextBatch = json['next_batch'] as String,
|
||||
rooms = (() {
|
||||
final temp = json.tryGetMap<String, dynamic>('rooms');
|
||||
return temp != null ? RoomsUpdate.fromJson(temp) : null;
|
||||
}()),
|
||||
presence = json
|
||||
.tryGetMap<String, List<dynamic>>('presence')?['events']
|
||||
?.map((i) => Presence.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
accountData = ((json['account_data'] as Map?)?['events'] as List?)
|
||||
?.map((i) => BasicEvent.fromJson(i))
|
||||
accountData = json
|
||||
.tryGetMap<String, List<dynamic>>('account_data')?['events']
|
||||
?.map((i) => BasicEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
toDevice = ((json['to_device'] as Map?)?['events'] as List?)
|
||||
?.map((i) => BasicEventWithSender.fromJson(i))
|
||||
toDevice = json
|
||||
.tryGetMap<String, List<dynamic>>('to_device')?['events']
|
||||
?.map(
|
||||
(i) => BasicEventWithSender.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
deviceLists = json['device_lists'] != null
|
||||
? DeviceListsUpdate.fromJson(json['device_lists'])
|
||||
: null,
|
||||
deviceOneTimeKeysCount = json['device_one_time_keys_count'] != null
|
||||
? Map<String, int>.from(json['device_one_time_keys_count'])
|
||||
: null,
|
||||
deviceUnusedFallbackKeyTypes = (json[
|
||||
'device_unused_fallback_key_types'] ??
|
||||
json[
|
||||
'org.matrix.msc2732.device_unused_fallback_key_types']) !=
|
||||
null
|
||||
? List<String>.from(json['device_unused_fallback_key_types'] ??
|
||||
json['org.matrix.msc2732.device_unused_fallback_key_types'])
|
||||
: null;
|
||||
deviceLists = (() {
|
||||
final temp = json.tryGetMap<String, dynamic>('device_lists');
|
||||
return temp != null ? DeviceListsUpdate.fromJson(temp) : null;
|
||||
}()),
|
||||
deviceOneTimeKeysCount =
|
||||
json.tryGetMap<String, int>('device_one_time_keys_count'),
|
||||
deviceUnusedFallbackKeyTypes =
|
||||
json.tryGetList<String>('device_unused_fallback_key_types') ??
|
||||
json.tryGetList<String>(
|
||||
'org.matrix.msc2732.device_unused_fallback_key_types');
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -126,18 +122,12 @@ class RoomsUpdate {
|
|||
});
|
||||
|
||||
RoomsUpdate.fromJson(Map<String, dynamic> json) {
|
||||
join = json['join'] != null
|
||||
? (json['join'] as Map)
|
||||
.map((k, v) => MapEntry(k, JoinedRoomUpdate.fromJson(v)))
|
||||
: null;
|
||||
invite = json['invite'] != null
|
||||
? (json['invite'] as Map)
|
||||
.map((k, v) => MapEntry(k, InvitedRoomUpdate.fromJson(v)))
|
||||
: null;
|
||||
leave = json['leave'] != null
|
||||
? (json['leave'] as Map)
|
||||
.map((k, v) => MapEntry(k, LeftRoomUpdate.fromJson(v)))
|
||||
: null;
|
||||
join = json.tryGetMap<String, dynamic>('join')?.catchMap((k, v) =>
|
||||
MapEntry(k, JoinedRoomUpdate.fromJson(v as Map<String, dynamic>)));
|
||||
invite = json.tryGetMap<String, dynamic>('invite')?.catchMap((k, v) =>
|
||||
MapEntry(k, InvitedRoomUpdate.fromJson(v as Map<String, dynamic>)));
|
||||
leave = json.tryGetMap<String, dynamic>('leave')?.catchMap((k, v) =>
|
||||
MapEntry(k, LeftRoomUpdate.fromJson(v as Map<String, dynamic>)));
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
|
|
@ -175,24 +165,22 @@ class JoinedRoomUpdate extends SyncRoomUpdate {
|
|||
});
|
||||
|
||||
JoinedRoomUpdate.fromJson(Map<String, dynamic> json)
|
||||
: summary = json['summary'] != null
|
||||
? RoomSummary.fromJson(json['summary'])
|
||||
: null,
|
||||
state = ((json['state'] as Map?)?['events'] as List?)
|
||||
?.map((i) => MatrixEvent.fromJson(i))
|
||||
: summary = json.tryGetFromJson('summary', RoomSummary.fromJson),
|
||||
state = json
|
||||
.tryGetMap<String, List<dynamic>>('state')?['events']
|
||||
?.map((i) => MatrixEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
timeline = json['timeline'] != null
|
||||
? TimelineUpdate.fromJson(json['timeline'])
|
||||
: null,
|
||||
ephemeral = ((json['ephemeral'] as Map?)?['events'] as List?)
|
||||
?.map((i) => BasicRoomEvent.fromJson(i))
|
||||
timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
|
||||
ephemeral = json
|
||||
.tryGetMap<String, List<dynamic>>('ephemeral')?['events']
|
||||
?.map((i) => BasicRoomEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
accountData = ((json['account_data'] as Map?)?['events'] as List?)
|
||||
?.map((i) => BasicRoomEvent.fromJson(i))
|
||||
accountData = json
|
||||
.tryGetMap<String, List<dynamic>>('account_data')?['events']
|
||||
?.map((i) => BasicRoomEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
unreadNotifications = json['unread_notifications'] != null
|
||||
? UnreadNotificationCounts.fromJson(json['unread_notifications'])
|
||||
: null;
|
||||
unreadNotifications = json.tryGetFromJson(
|
||||
'unread_notifications', UnreadNotificationCounts.fromJson);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -230,8 +218,9 @@ class InvitedRoomUpdate extends SyncRoomUpdate {
|
|||
InvitedRoomUpdate({this.inviteState});
|
||||
|
||||
InvitedRoomUpdate.fromJson(Map<String, dynamic> json)
|
||||
: inviteState = ((json['invite_state'] as Map?)?['events'] as List?)
|
||||
?.map((i) => StrippedStateEvent.fromJson(i))
|
||||
: inviteState = json
|
||||
.tryGetMap<String, List<dynamic>>('invite_state')?['events']
|
||||
?.map((i) => StrippedStateEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
|
|
@ -257,14 +246,14 @@ class LeftRoomUpdate extends SyncRoomUpdate {
|
|||
});
|
||||
|
||||
LeftRoomUpdate.fromJson(Map<String, dynamic> json)
|
||||
: state = ((json['state'] as Map?)?['events'] as List?)
|
||||
?.map((i) => MatrixEvent.fromJson(i))
|
||||
: state = json
|
||||
.tryGetMap<String, List<dynamic>>('state')?['events']
|
||||
?.map((i) => MatrixEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
timeline = json['timeline'] != null
|
||||
? TimelineUpdate.fromJson(json['timeline'])
|
||||
: null,
|
||||
accountData = ((json['account_data'] as Map?)?['events'] as List?)
|
||||
?.map((i) => BasicRoomEvent.fromJson(i))
|
||||
timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
|
||||
accountData = json
|
||||
.tryGetMap<String, List<dynamic>>('account_data')?['events']
|
||||
?.map((i) => BasicRoomEvent.fromJson(i as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
|
|
@ -297,13 +286,13 @@ class TimelineUpdate {
|
|||
this.prevBatch,
|
||||
});
|
||||
|
||||
TimelineUpdate.fromJson(Map<String, dynamic> json) {
|
||||
events = json['events'] != null
|
||||
? (json['events'] as List).map((i) => MatrixEvent.fromJson(i)).toList()
|
||||
: null;
|
||||
limited = json['limited'];
|
||||
prevBatch = json['prev_batch'];
|
||||
}
|
||||
TimelineUpdate.fromJson(Map<String, dynamic> json)
|
||||
: events = json
|
||||
.tryGetList<Map<String, dynamic>>('events')
|
||||
?.map((v) => MatrixEvent.fromJson(v))
|
||||
.toList(),
|
||||
limited = json.tryGet<bool>('limited'),
|
||||
prevBatch = json.tryGet<String>('prev_batch');
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -329,10 +318,9 @@ class UnreadNotificationCounts {
|
|||
this.highlightCount,
|
||||
});
|
||||
|
||||
UnreadNotificationCounts.fromJson(Map<String, dynamic> json) {
|
||||
highlightCount = json['highlight_count'];
|
||||
notificationCount = json['notification_count'];
|
||||
}
|
||||
UnreadNotificationCounts.fromJson(Map<String, dynamic> json)
|
||||
: highlightCount = json.tryGet<int>('highlight_count'),
|
||||
notificationCount = json.tryGet<int>('notification_count');
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
@ -355,10 +343,9 @@ class DeviceListsUpdate {
|
|||
this.left,
|
||||
});
|
||||
|
||||
DeviceListsUpdate.fromJson(Map<String, dynamic> json) {
|
||||
changed = List<String>.from(json['changed'] ?? []);
|
||||
left = List<String>.from(json['left'] ?? []);
|
||||
}
|
||||
DeviceListsUpdate.fromJson(Map<String, dynamic> json)
|
||||
: changed = json.tryGetList<String>('changed') ?? [],
|
||||
left = json.tryGetList<String>('left') ?? [];
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
|
|
|
|||
|
|
@ -100,6 +100,13 @@ extension TryGetMapExtension on Map<String, dynamic> {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
A? tryGetFromJson<A>(String key, A Function(Map<String, dynamic>) fromJson,
|
||||
[TryGet log = TryGet.optional]) {
|
||||
final value = tryGetMap<String, dynamic>(key, log);
|
||||
|
||||
return value != null ? fromJson(value) : null;
|
||||
}
|
||||
}
|
||||
|
||||
extension on StackTrace {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ void main() {
|
|||
'type': 'm.room.encryption',
|
||||
'unsigned': {'age': 1234}
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(MatrixEvent.fromJson(json!).parsedRoomEncryptionContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
|
|
@ -63,7 +63,7 @@ void main() {
|
|||
'type': 'm.room.encrypted',
|
||||
'unsigned': {'age': 1234}
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(MatrixEvent.fromJson(json!).parsedRoomEncryptedContent.toJson(),
|
||||
json['content']);
|
||||
json = <String, dynamic>{
|
||||
|
|
@ -84,7 +84,7 @@ void main() {
|
|||
'type': 'm.room.encrypted',
|
||||
'unsigned': {'age': 1234}
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(MatrixEvent.fromJson(json!).parsedRoomEncryptedContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
|
|
@ -98,7 +98,7 @@ void main() {
|
|||
},
|
||||
'type': 'm.room_key'
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(BasicEvent.fromJson(json!).parsedRoomKeyContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
|
|
@ -111,7 +111,7 @@ void main() {
|
|||
},
|
||||
'type': 'm.room_key_request'
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(BasicEvent.fromJson(json!).parsedRoomKeyRequestContent.toJson(),
|
||||
json['content']);
|
||||
json = <String, dynamic>{
|
||||
|
|
@ -128,7 +128,7 @@ void main() {
|
|||
},
|
||||
'type': 'm.room_key_request'
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(BasicEvent.fromJson(json!).parsedRoomKeyRequestContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
|
|
@ -148,7 +148,7 @@ void main() {
|
|||
},
|
||||
'type': 'm.forwarded_room_key'
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(BasicEvent.fromJson(json!).parsedForwardedRoomKeyContent.toJson(),
|
||||
json['content']);
|
||||
});
|
||||
|
|
@ -164,7 +164,7 @@ void main() {
|
|||
'recipient_keys': {'ed25519': '<our_ed25519_key>'},
|
||||
'keys': {'ed25519': '<sender_ed25519_key>'}
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>?;
|
||||
expect(OlmPlaintextPayload.fromJson(json!).toJson(), json);
|
||||
});
|
||||
test('Image Pack Content', () {
|
||||
|
|
@ -190,7 +190,7 @@ void main() {
|
|||
'org.custom': 'blah',
|
||||
},
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>;
|
||||
expect(BasicEvent.fromJson(json).parsedImagePackContent.toJson(),
|
||||
json['content']);
|
||||
|
||||
|
|
@ -205,7 +205,7 @@ void main() {
|
|||
},
|
||||
},
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>;
|
||||
expect(
|
||||
BasicEvent.fromJson(json)
|
||||
.parsedImagePackContent
|
||||
|
|
@ -223,7 +223,7 @@ void main() {
|
|||
},
|
||||
},
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>;
|
||||
expect(
|
||||
BasicEvent.fromJson(json)
|
||||
.parsedImagePackContent
|
||||
|
|
@ -242,7 +242,7 @@ void main() {
|
|||
},
|
||||
},
|
||||
};
|
||||
json = jsonDecode(jsonEncode(json));
|
||||
json = jsonDecode(jsonEncode(json)) as Map<String, dynamic>;
|
||||
expect(BasicEvent.fromJson(json).parsedImagePackContent.images['emote'],
|
||||
null);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue