Skip to content

Commit cf816ef

Browse files
committed
api [nfc]: Simplify move data parsing with null-checks
Signed-off-by: Zixuan James Li <[email protected]>
1 parent 40ccd50 commit cf816ef

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

lib/api/model/events.dart

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -811,27 +811,19 @@ class UpdateMessageMoveData {
811811
return null;
812812
}
813813

814-
if (origStreamId == null || newStreamId == null) {
814+
return UpdateMessageMoveData(
815815
// The `stream_id` field (aka origStreamId) is documented to be present on moves;
816816
// newStreamId should not be null either because it falls back to origStreamId.
817-
throw FormatException('Malformed UpdateMessageEvent: move but no origStreamId');
818-
}
819-
if (origTopic == null || newTopic == null) {
820-
// The `orig_subject` field (aka origTopic) is documented to be present on moves;
821-
// newTopic should not be null either because it falls back to origTopic.
822-
throw FormatException('Malformed UpdateMessageEvent: move but no origTopic');
823-
}
824-
if (propagateMode == null) {
817+
origStreamId: origStreamId!,
818+
newStreamId: newStreamId!,
819+
825820
// The `propagate_mode` field (aka propagateMode) is documented to be present on moves.
826-
throw FormatException('Malformed UpdateMessageEvent: move but no propagateMode');
827-
}
821+
propagateMode: propagateMode!,
828822

829-
return UpdateMessageMoveData(
830-
origStreamId: origStreamId,
831-
newStreamId: newStreamId,
832-
propagateMode: propagateMode,
833-
origTopic: origTopic,
834-
newTopic: newTopic,
823+
// The `orig_subject` field (aka origTopic) is documented to be present on moves;
824+
// newTopic should not be null either because it falls back to origTopic.
825+
origTopic: origTopic!,
826+
newTopic: newTopic!,
835827
);
836828
}
837829
}

test/model/message_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ void main() {
427427
'stream_id': 1,
428428
'new_stream_id': 2,
429429
'orig_subject': null,
430-
})).throws<FormatException>();
430+
})).throws();
431431
});
432432

433433
test('move but no subject or new_stream_id', () async {
@@ -441,15 +441,15 @@ void main() {
441441
await check(setupAndHandleEvent({ ...baseMoveJson,
442442
'stream_id': null,
443443
'new_stream_id': 2,
444-
})).throws<FormatException>();
444+
})).throws();
445445
});
446446

447447
test('move but no propagate_mode', () async {
448448
await check(setupAndHandleEvent({ ...baseMoveJson,
449449
'orig_subject': 'foo',
450450
'subject': 'bar',
451451
'propagate_mode': null,
452-
})).throws<FormatException>();
452+
})).throws();
453453
});
454454
});
455455

0 commit comments

Comments
 (0)