Skip to content

Commit 1ee4f1f

Browse files
committed
api: Add RealmUpdateEvent and guest DM warning flag
- Add realmEnableGuestUserDmWarning to PerAccountStore - Handle RealmUpdateEvent to keep setting in sync with server - Support feature level 348+ for realm configuration
1 parent 41e1a35 commit 1ee4f1f

File tree

6 files changed

+134
-38
lines changed

6 files changed

+134
-38
lines changed

lib/api/model/events.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ sealed class Event {
6767
case 'submessage': return SubmessageEvent.fromJson(json);
6868
case 'typing': return TypingEvent.fromJson(json);
6969
case 'reaction': return ReactionEvent.fromJson(json);
70+
case 'realm':
71+
switch(json['op'] as String){
72+
case 'update': return RealmUpdateEvent.fromJson(json);
73+
default: return UnexpectedEvent.fromJson(json);
74+
}
7075
case 'heartbeat': return HeartbeatEvent.fromJson(json);
7176
// TODO add many more event types
7277
default: return UnexpectedEvent.fromJson(json);
@@ -1151,6 +1156,46 @@ enum ReactionOp {
11511156
remove,
11521157
}
11531158

1159+
/// A Zulip event of type `realm`, with op `update`.
1160+
///
1161+
/// This is the simpler of two possible event types sent when realm configuration changes.
1162+
/// It updates a single realm setting at a time.
1163+
///
1164+
/// See: https://zulip.com/api/get-events#realm-update
1165+
@JsonSerializable(fieldRename: FieldRename.snake)
1166+
class RealmUpdateEvent extends Event {
1167+
@override
1168+
@JsonKey(includeToJson: true)
1169+
String get type => 'realm';
1170+
1171+
@JsonKey(includeToJson: true)
1172+
String get op => 'update';
1173+
1174+
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
1175+
final RealmPropertyName? property;
1176+
1177+
final dynamic value;
1178+
1179+
RealmUpdateEvent({
1180+
required super.id,
1181+
required this.property,
1182+
required this.value,
1183+
});
1184+
1185+
factory RealmUpdateEvent.fromJson(Map<String, dynamic> json) =>
1186+
_$RealmUpdateEventFromJson(json);
1187+
1188+
@override
1189+
Map<String, dynamic> toJson() => _$RealmUpdateEventToJson(this);
1190+
}
1191+
1192+
/// As in [RealmUpdateEvent.property].
1193+
@JsonEnum(fieldRename: FieldRename.snake)
1194+
enum RealmPropertyName {
1195+
@JsonValue('enable_guest_user_dm_warning')
1196+
realmEnableGuestUserDmWarning,
1197+
}
1198+
11541199
/// A Zulip event of type `heartbeat`: https://zulip.com/api/get-events#heartbeat
11551200
@JsonSerializable(fieldRename: FieldRename.snake)
11561201
class HeartbeatEvent extends Event {

lib/api/model/events.g.dart

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

lib/api/model/initial_snapshot.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class InitialSnapshot {
8686

8787
final int maxFileUploadSizeMib;
8888

89+
@JsonKey(defaultValue: false) // TODO(server-10): Remove default
90+
final bool realmEnableGuestUserDmWarning;
91+
8992
final Uri? serverEmojiDataUrl; // TODO(server-6)
9093

9194
final String? realmEmptyTopicDisplayName; // TODO(server-10)
@@ -144,6 +147,7 @@ class InitialSnapshot {
144147
required this.realmMessageContentEditLimitSeconds,
145148
required this.realmDefaultExternalAccounts,
146149
required this.maxFileUploadSizeMib,
150+
required this.realmEnableGuestUserDmWarning,
147151
required this.serverEmojiDataUrl,
148152
required this.realmEmptyTopicDisplayName,
149153
required this.realmUsers,

lib/api/model/initial_snapshot.g.dart

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

lib/model/store.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
471471
realmMandatoryTopics: initialSnapshot.realmMandatoryTopics,
472472
realmWaitingPeriodThreshold: initialSnapshot.realmWaitingPeriodThreshold,
473473
maxFileUploadSizeMib: initialSnapshot.maxFileUploadSizeMib,
474+
realmEnableGuestUserDmWarning: initialSnapshot.realmEnableGuestUserDmWarning,
474475
realmEmptyTopicDisplayName: initialSnapshot.realmEmptyTopicDisplayName,
475476
realmAllowMessageEditing: initialSnapshot.realmAllowMessageEditing,
476477
realmMessageContentEditLimitSeconds: initialSnapshot.realmMessageContentEditLimitSeconds,
@@ -510,6 +511,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
510511
required this.realmMandatoryTopics,
511512
required this.realmWaitingPeriodThreshold,
512513
required this.maxFileUploadSizeMib,
514+
required this.realmEnableGuestUserDmWarning,
513515
required String? realmEmptyTopicDisplayName,
514516
required this.realmAllowMessageEditing,
515517
required this.realmMessageContentEditLimitSeconds,
@@ -571,6 +573,13 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
571573
final int? realmMessageContentEditLimitSeconds; // TODO(#668): update this realm setting
572574
final int maxFileUploadSizeMib; // No event for this.
573575

576+
/// Whether to show a warning when composing a DM to a guest user.
577+
///
578+
/// See: https://zulip.com/api/get-events-types
579+
/// Changes: Added in Zulip 10.0 (feature level 348).
580+
// TODO(server-10): Remove default
581+
bool realmEnableGuestUserDmWarning = false;
582+
574583
/// The display name to use for empty topics.
575584
///
576585
/// This should only be accessed when FL >= 334, since topics cannot
@@ -899,6 +908,13 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
899908
assert(debugLog("server event: reaction/${event.op}"));
900909
_messages.handleReactionEvent(event);
901910

911+
case RealmUpdateEvent():
912+
assert(debugLog("server event: realm/${event.op}"));
913+
if (event.property == RealmPropertyName.realmEnableGuestUserDmWarning) {
914+
realmEnableGuestUserDmWarning = event.value as bool;
915+
notifyListeners();
916+
}
917+
break;
902918
case UnexpectedEvent():
903919
assert(debugLog("server event: ${jsonEncode(event.toJson())}")); // TODO log better
904920
}

test/example_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ InitialSnapshot initialSnapshot({
920920
int? realmMessageContentEditLimitSeconds,
921921
Map<String, RealmDefaultExternalAccount>? realmDefaultExternalAccounts,
922922
int? maxFileUploadSizeMib,
923+
bool? realmEnableGuestUserDmWarning,
923924
Uri? serverEmojiDataUrl,
924925
String? realmEmptyTopicDisplayName,
925926
List<User>? realmUsers,
@@ -959,6 +960,7 @@ InitialSnapshot initialSnapshot({
959960
realmMessageContentEditLimitSeconds: realmMessageContentEditLimitSeconds ?? 600,
960961
realmDefaultExternalAccounts: realmDefaultExternalAccounts ?? {},
961962
maxFileUploadSizeMib: maxFileUploadSizeMib ?? 25,
963+
realmEnableGuestUserDmWarning: realmEnableGuestUserDmWarning ?? false,
962964
serverEmojiDataUrl: serverEmojiDataUrl
963965
?? realmUrl.replace(path: '/static/emoji.json'),
964966
realmEmptyTopicDisplayName: realmEmptyTopicDisplayName ?? defaultRealmEmptyTopicDisplayName,

0 commit comments

Comments
 (0)