Skip to content

Commit

Permalink
model: Refactor Subscription to extend from ZulipStream
Browse files Browse the repository at this point in the history
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 zulip#182). This change makes is clearer which
properties of Subscription are specific to subscriptions.
  • Loading branch information
sirpengi committed Nov 14, 2023
1 parent 7b6a7e8 commit 130117a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 68 deletions.
92 changes: 41 additions & 51 deletions lib/api/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, dynamic> json) =>
Expand Down Expand Up @@ -301,75 +312,54 @@ enum StreamPostPolicy{
/// For docs, search for "subscriptions:"
/// in <https://zulip.com/api/register-queue>.
@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<int> subscribers; // we register with includeSubscribers false
class Subscription extends ZulipStream {
List<int>? 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<String, dynamic> json) =>
_$SubscriptionFromJson(json);

@override
Map<String, dynamic> toJson() => _$SubscriptionToJson(this);
}

Expand Down
37 changes: 22 additions & 15 deletions lib/api/model/model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions test/example_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 130117a

Please sign in to comment.