Skip to content

Commit fab0fb9

Browse files
committed
api: Convert User.role into enum
1 parent 0354b8a commit fab0fb9

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

lib/api/model/events.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class RealmUserUpdateEvent extends RealmUserEvent {
211211
@JsonKey(readValue: _readFromPerson) final int? avatarVersion;
212212
@JsonKey(readValue: _readFromPerson) final String? timezone;
213213
@JsonKey(readValue: _readFromPerson) final int? botOwnerId;
214-
@JsonKey(readValue: _readFromPerson) final int? role; // TODO enum
214+
@JsonKey(readValue: _readFromPerson, unknownEnumValue: UserRole.unknown) final UserRole? role;
215215
@JsonKey(readValue: _readFromPerson) final bool? isBillingAdmin;
216216
@JsonKey(readValue: _readFromPerson) final String? deliveryEmail; // TODO handle JSON `null`
217217
@JsonKey(readValue: _readFromPerson) final RealmUserUpdateCustomProfileField? customProfileField;

lib/api/model/events.g.dart

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

lib/api/model/model.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class User {
5656
bool isBot;
5757
int? botType; // TODO enum
5858
int? botOwnerId;
59-
int role; // TODO enum
59+
@JsonKey(unknownEnumValue: UserRole.unknown)
60+
UserRole role;
6061
String timezone;
6162
String? avatarUrl; // TODO distinguish null from missing https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20omitted.20vs.2E.20null.20in.20JSON/near/1551759
6263
int avatarVersion;
@@ -121,6 +122,25 @@ class ProfileFieldUserData {
121122
Map<String, dynamic> toJson() => _$ProfileFieldUserDataToJson(this);
122123
}
123124

125+
/// As in [User.role].
126+
@JsonEnum(valueField: "apiValue")
127+
enum UserRole{
128+
owner(apiValue: 100),
129+
administrator(apiValue: 200),
130+
moderator(apiValue: 300),
131+
member(apiValue: 400),
132+
guest(apiValue: 600),
133+
unknown(apiValue: null);
134+
135+
const UserRole({
136+
required this.apiValue,
137+
});
138+
139+
final int? apiValue;
140+
141+
int? toJson() => apiValue;
142+
}
143+
124144
/// As in `streams` in the initial snapshot.
125145
///
126146
/// Not called `Stream` because dart:async uses that name.

lib/api/model/model.g.dart

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

test/example_data.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ User user({
2828
isGuest: false,
2929
isBillingAdmin: false,
3030
isBot: false,
31-
role: 400,
31+
role: UserRole.member,
3232
timezone: 'UTC',
3333
avatarUrl: avatarUrl,
3434
avatarVersion: 0,

0 commit comments

Comments
 (0)