Skip to content

Commit 5a3f123

Browse files
committed
channel: Ensure SubscriptionRemoveEvent updates streams collections
1 parent 62326f0 commit 5a3f123

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/api/model/model.dart

+18
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,24 @@ class Subscription extends ZulipStream {
437437

438438
@override
439439
Map<String, dynamic> toJson() => _$SubscriptionToJson(this);
440+
441+
/// Returns a plain `ZulipStream` with same values as `this`
442+
///
443+
/// This is helpful when unsubscribing from a stream.
444+
ZulipStream toStream() => ZulipStream(
445+
streamId: streamId,
446+
name: name,
447+
description: description,
448+
renderedDescription: renderedDescription,
449+
dateCreated: dateCreated,
450+
firstMessageId: firstMessageId,
451+
inviteOnly: inviteOnly,
452+
isWebPublic: isWebPublic,
453+
historyPublicToSubscribers: historyPublicToSubscribers,
454+
messageRetentionDays: messageRetentionDays,
455+
channelPostPolicy: channelPostPolicy,
456+
canRemoveSubscribersGroup: canRemoveSubscribersGroup,
457+
streamWeeklyTraffic: streamWeeklyTraffic);
440458
}
441459

442460
@JsonEnum(fieldRename: FieldRename.snake, valueField: "apiValue")

lib/model/channel.dart

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class ChannelStoreImpl with ChannelStore {
173173
assert(streamsByName.containsKey(streams[streamId]!.name)
174174
&& streamsByName[streams[streamId]!.name] is Subscription);
175175
assert(subscriptions.containsKey(streamId));
176+
streams[streamId] = subscriptions[streamId]!.toStream();
177+
streamsByName[streams[streamId]!.name] = subscriptions[streamId]!.toStream();
176178
subscriptions.remove(streamId);
177179
}
178180

test/model/channel_test.dart

+11
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ void main() {
103103
value: false));
104104
check(store.subscriptions[stream.streamId]!.isMuted).isTrue();
105105
});
106+
107+
test('SubscriptionRemoveEvent updates streams and streamsByName', () async {
108+
final store = eg.store(initialSnapshot: eg.initialSnapshot(
109+
streams: [eg.subscription(stream)],
110+
subscriptions: [eg.subscription(stream)],
111+
));
112+
113+
await store.handleEvent(SubscriptionRemoveEvent(id: 1, streamIds: [stream.streamId]));
114+
check(store.streams[stream.streamId] is Subscription).isFalse();
115+
check(store.streams[stream.streamId] is ZulipStream).isTrue();
116+
});
106117
});
107118

108119
group('topic visibility', () {

0 commit comments

Comments
 (0)