@@ -30,6 +30,15 @@ sealed class Event {
30
30
case 'update' : return RealmUserUpdateEvent .fromJson (json);
31
31
default : return UnexpectedEvent .fromJson (json);
32
32
}
33
+ case 'subscription' :
34
+ switch (json['op' ] as String ) {
35
+ case 'add' : return SubscriptionAddEvent .fromJson (json);
36
+ case 'remove' : return SubscriptionRemoveEvent .fromJson (json);
37
+ case 'update' : return SubscriptionUpdateEvent .fromJson (json);
38
+ case 'peer_add' : return SubscriptionPeerAddEvent .fromJson (json);
39
+ case 'peer_remove' : return SubscriptionPeerRemoveEvent .fromJson (json);
40
+ default : return UnexpectedEvent .fromJson (json);
41
+ }
33
42
case 'stream' :
34
43
switch (json['op' ] as String ) {
35
44
case 'create' : return StreamCreateEvent .fromJson (json);
@@ -272,6 +281,185 @@ class RealmUserUpdateEvent extends RealmUserEvent {
272
281
Map <String , dynamic > toJson () => _$RealmUserUpdateEventToJson (this );
273
282
}
274
283
284
+ /// A Zulip event of type `subscription` .
285
+ ///
286
+ /// The corresponding API docs are in several places for
287
+ /// different values of `op` ; see subclasses.
288
+ sealed class SubscriptionEvent extends Event {
289
+ @override
290
+ @JsonKey (includeToJson: true )
291
+ String get type => 'subscription' ;
292
+
293
+ String get op;
294
+
295
+ SubscriptionEvent ({required super .id});
296
+ }
297
+
298
+ /// A [SubscriptionEvent] with op `add` : https://zulip.com/api/get-events#subscription-add
299
+ @JsonSerializable (fieldRename: FieldRename .snake)
300
+ class SubscriptionAddEvent extends SubscriptionEvent {
301
+ @override
302
+ @JsonKey (includeToJson: true )
303
+ String get op => 'add' ;
304
+
305
+ final List <Subscription > subscriptions;
306
+
307
+ SubscriptionAddEvent ({required super .id, required this .subscriptions});
308
+
309
+ factory SubscriptionAddEvent .fromJson (Map <String , dynamic > json) =>
310
+ _$SubscriptionAddEventFromJson (json);
311
+
312
+ @override
313
+ Map <String , dynamic > toJson () => _$SubscriptionAddEventToJson (this );
314
+ }
315
+
316
+ /// A [SubscriptionEvent] with op `remove` : https://zulip.com/api/get-events#subscription-remove
317
+ @JsonSerializable (fieldRename: FieldRename .snake)
318
+ class SubscriptionRemoveEvent extends SubscriptionEvent {
319
+ @override
320
+ @JsonKey (includeToJson: true )
321
+ String get op => 'remove' ;
322
+
323
+ @JsonKey (readValue: _readStreamIds)
324
+ final List <int > streamIds;
325
+
326
+ static List <int > _readStreamIds (Map json, String key) {
327
+ return (json['subscriptions' ] as List <dynamic >)
328
+ .map ((e) => (e as Map <String , dynamic >)['stream_id' ] as int )
329
+ .toList ();
330
+ }
331
+
332
+ SubscriptionRemoveEvent ({required super .id, required this .streamIds});
333
+
334
+ factory SubscriptionRemoveEvent .fromJson (Map <String , dynamic > json) =>
335
+ _$SubscriptionRemoveEventFromJson (json);
336
+
337
+ @override
338
+ Map <String , dynamic > toJson () => _$SubscriptionRemoveEventToJson (this );
339
+ }
340
+
341
+ /// A [SubscriptionEvent] with op `update` : https://zulip.com/api/get-events#subscription-update
342
+ @JsonSerializable (fieldRename: FieldRename .snake)
343
+ class SubscriptionUpdateEvent extends SubscriptionEvent {
344
+ @override
345
+ @JsonKey (includeToJson: true )
346
+ String get op => 'update' ;
347
+
348
+ final int streamId;
349
+
350
+ final SubscriptionProperty property;
351
+
352
+ /// The new value, or null if we don't recognize the setting.
353
+ ///
354
+ /// This will have the type appropriate for [property] ; for example,
355
+ /// if the setting is boolean, then `value is bool` will always be true.
356
+ /// This invariant is enforced by [SubscriptionUpdateEvent.fromJson] .
357
+ @JsonKey (readValue: _readValue)
358
+ final Object ? value;
359
+
360
+ /// [value] , with a check that its type corresponds to [property]
361
+ /// (e.g., `value as bool` ).
362
+ static Object ? _readValue (Map json, String key) {
363
+ final value = json['value' ];
364
+ switch (SubscriptionProperty .fromRawString (json['property' ] as String )) {
365
+ case SubscriptionProperty .color:
366
+ return value as String ;
367
+ case SubscriptionProperty .isMuted:
368
+ case SubscriptionProperty .inHomeView:
369
+ case SubscriptionProperty .pinToTop:
370
+ case SubscriptionProperty .desktopNotifications:
371
+ case SubscriptionProperty .audibleNotifications:
372
+ case SubscriptionProperty .pushNotifications:
373
+ case SubscriptionProperty .emailNotifications:
374
+ case SubscriptionProperty .wildcardMentionsNotify:
375
+ return value as bool ;
376
+ case SubscriptionProperty .unknown:
377
+ return null ;
378
+ }
379
+ }
380
+
381
+ SubscriptionUpdateEvent ({
382
+ required super .id,
383
+ required this .streamId,
384
+ required this .property,
385
+ required this .value,
386
+ });
387
+
388
+ factory SubscriptionUpdateEvent .fromJson (Map <String , dynamic > json) =>
389
+ _$SubscriptionUpdateEventFromJson (json);
390
+
391
+ @override
392
+ Map <String , dynamic > toJson () => _$SubscriptionUpdateEventToJson (this );
393
+ }
394
+
395
+ /// The name of a property in [Subscription] .
396
+ ///
397
+ /// Used in handling of [SubscriptionUpdateEvent] .
398
+ @JsonEnum (fieldRename: FieldRename .snake, alwaysCreate: true )
399
+ enum SubscriptionProperty {
400
+ color,
401
+ isMuted,
402
+ inHomeView,
403
+ pinToTop,
404
+ desktopNotifications,
405
+ audibleNotifications,
406
+ pushNotifications,
407
+ emailNotifications,
408
+ wildcardMentionsNotify,
409
+ unknown;
410
+
411
+ static SubscriptionProperty fromRawString (String raw) => _byRawString[raw] ?? unknown;
412
+
413
+ static final _byRawString = _$SubscriptionPropertyEnumMap
414
+ .map ((key, value) => MapEntry (value, key));
415
+ }
416
+
417
+ /// A [SubscriptionEvent] with op `peer_add` : https://zulip.com/api/get-events#subscription-peer_add
418
+ @JsonSerializable (fieldRename: FieldRename .snake)
419
+ class SubscriptionPeerAddEvent extends SubscriptionEvent {
420
+ @override
421
+ @JsonKey (includeToJson: true )
422
+ String get op => 'peer_add' ;
423
+
424
+ List <int > streamIds;
425
+ List <int > userIds;
426
+
427
+ SubscriptionPeerAddEvent ({
428
+ required super .id,
429
+ required this .streamIds,
430
+ required this .userIds,
431
+ });
432
+
433
+ factory SubscriptionPeerAddEvent .fromJson (Map <String , dynamic > json) =>
434
+ _$SubscriptionPeerAddEventFromJson (json);
435
+
436
+ @override
437
+ Map <String , dynamic > toJson () => _$SubscriptionPeerAddEventToJson (this );
438
+ }
439
+
440
+ /// A [SubscriptionEvent] with op `peer_remove` : https://zulip.com/api/get-events#subscription-peer_remove
441
+ @JsonSerializable (fieldRename: FieldRename .snake)
442
+ class SubscriptionPeerRemoveEvent extends SubscriptionEvent {
443
+ @override
444
+ @JsonKey (includeToJson: true )
445
+ String get op => 'peer_remove' ;
446
+
447
+ List <int > streamIds;
448
+ List <int > userIds;
449
+
450
+ SubscriptionPeerRemoveEvent ({
451
+ required super .id,
452
+ required this .streamIds,
453
+ required this .userIds,
454
+ });
455
+
456
+ factory SubscriptionPeerRemoveEvent .fromJson (Map <String , dynamic > json) =>
457
+ _$SubscriptionPeerRemoveEventFromJson (json);
458
+
459
+ @override
460
+ Map <String , dynamic > toJson () => _$SubscriptionPeerRemoveEventToJson (this );
461
+ }
462
+
275
463
/// A Zulip event of type `stream` .
276
464
///
277
465
/// The corresponding API docs are in several places for
0 commit comments