Skip to content

Commit b31c144

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 which used to be an array of the just-deleted channel objects is now an array of objects which only contains ID of the just-deleted channels (this would crash the app before this commit). - The same `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 d86207f commit b31c144

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

lib/api/model/events.dart

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

619-
final List<ZulipStream> streams;
619+
@JsonKey(readValue: _readChannelIds)
620+
final List<int> channelIds;
621+
622+
// TODO(server-10) simplify away; rely on stream_ids
623+
static List<int> _readChannelIds(Map<dynamic, dynamic> json, String key) {
624+
final channelIds = json['stream_ids'] as List<int>?;
625+
if (channelIds != null) return channelIds;
620626

621-
ChannelDeleteEvent({required super.id, required this.streams});
627+
final channels = json['streams'] as List<dynamic>;
628+
return channels
629+
.map((c) => (c as Map<String, dynamic>)['stream_id'] as int)
630+
.toList();
631+
}
632+
633+
ChannelDeleteEvent({
634+
required super.id,
635+
required this.channelIds,
636+
});
622637

623638
factory ChannelDeleteEvent.fromJson(Map<String, dynamic> json) =>
624639
_$ChannelDeleteEventFromJson(json);

lib/api/model/events.g.dart

Lines changed: 6 additions & 4 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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,15 @@ 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+
for (final channelId in event.channelIds) {
402+
final channel = streams[channelId];
403+
if (channel == null) break;
404+
assert(identical(streams[channel.streamId], streamsByName[channel.name]));
405+
assert(subscriptions[channelId] == null
406+
|| identical(subscriptions[channelId], streams[channelId]));
407+
streams.remove(channel.streamId);
408+
streamsByName.remove(channel.name);
409+
subscriptions.remove(channel.streamId);
408410
}
409411

410412
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, channelIds: [stream.streamId]),
866866
], queueId: null).toJson());
867867
}
868868

test/widgets/action_sheet_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ void main() {
340340

341341
testWidgets('unknown channel', (tester) async {
342342
await prepare();
343-
await store.handleEvent(ChannelDeleteEvent(id: 1, streams: [someChannel]));
343+
await store.handleEvent(ChannelDeleteEvent(id: 1,
344+
channelIds: [someChannel.streamId]));
344345
check(store.streams[someChannel.streamId]).isNull();
345346
await showFromTopicListAppBar(tester);
346347
check(findInHeader(find.byType(Icon))).findsNothing();

0 commit comments

Comments
 (0)