From e7073c251316658751f5c6c3718fa7f1f90bfaf9 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 10 Aug 2023 18:49:02 -0700 Subject: [PATCH] api: Add unreadMsgs in initial snapshot Our own data structure in PerAccountStore will be constructed from this value, and it will probably have quite a different shape; for example, there, we're likely to want to collapse the distinction between 1:1 and group DMs. But now we're validating this part of the /register response and making it available to feed into those new internal data structures. Related: #253 --- lib/api/model/initial_snapshot.dart | 101 ++++++++++++++++++++++++++ lib/api/model/initial_snapshot.g.dart | 81 +++++++++++++++++++++ test/example_data.dart | 21 ++++++ 3 files changed, 203 insertions(+) diff --git a/lib/api/model/initial_snapshot.dart b/lib/api/model/initial_snapshot.dart index 7ab896aee1..9f1fbd7d32 100644 --- a/lib/api/model/initial_snapshot.dart +++ b/lib/api/model/initial_snapshot.dart @@ -29,6 +29,8 @@ class InitialSnapshot { final List subscriptions; + final UnreadMessagesSnapshot unreadMsgs; + final List streams; // Servers pre-5.0 don't have `user_settings`, and instead provide whatever @@ -79,6 +81,7 @@ class InitialSnapshot { required this.customProfileFields, required this.recentPrivateConversations, required this.subscriptions, + required this.unreadMsgs, required this.streams, required this.userSettings, required this.maxFileUploadSizeMib, @@ -163,3 +166,101 @@ enum UserSettingName { static final _byRawString = _$UserSettingNameEnumMap .map((key, value) => MapEntry(value, key)); } + +/// The `unread_msgs` snapshot. +/// +/// For docs, search for "unread_msgs:" +/// in . +@JsonSerializable(fieldRename: FieldRename.snake) +class UnreadMessagesSnapshot { + final int count; + + @JsonKey(name: 'pms') + final List dms; + + final List streams; + final List huddles; + final List mentions; + final bool oldUnreadsMissing; + + UnreadMessagesSnapshot({ + required this.count, + required this.dms, + required this.streams, + required this.huddles, + required this.mentions, + required this.oldUnreadsMissing, + }); + + factory UnreadMessagesSnapshot.fromJson(Map json) => + _$UnreadMessagesSnapshotFromJson(json); + + Map toJson() => _$UnreadMessagesSnapshotToJson(this); +} + +/// An item in [UnreadMessagesSnapshot.dms]. +@JsonSerializable(fieldRename: FieldRename.snake) +class UnreadDmSnapshot { + @JsonKey(readValue: _readOtherUserId) + final int otherUserId; + + // The doc mistakenly calls this `unread_ids`: + // https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940 + final List unreadMessageIds; + + // other_user_id was introduced at FL 119 as the new name for sender_id: + // https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20When.20was.20.60unread_msgs.2Epms.5B.5D.2Eother_user_id.60.20added.3F/near/1623961 + // TODO(server-5): Simplify away. + static _readOtherUserId(Map json, String key) { + return json[key] ?? json['sender_id']; + } + + UnreadDmSnapshot({ + required this.otherUserId, + required this.unreadMessageIds, + }); + + factory UnreadDmSnapshot.fromJson(Map json) => + _$UnreadDmSnapshotFromJson(json); + + Map toJson() => _$UnreadDmSnapshotToJson(this); +} + +/// An item in [UnreadMessagesSnapshot.streams]. +@JsonSerializable(fieldRename: FieldRename.snake) +class UnreadStreamSnapshot { + final String topic; + final int streamId; + final List unreadMessageIds; + + UnreadStreamSnapshot({ + required this.topic, + required this.streamId, + required this.unreadMessageIds, + }); + + factory UnreadStreamSnapshot.fromJson(Map json) => + _$UnreadStreamSnapshotFromJson(json); + + Map toJson() => _$UnreadStreamSnapshotToJson(this); +} + +/// An item in [UnreadMessagesSnapshot.huddles]. +@JsonSerializable(fieldRename: FieldRename.snake) +class UnreadHuddleSnapshot { + final String userIdsString; + + // The doc mistakenly calls this `unread_ids`: + // https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940 + final List unreadMessageIds; + + UnreadHuddleSnapshot({ + required this.userIdsString, + required this.unreadMessageIds, + }); + + factory UnreadHuddleSnapshot.fromJson(Map json) => + _$UnreadHuddleSnapshotFromJson(json); + + Map toJson() => _$UnreadHuddleSnapshotToJson(this); +} diff --git a/lib/api/model/initial_snapshot.g.dart b/lib/api/model/initial_snapshot.g.dart index c0e89375ba..2356f87572 100644 --- a/lib/api/model/initial_snapshot.g.dart +++ b/lib/api/model/initial_snapshot.g.dart @@ -28,6 +28,8 @@ InitialSnapshot _$InitialSnapshotFromJson(Map json) => subscriptions: (json['subscriptions'] as List) .map((e) => Subscription.fromJson(e as Map)) .toList(), + unreadMsgs: UnreadMessagesSnapshot.fromJson( + json['unread_msgs'] as Map), streams: (json['streams'] as List) .map((e) => ZulipStream.fromJson(e as Map)) .toList(), @@ -62,6 +64,7 @@ Map _$InitialSnapshotToJson(InitialSnapshot instance) => 'custom_profile_fields': instance.customProfileFields, 'recent_private_conversations': instance.recentPrivateConversations, 'subscriptions': instance.subscriptions, + 'unread_msgs': instance.unreadMsgs, 'streams': instance.streams, 'user_settings': instance.userSettings, 'max_file_upload_size_mib': instance.maxFileUploadSizeMib, @@ -101,6 +104,84 @@ Map _$UserSettingsToJson(UserSettings instance) => 'display_emoji_reaction_users': instance.displayEmojiReactionUsers, }; +UnreadMessagesSnapshot _$UnreadMessagesSnapshotFromJson( + Map json) => + UnreadMessagesSnapshot( + count: json['count'] as int, + dms: (json['pms'] as List) + .map((e) => UnreadDmSnapshot.fromJson(e as Map)) + .toList(), + streams: (json['streams'] as List) + .map((e) => UnreadStreamSnapshot.fromJson(e as Map)) + .toList(), + huddles: (json['huddles'] as List) + .map((e) => UnreadHuddleSnapshot.fromJson(e as Map)) + .toList(), + mentions: + (json['mentions'] as List).map((e) => e as int).toList(), + oldUnreadsMissing: json['old_unreads_missing'] as bool, + ); + +Map _$UnreadMessagesSnapshotToJson( + UnreadMessagesSnapshot instance) => + { + 'count': instance.count, + 'pms': instance.dms, + 'streams': instance.streams, + 'huddles': instance.huddles, + 'mentions': instance.mentions, + 'old_unreads_missing': instance.oldUnreadsMissing, + }; + +UnreadDmSnapshot _$UnreadDmSnapshotFromJson(Map json) => + UnreadDmSnapshot( + otherUserId: + UnreadDmSnapshot._readOtherUserId(json, 'other_user_id') as int, + unreadMessageIds: (json['unread_message_ids'] as List) + .map((e) => e as int) + .toList(), + ); + +Map _$UnreadDmSnapshotToJson(UnreadDmSnapshot instance) => + { + 'other_user_id': instance.otherUserId, + 'unread_message_ids': instance.unreadMessageIds, + }; + +UnreadStreamSnapshot _$UnreadStreamSnapshotFromJson( + Map json) => + UnreadStreamSnapshot( + topic: json['topic'] as String, + streamId: json['stream_id'] as int, + unreadMessageIds: (json['unread_message_ids'] as List) + .map((e) => e as int) + .toList(), + ); + +Map _$UnreadStreamSnapshotToJson( + UnreadStreamSnapshot instance) => + { + 'topic': instance.topic, + 'stream_id': instance.streamId, + 'unread_message_ids': instance.unreadMessageIds, + }; + +UnreadHuddleSnapshot _$UnreadHuddleSnapshotFromJson( + Map json) => + UnreadHuddleSnapshot( + userIdsString: json['user_ids_string'] as String, + unreadMessageIds: (json['unread_message_ids'] as List) + .map((e) => e as int) + .toList(), + ); + +Map _$UnreadHuddleSnapshotToJson( + UnreadHuddleSnapshot instance) => + { + 'user_ids_string': instance.userIdsString, + 'unread_message_ids': instance.unreadMessageIds, + }; + const _$UserSettingNameEnumMap = { UserSettingName.twentyFourHourTime: 'twenty_four_hour_time', UserSettingName.displayEmojiReactionUsers: 'display_emoji_reaction_users', diff --git a/test/example_data.dart b/test/example_data.dart index d61c84e7c1..ac329161ec 100644 --- a/test/example_data.dart +++ b/test/example_data.dart @@ -92,6 +92,25 @@ ZulipStream stream({ ); } +UnreadMessagesSnapshot unreadMsgs({ + int? count, + List? dms, + List? streams, + List? huddles, + List? mentions, + bool? oldUnreadsMissing, +}) { + return UnreadMessagesSnapshot( + count: count ?? 0, + dms: dms ?? [], + streams: streams ?? [], + huddles: huddles ?? [], + mentions: mentions ?? [], + oldUnreadsMissing: oldUnreadsMissing ?? false, + ); +} +const _unreadMsgs = unreadMsgs; + final _messagePropertiesBase = { 'is_me_message': false, 'recipient_id': 32, // obsolescent in API, and ignored @@ -223,6 +242,7 @@ InitialSnapshot initialSnapshot({ List? customProfileFields, List? recentPrivateConversations, List? subscriptions, + UnreadMessagesSnapshot? unreadMsgs, List? streams, UserSettings? userSettings, int? maxFileUploadSizeMib, @@ -240,6 +260,7 @@ InitialSnapshot initialSnapshot({ customProfileFields: customProfileFields ?? [], recentPrivateConversations: recentPrivateConversations ?? [], subscriptions: subscriptions ?? [], // TODO add subscriptions to default + unreadMsgs: unreadMsgs ?? _unreadMsgs(), streams: streams ?? [], // TODO add streams to default userSettings: userSettings, // TODO add userSettings to default maxFileUploadSizeMib: maxFileUploadSizeMib ?? 25,