From 130117a19232d0031c4b533e73d530e039b8e1e5 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Fri, 10 Nov 2023 13:16:42 +0000 Subject: [PATCH] model: Refactor Subscription to extend from ZulipStream The properties of Subscription are a superset of ZulipStream properties. Unfortunately this means there are duplicate fields (such as the stream name) that live in two places and there is no mechanism yet to synchronize them (that is work left for #182). This change makes is clearer which properties of Subscription are specific to subscriptions. --- lib/api/model/model.dart | 92 +++++++++++++++++--------------------- lib/api/model/model.g.dart | 37 ++++++++------- test/example_data.dart | 6 ++- 3 files changed, 67 insertions(+), 68 deletions(-) diff --git a/lib/api/model/model.dart b/lib/api/model/model.dart index 6b6a5940689..dc119517a99 100644 --- a/lib/api/model/model.dart +++ b/lib/api/model/model.dart @@ -252,7 +252,17 @@ class ZulipStream { final StreamPostPolicy streamPostPolicy; // final bool isAnnouncementOnly; // deprecated for `streamPostPolicy`; ignore - final int? canRemoveSubscribersGroupId; // TODO(server-6) + // TODO(server-6): `canRemoveSubscribersGroupId` added in FL 142 + // TODO(server-8): in FL 197 renamed to `canRemoveSubscribersGroup` + @JsonKey(readValue: _readCanRemoveSubscribersGroup) + final int? canRemoveSubscribersGroup; + + // TODO(server-8): added in FL 199, was previously only on [Subscription] objects + final int? streamWeeklyTraffic; + + static int _readCanRemoveSubscribersGroup(Map json, String key) { + return json[key] ?? json['can_remove_subscribers_group_id']; + } ZulipStream({ required this.streamId, @@ -266,7 +276,8 @@ class ZulipStream { required this.historyPublicToSubscribers, required this.messageRetentionDays, required this.streamPostPolicy, - required this.canRemoveSubscribersGroupId, + required this.canRemoveSubscribersGroup, + required this.streamWeeklyTraffic, }); factory ZulipStream.fromJson(Map json) => @@ -301,75 +312,54 @@ enum StreamPostPolicy{ /// For docs, search for "subscriptions:" /// in . @JsonSerializable(fieldRename: FieldRename.snake) -class Subscription { - // First, fields that are about the stream and not the user's relation to it. - // These are largely the same as in [ZulipStream]. - - final int streamId; - final String name; - final String description; - final String renderedDescription; - - final int dateCreated; - final int? firstMessageId; - final int? streamWeeklyTraffic; - - final bool inviteOnly; - final bool? isWebPublic; // TODO(server-??): doc doesn't say when added - final bool historyPublicToSubscribers; - final int? messageRetentionDays; - // final List subscribers; // we register with includeSubscribers false +class Subscription extends ZulipStream { + List? subscribers; - final int streamPostPolicy; // TODO enum - // final bool? isAnnouncementOnly; // deprecated; ignore - final String emailAddress; + bool? desktopNotifications; + bool? emailNotifications; + bool? wildcardMentionsNotify; + bool? pushNotifications; + bool? audibleNotifications; - final int? canRemoveSubscribersGroupId; // TODO(server-6) + bool pinToTop; - // Then, fields that are specific to the subscription, - // i.e. the user's relationship to the stream. - - final bool? desktopNotifications; - final bool? emailNotifications; - final bool? wildcardMentionsNotify; - final bool? pushNotifications; - final bool? audibleNotifications; - - final bool pinToTop; - - final bool isMuted; + bool isMuted; // final bool? inHomeView; // deprecated; ignore - final String color; + String color; + String emailAddress; Subscription({ - required this.streamId, - required this.name, - required this.description, - required this.renderedDescription, - required this.dateCreated, - required this.inviteOnly, + required super.streamId, + required super.name, + required super.description, + required super.renderedDescription, + required super.dateCreated, + required super.firstMessageId, + required super.inviteOnly, + required super.isWebPublic, + required super.historyPublicToSubscribers, + required super.messageRetentionDays, + required super.streamPostPolicy, + required super.canRemoveSubscribersGroup, + required super.streamWeeklyTraffic, + + required this.subscribers, required this.desktopNotifications, required this.emailNotifications, required this.wildcardMentionsNotify, required this.pushNotifications, required this.audibleNotifications, required this.pinToTop, - required this.emailAddress, required this.isMuted, - required this.isWebPublic, required this.color, - required this.streamPostPolicy, - required this.messageRetentionDays, - required this.historyPublicToSubscribers, - required this.firstMessageId, - required this.streamWeeklyTraffic, - required this.canRemoveSubscribersGroupId, + required this.emailAddress, }); factory Subscription.fromJson(Map json) => _$SubscriptionFromJson(json); + @override Map toJson() => _$SubscriptionToJson(this); } diff --git a/lib/api/model/model.g.dart b/lib/api/model/model.g.dart index 925266bc8a0..54615114e12 100644 --- a/lib/api/model/model.g.dart +++ b/lib/api/model/model.g.dart @@ -157,8 +157,9 @@ ZulipStream _$ZulipStreamFromJson(Map json) => ZulipStream( messageRetentionDays: json['message_retention_days'] as int?, streamPostPolicy: $enumDecode(_$StreamPostPolicyEnumMap, json['stream_post_policy']), - canRemoveSubscribersGroupId: - json['can_remove_subscribers_group_id'] as int?, + canRemoveSubscribersGroup: ZulipStream._readCanRemoveSubscribersGroup( + json, 'can_remove_subscribers_group') as int?, + streamWeeklyTraffic: json['stream_weekly_traffic'] as int?, ); Map _$ZulipStreamToJson(ZulipStream instance) => @@ -174,7 +175,8 @@ Map _$ZulipStreamToJson(ZulipStream instance) => 'history_public_to_subscribers': instance.historyPublicToSubscribers, 'message_retention_days': instance.messageRetentionDays, 'stream_post_policy': instance.streamPostPolicy, - 'can_remove_subscribers_group_id': instance.canRemoveSubscribersGroupId, + 'can_remove_subscribers_group': instance.canRemoveSubscribersGroup, + 'stream_weekly_traffic': instance.streamWeeklyTraffic, }; const _$StreamPostPolicyEnumMap = { @@ -191,24 +193,28 @@ Subscription _$SubscriptionFromJson(Map json) => Subscription( description: json['description'] as String, renderedDescription: json['rendered_description'] as String, dateCreated: json['date_created'] as int, + firstMessageId: json['first_message_id'] as int?, inviteOnly: json['invite_only'] as bool, + isWebPublic: json['is_web_public'] as bool, + historyPublicToSubscribers: json['history_public_to_subscribers'] as bool, + messageRetentionDays: json['message_retention_days'] as int?, + streamPostPolicy: + $enumDecode(_$StreamPostPolicyEnumMap, json['stream_post_policy']), + canRemoveSubscribersGroup: ZulipStream._readCanRemoveSubscribersGroup( + json, 'can_remove_subscribers_group') as int?, + streamWeeklyTraffic: json['stream_weekly_traffic'] as int?, + subscribers: (json['subscribers'] as List?) + ?.map((e) => e as int) + .toList(), desktopNotifications: json['desktop_notifications'] as bool?, emailNotifications: json['email_notifications'] as bool?, wildcardMentionsNotify: json['wildcard_mentions_notify'] as bool?, pushNotifications: json['push_notifications'] as bool?, audibleNotifications: json['audible_notifications'] as bool?, pinToTop: json['pin_to_top'] as bool, - emailAddress: json['email_address'] as String, isMuted: json['is_muted'] as bool, - isWebPublic: json['is_web_public'] as bool?, color: json['color'] as String, - streamPostPolicy: json['stream_post_policy'] as int, - messageRetentionDays: json['message_retention_days'] as int?, - historyPublicToSubscribers: json['history_public_to_subscribers'] as bool, - firstMessageId: json['first_message_id'] as int?, - streamWeeklyTraffic: json['stream_weekly_traffic'] as int?, - canRemoveSubscribersGroupId: - json['can_remove_subscribers_group_id'] as int?, + emailAddress: json['email_address'] as String, ); Map _$SubscriptionToJson(Subscription instance) => @@ -219,14 +225,14 @@ Map _$SubscriptionToJson(Subscription instance) => 'rendered_description': instance.renderedDescription, 'date_created': instance.dateCreated, 'first_message_id': instance.firstMessageId, - 'stream_weekly_traffic': instance.streamWeeklyTraffic, 'invite_only': instance.inviteOnly, 'is_web_public': instance.isWebPublic, 'history_public_to_subscribers': instance.historyPublicToSubscribers, 'message_retention_days': instance.messageRetentionDays, 'stream_post_policy': instance.streamPostPolicy, - 'email_address': instance.emailAddress, - 'can_remove_subscribers_group_id': instance.canRemoveSubscribersGroupId, + 'can_remove_subscribers_group': instance.canRemoveSubscribersGroup, + 'stream_weekly_traffic': instance.streamWeeklyTraffic, + 'subscribers': instance.subscribers, 'desktop_notifications': instance.desktopNotifications, 'email_notifications': instance.emailNotifications, 'wildcard_mentions_notify': instance.wildcardMentionsNotify, @@ -235,6 +241,7 @@ Map _$SubscriptionToJson(Subscription instance) => 'pin_to_top': instance.pinToTop, 'is_muted': instance.isMuted, 'color': instance.color, + 'email_address': instance.emailAddress, }; StreamMessage _$StreamMessageFromJson(Map json) => diff --git a/test/example_data.dart b/test/example_data.dart index 9326a838ec1..612071c88b2 100644 --- a/test/example_data.dart +++ b/test/example_data.dart @@ -104,7 +104,8 @@ ZulipStream stream({ bool? historyPublicToSubscribers, int? messageRetentionDays, StreamPostPolicy? streamPostPolicy, - int? canRemoveSubscribersGroupId, + int? canRemoveSubscribersGroup, + int? streamWeeklyTraffic, }) { return ZulipStream( streamId: streamId ?? 123, // TODO generate example IDs @@ -118,7 +119,8 @@ ZulipStream stream({ historyPublicToSubscribers: historyPublicToSubscribers ?? true, messageRetentionDays: messageRetentionDays, streamPostPolicy: streamPostPolicy ?? StreamPostPolicy.any, - canRemoveSubscribersGroupId: canRemoveSubscribersGroupId ?? 123, + canRemoveSubscribersGroup: canRemoveSubscribersGroup ?? 123, + streamWeeklyTraffic: streamWeeklyTraffic, ); } const _stream = stream;