@@ -17,6 +17,14 @@ abstract class Event {
17
17
final type = json['type' ] as String ;
18
18
switch (type) {
19
19
case 'alert_words' : return AlertWordsEvent .fromJson (json);
20
+ case 'realm_user' :
21
+ final op = json['op' ] as String ;
22
+ switch (op) {
23
+ case 'add' : return RealmUserAddEvent .fromJson (json);
24
+ case 'remove' : return RealmUserRemoveEvent .fromJson (json);
25
+ case 'update' : return RealmUserUpdateEvent .fromJson (json);
26
+ default : return UnexpectedEvent .fromJson (json);
27
+ }
20
28
case 'message' : return MessageEvent .fromJson (json);
21
29
case 'heartbeat' : return HeartbeatEvent .fromJson (json);
22
30
// TODO add many more event types
@@ -61,6 +69,116 @@ class AlertWordsEvent extends Event {
61
69
Map <String , dynamic > toJson () => _$AlertWordsEventToJson (this );
62
70
}
63
71
72
+ /// A Zulip event of type `realm_user` .
73
+ abstract class RealmUserEvent extends Event {
74
+ @override
75
+ @JsonKey (includeToJson: true )
76
+ String get type => 'realm_user' ;
77
+
78
+ String get op;
79
+
80
+ RealmUserEvent ({required super .id});
81
+ }
82
+
83
+ /// A [RealmUserEvent] with op `add` : https://zulip.com/api/get-events#realm_user-add
84
+ @JsonSerializable (fieldRename: FieldRename .snake)
85
+ class RealmUserAddEvent extends RealmUserEvent {
86
+ @override
87
+ String get op => 'add' ;
88
+
89
+ final User person;
90
+
91
+ RealmUserAddEvent ({required super .id, required this .person});
92
+
93
+ factory RealmUserAddEvent .fromJson (Map <String , dynamic > json) =>
94
+ _$RealmUserAddEventFromJson (json);
95
+
96
+ @override
97
+ Map <String , dynamic > toJson () => _$RealmUserAddEventToJson (this );
98
+ }
99
+
100
+ /// A [RealmUserEvent] with op `remove` : https://zulip.com/api/get-events#realm_user-remove
101
+ class RealmUserRemoveEvent extends RealmUserEvent {
102
+ @override
103
+ String get op => 'remove' ;
104
+
105
+ final int userId;
106
+
107
+ RealmUserRemoveEvent ({required super .id, required this .userId});
108
+
109
+ factory RealmUserRemoveEvent .fromJson (Map <String , dynamic > json) {
110
+ return RealmUserRemoveEvent (
111
+ id: json['id' ] as int ,
112
+ userId: (json['person' ] as Map <String , dynamic >)['user_id' ] as int );
113
+ }
114
+
115
+ @override
116
+ Map <String , dynamic > toJson () {
117
+ return {'id' : id, 'type' : type, 'op' : op, 'person' : {'user_id' : userId}};
118
+ }
119
+ }
120
+
121
+ @JsonSerializable (fieldRename: FieldRename .snake)
122
+ class RealmUserUpdateCustomProfileField {
123
+ final int id;
124
+ final String ? value;
125
+ final String ? renderedValue;
126
+
127
+ RealmUserUpdateCustomProfileField ({required this .id, required this .value, required this .renderedValue});
128
+
129
+ factory RealmUserUpdateCustomProfileField .fromJson (Map <String , dynamic > json) =>
130
+ _$RealmUserUpdateCustomProfileFieldFromJson (json);
131
+
132
+ Map <String , dynamic > toJson () => _$RealmUserUpdateCustomProfileFieldToJson (this );
133
+ }
134
+
135
+ /// A [RealmUserEvent] with op `update` : https://zulip.com/api/get-events#realm_user-update
136
+ @JsonSerializable (fieldRename: FieldRename .snake)
137
+ class RealmUserUpdateEvent extends RealmUserEvent {
138
+ @override
139
+ String get op => 'update' ;
140
+
141
+ @JsonKey (readValue: _readFromPerson) final int userId;
142
+ @JsonKey (readValue: _readFromPerson) final String ? fullName;
143
+ @JsonKey (readValue: _readFromPerson) final String ? avatarUrl;
144
+ // @JsonKey(readValue: _readFromPerson) final String? avatarSource; // TODO obsolete?
145
+ // @JsonKey(readValue: _readFromPerson) final String? avatarUrlMedium; // TODO obsolete?
146
+ @JsonKey (readValue: _readFromPerson) final int ? avatarVersion;
147
+ @JsonKey (readValue: _readFromPerson) final String ? timezone;
148
+ @JsonKey (readValue: _readFromPerson) final int ? botOwnerId;
149
+ @JsonKey (readValue: _readFromPerson) final int ? role; // TODO enum
150
+ @JsonKey (readValue: _readFromPerson) final bool ? isBillingAdmin;
151
+ @JsonKey (readValue: _readFromPerson) final String ? deliveryEmail; // TODO handle JSON `null`
152
+ @JsonKey (readValue: _readFromPerson) final RealmUserUpdateCustomProfileField ? customProfileField;
153
+ @JsonKey (readValue: _readFromPerson) final String ? newEmail;
154
+
155
+ static Object ? _readFromPerson (Map json, String key) {
156
+ return (json['person' ] as Map <String , dynamic >)[key];
157
+ }
158
+
159
+ RealmUserUpdateEvent ({
160
+ required super .id,
161
+ required this .userId,
162
+ this .fullName,
163
+ this .avatarUrl,
164
+ this .avatarVersion,
165
+ this .timezone,
166
+ this .botOwnerId,
167
+ this .role,
168
+ this .isBillingAdmin,
169
+ this .deliveryEmail,
170
+ this .customProfileField,
171
+ this .newEmail,
172
+ });
173
+
174
+ factory RealmUserUpdateEvent .fromJson (Map <String , dynamic > json) =>
175
+ _$RealmUserUpdateEventFromJson (json);
176
+
177
+ // TODO make round-trip (see _readFromPerson)
178
+ @override
179
+ Map <String , dynamic > toJson () => _$RealmUserUpdateEventToJson (this );
180
+ }
181
+
64
182
/// A Zulip event of type `message` .
65
183
// TODO use [JsonSerializable] here too, using its customization features,
66
184
// in order to skip the boilerplate in [fromJson] and [toJson].
0 commit comments