Skip to content

Commit e7073c2

Browse files
chrisbobbegnprice
authored andcommitted
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: zulip#253
1 parent 585cae5 commit e7073c2

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

lib/api/model/initial_snapshot.dart

+101
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class InitialSnapshot {
2929

3030
final List<Subscription> subscriptions;
3131

32+
final UnreadMessagesSnapshot unreadMsgs;
33+
3234
final List<ZulipStream> streams;
3335

3436
// Servers pre-5.0 don't have `user_settings`, and instead provide whatever
@@ -79,6 +81,7 @@ class InitialSnapshot {
7981
required this.customProfileFields,
8082
required this.recentPrivateConversations,
8183
required this.subscriptions,
84+
required this.unreadMsgs,
8285
required this.streams,
8386
required this.userSettings,
8487
required this.maxFileUploadSizeMib,
@@ -163,3 +166,101 @@ enum UserSettingName {
163166
static final _byRawString = _$UserSettingNameEnumMap
164167
.map((key, value) => MapEntry(value, key));
165168
}
169+
170+
/// The `unread_msgs` snapshot.
171+
///
172+
/// For docs, search for "unread_msgs:"
173+
/// in <https://zulip.com/api/register-queue>.
174+
@JsonSerializable(fieldRename: FieldRename.snake)
175+
class UnreadMessagesSnapshot {
176+
final int count;
177+
178+
@JsonKey(name: 'pms')
179+
final List<UnreadDmSnapshot> dms;
180+
181+
final List<UnreadStreamSnapshot> streams;
182+
final List<UnreadHuddleSnapshot> huddles;
183+
final List<int> mentions;
184+
final bool oldUnreadsMissing;
185+
186+
UnreadMessagesSnapshot({
187+
required this.count,
188+
required this.dms,
189+
required this.streams,
190+
required this.huddles,
191+
required this.mentions,
192+
required this.oldUnreadsMissing,
193+
});
194+
195+
factory UnreadMessagesSnapshot.fromJson(Map<String, dynamic> json) =>
196+
_$UnreadMessagesSnapshotFromJson(json);
197+
198+
Map<String, dynamic> toJson() => _$UnreadMessagesSnapshotToJson(this);
199+
}
200+
201+
/// An item in [UnreadMessagesSnapshot.dms].
202+
@JsonSerializable(fieldRename: FieldRename.snake)
203+
class UnreadDmSnapshot {
204+
@JsonKey(readValue: _readOtherUserId)
205+
final int otherUserId;
206+
207+
// The doc mistakenly calls this `unread_ids`:
208+
// https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940
209+
final List<int> unreadMessageIds;
210+
211+
// other_user_id was introduced at FL 119 as the new name for sender_id:
212+
// 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
213+
// TODO(server-5): Simplify away.
214+
static _readOtherUserId(Map json, String key) {
215+
return json[key] ?? json['sender_id'];
216+
}
217+
218+
UnreadDmSnapshot({
219+
required this.otherUserId,
220+
required this.unreadMessageIds,
221+
});
222+
223+
factory UnreadDmSnapshot.fromJson(Map<String, dynamic> json) =>
224+
_$UnreadDmSnapshotFromJson(json);
225+
226+
Map<String, dynamic> toJson() => _$UnreadDmSnapshotToJson(this);
227+
}
228+
229+
/// An item in [UnreadMessagesSnapshot.streams].
230+
@JsonSerializable(fieldRename: FieldRename.snake)
231+
class UnreadStreamSnapshot {
232+
final String topic;
233+
final int streamId;
234+
final List<int> unreadMessageIds;
235+
236+
UnreadStreamSnapshot({
237+
required this.topic,
238+
required this.streamId,
239+
required this.unreadMessageIds,
240+
});
241+
242+
factory UnreadStreamSnapshot.fromJson(Map<String, dynamic> json) =>
243+
_$UnreadStreamSnapshotFromJson(json);
244+
245+
Map<String, dynamic> toJson() => _$UnreadStreamSnapshotToJson(this);
246+
}
247+
248+
/// An item in [UnreadMessagesSnapshot.huddles].
249+
@JsonSerializable(fieldRename: FieldRename.snake)
250+
class UnreadHuddleSnapshot {
251+
final String userIdsString;
252+
253+
// The doc mistakenly calls this `unread_ids`:
254+
// https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940
255+
final List<int> unreadMessageIds;
256+
257+
UnreadHuddleSnapshot({
258+
required this.userIdsString,
259+
required this.unreadMessageIds,
260+
});
261+
262+
factory UnreadHuddleSnapshot.fromJson(Map<String, dynamic> json) =>
263+
_$UnreadHuddleSnapshotFromJson(json);
264+
265+
Map<String, dynamic> toJson() => _$UnreadHuddleSnapshotToJson(this);
266+
}

lib/api/model/initial_snapshot.g.dart

+81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/example_data.dart

+21
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ ZulipStream stream({
9292
);
9393
}
9494

95+
UnreadMessagesSnapshot unreadMsgs({
96+
int? count,
97+
List<UnreadDmSnapshot>? dms,
98+
List<UnreadStreamSnapshot>? streams,
99+
List<UnreadHuddleSnapshot>? huddles,
100+
List<int>? mentions,
101+
bool? oldUnreadsMissing,
102+
}) {
103+
return UnreadMessagesSnapshot(
104+
count: count ?? 0,
105+
dms: dms ?? [],
106+
streams: streams ?? [],
107+
huddles: huddles ?? [],
108+
mentions: mentions ?? [],
109+
oldUnreadsMissing: oldUnreadsMissing ?? false,
110+
);
111+
}
112+
const _unreadMsgs = unreadMsgs;
113+
95114
final _messagePropertiesBase = {
96115
'is_me_message': false,
97116
'recipient_id': 32, // obsolescent in API, and ignored
@@ -223,6 +242,7 @@ InitialSnapshot initialSnapshot({
223242
List<CustomProfileField>? customProfileFields,
224243
List<RecentDmConversation>? recentPrivateConversations,
225244
List<Subscription>? subscriptions,
245+
UnreadMessagesSnapshot? unreadMsgs,
226246
List<ZulipStream>? streams,
227247
UserSettings? userSettings,
228248
int? maxFileUploadSizeMib,
@@ -240,6 +260,7 @@ InitialSnapshot initialSnapshot({
240260
customProfileFields: customProfileFields ?? [],
241261
recentPrivateConversations: recentPrivateConversations ?? [],
242262
subscriptions: subscriptions ?? [], // TODO add subscriptions to default
263+
unreadMsgs: unreadMsgs ?? _unreadMsgs(),
243264
streams: streams ?? [], // TODO add streams to default
244265
userSettings: userSettings, // TODO add userSettings to default
245266
maxFileUploadSizeMib: maxFileUploadSizeMib ?? 25,

0 commit comments

Comments
 (0)