Skip to content

Commit 8553310

Browse files
committed
api: Complete support for CustomProfileField
Change `CustomProfileField.type` into a proper enum, add support for server event `custom_profile_fields`.
1 parent c055372 commit 8553310

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

lib/api/model/events.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ sealed class Event {
2222
case 'update': return UserSettingsUpdateEvent.fromJson(json);
2323
default: return UnexpectedEvent.fromJson(json);
2424
}
25+
case 'custom_profile_fields': return CustomProfileFieldsEvent.fromJson(json);
2526
case 'realm_user':
2627
switch (json['op'] as String) {
2728
case 'add': return RealmUserAddEvent.fromJson(json);
@@ -130,6 +131,24 @@ class UserSettingsUpdateEvent extends Event {
130131
Map<String, dynamic> toJson() => _$UserSettingsUpdateEventToJson(this);
131132
}
132133

134+
/// A Zulip event of type `custom_profile_fields`: https://zulip.com/api/get-events#custom_profile_fields
135+
@JsonSerializable(fieldRename: FieldRename.snake)
136+
class CustomProfileFieldsEvent extends Event {
137+
@override
138+
@JsonKey(includeToJson: true)
139+
String get type => 'custom_profile_fields';
140+
141+
final List<CustomProfileField> fields;
142+
143+
CustomProfileFieldsEvent({required super.id, required this.fields});
144+
145+
factory CustomProfileFieldsEvent.fromJson(Map<String, dynamic> json) =>
146+
_$CustomProfileFieldsEventFromJson(json);
147+
148+
@override
149+
Map<String, dynamic> toJson() => _$CustomProfileFieldsEventToJson(this);
150+
}
151+
133152
/// A Zulip event of type `realm_user`.
134153
///
135154
/// The corresponding API docs are in several places for

lib/api/model/events.g.dart

Lines changed: 17 additions & 0 deletions
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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ part 'model.g.dart';
99
@JsonSerializable(fieldRename: FieldRename.snake)
1010
class CustomProfileField {
1111
final int id;
12-
final int type; // TODO enum; also TODO(server-6) a value added
12+
@JsonKey(unknownEnumValue: CustomProfileFieldType.unknown)
13+
final CustomProfileFieldType type;
1314
final int order;
1415
final String name;
1516
final String hint;
@@ -32,6 +33,28 @@ class CustomProfileField {
3233
Map<String, dynamic> toJson() => _$CustomProfileFieldToJson(this);
3334
}
3435

36+
/// As in [CustomProfileField.type].
37+
@JsonEnum(fieldRename: FieldRename.snake, valueField: "apiValue")
38+
enum CustomProfileFieldType {
39+
shortText(apiValue: 1),
40+
longText(apiValue: 2),
41+
choice(apiValue: 3),
42+
date(apiValue: 4),
43+
link(apiValue: 5),
44+
user(apiValue: 6),
45+
externalAccount(apiValue: 7),
46+
pronouns(apiValue: 8), // TODO(server-6) newly added
47+
unknown(apiValue: null);
48+
49+
const CustomProfileFieldType({
50+
required this.apiValue
51+
});
52+
53+
final int? apiValue;
54+
55+
int? toJson() => apiValue;
56+
}
57+
3558
/// As in [InitialSnapshot.realmUsers], [InitialSnapshot.realmNonActiveUsers], and [InitialSnapshot.crossRealmBots].
3659
///
3760
/// In the Zulip API, the items in realm_users, realm_non_active_users, and

lib/api/model/model.g.dart

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

lib/model/store.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class PerAccountStore extends ChangeNotifier {
152152
}) : zulipVersion = initialSnapshot.zulipVersion,
153153
maxFileUploadSizeMib = initialSnapshot.maxFileUploadSizeMib,
154154
realmDefaultExternalAccounts = initialSnapshot.realmDefaultExternalAccounts,
155+
customProfileFields = initialSnapshot.customProfileFields,
155156
userSettings = initialSnapshot.userSettings,
156157
users = Map.fromEntries(
157158
initialSnapshot.realmUsers
@@ -174,6 +175,7 @@ class PerAccountStore extends ChangeNotifier {
174175
final String zulipVersion; // TODO get from account; update there on initial snapshot
175176
final int maxFileUploadSizeMib; // No event for this.
176177
final Map<String, RealmDefaultExternalAccount> realmDefaultExternalAccounts;
178+
final List<CustomProfileField> customProfileFields;
177179

178180
// Data attached to the self-account on the realm.
179181
final UserSettings? userSettings; // TODO(server-5)
@@ -236,6 +238,11 @@ class PerAccountStore extends ChangeNotifier {
236238
userSettings?.emojiset = event.value as Emojiset;
237239
}
238240
notifyListeners();
241+
} else if (event is CustomProfileFieldsEvent) {
242+
assert(debugLog("server event: custom_profile_fields"));
243+
customProfileFields.clear();
244+
customProfileFields.addAll(event.fields);
245+
notifyListeners();
239246
} else if (event is RealmUserAddEvent) {
240247
assert(debugLog("server event: realm_user/add"));
241248
users[event.person.userId] = event.person;

0 commit comments

Comments
 (0)