Skip to content

Commit 9c6cbbd

Browse files
Merge pull request #2215 from famedly/krille/refactor-info-map
refactor: (BREAKING) Event info map
2 parents 14f0119 + eeb2154 commit 9c6cbbd

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

lib/src/event.dart

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,13 @@ class Event extends MatrixEvent {
519519
}
520520

521521
/// Gets the info map of file events, or a blank map if none present
522-
Map get infoMap =>
522+
Map<String, Object?> get infoMap =>
523523
content.tryGetMap<String, Object?>('info') ?? <String, Object?>{};
524524

525525
/// Gets the thumbnail info map of file events, or a blank map if nonepresent
526-
Map get thumbnailInfoMap => infoMap['thumbnail_info'] is Map
527-
? infoMap['thumbnail_info']
528-
: <String, dynamic>{};
526+
Map<String, Object?> get thumbnailInfoMap =>
527+
infoMap.tryGetMap<String, Object?>('thumbnail_info') ??
528+
<String, Object?>{};
529529

530530
/// Returns if a file event has an attachment
531531
bool get hasAttachment => content['url'] is String || content['file'] is Map;
@@ -541,20 +541,22 @@ class Event extends MatrixEvent {
541541
bool get isThumbnailEncrypted => infoMap['thumbnail_file'] is Map;
542542

543543
/// Gets the mimetype of the attachment of a file event, or a blank string if not present
544-
String get attachmentMimetype => infoMap['mimetype'] is String
545-
? infoMap['mimetype'].toLowerCase()
546-
: (content
547-
.tryGetMap<String, Object?>('file')
548-
?.tryGet<String>('mimetype') ??
549-
'');
544+
String get attachmentMimetype =>
545+
infoMap.tryGet<String>('mimetype')?.toLowerCase() ??
546+
content
547+
.tryGetMap<String, Object?>('file')
548+
?.tryGet<String>('mimetype')
549+
?.toLowerCase() ??
550+
'';
550551

551552
/// Gets the mimetype of the thumbnail of a file event, or a blank string if not present
552-
String get thumbnailMimetype => thumbnailInfoMap['mimetype'] is String
553-
? thumbnailInfoMap['mimetype'].toLowerCase()
554-
: (infoMap['thumbnail_file'] is Map &&
555-
infoMap['thumbnail_file']['mimetype'] is String
556-
? infoMap['thumbnail_file']['mimetype']
557-
: '');
553+
String get thumbnailMimetype =>
554+
thumbnailInfoMap.tryGet<String>('mimetype')?.toLowerCase() ??
555+
infoMap
556+
.tryGetMap<String, Object?>('thumbnail_file')
557+
?.tryGet<String>('mimetype')
558+
?.toLowerCase() ??
559+
'';
558560

559561
/// Gets the underlying mxc url of an attachment of a file event, or null if not present
560562
Uri? get attachmentMxcUrl {
@@ -565,19 +567,23 @@ class Event extends MatrixEvent {
565567
}
566568

567569
/// Gets the underlying mxc url of a thumbnail of a file event, or null if not present
568-
Uri? get thumbnailMxcUrl {
569-
final url = isThumbnailEncrypted
570-
? infoMap['thumbnail_file']['url']
571-
: infoMap['thumbnail_url'];
572-
return url is String ? Uri.tryParse(url) : null;
573-
}
570+
Uri? get thumbnailMxcUrl => Uri.tryParse(
571+
isThumbnailEncrypted
572+
? (infoMap
573+
.tryGetMap<String, Object?>('thumbnail_file')
574+
?.tryGet<String>('url') ??
575+
'')
576+
: (infoMap.tryGet<String>('thumbnail_url') ?? ''),
577+
);
574578

575579
/// Gets the mxc url of an attachment/thumbnail of a file event, taking sizes into account, or null if not present
576580
Uri? attachmentOrThumbnailMxcUrl({bool getThumbnail = false}) {
581+
final fileSize = infoMap.tryGet<int>('size');
582+
final thumbnailFileSize = thumbnailInfoMap.tryGet<int>('size');
577583
if (getThumbnail &&
578-
infoMap['size'] is int &&
579-
thumbnailInfoMap['size'] is int &&
580-
infoMap['size'] <= thumbnailInfoMap['size']) {
584+
fileSize != null &&
585+
thumbnailFileSize != null &&
586+
fileSize <= thumbnailFileSize) {
581587
getThumbnail = false;
582588
}
583589
if (getThumbnail && !hasThumbnail) {
@@ -620,13 +626,17 @@ class Event extends MatrixEvent {
620626
return null; // can't fetch from thumbnail
621627
}
622628
final thisInfoMap = useThumbnailMxcUrl ? thumbnailInfoMap : infoMap;
623-
final thisMxcUrl =
624-
useThumbnailMxcUrl ? infoMap['thumbnail_url'] : content['url'];
629+
final thisMxcUrl = useThumbnailMxcUrl
630+
? infoMap.tryGet<String>('thumbnail_url')
631+
: content.tryGet<String>('url');
632+
if (thisMxcUrl == null) return null;
633+
625634
// if we have as method scale, we can return safely the original image, should it be small enough
635+
final thisInfoMapSize = thisInfoMap.tryGet<int>('size');
626636
if (getThumbnail &&
627637
method == ThumbnailMethod.scale &&
628-
thisInfoMap['size'] is int &&
629-
thisInfoMap['size'] < minNoThumbSize) {
638+
thisInfoMapSize != null &&
639+
thisInfoMapSize < minNoThumbSize) {
630640
getThumbnail = false;
631641
}
632642
// now generate the actual URLs
@@ -675,13 +685,16 @@ class Event extends MatrixEvent {
675685
return null; // can't fetch from thumbnail
676686
}
677687
final thisInfoMap = useThumbnailMxcUrl ? thumbnailInfoMap : infoMap;
678-
final thisMxcUrl =
679-
useThumbnailMxcUrl ? infoMap['thumbnail_url'] : content['url'];
688+
final thisMxcUrl = useThumbnailMxcUrl
689+
? infoMap.tryGet<String>('thumbnail_url')
690+
: content.tryGet<String>('url');
691+
if (thisMxcUrl == null) return null;
680692
// if we have as method scale, we can return safely the original image, should it be small enough
693+
final thisInfoMapSize = thisInfoMap.tryGet<int>('size');
681694
if (getThumbnail &&
682695
method == ThumbnailMethod.scale &&
683-
thisInfoMap['size'] is int &&
684-
thisInfoMap['size'] < minNoThumbSize) {
696+
thisInfoMapSize != null &&
697+
thisInfoMapSize < minNoThumbSize) {
685698
getThumbnail = false;
686699
}
687700
// now generate the actual URLs
@@ -712,8 +725,9 @@ class Event extends MatrixEvent {
712725
final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap;
713726
final database = room.client.database;
714727

715-
final storeable = thisInfoMap['size'] is int &&
716-
thisInfoMap['size'] <= database.maxFileSize;
728+
final thisInfoMapSize = thisInfoMap.tryGet<int>('size');
729+
final storeable =
730+
thisInfoMapSize != null && thisInfoMapSize <= database.maxFileSize;
717731

718732
Uint8List? uint8list;
719733
if (storeable) {
@@ -758,8 +772,9 @@ class Event extends MatrixEvent {
758772

759773
// Is this file storeable?
760774
final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap;
761-
var storeable = thisInfoMap['size'] is int &&
762-
thisInfoMap['size'] <= database.maxFileSize;
775+
final thisInfoMapSize = thisInfoMap.tryGet<int>('size');
776+
var storeable =
777+
thisInfoMapSize != null && thisInfoMapSize <= database.maxFileSize;
763778

764779
Uint8List? uint8list;
765780
if (storeable) {
@@ -794,16 +809,24 @@ class Event extends MatrixEvent {
794809

795810
// Decrypt the file
796811
if (isEncrypted) {
797-
final fileMap =
798-
getThumbnail ? infoMap['thumbnail_file'] : content['file'];
799-
if (!fileMap['key']['key_ops'].contains('decrypt')) {
812+
final fileMap = getThumbnail
813+
? infoMap.tryGetMap<String, Object?>('thumbnail_file')
814+
: content.tryGetMap<String, Object?>('file');
815+
if (fileMap == null) throw ('No encrypted file info found');
816+
if (fileMap
817+
.tryGetMap<String, Object?>('key')
818+
?.tryGetList<String>('key_ops')
819+
?.contains('decrypt') !=
820+
true) {
800821
throw ("Missing 'decrypt' in 'key_ops'.");
801822
}
802823
final encryptedFile = EncryptedFile(
803824
data: uint8list,
804-
iv: fileMap['iv'],
805-
k: fileMap['key']['k'],
806-
sha256: fileMap['hashes']['sha256'],
825+
iv: fileMap.tryGet<String>('iv')!,
826+
k: fileMap.tryGetMap<String, Object?>('key')!.tryGet<String>('k')!,
827+
sha256: fileMap
828+
.tryGetMap<String, Object?>('hashes')!
829+
.tryGet<String>('sha256')!,
807830
);
808831
uint8list =
809832
await room.client.nativeImplementations.decryptFile(encryptedFile);

0 commit comments

Comments
 (0)