Skip to content

Commit 7e2474b

Browse files
committed
api: Update ChannelDeleteEvent to match new API changes
There are new changes made to `stream op: delete` event in server-10: - The `streams` field used to be an array of just-deleted channel objects, but now it is an array of objects each containing only ID of the just-deleted channels (this would crash the app). - The `streams` field is also deprecated and will be removed in a future release. - As a replacement to `streams`, `stream_ids` is introduced which is an array of the just-deleted channel IDs.
1 parent d49ee2a commit 7e2474b

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

lib/api/model/events.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,14 @@ class ChannelDeleteEvent extends ChannelEvent {
616616
@JsonKey(includeToJson: true)
617617
String get op => 'delete';
618618

619-
final List<ZulipStream> streams;
619+
final List<ZulipStreamId>? streams; // TODO(server-10): remove
620+
final List<int>? streamIds; // TODO(server-10): remove nullability
620621

621-
ChannelDeleteEvent({required super.id, required this.streams});
622+
ChannelDeleteEvent({
623+
required super.id,
624+
this.streams,
625+
required this.streamIds,
626+
}) : assert(streams != null || streamIds != null);
622627

623628
factory ChannelDeleteEvent.fromJson(Map<String, dynamic> json) =>
624629
_$ChannelDeleteEventFromJson(json);
@@ -627,6 +632,18 @@ class ChannelDeleteEvent extends ChannelEvent {
627632
Map<String, dynamic> toJson() => _$ChannelDeleteEventToJson(this);
628633
}
629634

635+
@JsonSerializable(fieldRename: FieldRename.snake)
636+
class ZulipStreamId {
637+
final int streamId;
638+
639+
ZulipStreamId({required this.streamId});
640+
641+
factory ZulipStreamId.fromJson(Map<String, dynamic> json) =>
642+
_$ZulipStreamIdFromJson(json);
643+
644+
Map<String, dynamic> toJson() => _$ZulipStreamIdToJson(this);
645+
}
646+
630647
/// A [ChannelEvent] with op `update`: https://zulip.com/api/get-events#stream-update
631648
@JsonSerializable(fieldRename: FieldRename.snake)
632649
class ChannelUpdateEvent extends ChannelEvent {

lib/api/model/events.g.dart

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/model/channel.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,16 @@ class ChannelStoreImpl extends HasUserStore with ChannelStore {
398398
// details will come in a later `subscription` event.)
399399

400400
case ChannelDeleteEvent():
401-
for (final stream in event.streams) {
402-
assert(identical(streams[stream.streamId], streamsByName[stream.name]));
403-
assert(subscriptions[stream.streamId] == null
404-
|| identical(subscriptions[stream.streamId], streams[stream.streamId]));
405-
streams.remove(stream.streamId);
406-
streamsByName.remove(stream.name);
407-
subscriptions.remove(stream.streamId);
401+
final channelIds = event.streamIds ?? event.streams!.map((e) => e.streamId);
402+
for (final channelId in channelIds) {
403+
final channel = streams[channelId];
404+
if (channel == null) break;
405+
assert(identical(streams[channel.streamId], streamsByName[channel.name]));
406+
assert(subscriptions[channelId] == null
407+
|| identical(subscriptions[channelId], streams[channelId]));
408+
streams.remove(channel.streamId);
409+
streamsByName.remove(channel.name);
410+
subscriptions.remove(channel.streamId);
408411
}
409412

410413
case ChannelUpdateEvent():

test/model/store_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ void main() {
862862
// Then prepare an event on which handleEvent will throw
863863
// because it hits that broken invariant.
864864
connection.prepare(json: GetEventsResult(events: [
865-
ChannelDeleteEvent(id: 1, streams: [stream]),
865+
ChannelDeleteEvent(id: 1, streamIds: [stream.streamId]),
866866
], queueId: null).toJson());
867867
}
868868

0 commit comments

Comments
 (0)